Пример #1
0
// Get retrieves a record in the database by its key.
//
// Returns the string value and nil in case of success, in case of errors,
// return a zero-valued string and an KCError instance (including when the key
// doesn't exist in the database).
func (d *DB) Get(key string) (string, error) {
	var resultLen C.size_t
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	lKey := C.size_t(len(key))
	cValue := C.kcdbget(d.db, cKey, lKey, &resultLen)
	defer C.free(unsafe.Pointer(cValue))
	if cValue == nil {
		errMsg := d.LastError()
		return "", KCError(fmt.Sprintf("Failed to get the record with the key %s: %s", key, errMsg))
	}
	return C.GoStringN(cValue, C.int(resultLen)), nil
}
Пример #2
0
func (kc *KCDB) Get(key []byte) (value []byte, err error) {
	ckey := (*C.char)(unsafe.Pointer(&key[0]))
	var vlen C.size_t
	cval := C.kcdbget(kc.db, ckey, C.size_t(len(key)), &vlen)
	if cval == nil {
		err = kc.error()
	} else {
		value = make([]byte, int(vlen))
		C.memcpy(unsafe.Pointer(&value[0]), unsafe.Pointer(cval), vlen)
		C.kcfree(unsafe.Pointer(cval))
	}
	return
}
Пример #3
0
func (self *KcDb) Get(key string, obj interface{}) error {
	var b []byte
	var err error
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	var vSize C.size_t
	vBuf := C.kcdbget(self.cdb, cKey, C.size_t(len(key)), &vSize)
	if vBuf == nil {
		return errors.New(fmt.Sprintf("get: %s", key))
	}
	defer C.kcfree(unsafe.Pointer(vBuf))
	b = C.GoBytes(unsafe.Pointer(vBuf), C.int(vSize))
	r := bytes.NewReader(b)
	err = decode(r, obj)
	if err != nil {
		return err
	}
	return nil
}