Example #1
0
func (db BerkeleyDB) Put(txn Transaction, key, val []byte, flags DbFlag) error {
	cKey := AllocDBT(key)
	defer cKey.Free()
	cVal := AllocDBT(val)
	defer cVal.Free()

	return Err(C.db_put(db.ptr, txn.ptr, cKey, cVal, C.u_int32_t(flags)))
}
Example #2
0
func (db *Db) Set(_txn *Txn, key []byte, value []byte, flags uint32) error {
	var txn *C.DB_TXN = nil
	if _txn != nil {
		txn = _txn.txn
	}
	ret := C.db_put(
		db.db,
		txn,
		(*C.char)(unsafe.Pointer(&key[0])),
		C.uint(len(key)),
		(*C.char)(unsafe.Pointer(&value[0])),
		C.uint(len(value)),
		C.uint(flags),
	)
	return ResultToError(ret)
}
Example #3
0
// Store records in the database. In combination with a queue or
// numbered database the append flags causes the keys of the records
// to be set to fresh record numbers, for any other database it
// prevents an existing record with the same key from being
// overwritten.
func (db Database) Put(txn Transaction, append bool, recs ...proto.Message) (err error) {
	dbtype, err := db.Type()
	if err != nil {
		return
	}

	var key, data C.DBT
	var flags C.u_int32_t = 0

	if append {
		key.flags |= C.DB_DBT_USERMEM

		switch dbtype {
		case Numbered, Queue:
			flags |= C.DB_APPEND
		default:
			flags |= C.DB_NOOVERWRITE
		}
	} else {
		key.flags |= C.DB_DBT_READONLY
	}

	data.flags |= C.DB_DBT_READONLY

	for _, rec := range recs {
		err = db.marshalData(&data, rec)
		if err != nil {
			return
		}

		err = db.marshalKey(&key, rec)
		if err == nil {
			key.ulen = key.size
		} else {
			return
		}

		err = check(C.db_put(db.ptr, txn.ptr, &key, &data, flags))
		if err != nil {
			return
		}
	}

	return
}
Example #4
0
func (db *DB) set(key, value []byte, flags int) error {
	db.lk.Lock()
	defer db.lk.Unlock()

	k := C.str{unsafe.Pointer(&key[0]), C.size_t(len(key))}
	v := C.str{unsafe.Pointer(&value[0]), C.size_t(len(value))}

	n, err := C.db_put(db.db, &k, &v, C.u_int(flags))

	switch {
	case n < 0:
		return err
	case n > 0:
		return ErrKeyAlreadyExists(string(key))
	}

	return nil
}