This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
containers_btree.go
222 lines (193 loc) · 4.57 KB
/
containers_btree.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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package roaring
import (
"fmt"
"io"
)
type bTreeContainers struct {
tree *tree
lastKey uint64
lastContainer *Container
}
func newBTreeContainers() *bTreeContainers {
return &bTreeContainers{
tree: treeNew(),
}
}
func NewBTreeBitmap(a ...uint64) *Bitmap {
b := &Bitmap{
Containers: newBTreeContainers(),
}
// We have no way to report this.
// Because we just created Bitmap, its OpWriter is nil, so there
// is no code path which would cause Add() to return an error.
// Therefore, it's safe to swallow this error.
_, _ = b.Add(a...)
return b
}
func (btc *bTreeContainers) Get(key uint64) *Container {
// Check the last* cache for same container.
if key == btc.lastKey {
return btc.lastContainer
}
var c *Container
el, ok := btc.tree.Get(key)
if ok {
c = el
btc.lastKey = key
btc.lastContainer = c
}
return c
}
func (btc *bTreeContainers) Put(key uint64, c *Container) {
// If we don't do this, a Put on a container we just got from
// Get can result in the tree containing a different container
// than we'll get on next lookup.
btc.lastKey, btc.lastContainer = key, c
btc.tree.Set(key, c)
}
func (btc *bTreeContainers) Remove(key uint64) {
btc.tree.Delete(key)
if key == btc.lastKey {
btc.lastKey = ^uint64(0)
btc.lastContainer = nil
}
}
func (btc *bTreeContainers) GetOrCreate(key uint64) *Container {
// Check the last* cache for same container.
if key == btc.lastKey {
return btc.lastContainer
}
btc.lastKey = key
v, ok := btc.tree.Get(key)
if !ok {
cont := NewContainer()
btc.tree.Set(key, cont)
btc.lastContainer = cont
return cont
}
btc.lastContainer = v
return btc.lastContainer
}
func (btc *bTreeContainers) Count() (n uint64) {
e, _ := btc.tree.Seek(0)
_, c, err := e.Next()
for err != io.EOF {
n += uint64(c.N())
_, c, err = e.Next()
}
return n
}
func (btc *bTreeContainers) Clone() Containers {
nbtc := newBTreeContainers()
itr, err := btc.tree.SeekFirst()
if err == io.EOF {
return nbtc
}
for {
k, v, err := itr.Next()
if err == io.EOF {
break
}
nbtc.tree.Set(k, v.Clone())
}
return nbtc
}
func (btc *bTreeContainers) Freeze() Containers {
nbtc := newBTreeContainers()
itr, err := btc.tree.SeekFirst()
if err == io.EOF {
return nbtc
}
for {
k, v, err := itr.Next()
if err == io.EOF {
break
}
nbtc.tree.Set(k, v.Freeze())
}
return nbtc
}
func (btc *bTreeContainers) Last() (key uint64, c *Container) {
if btc.tree.Len() == 0 {
return 0, nil
}
k, v := btc.tree.Last()
return k, v
}
func (btc *bTreeContainers) Size() int {
return btc.tree.Len()
}
func (btc *bTreeContainers) Reset() {
btc.tree = treeNew()
// use a definitely-invalid key, so we can distinguish between "you
// just looked that up, and it was a nil container" and "you have
// never looked that up before."
btc.lastKey = ^uint64(0)
btc.lastContainer = nil
}
func (btc *bTreeContainers) ResetN(n int) {
// we ignore n because it's impractical to preallocate the tree
btc.Reset()
}
func (btc *bTreeContainers) Iterator(key uint64) (citer ContainerIterator, found bool) {
e, ok := btc.tree.Seek(key)
if ok {
found = true
}
return &btcIterator{
e: e,
}, found
}
func (btc *bTreeContainers) Repair() {
e, _ := btc.tree.Seek(0)
_, c, err := e.Next()
for err != io.EOF {
c.Repair()
_, c, err = e.Next()
}
}
// Update calls fn (existing-container, existed), and expects
// (new-container, write). If write is true, the container is used to
// replace the given container.
func (btc *bTreeContainers) Update(key uint64, fn func(*Container, bool) (*Container, bool)) {
_, _ = btc.tree.Put(key, fn)
btc.lastKey = ^uint64(0)
btc.lastContainer = nil
}
// UpdateEvery calls fn (existing-container, existed), and expects
// (new-container, write). If write is true, the container is used to
// replace the given container.
func (btc *bTreeContainers) UpdateEvery(fn func(uint64, *Container, bool) (*Container, bool)) {
e, _ := btc.tree.Seek(0)
// currently not handling the error from this, but in practice it has
// to be io.EOF.
_ = e.Every(fn)
// invalidate cache.
btc.lastKey = ^uint64(0)
btc.lastContainer = nil
}
type btcIterator struct {
e *enumerator
key uint64
val *Container
}
func (i *btcIterator) Close() {}
func (i *btcIterator) Next() bool {
k, v, err := i.e.Next()
if err == io.EOF {
return false
}
if roaringParanoia {
if v == nil {
panic(fmt.Sprintf("got nil container for key %d", k))
}
}
i.key = k
i.val = v
return true
}
func (i *btcIterator) Value() (uint64, *Container) {
return i.key, i.val
}