-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathentry_listing_iterator.go
92 lines (80 loc) · 2 KB
/
entry_listing_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
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
package catalog
import (
"strings"
"github.com/treeverse/lakefs/pkg/graveler"
)
type entryListingIterator struct {
it EntryIterator
prefix string
delimiter string
nextFunc func() bool
value *EntryListing
}
func NewEntryListingIterator(it EntryIterator, prefix Path, delimiter Path) EntryListingIterator {
if len(prefix) > 0 {
it = NewPrefixIterator(it, prefix)
}
eli := &entryListingIterator{
it: it,
prefix: prefix.String(),
delimiter: delimiter.String(),
}
if len(delimiter) == 0 {
eli.nextFunc = eli.nextNoDelimiter
} else {
eli.nextFunc = eli.nextWithDelimiter
}
return eli
}
func (e *entryListingIterator) Next() bool {
return e.nextFunc()
}
func (e *entryListingIterator) SeekGE(id Path) {
e.value = nil
e.it.SeekGE(id)
}
func (e *entryListingIterator) Value() *EntryListing {
return e.value
}
func (e *entryListingIterator) Err() error {
return e.it.Err()
}
func (e *entryListingIterator) Close() {
e.it.Close()
}
func (e *entryListingIterator) nextNoDelimiter() bool {
hasNext := e.it.Next()
if !hasNext {
e.value = nil
return false
}
v := e.it.Value()
e.value = &EntryListing{Path: v.Path, Entry: v.Entry}
return true
}
func (e *entryListingIterator) nextWithDelimiter() bool {
if e.value != nil && e.value.CommonPrefix {
nextPath := e.value.Path
if upperBound := graveler.UpperBoundForPrefix([]byte(nextPath)); upperBound != nil {
nextPath = Path(upperBound)
}
e.it.SeekGE(nextPath)
}
if !e.it.Next() {
e.value = nil
return false
}
v := e.it.Value()
relevantPath := v.Path[len(e.prefix):]
delimiterIndex := strings.Index(relevantPath.String(), e.delimiter)
if delimiterIndex == -1 {
// listing for non-common prefix with value
e.value = &EntryListing{Path: v.Path, Entry: v.Entry}
} else {
// listing for common prefix key
commonPrefixLen := len(e.prefix) + delimiterIndex + len(e.delimiter)
commonPrefixKey := v.Path[:commonPrefixLen]
e.value = &EntryListing{CommonPrefix: true, Path: commonPrefixKey}
}
return true
}