forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_segments_terms_iter.go
225 lines (189 loc) · 5.75 KB
/
multi_segments_terms_iter.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2019 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 builder
import (
"errors"
"github.com/m3db/m3/src/m3ninx/index/segment"
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/postings/roaring"
xerrors "github.com/m3db/m3/src/x/errors"
bitmap "github.com/m3db/pilosa/roaring"
)
const (
defaultBitmapContainerPooling = 128
)
var (
errPostingsListNotRoaring = errors.New("postings list not a roaring postings list")
)
// Ensure for our use case that the terms iter from segments we return
// matches the signature for the terms iterator.
var _ segment.TermsIterator = &termsIterFromSegments{}
type termsIterFromSegments struct {
keyIter *multiKeyIterator
currPostingsList postings.MutableList
bitmapIter *bitmap.Iterator
segments []segmentTermsMetadata
err error
termsIters []*termsKeyIter
}
type segmentTermsMetadata struct {
segment segmentMetadata
termsIterable segment.TermsIterable
}
func newTermsIterFromSegments() *termsIterFromSegments {
b := bitmap.NewBitmapWithDefaultPooling(defaultBitmapContainerPooling)
return &termsIterFromSegments{
keyIter: newMultiKeyIterator(),
currPostingsList: roaring.NewPostingsListFromBitmap(b),
bitmapIter: &bitmap.Iterator{},
}
}
func (i *termsIterFromSegments) clear() {
i.segments = nil
i.clearTermIters()
}
func (i *termsIterFromSegments) clearTermIters() {
i.keyIter.reset()
i.currPostingsList.Reset()
i.err = nil
for _, termIter := range i.termsIters {
termIter.iter = nil
termIter.segment = segmentMetadata{}
}
}
func (i *termsIterFromSegments) reset(segments []segmentMetadata) {
i.clear()
for _, seg := range segments {
i.segments = append(i.segments, segmentTermsMetadata{
segment: seg,
termsIterable: seg.segment.TermsIterable(),
})
}
}
func (i *termsIterFromSegments) setField(field []byte) error {
i.clearTermIters()
// Alloc any required terms iter containers
numTermsIterAlloc := len(i.segments) - len(i.termsIters)
for j := 0; j < numTermsIterAlloc; j++ {
i.termsIters = append(i.termsIters, &termsKeyIter{})
}
// Add our de-duping multi key value iterator
i.keyIter.reset()
for j, seg := range i.segments {
iter, err := seg.termsIterable.Terms(field)
if err != nil {
return err
}
if !iter.Next() {
// Don't consume this iterator if no results
if err := xerrors.FirstError(iter.Err(), iter.Close()); err != nil {
return err
}
continue
}
tersmKeyIter := i.termsIters[j]
tersmKeyIter.iter = iter
tersmKeyIter.segment = seg.segment
i.keyIter.add(tersmKeyIter)
}
return nil
}
func (i *termsIterFromSegments) Next() bool {
if i.err != nil {
return false
}
if !i.keyIter.Next() {
return false
}
// Create the overlayed postings list for this term
i.currPostingsList.Reset()
for _, iter := range i.keyIter.CurrentIters() {
termsKeyIter := iter.(*termsKeyIter)
_, list := termsKeyIter.iter.Current()
if termsKeyIter.segment.offset == 0 {
// No offset, which means is first segment we are combining from
// so can just direct union
i.currPostingsList.Union(list)
continue
}
// We have to taken into account the offset and duplicates
var (
iter = i.bitmapIter
duplicates = termsKeyIter.segment.duplicatesAsc
negativeOffset postings.ID
)
bitmap, ok := roaring.BitmapFromPostingsList(list)
if !ok {
i.err = errPostingsListNotRoaring
return false
}
iter.Reset(bitmap)
for v, eof := iter.Next(); !eof; v, eof = iter.Next() {
curr := postings.ID(v)
for len(duplicates) > 0 && curr > duplicates[0] {
duplicates = duplicates[1:]
negativeOffset++
}
if len(duplicates) > 0 && curr == duplicates[0] {
duplicates = duplicates[1:]
negativeOffset++
// Also skip this value, as itself is a duplicate
continue
}
value := curr + termsKeyIter.segment.offset - negativeOffset
_ = i.currPostingsList.Insert(value)
}
}
return true
}
func (i *termsIterFromSegments) Current() ([]byte, postings.List) {
return i.keyIter.Current(), i.currPostingsList
}
func (i *termsIterFromSegments) Err() error {
if err := i.keyIter.Err(); err != nil {
return err
}
return i.err
}
func (i *termsIterFromSegments) Close() error {
err := i.keyIter.Close()
// Free resources
i.clearTermIters()
return err
}
// termsKeyIter needs to be a keyIterator and contains a terms iterator
var _ keyIterator = &termsKeyIter{}
type termsKeyIter struct {
iter segment.TermsIterator
segment segmentMetadata
}
func (i *termsKeyIter) Next() bool {
return i.iter.Next()
}
func (i *termsKeyIter) Current() []byte {
t, _ := i.iter.Current()
return t
}
func (i *termsKeyIter) Err() error {
return i.iter.Err()
}
func (i *termsKeyIter) Close() error {
return i.iter.Close()
}