Пример #1
0
Файл: lsm.go Проект: reusee/obox
func (self *LsmDb) Get(key string, obj interface{}) error {
	var cur *C.lsm_cursor
	rc := C.lsm_csr_open(self.cdb, &cur)
	if rc != C.LSM_OK {
		return errors.New("cannot create cursor")
	}
	defer C.lsm_csr_close(cur)
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	rc = C.lsm_csr_seek(cur, unsafe.Pointer(cKey), C.int(len(key)), C.LSM_SEEK_EQ)
	if rc != C.LSM_OK {
		return errors.New("error when seeking a key")
	}
	if C.lsm_csr_valid(cur) == lsmTrue {
		var valP unsafe.Pointer
		var valL C.int
		rc = C.lsm_csr_value(cur, &valP, &valL)
		if rc != C.LSM_OK {
			return errors.New("error when retrieving value")
		}
		bs := C.GoBytes(valP, valL)
		r := bytes.NewReader(bs)
		err := decode(r, obj)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #2
0
Файл: lsm.go Проект: reusee/obox
func (self *LsmDb) Iter(fun func(string, Getter) bool) {
	var cur *C.lsm_cursor
	rc := C.lsm_csr_open(self.cdb, &cur)
	if rc != C.LSM_OK {
		return
	}
	defer C.lsm_csr_close(cur)
	var keyP, valP unsafe.Pointer
	var keyL, valL C.int
	for rc := C.lsm_csr_first(cur); rc == C.LSM_OK && C.lsm_csr_valid(cur) == lsmTrue; rc = C.lsm_csr_next(cur) {
		rc = C.lsm_csr_key(cur, &keyP, &keyL)
		if rc != C.LSM_OK {
			break
		}
		rc = C.lsm_csr_value(cur, &valP, &valL)
		if rc != C.LSM_OK {
			break
		}
		key := string(C.GoBytes(keyP, keyL))
		value := C.GoBytes(valP, valL)
		r := bytes.NewReader(value)
		ret := fun(key, func(obj interface{}) error {
			return decode(r, obj)
		})
		if ret == false {
			break
		}
	}
}