-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreader.go
310 lines (275 loc) · 8.44 KB
/
reader.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
// Copyright (c) 2021 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 sear
import (
"bytes"
"context"
"fmt"
"sort"
"strings"
index "github.com/blevesearch/bleve_index_api"
"github.com/blevesearch/vellum"
vellev "github.com/blevesearch/vellum/levenshtein"
velreg "github.com/blevesearch/vellum/regexp"
)
var internalDocID = []byte{0}
// Reader is responsible for reading the index data
// It is also responsible for caching some portions
// of a read operation which can be used for subsequent
// reads.
type Reader struct {
s *Sear
velregCache map[string]*velreg.Regexp
levSlice []int
}
// NewReader returns a new reader for the provided Sear instance.
func NewReader(m *Sear) *Reader {
rv := &Reader{
s: m,
velregCache: make(map[string]*velreg.Regexp),
levSlice: make([]int, 64),
}
return rv
}
func (r *Reader) TermFieldReader(ctx context.Context, term []byte, field string, includeFreq, includeNorm,
includeTermVectors bool) (index.TermFieldReader, error) {
if r.s.doc == nil {
return termFieldReaderEmpty, nil
}
atf, l, err := r.s.doc.TokenFreqsAndLen(field)
if err != nil {
// only error is field doesn't exist in doc
return termFieldReaderEmpty, nil
}
tf, ok := atf[string(term)]
if !ok {
return termFieldReaderEmpty, nil
}
return NewTermFieldReaderFromTokenFreqAndLen(tf, l, includeFreq, includeNorm, includeTermVectors), nil
}
func (r *Reader) DocIDReaderAll() (index.DocIDReader, error) {
if r.s.doc == nil {
return docIDReaderEmpty, nil
}
return NewDocIDReader(), nil
}
func (r *Reader) DocIDReaderOnly(ids []string) (index.DocIDReader, error) {
if r.s.doc == nil {
return docIDReaderEmpty, nil
}
for _, id := range ids {
if id == r.s.doc.doc.ID() {
return NewDocIDReader(), nil
}
}
return docIDReaderEmpty, nil
}
func (r *Reader) FieldDict(field string) (index.FieldDict, error) {
if r.s.doc == nil {
return fieldDictEmpty, nil
}
fieldSortedTerms, err := r.s.doc.SortedTermsForField(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictEmpty, nil
}
return NewFieldDictWithTerms(fieldSortedTerms, nil), nil
}
func (r *Reader) FieldDictRange(field string, startTerm, endTerm []byte) (index.FieldDict, error) {
if r.s.doc == nil {
return fieldDictEmpty, nil
}
fieldSortedTerms, err := r.s.doc.SortedTermsForField(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictEmpty, nil
}
startIdx := sort.SearchStrings(fieldSortedTerms, string(startTerm))
endTermStr := string(endTerm)
endIdx := sort.SearchStrings(fieldSortedTerms[startIdx:], endTermStr)
endIdx += startIdx
// fix up inclusive end (required by bleve API)
if endIdx < len(fieldSortedTerms) && fieldSortedTerms[endIdx] == endTermStr {
endIdx++
}
return NewFieldDictWithTerms(fieldSortedTerms[startIdx:endIdx], nil), nil
}
func (r *Reader) FieldDictPrefix(field string, termPrefix []byte) (index.FieldDict, error) {
if r.s.doc == nil {
return fieldDictEmpty, nil
}
fieldSortedTerms, err := r.s.doc.SortedTermsForField(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictEmpty, nil
}
prefixStr := string(termPrefix)
startIdx := sort.SearchStrings(fieldSortedTerms, prefixStr)
rest := fieldSortedTerms[startIdx:]
endIdx := sort.Search(len(rest), func(i int) bool {
return !strings.HasPrefix(rest[i], prefixStr)
})
return NewFieldDictWithTerms(rest[:endIdx], fieldDictPrefix(prefixStr)), nil
}
func automatonMatch(la vellum.Automaton, termStr string) bool {
state := la.Start()
for i := range []byte(termStr) {
state = la.Accept(state, termStr[i])
if !la.CanMatch(state) {
return false
}
}
return la.IsMatch(state)
}
func (r *Reader) FieldDictRegexp(field, regexStr string) (index.FieldDict, error) {
fd, _, err := r.fieldDictRegexp(field, regexStr)
return fd, err
}
func (r *Reader) FieldDictRegexpAutomaton(field, regexStr string) (
index.FieldDict, index.RegexAutomaton, error) {
return r.fieldDictRegexp(field, regexStr)
}
func (r *Reader) fieldDictRegexp(field, regexStr string) (
index.FieldDict, index.RegexAutomaton, error) {
regex, cached := r.velregCache[regexStr]
if !cached {
var err error
regex, err = velreg.New(regexStr)
if err != nil {
return nil, nil, fmt.Errorf("error compiling regexp: %v", err)
}
r.velregCache[regexStr] = regex
}
if r.s.doc == nil {
return fieldDictEmpty, regex, nil
}
fieldSortedTerms, err := r.s.doc.SortedTermsForField(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictEmpty, regex, nil
}
return NewFieldDictWithTerms(fieldSortedTerms, func(s string) bool {
return automatonMatch(regex, s)
}), regex, nil
}
func (r *Reader) FieldDictFuzzy(field, term string, fuzziness int, prefix string) (
index.FieldDict, error) {
if r.s.doc == nil {
return fieldDictEmpty, nil
}
fieldSortedTerms, err := r.s.doc.SortedTermsForField(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictEmpty, nil
}
return NewFieldDictWithTerms(fieldSortedTerms, func(indexTerm string) bool {
var dist int
var exceeded bool
dist, exceeded, r.levSlice = levenshteinDistanceMaxReuseSlice(
term, indexTerm, fuzziness, r.levSlice)
if dist <= fuzziness && !exceeded {
return true
}
return false
}), nil
}
func (r *Reader) FieldDictFuzzyAutomaton(field, term string, fuzziness int, prefix string) (
index.FieldDict, index.FuzzyAutomaton, error) {
a, err := getLevAutomaton(term, uint8(fuzziness))
if err != nil {
return nil, nil, err
}
var fa index.FuzzyAutomaton
if vfa, ok := a.(vellum.FuzzyAutomaton); ok {
fa = vfa
}
fd, err := r.FieldDictFuzzy(field, term, fuzziness, prefix)
return fd, fa, err
}
func (r *Reader) FieldDictContains(field string) (index.FieldDictContains, error) {
if r.s.doc == nil {
return fieldDictContainsEmpty, nil
}
atf, _, err := r.s.doc.TokenFreqsAndLen(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictContainsEmpty, nil
}
return NewFieldDictContainsFromTokenFrequencies(atf), nil
}
func (r *Reader) Document(id string) (index.Document, error) {
if r.s.doc.doc.ID() == id {
return r.s.doc.doc, nil
}
return nil, fmt.Errorf("document not found")
}
func (r *Reader) DocValueReader(fields []string) (index.DocValueReader, error) {
return &DocValueReader{
r: r,
fields: fields,
}, nil
}
func (r *Reader) Fields() ([]string, error) {
if r.s.doc != nil {
return r.s.doc.Fields(), nil
}
return nil, nil
}
func (r *Reader) GetInternal(key []byte) ([]byte, error) {
return r.s.internal[string(key)], nil
}
func (r *Reader) DocCount() (uint64, error) {
if r.s.doc != nil {
return 1, nil
}
return 0, nil
}
func (r *Reader) ExternalID(id index.IndexInternalID) (string, error) {
if bytes.Equal(id, internalDocID) {
return r.s.doc.doc.ID(), nil
}
return "", fmt.Errorf("no such document with internal id: '%v'", id)
}
func (r *Reader) InternalID(id string) (index.IndexInternalID, error) {
if id == r.s.doc.doc.ID() {
return internalDocID, nil
}
return nil, fmt.Errorf("no such document with external id: %s", id)
}
func (r *Reader) Close() error {
return nil
}
// -----------------------------------------------------------------------------
// re usable, threadsafe levenshtein builders
var lb1, lb2 *vellev.LevenshteinAutomatonBuilder
func init() {
var err error
lb1, err = vellev.NewLevenshteinAutomatonBuilder(1, true)
if err != nil {
panic(fmt.Errorf("Levenshtein automaton ed1 builder err: %v", err))
}
lb2, err = vellev.NewLevenshteinAutomatonBuilder(2, true)
if err != nil {
panic(fmt.Errorf("Levenshtein automaton ed2 builder err: %v", err))
}
}
// https://github.com/blevesearch/bleve/blob/77458c4/index/scorch/snapshot_index.go#L291
func getLevAutomaton(term string, fuzziness uint8) (vellum.Automaton, error) {
if fuzziness == 1 {
return lb1.BuildDfa(term, fuzziness)
} else if fuzziness == 2 {
return lb2.BuildDfa(term, fuzziness)
}
return nil, fmt.Errorf("fuzziness exceeds the max limit")
}