forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
152 lines (119 loc) · 2.72 KB
/
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
package rel
import (
"context"
"io"
)
// Iterator alllows iterating through all record in database in batch.
type Iterator interface {
io.Closer
Next(record interface{}) error
}
// IteratorOption is used to configure iteration behaviour, such as batch size, start id and finish id.
type IteratorOption interface {
apply(*iterator)
}
type batchSize int
func (bs batchSize) apply(i *iterator) {
i.batchSize = int(bs)
}
// BatchSize specifies the size of iterator batch. Defaults to 1000.
func BatchSize(size int) IteratorOption {
return batchSize(size)
}
type start []interface{}
func (s start) apply(i *iterator) {
i.start = s
}
// Start specfies the primary value to start from (inclusive).
func Start(id ...interface{}) IteratorOption {
return start(id)
}
type finish []interface{}
func (f finish) apply(i *iterator) {
i.finish = f
}
// Finish specfies the primary value to finish at (inclusive).
func Finish(id ...interface{}) IteratorOption {
return finish(id)
}
type iterator struct {
ctx context.Context
start []interface{}
finish []interface{}
batchSize int
current int
query Query
adapter Adapter
cursor Cursor
fields []string
closed bool
}
func (i *iterator) Close() error {
if !i.closed && i.cursor != nil {
i.closed = true
return i.cursor.Close()
}
return nil
}
func (i *iterator) Next(record interface{}) error {
if i.current%i.batchSize == 0 {
if err := i.fetch(i.ctx, record); err != nil {
return err
}
}
if !i.cursor.Next() {
return io.EOF
}
var (
doc = NewDocument(record)
scanners = doc.Scanners(i.fields)
)
i.current++
return i.cursor.Scan(scanners...)
}
func (i *iterator) fetch(ctx context.Context, record interface{}) error {
if i.current == 0 {
i.init(record)
} else {
i.cursor.Close()
}
i.query = i.query.Limit(i.batchSize).Offset(i.current)
cursor, err := i.adapter.Query(ctx, i.query)
if err != nil {
return err
}
fields, err := cursor.Fields()
if err != nil {
return err
}
i.cursor = cursor
i.fields = fields
return nil
}
func (i *iterator) init(record interface{}) {
var (
doc = NewDocument(record)
)
if i.query.Table == "" {
i.query.Table = doc.Table()
}
if len(i.start) > 0 {
i.query = i.query.Where(filterDocumentPrimary(doc.PrimaryFields(), i.start, FilterGteOp))
}
if len(i.finish) > 0 {
i.query = i.query.Where(filterDocumentPrimary(doc.PrimaryFields(), i.finish, FilterLteOp))
}
i.query = i.query.SortAsc(doc.PrimaryFields()...)
}
func newIterator(ctx context.Context, adapter Adapter, query Query, options []IteratorOption) Iterator {
it := &iterator{
ctx: ctx,
batchSize: 1000,
query: query,
adapter: adapter,
}
for i := range options {
options[i].apply(it)
}
return it
}