Repository navigation
Expand file tree
/
Copy pathgeo_index_cache.go
More file actions
484 lines (392 loc) · 10.6 KB
/
Copy pathgeo_index_cache.go
File metadata and controls
484 lines (392 loc) · 10.6 KB
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
// Copyright (c) 2026 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 (
"encoding/binary"
"fmt"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/RoaringBitmap/roaring/v2"
)
type geoIndexCache struct {
m sync.RWMutex
cache map[uint16]*geoCacheEntry
closeCh chan struct{}
isClosed bool
}
var geoMonitorFreq = 1 * time.Second
func newGeoIndexCache() *geoIndexCache {
return &geoIndexCache{
cache: make(map[uint16]*geoCacheEntry),
closeCh: make(chan struct{}),
}
}
func (gc *geoIndexCache) Clear() {
gc.m.Lock()
if gc.isClosed {
gc.m.Unlock()
return
}
gc.isClosed = true
close(gc.closeCh)
for _, entry := range gc.cache {
entry.Close()
}
gc.cache = nil
gc.m.Unlock()
}
func (gc *geoIndexCache) loadOrCreate(field uint16, mem []byte, except *roaring.Bitmap, r *FileReader) (*geoData, error) {
gc.m.RLock()
if gc.isClosed {
gc.m.RUnlock()
return nil, nil
}
entry, ok := gc.cache[field]
if ok {
gc.m.RUnlock()
return entry.load(except), nil
}
gc.m.RUnlock()
gc.m.Lock()
defer gc.m.Unlock()
if gc.isClosed {
return nil, nil
}
entry, ok = gc.cache[field]
if ok {
return entry.load(except), nil
}
return gc.createAndCacheLocked(field, mem, except, r)
}
func (gc *geoIndexCache) createAndCacheLocked(field uint16, mem []byte,
except *roaring.Bitmap, r *FileReader) (*geoData, error) {
var pos uint64
// Load Num Docs
numDocs, n := binary.Uvarint(mem[pos : pos+binary.MaxVarintLen64])
pos += uint64(n)
if numDocs == 0 {
return nil, fmt.Errorf("no geo docs found")
}
// Load Doc ID to Doc Num mapping
docNums, docNumsMem, shift, err := r.ReadUint32Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load the Document Scores Inner
docScoresInner, docScoresInnerMem, shift, err := r.ReadUint64Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load the Document Scores Cross
docScoresCross, docScoresCrossMem, shift, err := r.ReadUint64Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load Inner Cells
innerCells, innerCellsMem, shift, err := r.ReadUint64Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load Inner Cell Doc IDs
innerDocIDs, innerDocIDsMem, shift, err := r.ReadUint32Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load Cross Cells
crossCells, crossCellsMem, shift, err := r.ReadUint64Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load Cross Cell Doc IDs
crossDocIDs, crossDocIDsMem, shift, err := r.ReadUint32Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
// Load BBox Metadata without expanding the BBox data
bBoxesOffsets, bBoxesOffsetsMem, shift, err := r.ReadUint64Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
bBoxesLen, n := binary.Uvarint(mem[pos : pos+binary.MaxVarintLen64])
pos += uint64(n)
bboxMem := mem[pos : pos+bBoxesLen]
pos += bBoxesLen
// Load Shape Metadata without expanding the Shape data
shapeOffsets, shapeOffsetsMem, shift, err := r.ReadUint64Array(mem[pos:])
if err != nil {
return nil, err
}
pos += shift
shapeLen, n := binary.Uvarint(mem[pos : pos+binary.MaxVarintLen64])
pos += uint64(n)
shapeMem := mem[pos : pos+shapeLen]
pos += shapeLen
rv := &geoCacheEntry{
innerCells: innerCells,
innerCellsMem: innerCellsMem,
innerDocIDs: innerDocIDs,
innerDocIDsMem: innerDocIDsMem,
crossCells: crossCells,
crossCellsMem: crossCellsMem,
crossDocIDs: crossDocIDs,
crossDocIDsMem: crossDocIDsMem,
bboxOffsets: bBoxesOffsets,
bboxOffsetsMem: bBoxesOffsetsMem,
bboxMem: bboxMem,
shapeOffsets: shapeOffsets,
shapeOffsetsMem: shapeOffsetsMem,
shapeMem: shapeMem,
numDocs: numDocs,
docNums: docNums,
docNumsMem: docNumsMem,
docScoresInner: docScoresInner,
docScoresInnerMem: docScoresInnerMem,
docScoresCross: docScoresCross,
docScoresCrossMem: docScoresCrossMem,
tracker: &ewma{
alpha: 0.4,
sample: 1,
},
refs: 1,
fileReader: r,
scoresPool: sync.Pool{
New: func() interface{} {
var scores map[uint32]uint64
if numDocs > 100 {
scores = make(map[uint32]uint64, 100)
} else {
scores = make(map[uint32]uint64, numDocs)
}
return &scores
},
},
}
gc.insertLOCKED(field, rv)
return &geoData{
geoCacheEntry: rv,
except: createNewExcludeBitmap(except, rv.docNums),
}, nil
}
// createNewExcludeBitmap translates an exclusion bitmap from segment doc
// number space into geo docID space
func createNewExcludeBitmap(except *roaring.Bitmap, docNums []uint32) *roaring.Bitmap {
if except == nil || except.IsEmpty() || len(docNums) == 0 {
return nil
}
// docNums is sorted ascending, so a disjoint range means nothing to exclude
lo, hi := docNums[0], docNums[len(docNums)-1]
if except.Maximum() < lo || except.Minimum() > hi {
return nil
}
var newExcept *roaring.Bitmap
it := except.Iterator()
it.AdvanceIfNeeded(lo)
for i := 0; it.HasNext() && i < len(docNums); {
docNum := it.Next()
if docNum > hi {
break
}
// search only the unscanned suffix - i advances monotonically
i += sort.Search(len(docNums)-i, func(k int) bool { return docNums[i+k] >= docNum })
// a doc with a multi-valued geo field owns a run of geo docIDs
for ; i < len(docNums) && docNums[i] == docNum; i++ {
if newExcept == nil {
newExcept = roaring.New()
}
newExcept.Add(uint32(i))
}
}
return newExcept
}
func (gc *geoIndexCache) insertLOCKED(field uint16, entry *geoCacheEntry) {
if len(gc.cache) == 0 {
go gc.monitor()
}
gc.cache[field] = entry
}
func (gc *geoIndexCache) monitor() {
ticker := time.NewTicker(geoMonitorFreq)
defer ticker.Stop()
for {
select {
case <-gc.closeCh:
return
case <-ticker.C:
exit := gc.cleanup()
if exit {
return
}
}
}
}
// cleanup runs one eviction pass over the cache. For each entry it folds the
// hit count accumulated since the last pass into the entry's moving average
// (see ewma.add), then evicts entries that have no outstanding references and
// whose average traffic has decayed to at most (1 - alpha)
// Returns true when the cache is empty, signalling the monitor goroutine to exit.
func (gc *geoIndexCache) cleanup() bool {
gc.m.Lock()
for field, entry := range gc.cache {
sample := atomic.LoadUint64(&entry.tracker.sample)
entry.tracker.add(sample)
refCount := atomic.LoadInt64(&entry.refs)
if refCount <= 0 && entry.tracker.avg <= (1-entry.tracker.alpha) {
atomic.StoreUint64(&entry.tracker.sample, 0)
delete(gc.cache, field)
entry.Close()
continue
}
atomic.StoreUint64(&entry.tracker.sample, 0)
}
rv := len(gc.cache) == 0
gc.m.Unlock()
return rv
}
// geoCacheEntry represents a cached entry for a specific field in the geo index.
// Each entry holds a copy of the memory to prevent the underlying memory from being
// released while the entry is still in use.
// Includes a pool for reusing score arrays to reduce memory allocations during queries.
type geoCacheEntry struct {
innerCells []uint64
innerCellsMem []byte
innerDocIDs []uint32
innerDocIDsMem []byte
crossCells []uint64
crossCellsMem []byte
crossDocIDs []uint32
crossDocIDsMem []byte
bboxOffsets []uint64
bboxOffsetsMem []byte
bboxMem []byte
shapeOffsets []uint64
shapeOffsetsMem []byte
shapeMem []byte
numDocs uint64
docNums []uint32
docNumsMem []byte
docScoresInner []uint64
docScoresInnerMem []byte
docScoresCross []uint64
docScoresCrossMem []byte
tracker *ewma
refs int64
fileReader *FileReader
scoresPool sync.Pool
}
// geoData is a per-load view over a shared geoCacheEntry.
// The exclude bitmap is derived fresh per load from the
// snapshot's except bitmap.
type geoData struct {
*geoCacheEntry
except *roaring.Bitmap
}
func (g *geoData) Excluded() *roaring.Bitmap {
return g.except
}
func (gce *geoCacheEntry) GetScoreMap() map[uint32]uint64 {
return *gce.scoresPool.Get().(*map[uint32]uint64)
}
func (gce *geoCacheEntry) PutScoreMap(scores map[uint32]uint64) {
if scores != nil {
clear(scores)
gce.scoresPool.Put(&scores)
}
}
func (gce *geoCacheEntry) Close() {
gce.decRef()
}
func (gce *geoCacheEntry) load(except *roaring.Bitmap) *geoData {
gce.incHits()
gce.incRef()
return &geoData{
geoCacheEntry: gce,
except: createNewExcludeBitmap(except, gce.docNums),
}
}
func (gce *geoCacheEntry) incHits() {
atomic.AddUint64(&gce.tracker.sample, 1)
}
func (gce *geoCacheEntry) incRef() {
atomic.AddInt64(&gce.refs, 1)
}
func (gce *geoCacheEntry) decRef() {
atomic.AddInt64(&gce.refs, -1)
}
func (gce *geoCacheEntry) InnerCells() []uint64 {
return gce.innerCells
}
func (gce *geoCacheEntry) InnerDocIDs() []uint32 {
return gce.innerDocIDs
}
func (gce *geoCacheEntry) CrossCells() []uint64 {
return gce.crossCells
}
func (gce *geoCacheEntry) CrossDocIDs() []uint32 {
return gce.crossDocIDs
}
func (gce *geoCacheEntry) BoundingBox(geoDocID uint32) ([]byte, error) {
if uint64(geoDocID) >= gce.numDocs {
return nil, fmt.Errorf("geo docID out of range")
}
var offsetStart uint64
if geoDocID != 0 {
offsetStart = gce.bboxOffsets[geoDocID-1]
}
offsetEnd := gce.bboxOffsets[geoDocID]
if offsetEnd == offsetStart {
return nil, fmt.Errorf("no bounding box for geo docID %d", geoDocID)
}
buf, err := gce.fileReader.process(gce.bboxMem[offsetStart:offsetEnd])
if err != nil {
return nil, err
}
return buf, nil
}
func (gce *geoCacheEntry) Shape(geoDocID uint32) ([]byte, error) {
if uint64(geoDocID) >= gce.numDocs {
return nil, fmt.Errorf("geo docID out of range")
}
var offsetStart uint64
if geoDocID != 0 {
offsetStart = gce.shapeOffsets[geoDocID-1]
}
offsetEnd := gce.shapeOffsets[geoDocID]
if offsetEnd == offsetStart {
return nil, fmt.Errorf("no shape for geo docID %d", geoDocID)
}
buf, err := gce.fileReader.process(gce.shapeMem[offsetStart:offsetEnd])
if err != nil {
return nil, err
}
return buf, nil
}
func (gce *geoCacheEntry) DocNums() []uint32 {
return gce.docNums
}
func (gce *geoCacheEntry) NumDocs() uint64 {
return gce.numDocs
}
func (gce *geoCacheEntry) DocScores() ([]uint64, []uint64) {
return gce.docScoresInner, gce.docScoresCross
}