-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patharray_data_slab.go
554 lines (455 loc) · 14.1 KB
/
array_data_slab.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
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
/*
* 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"
"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{}
// 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 {
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")
}
if index == uint64(len(a.elements)) {
a.elements = append(a.elements, storable)
} else {
a.elements = append(a.elements, nil)
copy(a.elements[index+1:], a.elements[index:])
a.elements[index] = storable
}
a.header.count++
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]
lastIndex := len(a.elements) - 1
if index != uint64(lastIndex) {
copy(a.elements[index:], a.elements[index+1:])
}
// NOTE: prevent memory leak
a.elements[lastIndex] = nil
a.elements = a.elements[:lastIndex]
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.
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
}
// Construct 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,
),
)
}
rightSlabCount := len(a.elements) - leftCount
rightSlab := &ArrayDataSlab{
header: ArraySlabHeader{
slabID: sID,
size: arrayDataSlabPrefixSize + dataSize - leftSize,
count: uint32(rightSlabCount),
},
next: a.next,
}
rightSlab.elements = make([]Storable, rightSlabCount)
copy(rightSlab.elements, a.elements[leftCount:])
// Modify left (original) slab
// NOTE: prevent memory leak
for i := leftCount; i < len(a.elements); i++ {
a.elements[i] = nil
}
a.elements = a.elements[:leftCount]
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 = append(a.elements, rightSlab.elements...)
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
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()
if leftSize-elemSize < midPoint && size-leftSize >= uint32(minThreshold) {
break
}
leftSize -= elemSize
leftCount--
}
// Update the right slab
//
// It is easier and less error-prone to realloc elements for the right slab.
elements := make([]Storable, count-leftCount)
n := copy(elements, a.elements[leftCount:])
copy(elements[n:], rightSlab.elements)
rightSlab.elements = elements
rightSlab.header.size = size - leftSize
rightSlab.header.count = count - leftCount
// Update left slab
// NOTE: prevent memory leak
for i := leftCount; i < uint32(len(a.elements)); i++ {
a.elements[i] = nil
}
a.elements = a.elements[:leftCount]
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
leftCount := a.header.count
leftSize := a.header.size
midPoint := (size + 1) >> 1
for _, e := range rightSlab.elements {
elemSize := e.ByteSize()
if leftSize+elemSize > midPoint {
if size-leftSize-elemSize >= uint32(minThreshold) {
// Include this element in left slab
leftSize += elemSize
leftCount++
}
break
}
leftSize += elemSize
leftCount++
}
rightStartIndex := leftCount - a.header.count
// Update left slab
a.elements = append(a.elements, rightSlab.elements[:rightStartIndex]...)
a.header.size = leftSize
a.header.count = leftCount
// Update right slab
// TODO: copy elements to front instead?
// NOTE: prevent memory leak
for i := uint32(0); i < rightStartIndex; i++ {
rightSlab.elements[i] = nil
}
rightSlab.elements = rightSlab.elements[rightStartIndex:]
rightSlab.header.size = size - leftSize
rightSlab.header.count = count - leftCount
return nil
}
func (a *ArrayDataSlab) IsFull() bool {
return a.header.size > uint32(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 uint32(minThreshold) > a.header.size {
return uint32(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 < uint32(minThreshold) {
return false
}
lendSize := uint32(0)
for i := 0; i < len(a.elements); i++ {
lendSize += a.elements[i].ByteSize()
if a.header.size-lendSize < uint32(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 < uint32(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 < uint32(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 uint64) 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 {
inlinedSize = inlinedSize -
arrayRootDataSlabPrefixSize +
inlinedArrayDataSlabPrefixSize
}
// Inlined byte size must be less than max inline size.
return uint64(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.
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
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) 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())
}
return &Array{
Storage: storage,
root: a,
}, nil
}
func (a *ArrayDataSlab) HasPointer() bool {
for _, e := range a.elements {
if hasPointer(e) {
return true
}
}
return false
}
func (a *ArrayDataSlab) ChildStorables() []Storable {
s := make([]Storable, len(a.elements))
copy(s, a.elements)
return s
}
func (a *ArrayDataSlab) getPrefixSize() uint32 {
if a.inlined {
return inlinedArrayDataSlabPrefixSize
}
if a.extraData != nil {
return arrayRootDataSlabPrefixSize
}
return arrayDataSlabPrefixSize
}