Esempio n. 1
0
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)
}
Esempio n. 2
0
// 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{}) {
	cur := C.kcdbcursor(d.db)
	defer C.kccurdel(cur)
	C.kccurjump(cur)
	for key, value := next(cur); value != ""; key, value = next(cur) {
		f(key, value, args...)
	}
}
Esempio n. 3
0
func (kc *KCDB) Cursor() (kcc *KCCUR) {
	cur := C.kcdbcursor(kc.db)
	if cur == nil {
		return nil
	}
	kcc = &KCCUR{cur}
	kcc.Jump()
	return
}
Esempio n. 4
0
// Similar to Apply, but asynchronous. Returns a Waiter object, so
// you can wait for the applying to finish
func (d *DB) AsyncApply(f ApplyFunc, args ...interface{}) Waiter {
	r := NewApplyResult()
	cur := C.kcdbcursor(d.db)
	defer C.kccurdel(cur)
	C.kccurjump(cur)
	for key, value := next(cur); value != ""; key, value = next(cur) {
		r.wg.Add(1)
		k, v := key, value
		go func() {
			f(k, v, args...)
			r.wg.Done()
		}()
	}
	return r
}
Esempio n. 5
0
// 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...)
	}
}