Example #1
0
func (it *Iterator) Key() []byte {
	var klen C.size_t
	kdata := C.leveldb_iter_key(it.it, &klen)
	if kdata == nil {
		return nil
	}

	return slice(unsafe.Pointer(kdata), int(C.int(klen)))
}
Example #2
0
// Key returns a copy the key in the database the iterator currently holds.
//
// If Valid returns false, this method will panic.
func (it *Iterator) Key() []byte {
	var klen C.size_t
	kdata := C.leveldb_iter_key(it.Iter, &klen)
	if kdata == nil {
		return nil
	}
	// Unlike DB.Get, the key, kdata, returned is not meant to be freed by the
	// client. It's a direct reference to data managed by the iterator_t
	// instead of a copy.  So, we must not free it here but simply copy it
	// with GoBytes.
	return C.GoBytes(unsafe.Pointer(kdata), C.int(klen))
}
Example #3
0
func (self *Leveldb) Iter(fun func(string, Getter) bool) {
	iterator := C.leveldb_create_iterator(self.cdb, self.read_options)
	defer C.leveldb_iter_destroy(iterator)
	var keyLen C.size_t
	var keyValue *C.char
	var valueLen C.size_t
	var valueValue *C.char
	var ret bool
	for C.leveldb_iter_seek_to_first(iterator); C.leveldb_iter_valid(iterator) != levelFalse; C.leveldb_iter_next(iterator) {
		keyValue = C.leveldb_iter_key(iterator, &keyLen)
		key := string(C.GoBytes(unsafe.Pointer(keyValue), C.int(keyLen)))
		valueValue = C.leveldb_iter_value(iterator, &valueLen)
		value := C.GoBytes(unsafe.Pointer(valueValue), C.int(valueLen))
		r := bytes.NewReader(value)
		ret = fun(key, func(e interface{}) error {
			return decode(r, e)
		})
		if ret == false {
			break
		}
	}
}
Example #4
0
// Key() returns a copy the key in the database the iterator currently holds.
//
// If Valid() returns false, this method will panic.
func (it *Iterator) Key() []byte {
	var klen C.size_t
	kdata := C.leveldb_iter_key(it.Iter, &klen)
	return C.GoBytes(unsafe.Pointer(kdata), C.int(klen))
}