Repository navigation
Expand file tree
/
Copy pathindex.go
More file actions
299 lines (268 loc) · 8.62 KB
/
Copy pathindex.go
File metadata and controls
299 lines (268 loc) · 8.62 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
// Copyright 2022 The bmdb Authors. All rights reserved.
//
// 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 bmdb
type IdxType interface {
BTree | Set | SortedSet | List
}
type defaultOp[T IdxType] struct {
idx map[BucketId]*T
}
func (op *defaultOp[T]) computeIfAbsent(id BucketId, f func() *T) *T {
if i, isExist := op.idx[id]; isExist {
return i
}
i := f()
op.idx[id] = i
return i
}
func (op *defaultOp[T]) delete(id BucketId) {
delete(op.idx, id)
}
func (op *defaultOp[T]) exist(id BucketId) (*T, bool) {
i, isExist := op.idx[id]
return i, isExist
}
func (op *defaultOp[T]) getIdxLen() int {
return len(op.idx)
}
func (op *defaultOp[T]) rangeIdx(f func(elem *T)) {
for _, t := range op.idx {
f(t)
}
}
type ListIdx struct {
*defaultOp[List]
}
func (idx ListIdx) getWithDefault(id BucketId) *List {
return idx.defaultOp.computeIfAbsent(id, func() *List {
return NewList()
})
}
// getOrCreateList ensures a concrete List exists for the bucket and also
// guarantees an AtomicList snapshot is created and cached. Callers that
// intend to mutate or access lists through the AtomicList should use this
// method to avoid races where a concrete List exists but no atomic snapshot
// has yet been initialized.
func (idx *index) getOrCreateList(id BucketId) *List {
l := idx.list.getWithDefault(id)
if _, ok := idx.atomicLists[id]; !ok {
idx.atomicLists[id] = NewAtomicList(l)
}
return l
}
type BTreeIdx struct {
*defaultOp[BTree]
}
func (idx BTreeIdx) getWithDefault(id BucketId) *BTree {
return idx.defaultOp.computeIfAbsent(id, func() *BTree {
return NewBTree()
})
}
type SetIdx struct {
*defaultOp[Set]
}
func (idx SetIdx) getWithDefault(id BucketId) *Set {
return idx.defaultOp.computeIfAbsent(id, func() *Set {
return NewSet()
})
}
type SortedSetIdx struct {
*defaultOp[SortedSet]
}
func (idx SortedSetIdx) getWithDefault(id BucketId, db *DB) *SortedSet {
return idx.defaultOp.computeIfAbsent(id, func() *SortedSet {
return NewSortedSet(db)
})
}
type index struct {
list ListIdx
bTree BTreeIdx
set SetIdx
sortedSet SortedSetIdx
// atomic snapshots for per-bucket BTree for lock-free reads
atomicBTrees map[BucketId]*AtomicBTree
// per-bucket atomic zset instances (lazily created by getAtomicZSet)
atomicZSets map[BucketId]*AtomicZSet
// per-bucket atomic list instances (lazily created by getAtomicList)
atomicLists map[BucketId]*AtomicList
// per-bucket atomic set instances (lazily created by getAtomicSet)
atomicSets map[BucketId]*AtomicSet
// per-bucket atomic fulltext inverted indexes
fulltext map[BucketId]*AtomicFulltext
// per-bucket fulltext bookkeeping: mapping key->docID and docID->terms
fulltextDocIDs map[BucketId]map[string]uint32
fulltextDocTerms map[BucketId]map[uint32][]string
fulltextNextDocID map[BucketId]uint32
// (reserved for future index types)
_ struct{}
}
func newIndex() *index {
i := new(index)
i.list = ListIdx{&defaultOp[List]{idx: map[BucketId]*List{}}}
i.bTree = BTreeIdx{&defaultOp[BTree]{idx: map[BucketId]*BTree{}}}
i.set = SetIdx{&defaultOp[Set]{idx: map[BucketId]*Set{}}}
i.sortedSet = SortedSetIdx{&defaultOp[SortedSet]{idx: map[BucketId]*SortedSet{}}}
i.atomicBTrees = make(map[BucketId]*AtomicBTree)
i.atomicZSets = make(map[BucketId]*AtomicZSet)
i.fulltext = make(map[BucketId]*AtomicFulltext)
i.fulltextDocIDs = make(map[BucketId]map[string]uint32)
i.fulltextDocTerms = make(map[BucketId]map[uint32][]string)
i.fulltextNextDocID = make(map[BucketId]uint32)
// atomic lists map
i.atomicLists = make(map[BucketId]*AtomicList)
i.atomicSets = make(map[BucketId]*AtomicSet)
// no fulltext indexes initialized here
// atomic lists
// Note: we don't yet create atomic lists for every bucket until needed.
// We'll lazily initialize them when migrating list usage.
// Use map[BucketId]*AtomicList to hold snapshots.
// (not strictly necessary for stage1, but added for future migration)
// _ = make(map[BucketId]*AtomicList)
return i
}
// (no fulltext wiring present)
// (placeholder) getAtomicList - not used yet. Kept to make future migration simpler.
func (idx *index) getAtomicList(id BucketId) *AtomicList {
// Always ensure an AtomicList exists for the bucket and return it.
// Creating the AtomicList lazily avoids surprises for callers that
// expect an atomic snapshot to be present. This is the first step
// toward enforcing atomic-only mutations: callers can rely on the
// presence of an AtomicList and use its COW methods instead of
// mutating concrete lists directly.
if al, ok := idx.atomicLists[id]; ok {
return al
}
// Ensure a concrete list exists (getWithDefault will create one if
// missing) and create an atomic snapshot from it.
l := idx.list.getWithDefault(id)
al := NewAtomicList(l)
idx.atomicLists[id] = al
return al
}
func (idx *index) getAtomicBTree(id BucketId) *AtomicBTree {
if ab, ok := idx.atomicBTrees[id]; ok {
return ab
}
// Only create an atomic snapshot if a concrete BTree exists for this bucket.
if bt, ok := idx.bTree.exist(id); ok {
ab := NewAtomicBTree(bt)
idx.atomicBTrees[id] = ab
return ab
}
return nil
}
func (idx *index) getAtomicSet(id BucketId) *AtomicSet {
// Only return an atomic snapshot if a concrete Set exists for this bucket.
if s, ok := idx.set.exist(id); ok {
if as, ok2 := idx.atomicSets[id]; ok2 {
return as
}
as := NewAtomicSet(s)
idx.atomicSets[id] = as
return as
}
return nil
}
func (idx *index) getAtomicZSet(id BucketId, db *DB) *AtomicZSet {
// Only return an atomic snapshot when a concrete SortedSet exists for
// this bucket. Creating or caching an empty AtomicZSet for missing
// buckets causes reads to observe an empty snapshot while the concrete
// index may later be populated, leading to semantic mismatches. For
// correctness we construct a fresh AtomicZSet from the current concrete
// SortedSet if present; do not create a placeholder when absent.
if zs, ok := idx.sortedSet.exist(id); ok {
// Return a fresh snapshot built from the current concrete SortedSet.
return NewAtomicZSet(zs)
}
return nil
}
func (idx *index) getFulltext(id BucketId) *AtomicFulltext {
if f, ok := idx.fulltext[id]; ok {
return f
}
f := NewAtomicFulltext(nil)
idx.fulltext[id] = f
return f
}
// assignDocID returns an existing docID for key or assigns a new one.
func (idx *index) assignDocID(bucket BucketId, key string) uint32 {
if _, ok := idx.fulltextDocIDs[bucket]; !ok {
idx.fulltextDocIDs[bucket] = make(map[string]uint32)
}
if _, ok := idx.fulltextDocTerms[bucket]; !ok {
idx.fulltextDocTerms[bucket] = make(map[uint32][]string)
}
if id, ok := idx.fulltextDocIDs[bucket][key]; ok {
return id
}
next := idx.fulltextNextDocID[bucket] + 1
if next == 0 {
next = 1
}
idx.fulltextNextDocID[bucket] = next
idx.fulltextDocIDs[bucket][key] = next
return next
}
func (idx *index) lookupDocID(bucket BucketId, key string) (uint32, bool) {
if m, ok := idx.fulltextDocIDs[bucket]; ok {
id, ok2 := m[key]
return id, ok2
}
return 0, false
}
func (idx *index) deleteDocID(bucket BucketId, key string) {
if m, ok := idx.fulltextDocIDs[bucket]; ok {
if id, ok2 := m[key]; ok2 {
delete(m, key)
if tm, ok3 := idx.fulltextDocTerms[bucket]; ok3 {
delete(tm, id)
}
}
}
}
func (idx *index) setDocTerms(bucket BucketId, docID uint32, terms []string) {
if _, ok := idx.fulltextDocTerms[bucket]; !ok {
idx.fulltextDocTerms[bucket] = make(map[uint32][]string)
}
idx.fulltextDocTerms[bucket][docID] = terms
}
func (idx *index) getDocTerms(bucket BucketId, docID uint32) ([]string, bool) {
if tm, ok := idx.fulltextDocTerms[bucket]; ok {
t, ok2 := tm[docID]
return t, ok2
}
return nil, false
}
// ExportZSetRetryMetrics returns a map of bucketId -> (per-key retry metrics, totalRetries)
// The per-key retry metrics is itself a map[string]int64.
func (idx *index) ExportZSetRetryMetrics() map[BucketId]struct {
Metrics map[string]int64
Total int64
} {
res := make(map[BucketId]struct {
Metrics map[string]int64
Total int64
})
for bid, az := range idx.atomicZSets {
if az == nil {
continue
}
metrics, total := az.GetRetryMetrics()
res[bid] = struct {
Metrics map[string]int64
Total int64
}{Metrics: metrics, Total: total}
}
return res
}