-
Notifications
You must be signed in to change notification settings - Fork 342
/
starting_point_iterator.go
78 lines (69 loc) · 2.65 KB
/
starting_point_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
package retention
import "github.com/treeverse/lakefs/pkg/graveler"
// A GCStartingPoint represents a commit from which the GC algorithm should start scanning.
// It could be either a branch HEAD, or a dangling commit.
// The CommitID field is always set, while BranchID is set only if the commit is a branch HEAD.
type GCStartingPoint struct {
BranchID graveler.BranchID
CommitID graveler.CommitID
}
// GCStartingPointIterator combines a branch iterator and a commit iterator. Both are assumed to be sorted and to contain no duplicates.
// Each returned GCStartingPoint object contains the commit id. If the entry came from the branch iterator, it also contains the branch id.
// Commits appearing in both iterators appear only once and include the branch id.
// Closing this iterator will close the two internal iterators.
type GCStartingPointIterator struct {
commitIterator graveler.CommitIterator
branchIterator graveler.BranchIterator
commitValue *graveler.CommitRecord
branchValue *graveler.BranchRecord
value *GCStartingPoint
}
func NewGCStartingPointIterator(commitIterator graveler.CommitIterator, branchIterator graveler.BranchIterator) *GCStartingPointIterator {
return &GCStartingPointIterator{commitIterator: commitIterator, branchIterator: branchIterator}
}
func (sp *GCStartingPointIterator) Next() bool {
if sp.branchValue == nil && sp.branchIterator.Next() {
sp.branchValue = sp.branchIterator.Value()
}
if sp.commitValue == nil && sp.commitIterator.Next() {
sp.commitValue = sp.commitIterator.Value()
}
if sp.branchValue == nil && sp.commitValue == nil {
return false
}
if sp.branchValue != nil && sp.commitValue != nil && sp.branchValue.CommitID == sp.commitValue.CommitID {
// commit is same as branch head - skip the commit
sp.commitValue = nil
}
if sp.commitValue == nil || (sp.branchValue != nil && sp.commitValue.CommitID > sp.branchValue.CommitID) {
// has only branch, or branch is before commit
sp.value = &GCStartingPoint{
BranchID: sp.branchValue.BranchID,
CommitID: sp.branchValue.CommitID,
}
sp.branchValue = nil
return true
}
if sp.branchValue == nil || sp.commitValue.CommitID < sp.branchValue.CommitID {
// has only commit, or commit is before branch
sp.value = &GCStartingPoint{
CommitID: sp.commitValue.CommitID,
}
sp.commitValue = nil
return true
}
return false
}
func (sp *GCStartingPointIterator) Value() *GCStartingPoint {
return sp.value
}
func (sp *GCStartingPointIterator) Err() error {
if sp.branchIterator != nil {
return sp.branchIterator.Err()
}
return sp.commitIterator.Err()
}
func (sp *GCStartingPointIterator) Close() {
sp.commitIterator.Close()
sp.branchIterator.Close()
}