-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsynonym_posting.go
239 lines (191 loc) · 6.47 KB
/
synonym_posting.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
// Copyright (c) 2024 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"
"fmt"
"reflect"
"github.com/RoaringBitmap/roaring"
"github.com/RoaringBitmap/roaring/roaring64"
segment "github.com/blevesearch/scorch_segment_api/v2"
)
var reflectStaticSizeSynonymsList int
var reflectStaticSizeSynonymsIterator int
var reflectStaticSizeSynonym int
func init() {
var sl SynonymsList
reflectStaticSizeSynonymsList = int(reflect.TypeOf(sl).Size())
var si SynonymsIterator
reflectStaticSizeSynonymsIterator = int(reflect.TypeOf(si).Size())
var s Synonym
reflectStaticSizeSynonym = int(reflect.TypeOf(s).Size())
}
// SynonymsList represents a list of synonyms for a term, stored in a Roaring64 bitmap.
type SynonymsList struct {
sb *SegmentBase
synonymsOffset uint64
synonyms *roaring64.Bitmap
except *roaring.Bitmap
synIDTermMap map[uint32][]byte
buffer *bytes.Reader
}
// immutable, empty synonyms list
var emptySynonymsList = &SynonymsList{}
func (p *SynonymsList) Size() int {
sizeInBytes := reflectStaticSizeSynonymsList + SizeOfPtr
if p.except != nil {
sizeInBytes += int(p.except.GetSizeInBytes())
}
return sizeInBytes
}
// Iterator creates and returns a SynonymsIterator for the SynonymsList.
// If the synonyms bitmap is nil, it returns an empty iterator.
func (s *SynonymsList) Iterator(prealloc segment.SynonymsIterator) segment.SynonymsIterator {
if s.synonyms == nil {
return emptySynonymsIterator
}
var preallocSI *SynonymsIterator
pi, ok := prealloc.(*SynonymsIterator)
if ok && pi != nil {
preallocSI = pi
}
if preallocSI == emptySynonymsIterator {
preallocSI = nil
}
return s.iterator(preallocSI)
}
// iterator initializes a SynonymsIterator for the SynonymsList and returns it.
// If a preallocated iterator is provided, it resets and reuses it; otherwise, it creates a new one.
func (s *SynonymsList) iterator(rv *SynonymsIterator) *SynonymsIterator {
if rv == nil {
rv = &SynonymsIterator{}
} else {
*rv = SynonymsIterator{} // clear the struct
}
rv.synonyms = s
rv.except = s.except
rv.Actual = s.synonyms.Iterator()
rv.ActualBM = s.synonyms
rv.synIDTermMap = s.synIDTermMap
return rv
}
// read initializes a SynonymsList by reading data from the given synonymsOffset in the Thesaurus.
// It reads and parses the Roaring64 bitmap that represents the synonyms.
func (rv *SynonymsList) read(synonymsOffset uint64, t *Thesaurus) error {
rv.synonymsOffset = synonymsOffset
var n uint64
var read int
var synonymsLen uint64
synonymsLen, read = binary.Uvarint(t.sb.mem[synonymsOffset+n : synonymsOffset+n+binary.MaxVarintLen64])
n += uint64(read)
roaringBytes := t.sb.mem[synonymsOffset+n : synonymsOffset+n+synonymsLen]
if rv.synonyms == nil {
rv.synonyms = roaring64.NewBitmap()
}
rv.buffer.Reset(roaringBytes)
_, err := rv.synonyms.ReadFrom(rv.buffer)
if err != nil {
return fmt.Errorf("error loading roaring bitmap: %v", err)
}
return nil
}
// -----------------------------------------------------------------------------
// SynonymsIterator provides a way to iterate through the synonyms list.
type SynonymsIterator struct {
synonyms *SynonymsList
except *roaring.Bitmap
Actual roaring64.IntPeekable64
ActualBM *roaring64.Bitmap
synIDTermMap map[uint32][]byte
nextSyn Synonym
}
// immutable, empty synonyms iterator
var emptySynonymsIterator = &SynonymsIterator{}
func (i *SynonymsIterator) Size() int {
sizeInBytes := reflectStaticSizeSynonymsIterator + SizeOfPtr +
i.nextSyn.Size()
return sizeInBytes
}
// Next returns the next Synonym in the iteration or an error if the end is reached.
func (i *SynonymsIterator) Next() (segment.Synonym, error) {
return i.next()
}
// next retrieves the next synonym from the iterator, populates the nextSyn field,
// and returns it. If no valid synonym is found, it returns an error.
func (i *SynonymsIterator) next() (segment.Synonym, error) {
synID, docNum, exists, err := i.nextSynonym()
if err != nil || !exists {
return nil, err
}
if i.synIDTermMap == nil {
return nil, fmt.Errorf("synIDTermMap is nil")
}
// If the synonymID is not found in the map, return an error
term, exists := i.synIDTermMap[synID]
if !exists {
return nil, fmt.Errorf("synonymID %d not found in map", synID)
}
i.nextSyn = Synonym{} // clear the struct
rv := &i.nextSyn
rv.term = string(term)
rv.docNum = docNum
return rv, nil
}
// nextSynonym decodes the next synonym from the roaring bitmap iterator,
// ensuring it is not in the "except" set. Returns the synonymID, docNum,
// and a flag indicating success.
func (i *SynonymsIterator) nextSynonym() (uint32, uint32, bool, error) {
// If no synonyms are available, return early
if i.Actual == nil || i.synonyms == nil || i.synonyms == emptySynonymsList {
return 0, 0, false, nil
}
var code uint64
var docNum uint32
var synID uint32
// Loop to find the next valid docNum, checking against the except
for i.Actual.HasNext() {
code = i.Actual.Next()
synID, docNum = decodeSynonym(code)
// If docNum is not in the 'except' set, it's a valid result
if i.except == nil || !i.except.Contains(docNum) {
return synID, docNum, true, nil
}
}
// If no valid docNum is found, return false
return 0, 0, false, nil
}
// Synonym represents a single synonym, containing the term, synonymID, and document number.
type Synonym struct {
term string
docNum uint32
}
// Size returns the memory size of the Synonym, including the length of the term string.
func (p *Synonym) Size() int {
sizeInBytes := reflectStaticSizeSynonym + SizeOfPtr +
len(p.term)
return sizeInBytes
}
// Term returns the term of the Synonym.
func (s *Synonym) Term() string {
return s.term
}
// Number returns the document number of the Synonym.
func (s *Synonym) Number() uint32 {
return s.docNum
}
// decodeSynonym decodes a synonymCode into its synonymID and document ID components.
func decodeSynonym(synonymCode uint64) (synonymID uint32, docID uint32) {
return uint32(synonymCode >> 32), uint32(synonymCode)
}