// transactional get for testing get commands over the wire // arg0: dbName // arg1: key func get(args [][]byte, txn *mdb.Txn) ([]byte, error) { if len(args) < 2 { return nil, fmt.Errorf("Get needs 2 arguments! Got %d args", len(args)) } dbName := string(args[0]) dbi, err := txn.DBIOpen(&dbName, mdb.CREATE) // create if not exists if err != nil { txn.Abort() return nil, err } ret, err := txn.Get(dbi, args[1]) if err != nil { txn.Abort() return nil, err } txn.Abort() return ret, err }
// remove if set to expected val // arg0: dbName // arg1: key // arg2: expectedVal // ret: [1] if removed, [0] otherwise func compareAndRemove(args [][]byte, txn *mdb.Txn) ([]byte, error) { dbName := string(args[0]) dbi, err := txn.DBIOpen(&dbName, mdb.CREATE) // create if not exists existingVal, err := txn.Get(dbi, args[1]) if err != nil && err != mdb.NotFound { txn.Abort() return nil, err } if err == mdb.NotFound || bytesEqual(args[2], existingVal) { err = txn.Del(dbi, args[1], nil) if err != nil { return nil, err } err = txn.Commit() return args[3], err } else { txn.Abort() return existingVal, nil } }