-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_ops.go
More file actions
601 lines (580 loc) · 15.4 KB
/
array_ops.go
File metadata and controls
601 lines (580 loc) · 15.4 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package tron
import (
"encoding/binary"
"fmt"
)
// ArrGet returns the value at index, if present.
func ArrGet(doc []byte, rootOff uint32, index uint32) (Value, bool, error) {
length, err := arrayRootLength(doc, rootOff)
if err != nil {
return Value{}, false, err
}
if index >= length {
return Value{}, false, fmt.Errorf("array index %d out of range", index)
}
return arrGet(doc, rootOff, index, true)
}
// ArrayRootLength returns the length stored on the array root node.
func ArrayRootLength(doc []byte, rootOff uint32) (uint32, error) {
return arrayRootLength(doc, rootOff)
}
// ArraySetNode updates an array node at rootOff and returns the new root offset.
func ArraySetNode(builder *Builder, rootOff uint32, index uint32, value Value, length uint32) (uint32, error) {
if builder == nil {
return 0, fmt.Errorf("nil builder")
}
return arrSet(builder, rootOff, index, value, length)
}
// ArrSetDocument replaces the value at index for a top-level array document.
func ArrSetDocument(doc []byte, index uint32, value Value) ([]byte, error) {
rootOff, length, builder, err := arrayDocumentBase(doc)
if err != nil {
return nil, err
}
if index >= length {
return nil, fmt.Errorf("array index %d out of range", index)
}
newRoot, err := arrSet(builder, rootOff, index, value, length)
if err != nil {
return nil, err
}
return builder.BytesWithTrailer(newRoot, rootOff), nil
}
// ArrAppendDocument appends values at the end.
func ArrAppendDocument(doc []byte, values ...Value) ([]byte, error) {
if len(values) == 0 {
return doc, nil
}
rootOff, length, builder, err := arrayDocumentBase(doc)
if err != nil {
return nil, err
}
prevRoot := rootOff
newRoot := rootOff
for _, v := range values {
updated, err := arrSet(builder, newRoot, length, v, length+1)
if err != nil {
return nil, err
}
newRoot = updated
length++
}
return builder.BytesWithTrailer(newRoot, prevRoot), nil
}
// ArrSliceDocument returns a new array document containing values[start:end].
func ArrSliceDocument(doc []byte, start, end uint32) ([]byte, error) {
rootOff, length, builder, err := arrayDocumentBase(doc)
if err != nil {
return nil, err
}
if start > end || end > length {
return nil, fmt.Errorf("array slice [%d:%d] out of range", start, end)
}
values, err := arrayDenseValues(doc, rootOff, length)
if err != nil {
return nil, err
}
sliced := values[start:end]
newRoot, err := buildArrayFromValues(builder, sliced)
if err != nil {
return nil, err
}
return builder.BytesWithTrailer(newRoot, rootOff), nil
}
func arrayDocumentBase(doc []byte) (uint32, uint32, *Builder, error) {
if _, err := DetectDocType(doc); err != nil {
return 0, 0, nil, err
}
tr, err := ParseTrailer(doc)
if err != nil {
return 0, 0, nil, err
}
root, err := DecodeValueAt(doc, tr.RootOffset)
if err != nil {
return 0, 0, nil, err
}
if root.Type != TypeArr {
return 0, 0, nil, fmt.Errorf("root is not an array")
}
length, err := arrayRootLength(doc, root.Offset)
if err != nil {
return 0, 0, nil, err
}
builder, _, err := NewBuilderFromDocument(doc)
if err != nil {
return 0, 0, nil, err
}
return tr.RootOffset, length, builder, nil
}
func arrayDenseValues(doc []byte, rootOff uint32, length uint32) ([]Value, error) {
values := make([]Value, length)
for i := uint32(0); i < length; i++ {
val, ok, err := arrGet(doc, rootOff, i, true)
if err != nil {
return nil, err
}
if ok {
values[i] = val
} else {
values[i] = Value{Type: TypeNil}
}
}
return values, nil
}
func buildArrayFromValues(builder *Builder, values []Value) (uint32, error) {
if builder == nil {
return 0, fmt.Errorf("nil builder")
}
if len(values) > int(^uint32(0)) {
return 0, fmt.Errorf("array length exceeds u32")
}
length := uint32(len(values))
entries := getArrayEntrySlice(len(values))
for i, v := range values {
entries[i] = arrayEntry{index: uint32(i), value: v}
}
shift := arrayRootShift(length)
root, err := buildArrayNode(entries, shift, length, true, nil)
putArrayEntrySlice(entries)
if err != nil {
return 0, err
}
return encodeArrayNode(builder, root, nil)
}
func arrayRootLength(doc []byte, rootOff uint32) (uint32, error) {
h, node, err := NodeSliceAt(doc, rootOff)
if err != nil {
return 0, err
}
if h.KeyType != KeyArr {
return 0, fmt.Errorf("root is not array")
}
if !h.IsRoot {
return 0, fmt.Errorf("array root missing root flag")
}
switch h.Kind {
case NodeLeaf:
leaf, err := ParseArrayLeafNode(node)
if err != nil {
return 0, err
}
defer releaseArrayLeafNode(&leaf)
return leaf.Length, nil
case NodeBranch:
branch, err := ParseArrayBranchNode(node)
if err != nil {
return 0, err
}
defer releaseArrayBranchNode(&branch)
return branch.Length, nil
default:
return 0, fmt.Errorf("unknown array node kind")
}
}
func arrGet(doc []byte, off uint32, index uint32, isRoot bool) (Value, bool, error) {
for {
h, node, err := NodeSliceAt(doc, off)
if err != nil {
return Value{}, false, err
}
if h.KeyType != KeyArr {
return Value{}, false, fmt.Errorf("node is not an array")
}
if h.Kind == NodeLeaf {
if !isRoot && h.IsRoot {
return Value{}, false, fmt.Errorf("array non-root leaf marked as root")
}
p := 1 + h.LenBytes
if int(h.NodeLen) < p+3 {
return Value{}, false, fmt.Errorf("array leaf node too small: %d", h.NodeLen)
}
shift := node[p]
p++
if shift != 0 {
return Value{}, false, fmt.Errorf("array leaf shift must be 0")
}
bitmap := binary.LittleEndian.Uint16(node[p : p+2])
p += 2
if h.IsRoot {
if p+4 > int(h.NodeLen) {
return Value{}, false, fmt.Errorf("array leaf root length truncated")
}
p += 4
}
slot := uint8(index & 0xF)
if ((bitmap >> slot) & 1) == 0 {
return Value{}, false, nil
}
mask := uint16((uint32(1) << slot) - 1)
idx := popcount16(bitmap & mask)
addrPos := p + idx*4
if addrPos+4 > int(h.NodeLen) {
return Value{}, false, fmt.Errorf("value address truncated")
}
addr := binary.LittleEndian.Uint32(node[addrPos : addrPos+4])
val, err := DecodeValueAt(doc, addr)
if err != nil {
return Value{}, false, err
}
return val, true, nil
}
if !isRoot && h.IsRoot {
return Value{}, false, fmt.Errorf("array non-root branch marked as root")
}
p := 1 + h.LenBytes
if int(h.NodeLen) < p+3 {
return Value{}, false, fmt.Errorf("array branch node too small: %d", h.NodeLen)
}
shift := node[p]
p++
if shift%4 != 0 {
return Value{}, false, fmt.Errorf("array branch shift must be multiple of 4")
}
bitmap := binary.LittleEndian.Uint16(node[p : p+2])
p += 2
if h.IsRoot {
if p+4 > int(h.NodeLen) {
return Value{}, false, fmt.Errorf("array branch root length truncated")
}
p += 4
}
slot := uint8((index >> shift) & 0xF)
if ((bitmap >> slot) & 1) == 0 {
return Value{}, false, nil
}
mask := uint16((uint32(1) << slot) - 1)
idx := popcount16(bitmap & mask)
childPos := p + idx*4
if childPos+4 > int(h.NodeLen) {
return Value{}, false, fmt.Errorf("child address truncated")
}
off = binary.LittleEndian.Uint32(node[childPos : childPos+4])
isRoot = false
}
}
func arrSet(builder *Builder, rootOff uint32, index uint32, value Value, length uint32) (uint32, error) {
rootOff, err := ensureArrayRoot(builder, rootOff, index, length)
if err != nil {
return 0, err
}
doc := builder.buf
newRoot, _, err := arrSetNode(doc, rootOff, index, value, builder, true, length)
if err != nil {
return 0, err
}
return newRoot, nil
}
func ensureArrayRoot(builder *Builder, rootOff uint32, index uint32, length uint32) (uint32, error) {
h, node, err := NodeSliceAt(builder.buf, rootOff)
if err != nil {
return 0, err
}
if h.KeyType != KeyArr || !h.IsRoot {
return 0, fmt.Errorf("array root missing root flag")
}
var shift uint8
switch h.Kind {
case NodeLeaf:
shift = 0
case NodeBranch:
branch, err := ParseArrayBranchNode(node)
if err != nil {
return 0, err
}
defer releaseArrayBranchNode(&branch)
shift = branch.Shift
default:
return 0, fmt.Errorf("unknown array node kind")
}
off := rootOff
for (index >> shift) > 0xF {
childOff, err := cloneArrayNodeAsChild(builder.buf, off, builder)
if err != nil {
return 0, err
}
shift += 4
branch := ArrayBranchNode{
Header: NodeHeader{Kind: NodeBranch, KeyType: KeyArr, IsRoot: true},
Shift: shift,
Bitmap: 1,
Length: length,
Children: []uint32{childOff},
}
newOff, err := appendArrayBranchNode(builder, branch)
if err != nil {
return 0, err
}
off = newOff
}
return off, nil
}
func arrSetNode(doc []byte, off uint32, index uint32, value Value, builder *Builder, isRoot bool, rootLength uint32) (uint32, bool, error) {
h, node, err := NodeSliceAt(doc, off)
if err != nil {
return 0, false, err
}
if h.KeyType != KeyArr {
return 0, false, fmt.Errorf("node is not an array")
}
if h.Kind == NodeLeaf {
if !isRoot && h.IsRoot {
return 0, false, fmt.Errorf("array non-root leaf marked as root")
}
p := 1 + h.LenBytes
if int(h.NodeLen) < p+3 {
return 0, false, fmt.Errorf("array leaf node too small: %d", h.NodeLen)
}
shift := node[p]
p++
if shift != 0 {
return 0, false, fmt.Errorf("array leaf shift must be 0")
}
bitmap := binary.LittleEndian.Uint16(node[p : p+2])
p += 2
length := uint32(0)
if h.IsRoot {
if p+4 > int(h.NodeLen) {
return 0, false, fmt.Errorf("array leaf root length truncated")
}
length = binary.LittleEndian.Uint32(node[p : p+4])
p += 4
}
slot := uint8(index & 0xF)
mask := uint16((uint32(1) << slot) - 1)
idx := popcount16(bitmap & mask)
has := ((bitmap >> slot) & 1) == 1
if has {
addrPos := p + idx*4
if addrPos+4 > int(h.NodeLen) {
return 0, false, fmt.Errorf("value address truncated")
}
cur, err := DecodeValueAt(doc, binary.LittleEndian.Uint32(node[addrPos:addrPos+4]))
if err != nil {
return 0, false, err
}
if valueEqual(cur, value) && (!isRoot || length == rootLength) {
return off, false, nil
}
}
newAddr, err := valueAddress(builder, value)
if err != nil {
return 0, false, err
}
entryCount := popcount16(bitmap)
newValues := getUint32Slice(entryCount)
if !has {
newValues = getUint32Slice(entryCount + 1)
}
pos := p
if has {
for i := 0; i < entryCount; i++ {
addr := binary.LittleEndian.Uint32(node[pos : pos+4])
pos += 4
if i == idx {
addr = newAddr
}
newValues[i] = addr
}
} else {
for i := 0; i < idx; i++ {
newValues[i] = binary.LittleEndian.Uint32(node[pos : pos+4])
pos += 4
}
newValues[idx] = newAddr
for i := idx; i < entryCount; i++ {
newValues[i+1] = binary.LittleEndian.Uint32(node[pos : pos+4])
pos += 4
}
bitmap |= 1 << slot
}
if isRoot {
length = rootLength
} else {
length = 0
}
newLeaf := ArrayLeafNode{
Header: NodeHeader{Kind: NodeLeaf, KeyType: KeyArr, IsRoot: isRoot},
Shift: 0,
Bitmap: bitmap,
Length: length,
ValueAddrs: newValues,
}
newOff, err := appendArrayLeafNode(builder, newLeaf)
putUint32Slice(newValues)
if err != nil {
return 0, false, err
}
return newOff, true, nil
}
if !isRoot && h.IsRoot {
return 0, false, fmt.Errorf("array non-root branch marked as root")
}
p := 1 + h.LenBytes
if int(h.NodeLen) < p+3 {
return 0, false, fmt.Errorf("array branch node too small: %d", h.NodeLen)
}
shift := node[p]
p++
if shift%4 != 0 {
return 0, false, fmt.Errorf("array branch shift must be multiple of 4")
}
bitmap := binary.LittleEndian.Uint16(node[p : p+2])
p += 2
length := uint32(0)
if h.IsRoot {
if p+4 > int(h.NodeLen) {
return 0, false, fmt.Errorf("array branch root length truncated")
}
length = binary.LittleEndian.Uint32(node[p : p+4])
p += 4
}
entryCount := popcount16(bitmap)
childrenBytes := entryCount * 4
if int(h.NodeLen) < p+childrenBytes {
return 0, false, fmt.Errorf("child address truncated")
}
slot := uint8((index >> shift) & 0xF)
mask := uint16((uint32(1) << slot) - 1)
idx := popcount16(bitmap & mask)
has := ((bitmap >> slot) & 1) == 1
var child uint32
if has {
childPos := p + idx*4
oldChild := binary.LittleEndian.Uint32(node[childPos : childPos+4])
newChild, childChanged, err := arrSetNode(doc, oldChild, index, value, builder, false, 0)
if err != nil {
return 0, false, err
}
if !childChanged && (!isRoot || length == rootLength) {
return off, false, nil
}
child = newChild
} else {
newChild, err := buildArrayPath(index, shift-4, value, builder)
if err != nil {
return 0, false, err
}
child = newChild
}
newChildren := getUint32Slice(entryCount)
if !has {
newChildren = getUint32Slice(entryCount + 1)
}
pos := p
if has {
for i := 0; i < entryCount; i++ {
addr := binary.LittleEndian.Uint32(node[pos : pos+4])
pos += 4
if i == idx {
addr = child
}
newChildren[i] = addr
}
} else {
for i := 0; i < idx; i++ {
newChildren[i] = binary.LittleEndian.Uint32(node[pos : pos+4])
pos += 4
}
newChildren[idx] = child
for i := idx; i < entryCount; i++ {
newChildren[i+1] = binary.LittleEndian.Uint32(node[pos : pos+4])
pos += 4
}
bitmap |= 1 << slot
}
if isRoot {
length = rootLength
} else {
length = 0
}
newBranch := ArrayBranchNode{
Header: NodeHeader{Kind: NodeBranch, KeyType: KeyArr, IsRoot: isRoot},
Shift: shift,
Bitmap: bitmap,
Length: length,
Children: newChildren,
}
newOff, err := appendArrayBranchNode(builder, newBranch)
putUint32Slice(newChildren)
if err != nil {
return 0, false, err
}
return newOff, true, nil
}
func buildArrayPath(index uint32, shift uint8, value Value, builder *Builder) (uint32, error) {
if shift%4 != 0 {
return 0, fmt.Errorf("array path shift must be multiple of 4")
}
if shift == 0 {
slot := uint8(index & 0xF)
addr, err := valueAddress(builder, value)
if err != nil {
return 0, err
}
leaf := ArrayLeafNode{
Header: NodeHeader{Kind: NodeLeaf, KeyType: KeyArr, IsRoot: false},
Shift: 0,
Bitmap: 1 << slot,
Length: 0,
ValueAddrs: []uint32{addr},
}
return appendArrayLeafNode(builder, leaf)
}
child, err := buildArrayPath(index, shift-4, value, builder)
if err != nil {
return 0, err
}
slot := uint8((index >> shift) & 0xF)
branch := ArrayBranchNode{
Header: NodeHeader{Kind: NodeBranch, KeyType: KeyArr, IsRoot: false},
Shift: shift,
Bitmap: 1 << slot,
Length: 0,
Children: []uint32{child},
}
return appendArrayBranchNode(builder, branch)
}
func cloneArrayNodeAsChild(doc []byte, off uint32, builder *Builder) (uint32, error) {
h, node, err := NodeSliceAt(doc, off)
if err != nil {
return 0, err
}
if h.KeyType != KeyArr {
return 0, fmt.Errorf("node is not an array")
}
if !h.IsRoot {
return off, nil
}
switch h.Kind {
case NodeLeaf:
leaf, err := ParseArrayLeafNode(node)
if err != nil {
return 0, err
}
defer releaseArrayLeafNode(&leaf)
child := ArrayLeafNode{
Header: NodeHeader{Kind: NodeLeaf, KeyType: KeyArr, IsRoot: false},
Shift: leaf.Shift,
Bitmap: leaf.Bitmap,
Length: 0,
ValueAddrs: leaf.ValueAddrs,
}
return appendArrayLeafNode(builder, child)
case NodeBranch:
branch, err := ParseArrayBranchNode(node)
if err != nil {
return 0, err
}
defer releaseArrayBranchNode(&branch)
child := ArrayBranchNode{
Header: NodeHeader{Kind: NodeBranch, KeyType: KeyArr, IsRoot: false},
Shift: branch.Shift,
Bitmap: branch.Bitmap,
Length: 0,
Children: branch.Children,
}
return appendArrayBranchNode(builder, child)
default:
return 0, fmt.Errorf("unknown array node kind")
}
}