func (self *KcDb) Iter(fun func(string, Getter) bool) { cur := C.kcdbcursor(self.cdb) C.kccurjump(cur) var kSize, vSize C.size_t var vBuff, kBuff *C.char var ret bool for { kBuff = C.kccurget(cur, &kSize, &vBuff, &vSize, kcTrue) if kBuff == nil { C.kcfree(unsafe.Pointer(kBuff)) break } key := string(C.GoBytes(unsafe.Pointer(kBuff), C.int(kSize))) value := C.GoBytes(unsafe.Pointer(vBuff), C.int(vSize)) r := bytes.NewReader(value) ret = fun(key, func(e interface{}) error { return decode(r, e) }) C.kcfree(unsafe.Pointer(kBuff)) if ret == false { break } } C.kccurdel(cur) }
func (kcc *KCCUR) Get(advance bool) (k, v []byte, err error) { var ksiz, vsiz C.size_t var vp *C.char var cadvance C.int32_t if advance { cadvance = 1 } else { cadvance = 0 } kp := C.kccurget(kcc.cur, &ksiz, &vp, &vsiz, cadvance) if kp != nil { k = make([]byte, ksiz) C.memcpy(unsafe.Pointer(&k[0]), unsafe.Pointer(kp), ksiz) v = make([]byte, vsiz) C.memcpy(unsafe.Pointer(&v[0]), unsafe.Pointer(vp), vsiz) C.kcfree(unsafe.Pointer(kp)) } else { err = kcc.error() } return }
// Applies a function to all records in the database // // The function is called with the key and the value as parameters. // All extra arguments passed to Apply are used in the call to f func (d *DB) Apply(f ApplyFunc, args ...interface{}) { var keyLen, valueLen C.size_t var valueBuffer *C.char defer C.free(unsafe.Pointer(valueBuffer)) cur := C.kcdbcursor(d.db) defer C.kccurdel(cur) C.kccurjump(cur) var next = func() *C.char { return C.kccurget(cur, &keyLen, &valueBuffer, &valueLen, 1) } for keyBuffer := next(); keyBuffer != nil; keyBuffer = next() { key := C.GoString(keyBuffer) C.free(unsafe.Pointer(keyBuffer)) value := C.GoString(valueBuffer) f(key, value, args...) } }