-
Notifications
You must be signed in to change notification settings - Fork 450
/
iterator.go
182 lines (161 loc) · 4.75 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package executor
import (
"github.com/m3db/m3/src/dbnode/tracepoint"
"github.com/m3db/m3/src/m3ninx/doc"
"github.com/m3db/m3/src/m3ninx/index"
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/search"
"github.com/m3db/m3/src/x/context"
"github.com/m3db/m3/src/x/errors"
)
// iterator is a wrapper around many doc.Iterators (one per segment) that provides a stream of docs for a index block.
//
// all segments are eagerly searched on the first call to Next(). eagerly searching all the segments for a block allows
// the iterator to yield without holding locks when processing the results set. yielding allows the goroutine to
// yield the index worker to another goroutine waiting and then can resume the iterator when it acquires a worker again.
// this prevents large queries with many result docs from starving small queries.
//
// for each segment, the searcher gets the postings list. the postings list are lazily processed by the returned
// doc iterators. for each posting in the list, the encoded document is retrieved.
type iterator struct {
// immutable state
query search.Query
searcher search.Searcher
readers index.Readers
ctx context.Context
// immutable state after the first call to Next()
iters []doc.Iterator
// mutable state
idx int
currDoc doc.Document
nextDoc doc.Document
done bool
err error
}
func newIterator(ctx context.Context, q search.Query, rs index.Readers) (doc.QueryDocIterator, error) {
s, err := q.Searcher()
if err != nil {
return nil, err
}
return &iterator{
ctx: ctx,
query: q,
searcher: s,
readers: rs,
}, nil
}
func (it *iterator) Done() bool {
return it.err != nil || it.done
}
func (it *iterator) Next() bool {
if it.Done() {
return false
}
if it.iters == nil {
if err := it.initIters(); err != nil {
it.err = err
return false
}
if !it.next() {
it.done = true
return false
}
it.nextDoc = it.current()
}
it.currDoc = it.nextDoc
if it.next() {
it.nextDoc = it.current()
} else {
it.done = true
}
return true
}
func (it *iterator) next() bool {
if it.idx == len(it.iters) {
return false
}
currIter := it.iters[it.idx]
for !currIter.Next() {
// Check if the current iterator encountered an error.
if err := currIter.Err(); err != nil {
it.err = err
return false
}
// move to next iterator so we don't try to Close twice.
it.idx++
// Close current iterator now that we are finished with it.
if err := currIter.Close(); err != nil {
it.err = err
return false
}
if it.idx == len(it.iters) {
return false
}
currIter = it.iters[it.idx]
}
return true
}
func (it *iterator) current() doc.Document {
return it.iters[it.idx].Current()
}
func (it *iterator) Current() doc.Document {
return it.currDoc
}
func (it *iterator) Err() error {
return it.err
}
func (it *iterator) Close() error {
if it.iters == nil {
return nil
}
var multiErr errors.MultiError
// close any iterators that weren't closed in Next.
for i := it.idx; i < len(it.iters); i++ {
multiErr = multiErr.Add(it.iters[i].Close())
}
return multiErr.FinalError()
}
func (it *iterator) initIters() error {
it.iters = make([]doc.Iterator, len(it.readers))
for i, reader := range it.readers {
_, sp := it.ctx.StartTraceSpan(tracepoint.SearchExecutorIndexSearch)
var (
pl postings.List
err error
)
if readThrough, ok := reader.(search.ReadThroughSegmentSearcher); ok {
pl, err = readThrough.Search(it.query, it.searcher)
} else {
pl, err = it.searcher.Search(reader)
}
sp.Finish()
if err != nil {
return err
}
iter, err := reader.Docs(pl)
if err != nil {
return err
}
it.iters[i] = iter
}
return nil
}