-
Notifications
You must be signed in to change notification settings - Fork 342
/
commit_iterator.go
161 lines (138 loc) · 3.84 KB
/
commit_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ref
import (
"container/heap"
"context"
"github.com/treeverse/lakefs/pkg/db"
"github.com/treeverse/lakefs/pkg/graveler"
)
type CommitIterator struct {
db db.Database
ctx context.Context
repositoryID graveler.RepositoryID
start graveler.CommitID
value *graveler.CommitRecord
queue commitsPriorityQueue
visit map[graveler.CommitID]struct{}
state commitIteratorState
err error
}
type commitIteratorState int
const (
commitIteratorStateInit commitIteratorState = iota
commitIteratorStateQuery
commitIteratorStateDone
)
type commitsPriorityQueue []*graveler.CommitRecord
func (c commitsPriorityQueue) Len() int {
return len(c)
}
func (c commitsPriorityQueue) Less(i, j int) bool {
if c[i].Commit.CreationDate.Equal(c[j].Commit.CreationDate) {
return c[i].CommitID > c[j].CommitID
}
return c[i].Commit.CreationDate.After(c[j].Commit.CreationDate)
}
func (c commitsPriorityQueue) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c *commitsPriorityQueue) Push(x interface{}) {
rec := x.(*graveler.CommitRecord)
*c = append(*c, rec)
}
func (c *commitsPriorityQueue) Pop() interface{} {
cc := *c
n := len(cc) - 1
item := cc[n]
*c = cc[:n]
return item
}
func NewCommitIterator(ctx context.Context, db db.Database, repositoryID graveler.RepositoryID, start graveler.CommitID) *CommitIterator {
return &CommitIterator{
db: db,
ctx: ctx,
repositoryID: repositoryID,
start: start,
queue: make(commitsPriorityQueue, 0),
visit: make(map[graveler.CommitID]struct{}),
}
}
func (ci *CommitIterator) getCommitRecord(commitID graveler.CommitID) (*graveler.CommitRecord, error) {
var rec commitRecord
err := ci.db.
Get(ci.ctx, &rec, `SELECT id, committer, message, creation_date, parents, meta_range_id, metadata, version
FROM graveler_commits
WHERE repository_id = $1 AND id = $2`,
ci.repositoryID, commitID)
if err != nil {
return nil, err
}
return rec.toGravelerCommitRecord(), nil
}
func (ci *CommitIterator) Next() bool {
if ci.err != nil || ci.state == commitIteratorStateDone {
return false
}
if ci.state == commitIteratorStateInit {
// first time we lookup the 'start' commit and push it into the queue
ci.state = commitIteratorStateQuery
rec, err := ci.getCommitRecord(ci.start)
if err != nil {
ci.value = nil
ci.err = err
return false
}
ci.queue.Push(rec)
}
// nothing in our queue - work is done
if ci.queue.Len() == 0 {
ci.value = nil
ci.state = commitIteratorStateDone
return false
}
// as long as we have something in the queue we will
// set it as the current value and push the current commit's parents to the queue
ci.value = heap.Pop(&ci.queue).(*graveler.CommitRecord)
for _, p := range ci.value.Parents {
rec, err := ci.getCommitRecord(p)
if err != nil {
ci.value = nil
ci.err = err
return false
}
// skip commits we already visited
if _, visited := ci.visit[rec.CommitID]; visited {
continue
}
ci.visit[rec.CommitID] = struct{}{}
heap.Push(&ci.queue, rec)
}
return true
}
// SeekGE skip under the point of 'id' commit ID based on a a new
// The list of commit
func (ci *CommitIterator) SeekGE(id graveler.CommitID) {
ci.err = nil
ci.queue = make(commitsPriorityQueue, 0)
ci.visit = make(map[graveler.CommitID]struct{})
ci.state = commitIteratorStateInit
// skip until we get into our commit
for ci.Next() {
if ci.Value().CommitID == id {
break
}
}
if ci.Err() != nil {
return
}
// step back - in order to have Next to read the value we just got,
// we push back the current value to our queue and set the current value to nil.
heap.Push(&ci.queue, ci.value)
ci.value = nil
}
func (ci *CommitIterator) Value() *graveler.CommitRecord {
return ci.value
}
func (ci *CommitIterator) Err() error {
return ci.err
}
func (ci *CommitIterator) Close() {}