-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathprefix_iterator.go
59 lines (51 loc) · 1.13 KB
/
prefix_iterator.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
package catalog
import (
"strings"
)
// prefixIterator use the underlying iterator to go over a specific prefix.
// will start by seek to the first item inside the prefix and end when it gets to the first item without the prefix.
type prefixIterator struct {
prefix string
it EntryIterator
ended bool
}
func NewPrefixIterator(it EntryIterator, prefix Path) EntryIterator {
it.SeekGE(prefix)
return &prefixIterator{
prefix: prefix.String(),
it: it,
}
}
func (p *prefixIterator) Next() bool {
if p.ended {
return false
}
// prefix it ends when there is no more data, or the next value doesn't match the prefix
if !p.it.Next() || !strings.HasPrefix(p.it.Value().Path.String(), p.prefix) {
p.ended = true
return false
}
return true
}
func (p *prefixIterator) SeekGE(id Path) {
var from Path
if id.String() <= p.prefix {
from = Path(p.prefix)
} else {
from = id
}
p.it.SeekGE(from)
p.ended = false
}
func (p *prefixIterator) Value() *EntryRecord {
if p.ended {
return nil
}
return p.it.Value()
}
func (p *prefixIterator) Err() error {
return p.it.Err()
}
func (p *prefixIterator) Close() {
p.it.Close()
}