-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy patharray_data_slab.go
More file actions
642 lines (527 loc) · 17.2 KB
/
Copy patharray_data_slab.go
File metadata and controls
642 lines (527 loc) · 17.2 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Atree - Scalable Arrays and Ordered Maps
*
* Copyright Flow Foundation
*
* 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 atree
import (
"fmt"
"slices"
"strings"
)
// ArrayDataSlab is leaf node, implementing ArraySlab.
type ArrayDataSlab struct {
next SlabID
header ArraySlabHeader
elements []Storable
// extraData is data that is prepended to encoded slab data.
// It isn't included in slab size calculation for splitting and merging.
extraData *ArrayExtraData
// inlined indicates whether this slab is stored inlined in its parent slab.
// This flag affects Encode(), ByteSize(), etc.
inlined bool
}
var _ ArraySlab = &ArrayDataSlab{}
var _ Slab = &ArrayDataSlab{}
var _ Storable = &ArrayDataSlab{}
var _ ContainerStorable = &ArrayDataSlab{}
// CopyNonRefSimple
func (a *ArrayDataSlab) CanCopyNonRefSimple() bool {
// ArrayDataSlab can't be copied because it contains
// a SlabID that must be unique per slab.
return false
}
func (a *ArrayDataSlab) CopyNonRefSimple() (Storable, error) {
return nil, fmt.Errorf("failed to copy ArrayDataSlab: can't copy container")
}
// canCopyWithoutSlabID returns true if the ArrayDataSlab (except SlabID) can be copied.
// Specifically, ArrayDataSlab can be copied if:
// - Next slab ID is empty, and
// - All elements can be copied.
// NOTE: Inlined ArrayDataSlab can be copied if all conditions are met.
func (a *ArrayDataSlab) canCopyWithoutSlabID() bool {
if a.next != SlabIDUndefined {
return false
}
for _, e := range a.elements {
if !e.CanCopyNonRefSimple() {
return false
}
}
return true
}
// copyWithNewSlabID returns a copy of the ArrayDataSlab.
// NOTE: Source ArrayDataSlab can be inlined, but copied ArrayDataSlab is never inlined.
func (a *ArrayDataSlab) copyWithNewSlabID(newID SlabID) (ArraySlab, error) {
if a.next != SlabIDUndefined {
return nil, fmt.Errorf("failed to copy ArrayDataSlab: can't copy ArrayDataSlab with next slab ID")
}
var err error
copiedElements := make([]Storable, len(a.elements))
for i, e := range a.elements {
copiedElements[i], err = e.CopyNonRefSimple()
if err != nil {
return nil, wrapErrorAsExternalErrorIfNeeded(err)
}
}
var copiedSlab ArrayDataSlab
if a.extraData != nil {
copiedSlab.extraData = &ArrayExtraData{
TypeInfo: a.extraData.TypeInfo.Copy(),
}
}
copiedSlab.header = ArraySlabHeader{
slabID: newID,
size: a.header.size,
count: a.header.count,
}
copiedSlab.elements = copiedElements
// Adjust size if source array is inlined.
if a.inlined {
copiedSlab.header.size = a.header.size -
inlinedArrayDataSlabPrefixSize +
arrayRootDataSlabPrefixSize
}
return &copiedSlab, nil
}
// Array operations (get, set, insert, remove, and pop iterate)
func (a *ArrayDataSlab) Get(_ SlabStorage, index uint64) (Storable, error) {
if index >= uint64(len(a.elements)) {
return nil, NewIndexOutOfBoundsError(index, 0, uint64(len(a.elements)))
}
return a.elements[index], nil
}
func (a *ArrayDataSlab) Set(storage SlabStorage, address Address, index uint64, value Value) (Storable, error) {
if index >= uint64(len(a.elements)) {
return nil, NewIndexOutOfBoundsError(index, 0, uint64(len(a.elements)))
}
oldElem := a.elements[index]
storable, err := value.Storable(storage, address, maxInlineArrayElementSize)
if err != nil {
// Wrap err as external error (if needed) because err is returned by Value interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to get value's storable")
}
a.elements[index] = storable
// Recompute slab size by adding all element sizes instead of using the size diff of old and new element because
// oldElem can be the same storable when the same value is reset and oldElem.ByteSize() can equal storable.ByteSize().
// Given this, size diff of the old and new element can be 0 even when its actual size changed.
size := a.getPrefixSize()
for _, e := range a.elements {
// This addition is safe from overflow because slab size is limited by
// maxThreshold (48KiB with maxSlabSize of 32KiB), well within uint32.
size += e.ByteSize()
}
a.header.size = size
if !a.inlined {
err := storeSlab(storage, a)
if err != nil {
return nil, err
}
}
return oldElem, nil
}
func (a *ArrayDataSlab) Insert(storage SlabStorage, address Address, index uint64, value Value) error {
if index > uint64(len(a.elements)) {
return NewIndexOutOfBoundsError(index, 0, uint64(len(a.elements)))
}
storable, err := value.Storable(storage, address, maxInlineArrayElementSize)
if err != nil {
// Wrap err as external error (if needed) because err is returned by Value interface.
return wrapErrorfAsExternalErrorIfNeeded(err, "failed to get value's storable")
}
// Insert new value at index in a.elements
a.elements = slices.Insert(a.elements, int(index), storable)
a.header.count++
// This addition is safe from overflow because slab size is checked
// against maxThreshold and the slab is split if needed.
a.header.size += storable.ByteSize()
if !a.inlined {
err = storeSlab(storage, a)
if err != nil {
return err
}
}
return nil
}
func (a *ArrayDataSlab) Remove(storage SlabStorage, index uint64) (Storable, error) {
if index >= uint64(len(a.elements)) {
return nil, NewIndexOutOfBoundsError(index, 0, uint64(len(a.elements)))
}
v := a.elements[index]
// Delete element at index from a.elements
a.elements = slices.Delete(
a.elements,
int(index),
int(index+1),
)
a.header.count--
a.header.size -= v.ByteSize()
if !a.inlined {
err := storeSlab(storage, a)
if err != nil {
return nil, err
}
}
return v, nil
}
func (a *ArrayDataSlab) PopIterate(_ SlabStorage, fn ArrayPopIterationFunc) error {
// Iterate and reset elements backwards
for i := len(a.elements) - 1; i >= 0; i-- {
fn(a.elements[i])
}
// Reset data slab
a.elements = nil
a.header.count = 0
a.header.size = a.getPrefixSize()
return nil
}
// Slab operations (split, merge, and lend/borrow)
func (a *ArrayDataSlab) Split(storage SlabStorage) (Slab, Slab, error) {
if len(a.elements) < 2 {
// Can't split slab with less than two elements
return nil, nil, NewSlabSplitErrorf("ArrayDataSlab (%s) has less than 2 elements", a.header.slabID)
}
// This computes the ceil of split to give the first slab with more elements.
// This is safe because header.size >= arrayDataSlabPrefixSize,
// as header.size = arrayDataSlabPrefixSize + the sum of element sizes.
dataSize := a.header.size - arrayDataSlabPrefixSize
midPoint := (dataSize + 1) >> 1
leftSize := uint32(0)
leftCount := 0
for i, e := range a.elements {
elemSize := e.ByteSize()
if leftSize+elemSize >= midPoint {
// i is mid point element. Place i on the small side.
if leftSize <= dataSize-leftSize-elemSize {
leftSize += elemSize
leftCount = i + 1
} else {
leftCount = i
}
break
}
// left slab size < midPoint
leftSize += elemSize
}
// Split elements
var rightElements []Storable
a.elements, rightElements = split(a.elements, leftCount)
// Create right slab
sID, err := storage.GenerateSlabID(a.header.slabID.address)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(
err,
fmt.Sprintf(
"failed to generate slab ID for address 0x%x",
a.header.slabID.address,
),
)
}
// This computation is safe because leftSize is the cumulative size
// of a subset of elements, and it can't exceed dataSize, the
// cumulative size of all elements.
size := arrayDataSlabPrefixSize + dataSize - leftSize
rightSlab := &ArrayDataSlab{
header: ArraySlabHeader{
slabID: sID,
size: size,
count: uint32(len(rightElements)),
},
next: a.next,
elements: rightElements,
}
// Modify left (original) slab
a.header.size = arrayDataSlabPrefixSize + leftSize
a.header.count = uint32(leftCount)
a.next = rightSlab.header.slabID
return a, rightSlab, nil
}
func (a *ArrayDataSlab) Merge(slab Slab) error {
rightSlab := slab.(*ArrayDataSlab)
a.elements = merge(a.elements, rightSlab.elements)
// This computation is safe because merge is only called when the combined size of
// two slabs fits in a single slab, so the merged slab size <= maxThreshold.
// Also, rightSlab.header.size >= arrayDataSlabPrefixSize because
// header.size = arrayDataSlabPrefixSize + sum of element sizes.
a.header.size = a.header.size + rightSlab.header.size - arrayDataSlabPrefixSize
a.header.count += rightSlab.header.count
a.next = rightSlab.next
return nil
}
// LendToRight rebalances slabs by moving elements from left slab to right slab
func (a *ArrayDataSlab) LendToRight(slab Slab) error {
rightSlab := slab.(*ArrayDataSlab)
count := a.header.count + rightSlab.header.count
size := a.header.size + rightSlab.header.size
oldLeftCount := a.header.count
leftCount := a.header.count
leftSize := a.header.size
midPoint := (size + 1) >> 1
// Left slab size is as close to midPoint as possible while right slab size >= minThreshold
for i := len(a.elements) - 1; i >= 0; i-- {
elemSize := a.elements[i].ByteSize()
// This computation is safe because leftSize starts at a.header.size
// (slab size including all elements) and each elemSize is one element's
// size, so leftSize >= elemSize at every iteration.
if leftSize-elemSize < midPoint && size-leftSize >= minThreshold {
break
}
leftSize -= elemSize
leftCount--
}
// Move elements
moveCount := oldLeftCount - leftCount
a.elements, rightSlab.elements = lendToRight(a.elements, rightSlab.elements, int(moveCount))
// Update right slab
rightSlab.header.size = size - leftSize
rightSlab.header.count = count - leftCount
// Update left slab
a.header.size = leftSize
a.header.count = leftCount
return nil
}
// BorrowFromRight rebalances slabs by moving elements from right slab to left slab.
func (a *ArrayDataSlab) BorrowFromRight(slab Slab) error {
rightSlab := slab.(*ArrayDataSlab)
count := a.header.count + rightSlab.header.count
size := a.header.size + rightSlab.header.size
oldLeftCount := a.header.count
leftCount := a.header.count
leftSize := a.header.size
midPoint := (size + 1) >> 1
for _, e := range rightSlab.elements {
elemSize := e.ByteSize()
if leftSize+elemSize > midPoint {
// This computation is safe because size - leftSize - elemSize >= 0 during iteration:
// size = leftSize + rightSize, and leftSize + elemSize never exceeds size.
if size-leftSize-elemSize >= minThreshold {
// Include this element in left slab
leftSize += elemSize
leftCount++
}
break
}
leftSize += elemSize
leftCount++
}
// Move elements
moveCount := leftCount - oldLeftCount
a.elements, rightSlab.elements = borrowFromRight(a.elements, rightSlab.elements, int(moveCount))
// Update left slab
a.header.size = leftSize
a.header.count = leftCount
// Update right slab
rightSlab.header.size = size - leftSize
rightSlab.header.count = count - leftCount
return nil
}
func (a *ArrayDataSlab) IsFull() bool {
return a.header.size > maxThreshold
}
// IsUnderflow returns the number of bytes needed for the data slab
// to reach the min threshold.
// Returns true if the min threshold has not been reached yet.
func (a *ArrayDataSlab) IsUnderflow() (uint32, bool) {
if minThreshold > a.header.size {
return minThreshold - a.header.size, true
}
return 0, false
}
// CanLendToLeft returns true if elements on the left of the slab could be removed
// so that the slab still stores more than the min threshold.
func (a *ArrayDataSlab) CanLendToLeft(size uint32) bool {
if len(a.elements) < 2 {
return false
}
if a.header.size-size < minThreshold {
return false
}
lendSize := uint32(0)
for i := range a.elements {
lendSize += a.elements[i].ByteSize()
if a.header.size-lendSize < minThreshold {
return false
}
if lendSize >= size {
return true
}
}
return false
}
// CanLendToRight returns true if elements on the right of the slab could be removed
// so that the slab still stores more than the min threshold.
func (a *ArrayDataSlab) CanLendToRight(size uint32) bool {
if len(a.elements) < 2 {
return false
}
if a.header.size-size < minThreshold {
return false
}
lendSize := uint32(0)
for i := len(a.elements) - 1; i >= 0; i-- {
lendSize += a.elements[i].ByteSize()
if a.header.size-lendSize < minThreshold {
return false
}
if lendSize >= size {
return true
}
}
return false
}
// Inline operations
// Inlinable returns true if
// - array data slab is root slab
// - size of inlined array data slab <= maxInlineSize
func (a *ArrayDataSlab) Inlinable(maxInlineSize uint32) bool {
if a.extraData == nil {
// Non-root data slab is not inlinable.
return false
}
// At this point, this data slab is either
// - inlined data slab, or
// - not inlined root data slab
// Compute inlined size from cached slab size
inlinedSize := a.header.size
if !a.inlined {
// This computation is safe because when not inlined,
// header.size >= arrayRootDataSlabPrefixSize, as
// header.size = arrayRootDataSlabPrefixSize + sum of element sizes.
inlinedSize = inlinedSize -
arrayRootDataSlabPrefixSize +
inlinedArrayDataSlabPrefixSize
}
// Inlined byte size must be less than max inline size.
return inlinedSize <= maxInlineSize
}
// Inline converts not-inlined ArrayDataSlab to inlined ArrayDataSlab and removes it from storage.
func (a *ArrayDataSlab) Inline(storage SlabStorage) error {
if a.inlined {
return NewFatalError(fmt.Errorf("failed to inline ArrayDataSlab %s: it is inlined already", a.header.slabID))
}
id := a.header.slabID
// Remove slab from storage because it is going to be inlined.
err := storage.Remove(id)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", id))
}
// Update data slab size as inlined slab.
// This computation is safe because when not inlined header.size >= arrayRootDataSlabPrefixSize,
// as header.size = arrayRootDataSlabPrefixSize + sum of element sizes.
a.header.size = a.header.size -
arrayRootDataSlabPrefixSize +
inlinedArrayDataSlabPrefixSize
// Update data slab inlined status.
a.inlined = true
return nil
}
// Uninline converts an inlined ArrayDataSlab to uninlined ArrayDataSlab and stores it in storage.
func (a *ArrayDataSlab) Uninline(storage SlabStorage) error {
if !a.inlined {
return NewFatalError(fmt.Errorf("failed to un-inline ArrayDataSlab %s: it is not inlined", a.header.slabID))
}
// Update data slab size.
// This computation is safe because when inlined header.size >= inlinedArrayDataSlabPrefixSize,
// as header.size = inlinedArrayDataSlabPrefixSize + sum of element sizes.
a.header.size = a.header.size -
inlinedArrayDataSlabPrefixSize +
arrayRootDataSlabPrefixSize
// Update data slab inlined status
a.inlined = false
// Store slab in storage
return storeSlab(storage, a)
}
func (a *ArrayDataSlab) Inlined() bool {
return a.inlined
}
// Other operations
func (a *ArrayDataSlab) SetSlabID(id SlabID) {
a.header.slabID = id
}
func (a *ArrayDataSlab) Header() ArraySlabHeader {
return a.header
}
func (a *ArrayDataSlab) MutationCount() uint64 {
return a.header.mutationCount
}
func (a *ArrayDataSlab) BumpMutationCount() {
a.header.mutationCount++
}
func (a *ArrayDataSlab) IsData() bool {
return true
}
func (a *ArrayDataSlab) SlabID() SlabID {
return a.header.slabID
}
func (a *ArrayDataSlab) ByteSize() uint32 {
return a.header.size
}
func (a *ArrayDataSlab) ExtraData() *ArrayExtraData {
return a.extraData
}
func (a *ArrayDataSlab) RemoveExtraData() *ArrayExtraData {
extraData := a.extraData
a.extraData = nil
return extraData
}
func (a *ArrayDataSlab) SetExtraData(extraData *ArrayExtraData) {
a.extraData = extraData
}
func (a *ArrayDataSlab) String() string {
elemsStr := make([]string, len(a.elements))
for i, e := range a.elements {
elemsStr[i] = fmt.Sprint(e)
}
return fmt.Sprintf("ArrayDataSlab id:%s size:%d count:%d elements: [%s]",
a.header.slabID,
a.header.size,
a.header.count,
strings.Join(elemsStr, " "),
)
}
func (a *ArrayDataSlab) StoredValue(storage SlabStorage) (Value, error) {
if a.extraData == nil {
return nil, NewNotValueError(a.SlabID())
}
rootID := a.SlabID()
// Share state with any existing *Array instance for this container.
// See array_state.go for rationale.
state := storage.ArrayState(rootID)
if state == nil {
state = newArrayState(a)
storage.SetArrayState(rootID, state)
}
return &Array{
Storage: storage,
state: state,
}, nil
}
func (a *ArrayDataSlab) HasPointer() bool {
return slices.ContainsFunc(a.elements, hasPointer)
}
func (a *ArrayDataSlab) ChildStorables() []Storable {
return slices.Clone(a.elements)
}
func (a *ArrayDataSlab) getPrefixSize() uint32 {
if a.inlined {
return inlinedArrayDataSlabPrefixSize
}
if a.extraData != nil {
return arrayRootDataSlabPrefixSize
}
return arrayDataSlabPrefixSize
}