-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
65 lines (56 loc) · 1.28 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
package aerospike
import (
"context"
as "github.com/aerospike/aerospike-client-go/v6"
"io"
)
type rowsIterator interface {
Read(ctx context.Context) (record *as.Record, err error)
}
type RowsScanReader struct {
*as.Recordset
}
func (r *RowsScanReader) Read(ctx context.Context) (*as.Record, error) {
if r.Recordset == nil {
return nil, io.EOF
}
channel := r.Recordset.Results()
select {
case result, ok := <-channel:
if !ok || result.Record == nil {
return nil, io.EOF
}
return result.Record, result.Err
case <-ctx.Done():
return nil, ctx.Err()
}
}
type RowsReader struct {
index int
records []*as.Record
}
func (r *RowsReader) Read(ctx context.Context) (record *as.Record, err error) {
if r.index >= len(r.records) {
return nil, io.EOF
}
record = r.records[r.index]
r.index++
return record, nil
}
func newRowsReader(records []*as.Record) *RowsReader {
return &RowsReader{
records: records,
}
}
func newInterfaceReader(records []interface{}) *RowsReader {
var asRecords []*as.Record
for i := range records {
var binMap = make(map[string]interface{})
for k, v := range records[i].(map[interface{}]interface{}) {
key := k.(string)
binMap[key] = v
}
asRecords = append(asRecords, &as.Record{Bins: binMap})
}
return newRowsReader(asRecords)
}