forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
168 lines (132 loc) · 2.98 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package rel
import (
"context"
"fmt"
"io"
)
// Iterator allows iterating through all entity in database in batch.
type Iterator interface {
io.Closer
Next(entity any) 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)
}
// String representation.
func (bs batchSize) String() string {
return fmt.Sprintf("rel.BatchSize(%d)", bs)
}
// BatchSize specifies the size of iterator batch. Defaults to 1000.
func BatchSize(size int) IteratorOption {
return batchSize(size)
}
type start []any
func (s start) apply(i *iterator) {
i.start = s
}
// String representation.
func (s start) String() string {
return fmt.Sprintf("rel.Start(%s)", fmtAnys(s))
}
// Start specifies the primary value to start from (inclusive).
func Start(id ...any) IteratorOption {
return start(id)
}
type finish []any
func (f finish) apply(i *iterator) {
i.finish = f
}
// String representation.
func (f finish) String() string {
return fmt.Sprintf("rel.Finish(%s)", fmtAnys(f))
}
// Finish specifies the primary value to finish at (inclusive).
func Finish(id ...any) IteratorOption {
return finish(id)
}
type iterator struct {
ctx context.Context
start []any
finish []any
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(entity any) error {
if i.current%i.batchSize == 0 {
if err := i.fetch(i.ctx, entity); err != nil {
return err
}
}
if !i.cursor.Next() {
return io.EOF
}
var (
doc = NewDocument(entity)
scanners = doc.Scanners(i.fields)
)
i.current++
return i.cursor.Scan(scanners...)
}
func (i *iterator) fetch(ctx context.Context, entity any) error {
if i.current == 0 {
i.init(entity)
} 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(entity any) {
var (
doc = NewDocument(entity)
)
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
}