-
Notifications
You must be signed in to change notification settings - Fork 356
/
kv_run_results_iterator.go
93 lines (80 loc) · 2 KB
/
kv_run_results_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
package actions
import (
"context"
"errors"
"fmt"
"github.com/treeverse/lakefs/pkg/kv"
)
var ErrParamConflict = errors.New("parameters conflict")
type KVRunResultIterator struct {
it kv.MessageIterator
err error
entry *RunResult
}
// NewKVRunResultIterator returns a new iterator over actions run results
// 'after' determines the runID which we should start the scan from, used for pagination
func NewKVRunResultIterator(ctx context.Context, store kv.Store, repositoryID, branchID, commitID, after string) (*KVRunResultIterator, error) {
if branchID != "" && commitID != "" {
return nil, fmt.Errorf("can't use both branchID and CommitID: %w", ErrParamConflict)
}
var (
prefix string
err error
)
secondary := true
switch {
case branchID != "":
prefix = byBranchPath(repositoryID, branchID)
case commitID != "":
prefix = byCommitPath(repositoryID, commitID)
default:
prefix = kv.FormatPath(baseActionsPath(repositoryID), runsPrefix)
secondary = false
}
if after != "" {
after = kv.FormatPath(prefix, after)
}
var it kv.MessageIterator
if secondary {
it, err = kv.NewSecondaryIterator(ctx, store, (&RunResultData{}).ProtoReflect().Type(), PartitionKey, []byte(prefix), []byte(after))
} else {
it, err = kv.NewPrimaryIterator(ctx, store, (&RunResultData{}).ProtoReflect().Type(), PartitionKey, []byte(prefix), kv.IteratorOptionsAfter([]byte(after)))
}
if err != nil {
return nil, err
}
return &KVRunResultIterator{
it: it,
}, nil
}
func (i *KVRunResultIterator) Next() bool {
if i.Err() != nil {
return false
}
if !i.it.Next() {
i.entry = nil
return false
}
e := i.it.Entry()
if e == nil {
i.err = ErrNilValue
return false
}
i.entry = RunResultFromProto(e.Value.(*RunResultData))
return true
}
func (i *KVRunResultIterator) Value() *RunResult {
if i.Err() != nil {
return nil
}
return i.entry
}
func (i *KVRunResultIterator) Err() error {
if i.err != nil {
return i.err
}
return i.it.Err()
}
func (i *KVRunResultIterator) Close() {
i.it.Close()
}