-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiterator.v
More file actions
103 lines (92 loc) · 1.86 KB
/
Copy pathiterator.v
File metadata and controls
103 lines (92 loc) · 1.86 KB
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
module leveldb
pub struct Iterator {
mut:
entries []BlockEntry
pos int = -1
}
pub fn (mut db DB) new_iterator(ro ReadOptions) !&Iterator {
mut all := []BlockEntry{}
mut mit := db.mem.iterator()
for ok := mit.seek_to_first(); ok; ok = mit.next() {
all << BlockEntry{
key: mit.key().clone()
value: mit.value().clone()
}
}
for level in 0 .. num_levels {
for f in db.vs.current.levels[level] {
tr := db.table(f.num)!
all << tr.all_entries()!
}
}
all.sort_with_compare(fn (a &BlockEntry, b &BlockEntry) int {
return compare_internal(a.key, b.key)
})
mut resolved := []BlockEntry{}
mut last_ukey := []u8{}
mut has_last := false
for e in all {
pk := parse_internal_key(e.key)!
if has_last && compare_bytes(pk.ukey, last_ukey) == 0 {
continue
}
last_ukey = pk.ukey.clone()
has_last = true
if pk.kt == .del {
continue
}
resolved << BlockEntry{
key: pk.ukey.clone()
value: e.value
}
}
return &Iterator{
entries: resolved
}
}
pub fn (mut it Iterator) first() bool {
it.pos = 0
return it.valid()
}
pub fn (mut it Iterator) last() bool {
it.pos = it.entries.len - 1
return it.valid()
}
pub fn (mut it Iterator) next() bool {
if it.pos < it.entries.len {
it.pos++
}
return it.valid()
}
pub fn (mut it Iterator) prev() bool {
if it.pos >= 0 {
it.pos--
}
return it.valid()
}
pub fn (mut it Iterator) seek(key []u8) bool {
mut lo := 0
mut hi := it.entries.len
for lo < hi {
mid := (lo + hi) / 2
if compare_bytes(it.entries[mid].key, key) < 0 {
lo = mid + 1
} else {
hi = mid
}
}
it.pos = lo
return it.valid()
}
pub fn (it &Iterator) valid() bool {
return it.pos >= 0 && it.pos < it.entries.len
}
pub fn (it &Iterator) key() []u8 {
return it.entries[it.pos].key
}
pub fn (it &Iterator) value() []u8 {
return it.entries[it.pos].value
}
pub fn (it &Iterator) len() int {
return it.entries.len
}