-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveldb_iter.go
104 lines (88 loc) · 1.88 KB
/
leveldb_iter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package ezdb
import "github.com/syndtr/goleveldb/leveldb/iterator"
type LevelDBIterator[T any] struct {
i iterator.Iterator
m DocumentMarshaler[T, []byte]
}
func (i *LevelDBIterator[T]) Count() int {
n := 0
for i.Next() {
n++
}
return n
}
func (i *LevelDBIterator[T]) Filter(f FilterFunc[T]) Iterator[T] {
i.First()
m := map[string]T{}
for i.Next() {
key, value, err := i.Get()
if err != nil {
continue
}
m[key] = value
}
return newMemoryIterator(m, i)
}
func (i *LevelDBIterator[T]) First() bool {
return i.i.First()
}
func (i *LevelDBIterator[T]) Get() (string, T, error) {
value, err := i.Value()
return i.Key(), value, err
}
func (i *LevelDBIterator[T]) GetAll() (map[string]T, error) {
values := map[string]T{}
i.First()
key, value, err := i.Get()
if err != nil {
return values, err
}
values[key] = value
for i.Next() {
key, value, err := i.Get()
if err != nil {
return values, err
}
values[key] = value
}
return values, nil
}
func (i *LevelDBIterator[T]) GetAllKeys() []string {
keys := []string{}
i.First()
keys = append(keys, i.Key())
for i.Next() {
keys = append(keys, i.Key())
}
return keys
}
func (i *LevelDBIterator[T]) Key() string {
return string(i.i.Key())
}
func (i *LevelDBIterator[T]) Last() bool {
return i.i.Last()
}
func (i *LevelDBIterator[T]) Next() bool {
return i.i.Next()
}
func (i *LevelDBIterator[T]) Prev() bool {
return i.i.Prev()
}
func (i *LevelDBIterator[T]) Release() {
i.i.Release()
}
func (i *LevelDBIterator[T]) Sort(f SortFunc[T]) Iterator[T] {
all, _ := i.GetAll()
m := newMemoryIterator(all, i)
return m.Sort(f)
}
func (i *LevelDBIterator[T]) SortKeys(f SortFunc[string]) Iterator[T] {
all, _ := i.GetAll()
m := newMemoryIterator(all, i)
return m.SortKeys(f)
}
func (i *LevelDBIterator[T]) Value() (T, error) {
value := i.m.Factory()
err := i.m.Unmarshal(i.i.Value(), value)
return value, err
}