示例#1
0
// Has returns true if the database has a value for the key.
func (db *Database) Has(key []byte) (bool, error) {
	e := C.sp_get(db.Pointer, unsafe.Pointer(&key[0]), C.size_t(len(key)), nil, nil)
	switch int(e) {
	case -1:
		return false, db.Error()
	case 0:
		return false, nil
	case 1:
		return true, nil
	}
	return false, fmt.Errorf("ERROR: unexpected return value from sp_get: %v", e)
}
示例#2
0
// Get retrieves the value for the key.
func (db *Database) Get(key []byte) ([]byte, error) {
	var vptr unsafe.Pointer
	var size C.size_t

	e := C.sp_get(db.Pointer, unsafe.Pointer(&key[0]), C.size_t(len(key)), &vptr, (*C.size_t)(&size))
	switch int(e) {
	case -1:
		return nil, db.Error()
	case 0:
		return nil, ErrNotFound
	case 1:
		// Continue after the switch
	default:
		return nil, fmt.Errorf("ERROR: unexpected return value from sp_get: %v", e)
	}
	value := C.GoBytes(vptr, C.int(size))
	C.sp_destroy(vptr)
	return value, nil
}