-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsection_inverted_text_index.go
1038 lines (861 loc) · 27 KB
/
section_inverted_text_index.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2023 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 zap
import (
"bytes"
"encoding/binary"
"math"
"sort"
"sync/atomic"
"github.com/RoaringBitmap/roaring"
index "github.com/blevesearch/bleve_index_api"
seg "github.com/blevesearch/scorch_segment_api/v2"
"github.com/blevesearch/vellum"
)
func init() {
registerSegmentSection(SectionInvertedTextIndex, &invertedTextIndexSection{})
}
type invertedTextIndexSection struct {
}
// This function checks whether the inverted text index section should avoid processing
// a particular field, preventing unnecessary work if another section will handle it.
//
// NOTE: The exclusion check is applicable only to the InvertedTextIndexSection
// because it serves as a catch-all section. This section processes every field
// unless explicitly excluded, similar to a "default" case in a switch statement.
// Other sections, such as VectorSection and SynonymSection, rely on inclusion
// checks to process only specific field types (e.g., index.VectorField or
// index.SynonymField). Any new section added in the future must define its
// special field type and inclusion logic explicitly.
var isFieldExcludedFromInvertedTextIndexSection = func(field index.Field) bool {
for _, excludeField := range invertedTextIndexSectionExclusionChecks {
if excludeField(field) {
// atleast one section has agreed to exclude this field
// from inverted text index section processing and has
// agreed to process it independently
return true
}
}
// no section has excluded this field from inverted index processing
// so it should be processed by the inverted index section
return false
}
// List of checks to determine if a field is excluded from the inverted text index section
var invertedTextIndexSectionExclusionChecks = make([]func(field index.Field) bool, 0)
func (i *invertedTextIndexSection) Process(opaque map[int]resetable, docNum uint32, field index.Field, fieldID uint16) {
if !isFieldExcludedFromInvertedTextIndexSection(field) {
invIndexOpaque := i.getInvertedIndexOpaque(opaque)
invIndexOpaque.process(field, fieldID, docNum)
}
}
func (i *invertedTextIndexSection) Persist(opaque map[int]resetable, w *CountHashWriter) (n int64, err error) {
invIndexOpaque := i.getInvertedIndexOpaque(opaque)
_, err = invIndexOpaque.writeDicts(w)
return 0, err
}
func (i *invertedTextIndexSection) AddrForField(opaque map[int]resetable, fieldID int) int {
invIndexOpaque := i.getInvertedIndexOpaque(opaque)
return invIndexOpaque.fieldAddrs[fieldID]
}
func mergeAndPersistInvertedSection(segments []*SegmentBase, dropsIn []*roaring.Bitmap,
fieldsInv []string, fieldsMap map[string]uint16, fieldsSame bool,
newDocNumsIn [][]uint64, newSegDocCount uint64, chunkMode uint32,
w *CountHashWriter, closeCh chan struct{}) (map[int]int, uint64, error) {
var bufMaxVarintLen64 []byte = make([]byte, binary.MaxVarintLen64)
var bufLoc []uint64
var postings *PostingsList
var postItr *PostingsIterator
fieldAddrs := make(map[int]int)
dictOffsets := make([]uint64, len(fieldsInv))
fieldDvLocsStart := make([]uint64, len(fieldsInv))
fieldDvLocsEnd := make([]uint64, len(fieldsInv))
// these int coders are initialized with chunk size 1024
// however this will be reset to the correct chunk size
// while processing each individual field-term section
tfEncoder := newChunkedIntCoder(1024, newSegDocCount-1)
locEncoder := newChunkedIntCoder(1024, newSegDocCount-1)
var vellumBuf bytes.Buffer
newVellum, err := vellum.New(&vellumBuf, nil)
if err != nil {
return nil, 0, err
}
newRoaring := roaring.NewBitmap()
newDocNums := make([][]uint64, 0, len(segments))
drops := make([]*roaring.Bitmap, 0, len(segments))
dicts := make([]*Dictionary, 0, len(segments))
itrs := make([]vellum.Iterator, 0, len(segments))
segmentsInFocus := make([]*SegmentBase, 0, len(segments))
// for each field
for fieldID, fieldName := range fieldsInv {
// collect FST iterators from all active segments for this field
newDocNums = newDocNums[:0]
drops = drops[:0]
dicts = dicts[:0]
itrs = itrs[:0]
segmentsInFocus = segmentsInFocus[:0]
for segmentI, segment := range segments {
// check for the closure in meantime
if isClosed(closeCh) {
return nil, 0, seg.ErrClosed
}
dict, err2 := segment.dictionary(fieldName)
if err2 != nil {
return nil, 0, err2
}
if dict != nil && dict.fst != nil {
itr, err2 := dict.fst.Iterator(nil, nil)
if err2 != nil && err2 != vellum.ErrIteratorDone {
return nil, 0, err2
}
if itr != nil {
newDocNums = append(newDocNums, newDocNumsIn[segmentI])
if dropsIn[segmentI] != nil && !dropsIn[segmentI].IsEmpty() {
drops = append(drops, dropsIn[segmentI])
} else {
drops = append(drops, nil)
}
dicts = append(dicts, dict)
itrs = append(itrs, itr)
segmentsInFocus = append(segmentsInFocus, segment)
}
}
}
var prevTerm []byte
newRoaring.Clear()
var lastDocNum, lastFreq, lastNorm uint64
// determines whether to use "1-hit" encoding optimization
// when a term appears in only 1 doc, with no loc info,
// has freq of 1, and the docNum fits into 31-bits
use1HitEncoding := func(termCardinality uint64) (bool, uint64, uint64) {
if termCardinality == uint64(1) && locEncoder.FinalSize() <= 0 {
docNum := uint64(newRoaring.Minimum())
if under32Bits(docNum) && docNum == lastDocNum && lastFreq == 1 {
return true, docNum, lastNorm
}
}
return false, 0, 0
}
finishTerm := func(term []byte) error {
tfEncoder.Close()
locEncoder.Close()
postingsOffset, err := writePostings(newRoaring,
tfEncoder, locEncoder, use1HitEncoding, w, bufMaxVarintLen64)
if err != nil {
return err
}
if postingsOffset > 0 {
err = newVellum.Insert(term, postingsOffset)
if err != nil {
return err
}
}
newRoaring.Clear()
tfEncoder.Reset()
locEncoder.Reset()
lastDocNum = 0
lastFreq = 0
lastNorm = 0
return nil
}
enumerator, err := newEnumerator(itrs)
for err == nil {
term, itrI, postingsOffset := enumerator.Current()
if !bytes.Equal(prevTerm, term) {
// check for the closure in meantime
if isClosed(closeCh) {
return nil, 0, seg.ErrClosed
}
// if the term changed, write out the info collected
// for the previous term
err = finishTerm(prevTerm)
if err != nil {
return nil, 0, err
}
}
if !bytes.Equal(prevTerm, term) || prevTerm == nil {
// compute cardinality of field-term in new seg
var newCard uint64
lowItrIdxs, lowItrVals := enumerator.GetLowIdxsAndValues()
for i, idx := range lowItrIdxs {
pl, err := dicts[idx].postingsListFromOffset(lowItrVals[i], drops[idx], nil)
if err != nil {
return nil, 0, err
}
newCard += pl.Count()
}
// compute correct chunk size with this
chunkSize, err := getChunkSize(chunkMode, newCard, newSegDocCount)
if err != nil {
return nil, 0, err
}
// update encoders chunk
tfEncoder.SetChunkSize(chunkSize, newSegDocCount-1)
locEncoder.SetChunkSize(chunkSize, newSegDocCount-1)
}
postings, err = dicts[itrI].postingsListFromOffset(
postingsOffset, drops[itrI], postings)
if err != nil {
return nil, 0, err
}
postItr = postings.iterator(true, true, true, postItr)
if fieldsSame {
// can optimize by copying freq/norm/loc bytes directly
lastDocNum, lastFreq, lastNorm, err = mergeTermFreqNormLocsByCopying(
term, postItr, newDocNums[itrI], newRoaring,
tfEncoder, locEncoder)
} else {
lastDocNum, lastFreq, lastNorm, bufLoc, err = mergeTermFreqNormLocs(
fieldsMap, term, postItr, newDocNums[itrI], newRoaring,
tfEncoder, locEncoder, bufLoc)
}
if err != nil {
return nil, 0, err
}
prevTerm = prevTerm[:0] // copy to prevTerm in case Next() reuses term mem
prevTerm = append(prevTerm, term...)
err = enumerator.Next()
}
if err != vellum.ErrIteratorDone {
return nil, 0, err
}
// close the enumerator to free the underlying iterators
err = enumerator.Close()
if err != nil {
return nil, 0, err
}
err = finishTerm(prevTerm)
if err != nil {
return nil, 0, err
}
dictOffset := uint64(w.Count())
err = newVellum.Close()
if err != nil {
return nil, 0, err
}
vellumData := vellumBuf.Bytes()
// write out the length of the vellum data
n := binary.PutUvarint(bufMaxVarintLen64, uint64(len(vellumData)))
_, err = w.Write(bufMaxVarintLen64[:n])
if err != nil {
return nil, 0, err
}
// write this vellum to disk
_, err = w.Write(vellumData)
if err != nil {
return nil, 0, err
}
dictOffsets[fieldID] = dictOffset
fieldDvLocsStart[fieldID] = uint64(w.Count())
// update the field doc values
// NOTE: doc values continue to use legacy chunk mode
chunkSize, err := getChunkSize(LegacyChunkMode, 0, 0)
if err != nil {
return nil, 0, err
}
fdvEncoder := newChunkedContentCoder(chunkSize, newSegDocCount-1, w, true)
fdvReadersAvailable := false
var dvIterClone *docValueReader
for segmentI, segment := range segmentsInFocus {
// check for the closure in meantime
if isClosed(closeCh) {
return nil, 0, seg.ErrClosed
}
fieldIDPlus1 := uint16(segment.fieldsMap[fieldName])
if dvIter, exists := segment.fieldDvReaders[SectionInvertedTextIndex][fieldIDPlus1-1]; exists &&
dvIter != nil {
fdvReadersAvailable = true
dvIterClone = dvIter.cloneInto(dvIterClone)
err = dvIterClone.iterateAllDocValues(segment, func(docNum uint64, terms []byte) error {
if newDocNums[segmentI][docNum] == docDropped {
return nil
}
err := fdvEncoder.Add(newDocNums[segmentI][docNum], terms)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, 0, err
}
}
}
if fdvReadersAvailable {
err = fdvEncoder.Close()
if err != nil {
return nil, 0, err
}
// persist the doc value details for this field
_, err = fdvEncoder.Write()
if err != nil {
return nil, 0, err
}
// get the field doc value offset (end)
fieldDvLocsEnd[fieldID] = uint64(w.Count())
} else {
fieldDvLocsStart[fieldID] = fieldNotUninverted
fieldDvLocsEnd[fieldID] = fieldNotUninverted
}
fieldStart := w.Count()
n = binary.PutUvarint(bufMaxVarintLen64, fieldDvLocsStart[fieldID])
_, err = w.Write(bufMaxVarintLen64[:n])
if err != nil {
return nil, 0, err
}
n = binary.PutUvarint(bufMaxVarintLen64, fieldDvLocsEnd[fieldID])
_, err = w.Write(bufMaxVarintLen64[:n])
if err != nil {
return nil, 0, err
}
n = binary.PutUvarint(bufMaxVarintLen64, dictOffsets[fieldID])
_, err = w.Write(bufMaxVarintLen64[:n])
if err != nil {
return nil, 0, err
}
fieldAddrs[fieldID] = fieldStart
// reset vellum buffer and vellum builder
vellumBuf.Reset()
err = newVellum.Reset(&vellumBuf)
if err != nil {
return nil, 0, err
}
}
fieldDvLocsOffset := uint64(w.Count())
return fieldAddrs, fieldDvLocsOffset, nil
}
func (i *invertedTextIndexSection) Merge(opaque map[int]resetable, segments []*SegmentBase,
drops []*roaring.Bitmap, fieldsInv []string, newDocNumsIn [][]uint64,
w *CountHashWriter, closeCh chan struct{}) error {
io := i.getInvertedIndexOpaque(opaque)
fieldAddrs, _, err := mergeAndPersistInvertedSection(segments, drops, fieldsInv,
io.FieldsMap, io.fieldsSame, newDocNumsIn, io.numDocs, io.chunkMode, w, closeCh)
if err != nil {
return err
}
io.fieldAddrs = fieldAddrs
return nil
}
func (i *invertedIndexOpaque) grabBuf(size int) []byte {
buf := i.tmp0
if cap(buf) < size {
buf = make([]byte, size)
i.tmp0 = buf
}
return buf[:size]
}
func (i *invertedIndexOpaque) incrementBytesWritten(bytes uint64) {
i.bytesWritten += bytes
}
func (i *invertedIndexOpaque) BytesWritten() uint64 {
return i.bytesWritten
}
func (i *invertedIndexOpaque) BytesRead() uint64 {
return 0
}
func (i *invertedIndexOpaque) ResetBytesRead(uint64) {}
func (io *invertedIndexOpaque) writeDicts(w *CountHashWriter) (dictOffsets []uint64, err error) {
if io.results == nil || len(io.results) == 0 {
return nil, nil
}
dictOffsets = make([]uint64, len(io.FieldsInv))
fdvOffsetsStart := make([]uint64, len(io.FieldsInv))
fdvOffsetsEnd := make([]uint64, len(io.FieldsInv))
buf := io.grabBuf(binary.MaxVarintLen64)
// these int coders are initialized with chunk size 1024
// however this will be reset to the correct chunk size
// while processing each individual field-term section
tfEncoder := newChunkedIntCoder(1024, uint64(len(io.results)-1))
locEncoder := newChunkedIntCoder(1024, uint64(len(io.results)-1))
var docTermMap [][]byte
if io.builder == nil {
io.builder, err = vellum.New(&io.builderBuf, nil)
if err != nil {
return nil, err
}
}
for fieldID, terms := range io.DictKeys {
if cap(docTermMap) < len(io.results) {
docTermMap = make([][]byte, len(io.results))
} else {
docTermMap = docTermMap[:len(io.results)]
for docNum := range docTermMap { // reset the docTermMap
docTermMap[docNum] = docTermMap[docNum][:0]
}
}
dict := io.Dicts[fieldID]
for _, term := range terms { // terms are already sorted
pid := dict[term] - 1
postingsBS := io.Postings[pid]
freqNorms := io.FreqNorms[pid]
freqNormOffset := 0
locs := io.Locs[pid]
locOffset := 0
var cardinality uint64
if postingsBS != nil {
cardinality = postingsBS.GetCardinality()
}
chunkSize, err := getChunkSize(io.chunkMode, cardinality, uint64(len(io.results)))
if err != nil {
return nil, err
}
tfEncoder.SetChunkSize(chunkSize, uint64(len(io.results)-1))
locEncoder.SetChunkSize(chunkSize, uint64(len(io.results)-1))
postingsItr := postingsBS.Iterator()
for postingsItr.HasNext() {
docNum := uint64(postingsItr.Next())
freqNorm := freqNorms[freqNormOffset]
// check if freq/norm is enabled
if freqNorm.freq > 0 {
err = tfEncoder.Add(docNum,
encodeFreqHasLocs(freqNorm.freq, freqNorm.numLocs > 0),
uint64(math.Float32bits(freqNorm.norm)))
} else {
// if disabled, then skip the norm part
err = tfEncoder.Add(docNum,
encodeFreqHasLocs(freqNorm.freq, freqNorm.numLocs > 0))
}
if err != nil {
return nil, err
}
if freqNorm.numLocs > 0 {
numBytesLocs := 0
for _, loc := range locs[locOffset : locOffset+freqNorm.numLocs] {
numBytesLocs += totalUvarintBytes(
uint64(loc.fieldID), loc.pos, loc.start, loc.end,
uint64(len(loc.arrayposs)), loc.arrayposs)
}
err = locEncoder.Add(docNum, uint64(numBytesLocs))
if err != nil {
return nil, err
}
for _, loc := range locs[locOffset : locOffset+freqNorm.numLocs] {
err = locEncoder.Add(docNum,
uint64(loc.fieldID), loc.pos, loc.start, loc.end,
uint64(len(loc.arrayposs)))
if err != nil {
return nil, err
}
err = locEncoder.Add(docNum, loc.arrayposs...)
if err != nil {
return nil, err
}
}
locOffset += freqNorm.numLocs
}
freqNormOffset++
docTermMap[docNum] = append(
append(docTermMap[docNum], term...),
termSeparator)
}
tfEncoder.Close()
locEncoder.Close()
io.incrementBytesWritten(locEncoder.getBytesWritten())
io.incrementBytesWritten(tfEncoder.getBytesWritten())
postingsOffset, err :=
writePostings(postingsBS, tfEncoder, locEncoder, nil, w, buf)
if err != nil {
return nil, err
}
if postingsOffset > uint64(0) {
err = io.builder.Insert([]byte(term), postingsOffset)
if err != nil {
return nil, err
}
}
tfEncoder.Reset()
locEncoder.Reset()
}
err = io.builder.Close()
if err != nil {
return nil, err
}
// record where this dictionary starts
dictOffsets[fieldID] = uint64(w.Count())
vellumData := io.builderBuf.Bytes()
// write out the length of the vellum data
n := binary.PutUvarint(buf, uint64(len(vellumData)))
_, err = w.Write(buf[:n])
if err != nil {
return nil, err
}
io.incrementBytesWritten(uint64(len(vellumData)))
// write this vellum to disk
_, err = w.Write(vellumData)
if err != nil {
return nil, err
}
// reset vellum for reuse
io.builderBuf.Reset()
err = io.builder.Reset(&io.builderBuf)
if err != nil {
return nil, err
}
// write the field doc values
// NOTE: doc values continue to use legacy chunk mode
chunkSize, err := getChunkSize(LegacyChunkMode, 0, 0)
if err != nil {
return nil, err
}
fdvEncoder := newChunkedContentCoder(chunkSize, uint64(len(io.results)-1), w, false)
if io.IncludeDocValues[fieldID] {
for docNum, docTerms := range docTermMap {
if len(docTerms) > 0 {
err = fdvEncoder.Add(uint64(docNum), docTerms)
if err != nil {
return nil, err
}
}
}
err = fdvEncoder.Close()
if err != nil {
return nil, err
}
io.incrementBytesWritten(fdvEncoder.getBytesWritten())
fdvOffsetsStart[fieldID] = uint64(w.Count())
_, err = fdvEncoder.Write()
if err != nil {
return nil, err
}
fdvOffsetsEnd[fieldID] = uint64(w.Count())
fdvEncoder.Reset()
} else {
fdvOffsetsStart[fieldID] = fieldNotUninverted
fdvOffsetsEnd[fieldID] = fieldNotUninverted
}
fieldStart := w.Count()
n = binary.PutUvarint(buf, fdvOffsetsStart[fieldID])
_, err = w.Write(buf[:n])
if err != nil {
return nil, err
}
n = binary.PutUvarint(buf, fdvOffsetsEnd[fieldID])
_, err = w.Write(buf[:n])
if err != nil {
return nil, err
}
n = binary.PutUvarint(buf, dictOffsets[fieldID])
_, err = w.Write(buf[:n])
if err != nil {
return nil, err
}
io.fieldAddrs[fieldID] = fieldStart
}
return dictOffsets, nil
}
func (io *invertedIndexOpaque) process(field index.Field, fieldID uint16, docNum uint32) {
if !io.init && io.results != nil {
io.realloc()
io.init = true
}
// if the fieldID is MaxUint16, it's mainly indicated that the caller has
// finished invoking the process() for every field on that doc.
if fieldID == math.MaxUint16 {
for fid, tfs := range io.reusableFieldTFs {
dict := io.Dicts[fid]
norm := math.Float32frombits(uint32(io.reusableFieldLens[fid]))
for term, tf := range tfs {
pid := dict[term] - 1
bs := io.Postings[pid]
bs.Add(uint32(docNum))
io.FreqNorms[pid] = append(io.FreqNorms[pid],
interimFreqNorm{
freq: uint64(tf.Frequency()),
norm: norm,
numLocs: len(tf.Locations),
})
if len(tf.Locations) > 0 {
locs := io.Locs[pid]
for _, loc := range tf.Locations {
var locf = uint16(fid)
if loc.Field != "" {
locf = uint16(io.getOrDefineField(loc.Field))
}
var arrayposs []uint64
if len(loc.ArrayPositions) > 0 {
arrayposs = loc.ArrayPositions
}
locs = append(locs, interimLoc{
fieldID: locf,
pos: uint64(loc.Position),
start: uint64(loc.Start),
end: uint64(loc.End),
arrayposs: arrayposs,
})
}
io.Locs[pid] = locs
}
}
}
for i := 0; i < len(io.FieldsInv); i++ { // clear these for reuse
io.reusableFieldLens[i] = 0
io.reusableFieldTFs[i] = nil
}
return
}
io.reusableFieldLens[fieldID] += field.AnalyzedLength()
existingFreqs := io.reusableFieldTFs[fieldID]
if existingFreqs != nil {
existingFreqs.MergeAll(field.Name(), field.AnalyzedTokenFrequencies())
} else {
io.reusableFieldTFs[fieldID] = field.AnalyzedTokenFrequencies()
}
}
func (i *invertedIndexOpaque) realloc() {
var pidNext int
var totTFs int
var totLocs int
i.FieldsMap = map[string]uint16{}
i.getOrDefineField("_id") // _id field is fieldID 0
for _, result := range i.results {
result.VisitComposite(func(field index.CompositeField) {
i.getOrDefineField(field.Name())
})
result.VisitFields(func(field index.Field) {
i.getOrDefineField(field.Name())
})
}
sort.Strings(i.FieldsInv[1:]) // keep _id as first field
for fieldID, fieldName := range i.FieldsInv {
i.FieldsMap[fieldName] = uint16(fieldID + 1)
}
visitField := func(field index.Field) {
fieldID := uint16(i.getOrDefineField(field.Name()))
dict := i.Dicts[fieldID]
dictKeys := i.DictKeys[fieldID]
tfs := field.AnalyzedTokenFrequencies()
for term, tf := range tfs {
pidPlus1, exists := dict[term]
if !exists {
pidNext++
pidPlus1 = uint64(pidNext)
dict[term] = pidPlus1
dictKeys = append(dictKeys, term)
i.numTermsPerPostingsList = append(i.numTermsPerPostingsList, 0)
i.numLocsPerPostingsList = append(i.numLocsPerPostingsList, 0)
}
pid := pidPlus1 - 1
i.numTermsPerPostingsList[pid] += 1
i.numLocsPerPostingsList[pid] += len(tf.Locations)
totLocs += len(tf.Locations)
}
totTFs += len(tfs)
i.DictKeys[fieldID] = dictKeys
if field.Options().IncludeDocValues() {
i.IncludeDocValues[fieldID] = true
}
}
if cap(i.IncludeDocValues) >= len(i.FieldsInv) {
i.IncludeDocValues = i.IncludeDocValues[:len(i.FieldsInv)]
} else {
i.IncludeDocValues = make([]bool, len(i.FieldsInv))
}
for _, result := range i.results {
// walk each composite field
result.VisitComposite(func(field index.CompositeField) {
visitField(field)
})
// walk each field
result.VisitFields(visitField)
}
numPostingsLists := pidNext
if cap(i.Postings) >= numPostingsLists {
i.Postings = i.Postings[:numPostingsLists]
} else {
postings := make([]*roaring.Bitmap, numPostingsLists)
copy(postings, i.Postings[:cap(i.Postings)])
for i := 0; i < numPostingsLists; i++ {
if postings[i] == nil {
postings[i] = roaring.New()
}
}
i.Postings = postings
}
if cap(i.FreqNorms) >= numPostingsLists {
i.FreqNorms = i.FreqNorms[:numPostingsLists]
} else {
i.FreqNorms = make([][]interimFreqNorm, numPostingsLists)
}
if cap(i.freqNormsBacking) >= totTFs {
i.freqNormsBacking = i.freqNormsBacking[:totTFs]
} else {
i.freqNormsBacking = make([]interimFreqNorm, totTFs)
}
freqNormsBacking := i.freqNormsBacking
for pid, numTerms := range i.numTermsPerPostingsList {
i.FreqNorms[pid] = freqNormsBacking[0:0]
freqNormsBacking = freqNormsBacking[numTerms:]
}
if cap(i.Locs) >= numPostingsLists {
i.Locs = i.Locs[:numPostingsLists]
} else {
i.Locs = make([][]interimLoc, numPostingsLists)
}
if cap(i.locsBacking) >= totLocs {
i.locsBacking = i.locsBacking[:totLocs]
} else {
i.locsBacking = make([]interimLoc, totLocs)
}
locsBacking := i.locsBacking
for pid, numLocs := range i.numLocsPerPostingsList {
i.Locs[pid] = locsBacking[0:0]
locsBacking = locsBacking[numLocs:]
}
for _, dict := range i.DictKeys {
sort.Strings(dict)
}
if cap(i.reusableFieldTFs) >= len(i.FieldsInv) {
i.reusableFieldTFs = i.reusableFieldTFs[:len(i.FieldsInv)]
} else {
i.reusableFieldTFs = make([]index.TokenFrequencies, len(i.FieldsInv))
}
if cap(i.reusableFieldLens) >= len(i.FieldsInv) {
i.reusableFieldLens = i.reusableFieldLens[:len(i.FieldsInv)]
} else {
i.reusableFieldLens = make([]int, len(i.FieldsInv))
}
}
func (i *invertedTextIndexSection) getInvertedIndexOpaque(opaque map[int]resetable) *invertedIndexOpaque {
if _, ok := opaque[SectionInvertedTextIndex]; !ok {
opaque[SectionInvertedTextIndex] = i.InitOpaque(nil)
}
return opaque[SectionInvertedTextIndex].(*invertedIndexOpaque)
}
func (i *invertedIndexOpaque) getOrDefineField(fieldName string) int {
fieldIDPlus1, exists := i.FieldsMap[fieldName]
if !exists {
fieldIDPlus1 = uint16(len(i.FieldsInv) + 1)
i.FieldsMap[fieldName] = fieldIDPlus1
i.FieldsInv = append(i.FieldsInv, fieldName)
i.Dicts = append(i.Dicts, make(map[string]uint64))
n := len(i.DictKeys)
if n < cap(i.DictKeys) {
i.DictKeys = i.DictKeys[:n+1]
i.DictKeys[n] = i.DictKeys[n][:0]
} else {
i.DictKeys = append(i.DictKeys, []string(nil))
}
}
return int(fieldIDPlus1 - 1)
}
func (i *invertedTextIndexSection) InitOpaque(args map[string]interface{}) resetable {
rv := &invertedIndexOpaque{
fieldAddrs: map[int]int{},
}
for k, v := range args {
rv.Set(k, v)
}
return rv
}
type invertedIndexOpaque struct {
results []index.Document
chunkMode uint32
// indicates whethere the following structs are initialized
init bool
// FieldsMap adds 1 to field id to avoid zero value issues
// name -> field id + 1
FieldsMap map[string]uint16
// FieldsInv is the inverse of FieldsMap
// field id -> name
FieldsInv []string
// Term dictionaries for each field
// field id -> term -> postings list id + 1
Dicts []map[string]uint64
// Terms for each field, where terms are sorted ascending
// field id -> []term
DictKeys [][]string
// Fields whose IncludeDocValues is true
// field id -> bool
IncludeDocValues []bool
// postings id -> bitmap of docNums
Postings []*roaring.Bitmap
// postings id -> freq/norm's, one for each docNum in postings
FreqNorms [][]interimFreqNorm
freqNormsBacking []interimFreqNorm
// postings id -> locs, one for each freq
Locs [][]interimLoc
locsBacking []interimLoc
numTermsPerPostingsList []int // key is postings list id
numLocsPerPostingsList []int // key is postings list id
builder *vellum.Builder
builderBuf bytes.Buffer
// reusable stuff for processing fields etc.
reusableFieldLens []int
reusableFieldTFs []index.TokenFrequencies
tmp0 []byte
fieldAddrs map[int]int
bytesWritten uint64
fieldsSame bool
numDocs uint64
}
func (io *invertedIndexOpaque) Reset() (err error) {
// cleanup stuff over here
io.results = nil
io.init = false
io.chunkMode = 0
io.FieldsMap = nil
io.FieldsInv = nil
for i := range io.Dicts {
io.Dicts[i] = nil
}
io.Dicts = io.Dicts[:0]
for i := range io.DictKeys {
io.DictKeys[i] = io.DictKeys[i][:0]
}
io.DictKeys = io.DictKeys[:0]
for i := range io.IncludeDocValues {
io.IncludeDocValues[i] = false
}
io.IncludeDocValues = io.IncludeDocValues[:0]
for _, idn := range io.Postings {
idn.Clear()
}
io.Postings = io.Postings[:0]
io.FreqNorms = io.FreqNorms[:0]
for i := range io.freqNormsBacking {
io.freqNormsBacking[i] = interimFreqNorm{}