Пример #1
0
func (db *Db) Get(_txn *Txn, key []byte, getbuff *uintptr, flags uint32) ([]byte, error) {
	var data *C.char = (*C.char)(unsafe.Pointer(*getbuff))
	var datalen C.uint

	var txn *C.DB_TXN = nil
	if _txn != nil {
		txn = _txn.txn
	}

	ret := C.db_get(
		db.db,
		txn,
		(*C.char)(unsafe.Pointer(&key[0])),
		C.uint(len(key)),
		&data,
		&datalen,
		C.uint(flags),
	)
	err := ResultToError(ret)
	if err == nil {
		*getbuff = uintptr(unsafe.Pointer(data))
		if data == nil {
			return nil, nil
		} else {
			r := C.GoBytes(unsafe.Pointer(data), C.int(datalen))
			//C.free(unsafe.Pointer(data))
			return r, nil
		}
	}
	return nil, err
}
Пример #2
0
func (db BerkeleyDB) Get(txn Transaction, key []byte, flags DbFlag) ([]byte, error) {
	data := &C.DBT{flags: C.DB_DBT_REALLOC}
	defer data.Free()

	cKey := AllocDBT(key)
	defer cKey.Free()

	err := Err(C.db_get(db.ptr, txn.ptr, cKey, data, C.u_int32_t(flags)))
	if err != nil {
		return nil, err
	}

	return cloneToBytes(data), ok
}
Пример #3
0
func (db *DB) Get(key []byte) ([]byte, error) {
	db.lk.Lock()
	defer db.lk.Unlock()

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

	n, err := C.db_get(db.db, &k, &v)

	switch {
	case n < 0:
		return []byte{}, err
	case n > 0:
		return []byte{}, ErrKeyNotFound(string(key))
	}

	return C.GoBytes(v.data, C.int(v.size)), nil
}
Пример #4
0
// Get records from the database. The consume flag makes sense only in
// combination with a queue database and causes the operation to wait
// for and obtain the next enqueued record.
func (db Database) Get(txn Transaction, consume bool, recs ...proto.Message) (err error) {
	var key, data C.DBT
	var flags C.u_int32_t = 0

	if consume {
		key.flags |= C.DB_DBT_USERMEM
		flags |= C.DB_CONSUME_WAIT
	} else {
		key.flags |= C.DB_DBT_READONLY
	}

	data.flags |= C.DB_DBT_REALLOC
	defer C.free(data.data)

	for _, rec := range recs {
		err = db.marshalKey(&key, rec)
		if err == nil {
			key.ulen = key.size
		} else {
			return
		}

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

		err = db.unmarshalData(&data, rec)
		if err != nil {
			return
		}

		err = db.unmarshalKey(&key, rec)
		if err != nil {
			return
		}
	}

	return
}