forked from ipfs/js-hamt-sharding
/
bucket.ts
276 lines (224 loc) · 6.56 KB
/
bucket.ts
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
// @ts-ignore
import SparseArray from 'sparse-array'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import type { InfiniteHash } from './consumable-hash.js'
interface BucketChild<V> {
key: string
value: V
hash: InfiniteHash
}
interface SA<B> {
length: number
compactArray: () => B[]
get: (i: number) => B
set: (i: number, value: B) => void
reduce: <A> (fn: (acc: A, curr: B, index: number) => A, initial: A) => B
find: (fn: (item: B) => boolean) => B | undefined
bitField: () => number[]
unset: (i: number) => void
}
interface BucketPosition<T> {
bucket: Bucket<T>
pos: number
hash: InfiniteHash
existingChild?: BucketChild<T>
}
interface BucketOptions {
bits: number
hash: (value: Uint8Array | InfiniteHash) => InfiniteHash
}
class Bucket<T> {
_options: BucketOptions
_popCount: number
_parent?: Bucket<T>
_posAtParent: number
_children: SA<Bucket<T> | BucketChild<T>>
key: string | null
constructor (options: BucketOptions, parent?: Bucket<T>, posAtParent = 0) {
this._options = options
this._popCount = 0
this._parent = parent
this._posAtParent = posAtParent
this._children = new SparseArray()
this.key = null
}
async put (key: string, value: T) {
const place = await this._findNewBucketAndPos(key)
await place.bucket._putAt(place, key, value)
}
async get (key: string) {
const child = await this._findChild(key)
if (child) {
return child.value
}
}
async del (key: string) {
const place = await this._findPlace(key)
const child = place.bucket._at(place.pos)
if (child && child.key === key) {
place.bucket._delAt(place.pos)
}
}
leafCount (): number {
const children = this._children.compactArray()
return children.reduce((acc, child) => {
if (child instanceof Bucket) {
return acc + child.leafCount()
}
return acc + 1
}, 0)
}
childrenCount () {
return this._children.length
}
onlyChild () {
return this._children.get(0)
}
* eachLeafSeries (): Iterable<BucketChild<T>> {
const children = this._children.compactArray()
for (const child of children) {
if (child instanceof Bucket) {
yield * Array.from(child.eachLeafSeries())
} else {
yield child
}
}
}
serialize (map: (value: BucketChild<T>, index: number) => T, reduce: (reduced: any) => any) {
const acc: T[] = []
// serialize to a custom non-sparse representation
return reduce(this._children.reduce((acc, child, index) => {
if (child) {
if (child instanceof Bucket) {
acc.push(child.serialize(map, reduce))
} else {
acc.push(map(child, index))
}
}
return acc
}, acc))
}
asyncTransform (asyncMap: (value: BucketChild<T>) => Promise<T[]>, asyncReduce: (reduced: any) => Promise<any>) {
return asyncTransformBucket(this, asyncMap, asyncReduce)
}
toJSON () {
return this.serialize(mapNode, reduceNodes)
}
prettyPrint () {
return JSON.stringify(this.toJSON(), null, ' ')
}
tableSize () {
return Math.pow(2, this._options.bits)
}
async _findChild (key: string) {
const result = await this._findPlace(key)
const child = result.bucket._at(result.pos)
if (child instanceof Bucket) {
// should not be possible, this._findPlace should always
// return a location for a child, not a bucket
return undefined
}
if (child && child.key === key) {
return child
}
}
async _findPlace (key: string | InfiniteHash): Promise<BucketPosition<T>> {
const hashValue = this._options.hash(typeof key === 'string' ? uint8ArrayFromString(key) : key)
const index = await hashValue.take(this._options.bits)
const child = this._children.get(index)
if (child instanceof Bucket) {
return child._findPlace(hashValue)
}
return {
bucket: this,
pos: index,
hash: hashValue,
existingChild: child
}
}
async _findNewBucketAndPos (key: string | InfiniteHash): Promise<BucketPosition<T>> {
const place = await this._findPlace(key)
if (place.existingChild && place.existingChild.key !== key) {
// conflict
const bucket = new Bucket(this._options, place.bucket, place.pos)
place.bucket._putObjectAt(place.pos, bucket)
// put the previous value
const newPlace = await bucket._findPlace(place.existingChild.hash)
newPlace.bucket._putAt(newPlace, place.existingChild.key, place.existingChild.value)
return bucket._findNewBucketAndPos(place.hash)
}
// no conflict, we found the place
return place
}
_putAt (place: BucketPosition<T>, key: string, value: T) {
this._putObjectAt(place.pos, {
key: key,
value: value,
hash: place.hash
})
}
_putObjectAt (pos: number, object: Bucket<T> | BucketChild<T>) {
if (!this._children.get(pos)) {
this._popCount++
}
this._children.set(pos, object)
}
_delAt (pos: number) {
if (pos === -1) {
throw new Error('Invalid position')
}
if (this._children.get(pos)) {
this._popCount--
}
this._children.unset(pos)
this._level()
}
_level () {
if (this._parent && this._popCount <= 1) {
if (this._popCount === 1) {
// remove myself from parent, replacing me with my only child
const onlyChild = this._children.find(exists)
if (onlyChild && !(onlyChild instanceof Bucket)) {
const hash = onlyChild.hash
hash.untake(this._options.bits)
const place = {
pos: this._posAtParent,
hash: hash,
bucket: this._parent
}
this._parent._putAt(place, onlyChild.key, onlyChild.value)
}
} else {
this._parent._delAt(this._posAtParent)
}
}
}
_at (index:number ) {
return this._children.get(index)
}
}
function exists (o: any) {
return Boolean(o)
}
function mapNode (node: any, _: number) {
return node.key
}
function reduceNodes (nodes: any) {
return nodes
}
async function asyncTransformBucket<T> (bucket: Bucket<T>, asyncMap: (value: BucketChild<T>) => Promise<T[]>, asyncReduce: (reduced: any) => Promise<any>) {
const output = []
for (const child of bucket._children.compactArray()) {
if (child instanceof Bucket) {
await asyncTransformBucket(child, asyncMap, asyncReduce)
} else {
const mappedChildren = await asyncMap(child)
output.push({
bitField: bucket._children.bitField(),
children: mappedChildren
})
}
}
return asyncReduce(output)
}
export default Bucket