-
Notifications
You must be signed in to change notification settings - Fork 351
/
db_task_result_iterator.go
97 lines (84 loc) · 1.91 KB
/
db_task_result_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
package actions
import (
"context"
sq "github.com/Masterminds/squirrel"
"github.com/treeverse/lakefs/pkg/db"
)
type DBTaskResultIterator struct {
db db.Database
ctx context.Context
value *TaskResult
buf []*TaskResult
done bool
fetchSize int
err error
repositoryID string
runID string
offset string
}
func NewDBTaskResultIterator(ctx context.Context, db db.Database, fetchSize int, repositoryID, runID, after string) *DBTaskResultIterator {
return &DBTaskResultIterator{
db: db,
ctx: ctx,
repositoryID: repositoryID,
runID: runID,
fetchSize: fetchSize,
offset: after,
buf: make([]*TaskResult, 0, fetchSize),
}
}
func (it *DBTaskResultIterator) Next() bool {
if it.err != nil {
return false
}
it.maybeFetch()
// stage a value and increment offset
if len(it.buf) == 0 {
return false
}
it.value = it.buf[0]
it.buf = it.buf[1:]
it.offset = it.value.HookRunID
return true
}
func (it *DBTaskResultIterator) maybeFetch() {
if it.done {
return
}
if len(it.buf) > 0 {
return
}
q := psql.
Select("run_id", "hook_run_id", "hook_id", "action_name", "start_time", "end_time", "passed").
From("actions_run_hooks").
Where(sq.Eq{"repository_id": it.repositoryID, "run_id": it.runID}).
Where(sq.Gt{"hook_run_id": it.offset}).
OrderBy("hook_run_id").
Limit(uint64(it.fetchSize))
var sql string
var args []interface{}
sql, args, it.err = q.ToSql()
if it.err != nil {
return
}
it.err = it.db.Select(it.ctx, &it.buf, sql, args...)
if it.err != nil {
return
}
if len(it.buf) < it.fetchSize {
it.done = true
}
}
func (it *DBTaskResultIterator) Value() *TaskResult {
if it.err != nil {
return nil
}
return it.value
}
func (it *DBTaskResultIterator) Err() error {
return it.err
}
func (it *DBTaskResultIterator) Close() {
it.err = ErrIteratorClosed
it.buf = nil
}