// aborts transactionsuppressing any errors, and frees func (e *env) CloseTransaction(t *mdb.Txn) error { t.Abort() // ignore error e.l.Lock() defer e.l.Unlock() e.numOpen-- if e.shouldClose && e.numOpen == 0 && !e.closed { e.e.Close() e.closed = true } e.e.Close() return nil }
// remove // arg0: dbName // arg1: key // returns nil func remove(args [][]byte, txn *mdb.Txn) ([]byte, error) { dbName := string(args[0]) dbi, err := txn.DBIOpen(&dbName, mdb.CREATE) // create if not exists if err != nil { txn.Abort() return nil, err } err = txn.Del(dbi, args[1], nil) if err != nil { txn.Abort() return nil, err } return nil, txn.Commit() }
// put // arg0: dbName // arg1: key // arg2: value // returns nil func put(args [][]byte, txn *mdb.Txn) ([]byte, error) { if len(args) < 3 { return nil, fmt.Errorf("Put needs 3 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 } err = txn.Put(dbi, args[1], args[2], 0) if err != nil { txn.Abort() return nil, err } err = txn.Commit() return nil, 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 } }
// 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 }
// put if not already set // arg0: dbName // arg1: key // arg2: value // return: [1] if added, [0] otherwise func putIfAbsent(args [][]byte, txn *mdb.Txn) ([]byte, error) { dbName := string(args[0]) dbi, err := txn.DBIOpen(&dbName, mdb.CREATE) // create if not exists if err != nil { txn.Abort() return nil, err } err = txn.Put(dbi, args[1], args[2], mdb.NOOVERWRITE) if err == mdb.KeyExist { txn.Abort() return []byte{0}, nil } if err != nil { txn.Abort() return nil, err } err = txn.Commit() return []byte{1}, err }