Пример #1
0
func (db *LevelDB) Get(key []byte) ([]byte, error) {
	var vlen C.size_t
	var err *C.char
	val := C.leveldb_get(db.db, db.readOpts.Opts, C.CString(string(key)), C.size_t(len(key)), &vlen, &err)
	if err != nil {
		e := &DBError{}
		e.Err = C.GoString(err)
		return nil, e
	}
	if vlen == 0 {
		return nil, nil
	}
	return C.GoBytes(unsafe.Pointer(val), C.int(vlen)), nil
}
Пример #2
0
func (self *Leveldb) Get(key string, obj interface{}) error {
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	var valueLen C.size_t
	var cerr *C.char
	valueValue := C.leveldb_get(self.cdb, self.read_options, cKey, C.size_t(len(key)), &valueLen, &cerr)
	if cerr != nil {
		return errors.New(fmt.Sprintf("%s", cerr))
	}
	bs := C.GoBytes(unsafe.Pointer(valueValue), C.int(valueLen))
	r := bytes.NewReader(bs)
	err := decode(r, obj)
	if err != nil {
		return err
	}
	return nil
}
Пример #3
0
// Get returns the data associated with the key from the database.
//
// If the key does not exist in the database, a nil []byte is returned. If the
// key does exist, but the data is zero-length in the database, a zero-length
// []byte will be returned.
//
// The key byte slice may be reused safely. Get takes a copy of
// them before returning.
func (db *DB) Get(ro *ReadOptions, key []byte) ([]byte, error) {
	var errStr *C.char
	var vallen C.size_t
	var k *C.char
	if len(key) != 0 {
		k = (*C.char)(unsafe.Pointer(&key[0]))
	}

	value := C.leveldb_get(db.Ldb, ro.Opt,
		k, C.size_t(len(key)),
		&vallen, &errStr)

	if errStr != nil {
		return nil, DatabaseError(C.GoString(errStr))
	}

	if value == nil {
		return nil, nil
	}
	return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil
}
Пример #4
0
Файл: db.go Проект: rechen/wharf
func (db *DB) getSlice(ro *ReadOptions, key []byte) (driver.ISlice, error) {
	var errStr *C.char
	var vallen C.size_t
	var k *C.char
	if len(key) != 0 {
		k = (*C.char)(unsafe.Pointer(&key[0]))
	}

	value := C.leveldb_get(
		db.db, ro.Opt, k, C.size_t(len(key)), &vallen, &errStr)

	if errStr != nil {
		return nil, saveError(errStr)
	}

	if value == nil {
		return nil, nil
	}

	return NewCSlice(unsafe.Pointer(value), int(vallen)), nil
}
Пример #5
0
func (ldb *Db) Get(key []byte, read_option *Readoptions) ([]byte, error) {
	var db_err *C.char
	var value_len C.size_t

	key_len := len(key)
	if key_len == 0 {
		return nil, ldb_error("key must not empty")
	}

	key_c := (*C.char)(unsafe.Pointer(&key[0]))
	value := C.leveldb_get(ldb.ldb, read_option.options, key_c, C.size_t(key_len),
		&value_len, &db_err)

	if db_err != nil {
		return nil, ldb_error(C.GoString(db_err))
	}

	if value == nil {
		return nil, nil
	}

	return C.GoBytes(unsafe.Pointer(value), C.int(value_len)), nil
}
func (db *LevelDB) Get(key string) (value string, err error) {

	opt := C.leveldb_readoptions_create() // write option
	defer C.leveldb_free(unsafe.Pointer(opt))

	k := C.CString(key) // copy
	defer C.leveldb_free(unsafe.Pointer(k))

	var vallen C.size_t
	var cerr *C.char
	cvalue := C.leveldb_get(db.CLevelDB, opt, k, C.size_t(len(key)), &vallen, &cerr)

	if cerr != nil {
		defer C.leveldb_free(unsafe.Pointer(cerr))
		return "", errors.New(C.GoString(cerr))
	}

	if cvalue == nil {
		return "", nil
	}

	defer C.leveldb_free(unsafe.Pointer(cvalue))
	return C.GoString(cvalue), nil
}