-
Notifications
You must be signed in to change notification settings - Fork 0
/
leveldb_iter.go
126 lines (98 loc) · 2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
if i.First() {
n++
}
for i.Next() {
n++
}
return n
}
func (i *LevelDBIterator[T]) Filter(f FilterFunc[T]) Iterator[T] {
m := map[string]T{}
if i.First() {
key, value, err := i.Get()
if err == nil {
m[key] = value
}
}
for i.Next() {
key, value, err := i.Get()
if err != nil {
continue
}
m[key] = value
}
return MemoryIter(m, nil, 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{}
if 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{}
if 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 := MemoryIter(all, nil, i)
return m.Sort(f)
}
func (i *LevelDBIterator[T]) SortKeys(f SortFunc[string]) Iterator[T] {
all, _ := i.GetAll()
m := MemoryIter(all, nil, 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
}