-
Notifications
You must be signed in to change notification settings - Fork 342
/
tag_iterator.go
96 lines (87 loc) · 1.91 KB
/
tag_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
93
94
95
96
package ref
import (
"context"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/kv"
)
type TagIterator struct {
ctx context.Context
it kv.MessageIterator
err error
value *graveler.TagRecord
repoPartition string
store kv.Store
closed bool
}
func NewTagIterator(ctx context.Context, store kv.Store, repo *graveler.RepositoryRecord) (*TagIterator, error) {
repoPartition := graveler.RepoPartition(repo)
it, err := kv.NewPrimaryIterator(ctx, store, (&graveler.TagData{}).ProtoReflect().Type(),
graveler.RepoPartition(repo),
[]byte(graveler.TagPath("")), kv.IteratorOptionsFrom([]byte("")))
if err != nil {
return nil, err
}
return &TagIterator{
ctx: ctx,
it: it,
store: store,
repoPartition: repoPartition,
closed: false,
}, nil
}
func (i *TagIterator) Next() bool {
if i.Err() != nil || i.closed {
return false
}
if !i.it.Next() {
i.value = nil
return false
}
e := i.it.Entry()
if e == nil {
i.err = graveler.ErrReadingFromStore
return false
}
tag, ok := e.Value.(*graveler.TagData)
if !ok {
i.err = graveler.ErrReadingFromStore
return false
}
i.value = graveler.TagFromProto(tag)
return true
}
func (i *TagIterator) SeekGE(id graveler.TagID) {
if i.Err() != nil {
return
}
i.Close()
it, err := kv.NewPrimaryIterator(i.ctx, i.store, (&graveler.TagData{}).ProtoReflect().Type(),
i.repoPartition,
[]byte(graveler.TagPath("")), kv.IteratorOptionsFrom([]byte(graveler.TagPath(id))))
i.it = it
i.err = err
i.value = nil
i.closed = err != nil
}
func (i *TagIterator) Value() *graveler.TagRecord {
if i.Err() != nil {
return nil
}
return i.value
}
func (i *TagIterator) Err() error {
if i.err != nil {
return i.err
}
if !i.closed {
return i.it.Err()
}
return nil
}
func (i *TagIterator) Close() {
if i.closed {
return
}
i.it.Close()
i.closed = true
}