示例#1
0
文件: lib.go 项目: jbooth/flotilla
// 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
}
示例#2
0
文件: lib.go 项目: jbooth/flotilla
// 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
}
示例#3
0
文件: lib.go 项目: jbooth/flotilla
// compare and swap
// arg0: dbName
// arg1: key
// arg2: expectedValue
// arg3: newValue
// return: new row contents as []byte
func compareAndSwap(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.Put(dbi, args[1], args[3], 0)
		if err != nil {
			return nil, err
		}
		err = txn.Commit()
		return args[3], err
	} else {
		txn.Abort()
		return existingVal, nil
	}
}