Ejemplo n.º 1
0
// key顺序扫描,常用于数据导出、附近搜索
// 返回的key是面向用户的key,而非内部结构的raw_key
func (l *LevelRedis) KeyEnumerate(seek []byte, direction IterDirection, fn func(i int, key, keytype, value []byte, quit *bool)) {
	var iter *gorocks.Iterator
	if l.snap != nil {
		iter = l.db.NewIterator(l.ro)
	} else {
		ro := gorocks.NewReadOptions()
		ro.SetFillCache(false)
		defer ro.Close()
		iter = l.db.NewIterator(ro)
	}
	defer iter.Close()

	minkey := joinStringBytes(KEY_PREFIX, SEP_LEFT, string(seek))
	maxkey := []byte{MAXBYTE}
	prefix := joinStringBytes(KEY_PREFIX, SEP_LEFT)
	l.Enumerate(iter, minkey, maxkey, direction, func(i int, key, value []byte, quit *bool) {
		if !bytes.HasPrefix(key, prefix) {
			*quit = true
			return
		}
		left := bytes.Index(key, []byte(SEP_LEFT))
		right := bytes.LastIndex(key, []byte(SEP_RIGHT))
		if left == -1 || right == -1 {
			return // just skip
		}
		fn(i, key[left+1:right], key[right+1:], value, quit)
	})
}
Ejemplo n.º 2
0
func (l *LevelRedis) RangeEnumerate(min, max []byte, direction IterDirection, fn func(i int, key, value []byte, quit *bool)) {
	var iter *gorocks.Iterator
	if l.snap != nil {
		iter = l.db.NewIterator(l.ro)
	} else {
		ro := gorocks.NewReadOptions()
		ro.SetFillCache(false)
		defer ro.Close()
		iter = l.db.NewIterator(ro)
	}
	defer iter.Close()
	l.Enumerate(iter, min, max, direction, fn)
}