This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathiterator.go
98 lines (77 loc) · 1.53 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
package pilosa
import (
"io"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)
type locationValueIter struct {
locations [][]byte
pos int
}
func (i *locationValueIter) Next() ([]byte, error) {
if i.pos >= len(i.locations) {
return nil, io.EOF
}
i.pos++
return i.locations[i.pos-1], nil
}
func (i *locationValueIter) Close() error {
i.locations = nil
return nil
}
type indexValueIter struct {
offset uint64
total uint64
bits []uint64
mapping *mapping
indexName string
// share transaction and bucket on all getLocation calls
bucket *bolt.Bucket
tx *bolt.Tx
closed bool
}
func (it *indexValueIter) Next() ([]byte, error) {
if it.bucket == nil {
if err := it.mapping.open(); err != nil {
return nil, err
}
bucket, err := it.mapping.getBucket(it.indexName, false)
if err != nil {
_ = it.Close()
return nil, err
}
it.bucket = bucket
it.tx = bucket.Tx()
}
if it.offset >= it.total {
if err := it.Close(); err != nil {
logrus.WithField("err", err.Error()).
Error("unable to close the pilosa index value iterator")
}
if it.tx != nil {
_ = it.tx.Rollback()
}
return nil, io.EOF
}
var colID uint64
if it.bits == nil {
colID = it.offset
} else {
colID = it.bits[it.offset]
}
it.offset++
return it.mapping.getLocationFromBucket(it.bucket, colID)
}
func (it *indexValueIter) Close() error {
if it.closed {
return nil
}
it.closed = true
if it.tx != nil {
_ = it.tx.Rollback()
}
if it.bucket != nil {
return it.mapping.close()
}
return nil
}