-
Notifications
You must be signed in to change notification settings - Fork 15
/
segment.go
341 lines (290 loc) · 8.57 KB
/
segment.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2020 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ice
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"sync"
"github.com/RoaringBitmap/roaring"
"github.com/blevesearch/vellum"
segment "github.com/blugelabs/bluge_segment_api"
)
const Version uint32 = 2
const Type string = "ice"
type Segment struct {
data *segment.Data
footer *footer
fieldsMap map[string]uint16 // fieldName -> fieldID+1
fieldsInv []string // fieldID -> fieldName
fieldDocs map[uint16]uint64 // fieldID -> # docs with value in field
fieldFreqs map[uint16]uint64 // fieldID -> # total tokens in field
storedFieldChunkOffsets []uint64 // stored field chunk offset
storedFieldChunkUncompressed []byte // for uncompress cache
dictLocs []uint64
fieldDvReaders map[uint16]*docValueReader // naive chunk cache per field
fieldDvNames []string // field names cached in fieldDvReaders
size uint64
// state loaded dynamically
m sync.Mutex
fieldFSTs map[uint16]*vellum.FST
}
func (s *Segment) WriteTo(w io.Writer, _ chan struct{}) (int64, error) {
bw := bufio.NewWriter(w)
n, err := s.data.WriteTo(w)
if err != nil {
return n, fmt.Errorf("error persisting segment: %w", err)
}
err = persistFooter(s.footer, bw)
if err != nil {
return n, fmt.Errorf("error persisting segment footer: %w", err)
}
err = bw.Flush()
if err != nil {
return n, err
}
return n + footerLen, nil
}
func (s *Segment) Type() string {
return Type
}
// Version returns the file version in the file footer
func (s *Segment) Version() uint32 {
return s.footer.version
}
func (s *Segment) Size() int {
return int(s.size)
}
func (s *Segment) updateSize() {
sizeInBytes := reflectStaticSizeSegment +
s.data.Size()
// fieldsMap
for k := range s.fieldsMap {
sizeInBytes += (len(k) + sizeOfString) + sizeOfUint16
}
// fieldsInv, dictLocs
for _, entry := range s.fieldsInv {
sizeInBytes += len(entry) + sizeOfString
}
sizeInBytes += len(s.dictLocs) * sizeOfUint64
// fieldDvReaders
for _, v := range s.fieldDvReaders {
sizeInBytes += sizeOfUint16 + sizeOfPtr
if v != nil {
sizeInBytes += v.size()
}
}
s.size = uint64(sizeInBytes)
}
// DictionaryReader returns the term dictionary for the specified field
func (s *Segment) Dictionary(field string) (segment.Dictionary, error) {
dict, err := s.dictionary(field)
if err == nil && dict == nil {
return emptyDictionary, nil
}
return dict, err
}
func (s *Segment) dictionary(field string) (rv *Dictionary, err error) {
fieldIDPlus1 := s.fieldsMap[field]
if fieldIDPlus1 > 0 {
rv = &Dictionary{
sb: s,
field: field,
fieldID: fieldIDPlus1 - 1,
}
dictStart := s.dictLocs[rv.fieldID]
if dictStart > 0 {
var ok bool
s.m.Lock()
if rv.fst, ok = s.fieldFSTs[rv.fieldID]; !ok {
// read the length of the vellum data
var vellumLenData []byte
vellumLenData, err = s.data.Read(int(dictStart), int(dictStart+binary.MaxVarintLen64))
if err != nil {
return nil, err
}
vellumLen, read := binary.Uvarint(vellumLenData)
var fstBytes []byte
fstBytes, err = s.data.Read(int(dictStart+uint64(read)), int(dictStart+uint64(read)+vellumLen))
if err != nil {
return nil, err
}
rv.fst, err = vellum.Load(fstBytes)
if err != nil {
s.m.Unlock()
return nil, fmt.Errorf("dictionary field %s vellum err: %v", field, err)
}
s.fieldFSTs[rv.fieldID] = rv.fst
}
s.m.Unlock()
rv.fstReader, err = rv.fst.Reader()
if err != nil {
return nil, fmt.Errorf("dictionary field %s vellum reader err: %v", field, err)
}
}
}
return rv, nil
}
// visitDocumentCtx holds data structures that are reusable across
// multiple VisitStoredFields() calls to avoid memory allocations
type visitDocumentCtx struct {
buf []byte
reader bytes.Reader
}
var visitDocumentCtxPool = sync.Pool{
New: func() interface{} {
reuse := &visitDocumentCtx{}
return reuse
},
}
// VisitStoredFields invokes the DocFieldValueVistor for each stored field
// for the specified doc number
func (s *Segment) VisitStoredFields(num uint64, visitor segment.StoredFieldVisitor) error {
vdc := visitDocumentCtxPool.Get().(*visitDocumentCtx)
defer visitDocumentCtxPool.Put(vdc)
return s.visitDocument(vdc, num, visitor)
}
func (s *Segment) visitDocument(vdc *visitDocumentCtx, num uint64,
visitor segment.StoredFieldVisitor) error {
// first make sure this is a valid number in this segment
if num < s.footer.numDocs {
meta, uncompressed, err := s.getDocStoredMetaAndUnCompressed(num)
if err != nil {
return err
}
vdc.reader.Reset(meta)
var keepGoing = true
for keepGoing {
field, err := binary.ReadUvarint(&vdc.reader)
if err == io.EOF {
break
}
if err != nil {
return err
}
offset, err := binary.ReadUvarint(&vdc.reader)
if err != nil {
return err
}
l, err := binary.ReadUvarint(&vdc.reader)
if err != nil {
return err
}
value := uncompressed[offset : offset+l]
keepGoing = visitor(s.fieldsInv[field], value)
}
vdc.buf = uncompressed
}
return nil
}
// Count returns the number of documents in this segment.
func (s *Segment) Count() uint64 {
return s.footer.numDocs
}
func (s *Segment) DocsMatchingTerms(terms []segment.Term) (*roaring.Bitmap, error) {
rv := roaring.New()
if len(s.fieldsMap) > 0 {
// we expect the common case to be the same field for all
// so we optimize for that, but allow it to work if that
// isn't the case
var err error
var lastField string
var dict *Dictionary
for i, term := range terms {
thisField := term.Field()
if thisField != lastField {
dict, err = s.dictionary(term.Field())
if err != nil {
return nil, err
}
lastField = thisField
}
term := terms[i]
postingsList := emptyPostingsList
postingsList, err = dict.postingsList(term.Term(), nil, postingsList)
if err != nil {
return nil, err
}
postingsList.OrInto(rv)
}
}
return rv, nil
}
// Fields returns the field names used in this segment
func (s *Segment) Fields() []string {
return s.fieldsInv
}
// CRC returns the CRC value stored in the file footer
func (s *Segment) CRC() uint32 {
return s.footer.crc
}
// ChunkFactor returns the chunk factor in the file footer
func (s *Segment) ChunkMode() uint32 {
return s.footer.chunkMode
}
// FieldsIndexOffset returns the fields index offset in the file footer
func (s *Segment) FieldsIndexOffset() uint64 {
return s.footer.fieldsIndexOffset
}
// StoredIndexOffset returns the stored value index offset in the file footer
func (s *Segment) StoredIndexOffset() uint64 {
return s.footer.storedIndexOffset
}
// DocValueOffset returns the docValue offset in the file footer
func (s *Segment) DocValueOffset() uint64 {
return s.footer.docValueOffset
}
// NumDocs returns the number of documents in the file footer
func (s *Segment) NumDocs() uint64 {
return s.footer.numDocs
}
func (s *Segment) loadDvReaders() error {
if s.footer.docValueOffset == fieldNotUninverted || s.footer.numDocs == 0 {
return nil
}
var read uint64
for fieldID, field := range s.fieldsInv {
var fieldLocStart, fieldLocEnd uint64
var n int
fieldLocStartData, err := s.data.Read(int(s.footer.docValueOffset+read), int(s.footer.docValueOffset+read+binary.MaxVarintLen64))
if err != nil {
return err
}
fieldLocStart, n = binary.Uvarint(fieldLocStartData)
if n <= 0 {
return fmt.Errorf("loadDvReaders: failed to read the docvalue offset start for field %d", fieldID)
}
read += uint64(n)
fieldLocEndData, err := s.data.Read(int(s.footer.docValueOffset+read), int(s.footer.docValueOffset+read+binary.MaxVarintLen64))
if err != nil {
return err
}
fieldLocEnd, n = binary.Uvarint(fieldLocEndData)
if n <= 0 {
return fmt.Errorf("loadDvReaders: failed to read the docvalue offset end for field %d", fieldID)
}
read += uint64(n)
fieldDvReader, err := s.loadFieldDocValueReader(field, fieldLocStart, fieldLocEnd)
if err != nil {
return err
}
if fieldDvReader != nil {
s.fieldDvReaders[uint16(fieldID)] = fieldDvReader
s.fieldDvNames = append(s.fieldDvNames, field)
}
}
return nil
}