diff --git a/array.go b/array.go index 4ff9bcff..ddf3e95b 100644 --- a/array.go +++ b/array.go @@ -28,20 +28,20 @@ import ( ) const ( - storageIDSize = 16 + slabIDSize = 16 // version and flag size: version (1 byte) + flag (1 byte) versionAndFlagSize = 2 - // slab header size: storage id (16 bytes) + count (4 bytes) + size (4 bytes) - arraySlabHeaderSize = storageIDSize + 4 + 4 + // slab header size: slab id (16 bytes) + count (4 bytes) + size (4 bytes) + arraySlabHeaderSize = slabIDSize + 4 + 4 // meta data slab prefix size: version (1 byte) + flag (1 byte) + child header count (2 bytes) arrayMetaDataSlabPrefixSize = versionAndFlagSize + 2 // version (1 byte) + flag (1 byte) + next id (16 bytes) + CBOR array size (3 bytes) // up to 65535 array elements are supported - arrayDataSlabPrefixSize = versionAndFlagSize + storageIDSize + 3 + arrayDataSlabPrefixSize = versionAndFlagSize + slabIDSize + 3 // version (1 byte) + flag (1 byte) + CBOR array size (3 bytes) // up to 65535 array elements are supported @@ -52,9 +52,9 @@ const ( ) type ArraySlabHeader struct { - id StorageID // id is used to retrieve slab from storage - size uint32 // size is used to split and merge; leaf: size of all element; internal: size of all headers - count uint32 // count is used to lookup element; leaf: number of elements; internal: number of elements in all its headers + slabID SlabID // id is used to retrieve slab from storage + size uint32 // size is used to split and merge; leaf: size of all element; internal: size of all headers + count uint32 // count is used to lookup element; leaf: number of elements; internal: number of elements in all its headers } type ArrayExtraData struct { @@ -63,7 +63,7 @@ type ArrayExtraData struct { // ArrayDataSlab is leaf node, implementing ArraySlab. type ArrayDataSlab struct { - next StorageID + next SlabID header ArraySlabHeader elements []Storable @@ -74,7 +74,7 @@ type ArrayDataSlab struct { func (a *ArrayDataSlab) StoredValue(storage SlabStorage) (Value, error) { if a.extraData == nil { - return nil, NewNotValueError(a.ID()) + return nil, NewNotValueError(a.SlabID()) } return &Array{ Storage: storage, @@ -102,7 +102,7 @@ var _ ArraySlab = &ArrayMetaDataSlab{} func (a *ArrayMetaDataSlab) StoredValue(storage SlabStorage) (Value, error) { if a.extraData == nil { - return nil, NewNotValueError(a.ID()) + return nil, NewNotValueError(a.SlabID()) } return &Array{ Storage: storage, @@ -112,7 +112,6 @@ func (a *ArrayMetaDataSlab) StoredValue(storage SlabStorage) (Value, error) { type ArraySlab interface { Slab - fmt.Stringer Get(storage SlabStorage, index uint64) (Storable, error) Set(storage SlabStorage, address Address, index uint64, value Value) (Storable, error) @@ -126,7 +125,7 @@ type ArraySlab interface { CanLendToLeft(size uint32) bool CanLendToRight(size uint32) bool - SetID(StorageID) + SetSlabID(SlabID) Header() ArraySlabHeader @@ -146,11 +145,11 @@ type Array struct { var _ Value = &Array{} func (a *Array) Address() Address { - return a.root.ID().Address + return a.root.SlabID().address } func (a *Array) Storable(_ SlabStorage, _ Address, _ uint64) (Storable, error) { - return StorageIDStorable(a.StorageID()), nil + return SlabIDStorable(a.SlabID()), nil } const arrayExtraDataLength = 1 @@ -255,7 +254,7 @@ func (a *ArrayExtraData) Encode(enc *Encoder, flag byte) error { } func newArrayDataSlabFromData( - id StorageID, + id SlabID, data []byte, decMode cbor.DecMode, decodeStorable StorableDecoder, @@ -305,22 +304,22 @@ func newArrayDataSlabFromData( ) } - var next StorageID + var next SlabID var contentOffset int if !isRootSlab { - // Decode next storage ID - const nextStorageIDOffset = versionAndFlagSize + // Decode next slab ID + const nextSlabIDOffset = versionAndFlagSize var err error - next, err = NewStorageIDFromRawBytes(data[nextStorageIDOffset:]) + next, err = NewSlabIDFromRawBytes(data[nextSlabIDOffset:]) if err != nil { - // error returned from NewStorageIDFromRawBytes is categorized already. + // error returned from NewSlabIDFromRawBytes is categorized already. return nil, err } - contentOffset = nextStorageIDOffset + storageIDSize + contentOffset = nextSlabIDOffset + slabIDSize } else { contentOffset = versionAndFlagSize @@ -336,7 +335,7 @@ func newArrayDataSlabFromData( elements := make([]Storable, elemCount) for i := 0; i < int(elemCount); i++ { - storable, err := decodeStorable(cborDec, StorageIDUndefined) + storable, err := decodeStorable(cborDec, SlabIDUndefined) if err != nil { // Wrap err as external error (if needed) because err is returned by StorableDecoder callback. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode array element") @@ -345,9 +344,9 @@ func newArrayDataSlabFromData( } header := ArraySlabHeader{ - id: id, - size: uint32(len(data)), - count: uint32(elemCount), + slabID: id, + size: uint32(len(data)), + count: uint32(elemCount), } return &ArrayDataSlab{ @@ -363,7 +362,7 @@ func newArrayDataSlabFromData( // Header (18 bytes): // // +-------------------------------+--------------------------------+ -// | slab version + flag (2 bytes) | next sib storage ID (16 bytes) | +// | slab version + flag (2 bytes) | next sib slab ID (16 bytes) | // +-------------------------------+--------------------------------+ // // Content (for now): @@ -399,18 +398,18 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error { var contentOffset int - // Encode next storage ID for non-root data slabs + // Encode next slab ID for non-root data slabs if a.extraData == nil { - // Encode next storage ID to scratch - const nextStorageIDOffset = versionAndFlagSize - _, err := a.next.ToRawBytes(enc.Scratch[nextStorageIDOffset:]) + // Encode next slab ID to scratch + const nextSlabIDOffset = versionAndFlagSize + _, err := a.next.ToRawBytes(enc.Scratch[nextSlabIDOffset:]) if err != nil { - // Don't need to wrap because err is already categorized by StorageID.ToRawBytes(). + // Don't need to wrap because err is already categorized by SlabID.ToRawBytes(). return err } - contentOffset = nextStorageIDOffset + storageIDSize + contentOffset = nextSlabIDOffset + slabIDSize } else { contentOffset = versionAndFlagSize } @@ -452,7 +451,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error { func (a *ArrayDataSlab) hasPointer() bool { for _, e := range a.elements { - if _, ok := e.(StorageIDStorable); ok { + if _, ok := e.(SlabIDStorable); ok { return true } } @@ -489,10 +488,10 @@ func (a *ArrayDataSlab) Set(storage SlabStorage, address Address, index uint64, a.elements[index] = storable a.header.size = a.header.size - oldSize + storable.ByteSize() - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return oldElem, nil @@ -520,10 +519,10 @@ func (a *ArrayDataSlab) Insert(storage SlabStorage, address Address, index uint6 a.header.count++ a.header.size += storable.ByteSize() - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil @@ -550,10 +549,10 @@ func (a *ArrayDataSlab) Remove(storage SlabStorage, index uint64) (Storable, err a.header.count-- a.header.size -= v.ByteSize() - err := storage.Store(a.header.id, a) + err := storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return v, nil @@ -562,7 +561,7 @@ func (a *ArrayDataSlab) Remove(storage SlabStorage, index uint64) (Storable, err 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.id) + 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. @@ -588,23 +587,23 @@ func (a *ArrayDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { } // Construct right slab - sID, err := storage.GenerateStorageID(a.header.id.Address) + 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 storage ID for address 0x%x", - a.header.id.Address, + "failed to generate slab ID for address 0x%x", + a.header.slabID.address, ), ) } rightSlabCount := len(a.elements) - leftCount rightSlab := &ArrayDataSlab{ header: ArraySlabHeader{ - id: sID, - size: arrayDataSlabPrefixSize + dataSize - leftSize, - count: uint32(rightSlabCount), + slabID: sID, + size: arrayDataSlabPrefixSize + dataSize - leftSize, + count: uint32(rightSlabCount), }, next: a.next, } @@ -620,7 +619,7 @@ func (a *ArrayDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { a.elements = a.elements[:leftCount] a.header.size = arrayDataSlabPrefixSize + leftSize a.header.count = uint32(leftCount) - a.next = rightSlab.header.id + a.next = rightSlab.header.slabID return a, rightSlab, nil } @@ -785,8 +784,8 @@ func (a *ArrayDataSlab) CanLendToRight(size uint32) bool { return false } -func (a *ArrayDataSlab) SetID(id StorageID) { - a.header.id = id +func (a *ArrayDataSlab) SetSlabID(id SlabID) { + a.header.slabID = id } func (a *ArrayDataSlab) Header() ArraySlabHeader { @@ -797,8 +796,8 @@ func (a *ArrayDataSlab) IsData() bool { return true } -func (a *ArrayDataSlab) ID() StorageID { - return a.header.id +func (a *ArrayDataSlab) SlabID() SlabID { + return a.header.slabID } func (a *ArrayDataSlab) ByteSize() uint32 { @@ -841,7 +840,7 @@ func (a *ArrayDataSlab) String() string { } return fmt.Sprintf("ArrayDataSlab id:%s size:%d count:%d elements: [%s]", - a.header.id, + a.header.slabID, a.header.size, a.header.count, strings.Join(elemsStr, " "), @@ -849,7 +848,7 @@ func (a *ArrayDataSlab) String() string { } func newArrayMetaDataSlabFromData( - id StorageID, + id SlabID, data []byte, decMode cbor.DecMode, decodeTypeInfo TypeInfoDecoder, @@ -910,13 +909,13 @@ func newArrayMetaDataSlabFromData( offset := childHeaderCountOffset + 2 for i := 0; i < int(childHeaderCount); i++ { - storageID, err := NewStorageIDFromRawBytes(data[offset:]) + slabID, err := NewSlabIDFromRawBytes(data[offset:]) if err != nil { - // Don't need to wrap because err is already categorized by NewStorageIDFromRawBytes(). + // Don't need to wrap because err is already categorized by NewSlabIDFromRawBytes(). return nil, err } - countOffset := offset + storageIDSize + countOffset := offset + slabIDSize count := binary.BigEndian.Uint32(data[countOffset:]) sizeOffset := countOffset + 4 @@ -925,9 +924,9 @@ func newArrayMetaDataSlabFromData( totalCount += count childrenHeaders[i] = ArraySlabHeader{ - id: storageID, - count: count, - size: size, + slabID: slabID, + count: count, + size: size, } childrenCountSum[i] = totalCount @@ -935,9 +934,9 @@ func newArrayMetaDataSlabFromData( } header := ArraySlabHeader{ - id: id, - size: uint32(len(data)), - count: totalCount, + slabID: id, + size: uint32(len(data)), + count: totalCount, } return &ArrayMetaDataSlab{ @@ -958,7 +957,7 @@ func newArrayMetaDataSlabFromData( // // Content (n * 16 bytes): // -// [[count, size, storage id], ...] +// [[count, size, slab ID], ...] // // If this is root slab, extra data section is prepended to slab's encoded content. // See ArrayExtraData.Encode() for extra data section format. @@ -999,13 +998,13 @@ func (a *ArrayMetaDataSlab) Encode(enc *Encoder) error { // Encode children headers for _, h := range a.childrenHeaders { - _, err := h.id.ToRawBytes(enc.Scratch[:]) + _, err := h.slabID.ToRawBytes(enc.Scratch[:]) if err != nil { - // Don't need to wrap because err is already categorized by StorageID.ToRawBytes(). + // Don't need to wrap because err is already categorized by SlabID.ToRawBytes(). return err } - const countOffset = storageIDSize + const countOffset = slabIDSize binary.BigEndian.PutUint32(enc.Scratch[countOffset:], h.count) const sizeOffset = countOffset + 4 @@ -1026,7 +1025,7 @@ func (a *ArrayMetaDataSlab) ChildStorables() []Storable { childIDs := make([]Storable, len(a.childrenHeaders)) for i, h := range a.childrenHeaders { - childIDs[i] = StorageIDStorable(h.id) + childIDs[i] = SlabIDStorable(h.slabID) } return childIDs @@ -1038,11 +1037,11 @@ func (a *ArrayMetaDataSlab) childSlabIndexInfo( ) ( childHeaderIndex int, adjustedIndex uint64, - childID StorageID, + childID SlabID, err error, ) { if index >= uint64(a.header.count) { - return 0, 0, StorageID{}, NewIndexOutOfBoundsError(index, 0, uint64(a.header.count)) + return 0, 0, SlabID{}, NewIndexOutOfBoundsError(index, 0, uint64(a.header.count)) } // Either perform a linear scan (for small number of children), @@ -1079,7 +1078,7 @@ func (a *ArrayMetaDataSlab) childSlabIndexInfo( childHeader := a.childrenHeaders[childHeaderIndex] adjustedIndex = index + uint64(childHeader.count) - uint64(a.childrenCountSum[childHeaderIndex]) - childID = childHeader.id + childID = childHeader.slabID return childHeaderIndex, adjustedIndex, childID, nil } @@ -1145,10 +1144,10 @@ func (a *ArrayMetaDataSlab) Set(storage SlabStorage, address Address, index uint return existingElem, nil } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return existingElem, nil } @@ -1161,13 +1160,13 @@ func (a *ArrayMetaDataSlab) Insert(storage SlabStorage, address Address, index u return NewIndexOutOfBoundsError(index, 0, uint64(a.header.count)) } - var childID StorageID + var childID SlabID var childHeaderIndex int var adjustedIndex uint64 if index == uint64(a.header.count) { childHeaderIndex = len(a.childrenHeaders) - 1 h := a.childrenHeaders[childHeaderIndex] - childID = h.id + childID = h.slabID adjustedIndex = uint64(h.count) } else { var err error @@ -1210,10 +1209,10 @@ func (a *ArrayMetaDataSlab) Insert(storage SlabStorage, address Address, index u // Insertion always increases the size, // so there is no need to check underflow - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil @@ -1266,10 +1265,10 @@ func (a *ArrayMetaDataSlab) Remove(storage SlabStorage, index uint64) (Storable, // Removal always decreases the size, // so there is no need to check isFull - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return v, nil @@ -1302,22 +1301,22 @@ func (a *ArrayMetaDataSlab) SplitChildSlab(storage SlabStorage, child ArraySlab, a.header.size += arraySlabHeaderSize // Store modified slabs - err = storage.Store(left.ID(), left) + err = storage.Store(left.SlabID(), left) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.SlabID())) } - err = storage.Store(right.ID(), right) + err = storage.Store(right.SlabID(), right) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil @@ -1345,7 +1344,7 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( // Retrieve left and right siblings of the same parent. var leftSib, rightSib ArraySlab if childHeaderIndex > 0 { - leftSibID := a.childrenHeaders[childHeaderIndex-1].id + leftSibID := a.childrenHeaders[childHeaderIndex-1].slabID var err error leftSib, err = getArraySlab(storage, leftSibID) @@ -1355,7 +1354,7 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( } } if childHeaderIndex < len(a.childrenHeaders)-1 { - rightSibID := a.childrenHeaders[childHeaderIndex+1].id + rightSibID := a.childrenHeaders[childHeaderIndex+1].slabID var err error rightSib, err = getArraySlab(storage, rightSibID) @@ -1388,20 +1387,20 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.childrenCountSum[childHeaderIndex] = baseCountSum + child.Header().count // Store modified slabs - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(rightSib.ID(), rightSib) + err = storage.Store(rightSib.SlabID(), rightSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil } @@ -1423,20 +1422,20 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.childrenCountSum[childHeaderIndex-1] = baseCountSum + leftSib.Header().count // Store modified slabs - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil } @@ -1458,20 +1457,20 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.childrenCountSum[childHeaderIndex-1] = baseCountSum + leftSib.Header().count // Store modified slabs - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil } else { @@ -1492,20 +1491,20 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.childrenCountSum[childHeaderIndex] = baseCountSum + child.Header().count // Store modified slabs - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(rightSib.ID(), rightSib) + err = storage.Store(rightSib.SlabID(), rightSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil } @@ -1535,23 +1534,23 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.header.size -= arraySlabHeaderSize // Store modified slabs in storage - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } // Remove right sib from storage - err = storage.Remove(rightSib.ID()) + err = storage.Remove(rightSib.SlabID()) 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", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", rightSib.SlabID())) } return nil @@ -1579,23 +1578,23 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.header.size -= arraySlabHeaderSize // Store modified slabs in storage - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } // Remove child from storage - err = storage.Remove(child.ID()) + err = storage.Remove(child.SlabID()) 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", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", child.SlabID())) } return nil @@ -1622,22 +1621,22 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.header.size -= arraySlabHeaderSize // Store modified slabs in storage - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } // Remove child from storage - err = storage.Remove(child.ID()) + err = storage.Remove(child.SlabID()) 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", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", child.SlabID())) } return nil @@ -1663,22 +1662,22 @@ func (a *ArrayMetaDataSlab) MergeOrRebalanceChildSlab( a.header.size -= arraySlabHeaderSize // Store modified slabs in storage - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(a.header.id, a) + err = storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } // Remove rightSib from storage - err = storage.Remove(rightSib.ID()) + err = storage.Remove(rightSib.SlabID()) 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", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", rightSib.SlabID())) } return nil @@ -1710,7 +1709,7 @@ func (a *ArrayMetaDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { if len(a.childrenHeaders) < 2 { // Can't split meta slab with less than 2 headers - return nil, nil, NewSlabSplitErrorf("ArrayMetaDataSlab (%s) has less than 2 child headers", a.header.id) + return nil, nil, NewSlabSplitErrorf("ArrayMetaDataSlab (%s) has less than 2 child headers", a.header.slabID) } leftChildrenCount := int(math.Ceil(float64(len(a.childrenHeaders)) / 2)) @@ -1722,19 +1721,19 @@ func (a *ArrayMetaDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { } // Construct right slab - sID, err := storage.GenerateStorageID(a.header.id.Address) + 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 storage ID for address 0x%x", a.header.id.Address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", a.header.slabID.address)) } rightSlab := &ArrayMetaDataSlab{ header: ArraySlabHeader{ - id: sID, - size: a.header.size - uint32(leftSize), - count: a.header.count - leftCount, + slabID: sID, + size: a.header.size - uint32(leftSize), + count: a.header.count - leftCount, }, } @@ -1859,8 +1858,8 @@ func (a ArrayMetaDataSlab) IsData() bool { return false } -func (a *ArrayMetaDataSlab) SetID(id StorageID) { - a.header.id = id +func (a *ArrayMetaDataSlab) SetSlabID(id SlabID) { + a.header.slabID = id } func (a *ArrayMetaDataSlab) Header() ArraySlabHeader { @@ -1871,8 +1870,8 @@ func (a *ArrayMetaDataSlab) ByteSize() uint32 { return a.header.size } -func (a *ArrayMetaDataSlab) ID() StorageID { - return a.header.id +func (a *ArrayMetaDataSlab) SlabID() SlabID { + return a.header.slabID } func (a *ArrayMetaDataSlab) ExtraData() *ArrayExtraData { @@ -1894,7 +1893,7 @@ func (a *ArrayMetaDataSlab) PopIterate(storage SlabStorage, fn ArrayPopIteration // Iterate child slabs backwards for i := len(a.childrenHeaders) - 1; i >= 0; i-- { - childID := a.childrenHeaders[i].id + childID := a.childrenHeaders[i].slabID child, err := getArraySlab(storage, childID) if err != nil { @@ -1930,11 +1929,11 @@ func (a *ArrayMetaDataSlab) PopIterate(storage SlabStorage, fn ArrayPopIteration func (a *ArrayMetaDataSlab) String() string { elemsStr := make([]string, len(a.childrenHeaders)) for i, h := range a.childrenHeaders { - elemsStr[i] = fmt.Sprintf("{id:%s size:%d count:%d}", h.id, h.size, h.count) + elemsStr[i] = fmt.Sprintf("{id:%s size:%d count:%d}", h.slabID, h.size, h.count) } return fmt.Sprintf("ArrayMetaDataSlab id:%s size:%d count:%d children: [%s]", - a.header.id, + a.header.slabID, a.header.size, a.header.count, strings.Join(elemsStr, " "), @@ -1945,26 +1944,26 @@ func NewArray(storage SlabStorage, address Address, typeInfo TypeInfo) (*Array, extraData := &ArrayExtraData{TypeInfo: typeInfo} - sID, err := storage.GenerateStorageID(address) + sID, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return nil, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } root := &ArrayDataSlab{ header: ArraySlabHeader{ - id: sID, - size: arrayRootDataSlabPrefixSize, + slabID: sID, + size: arrayRootDataSlabPrefixSize, }, extraData: extraData, } - err = storage.Store(root.header.id, root) + err = storage.Store(root.header.slabID, root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.header.slabID)) } return &Array{ @@ -1973,9 +1972,9 @@ func NewArray(storage SlabStorage, address Address, typeInfo TypeInfo) (*Array, }, nil } -func NewArrayWithRootID(storage SlabStorage, rootID StorageID) (*Array, error) { - if rootID == StorageIDUndefined { - return nil, NewStorageIDErrorf("cannot create Array from undefined storage id") +func NewArrayWithRootID(storage SlabStorage, rootID SlabID) (*Array, error) { + if rootID == SlabIDUndefined { + return nil, NewSlabIDErrorf("cannot create Array from undefined slab ID") } root, err := getArraySlab(storage, rootID) @@ -2028,7 +2027,7 @@ func (a *Array) Set(index uint64, value Value) (Storable, error) { if !a.root.IsData() { root := a.root.(*ArrayMetaDataSlab) if len(root.childrenHeaders) == 1 { - err = a.promoteChildAsNewRoot(root.childrenHeaders[0].id) + err = a.promoteChildAsNewRoot(root.childrenHeaders[0].slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by Array.promoteChildAsNewRoot(). return nil, err @@ -2070,7 +2069,7 @@ func (a *Array) Remove(index uint64) (Storable, error) { // Set root to its child slab if root has one child slab. root := a.root.(*ArrayMetaDataSlab) if len(root.childrenHeaders) == 1 { - err = a.promoteChildAsNewRoot(root.childrenHeaders[0].id) + err = a.promoteChildAsNewRoot(root.childrenHeaders[0].slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by Array.promoteChildAsNewRoot(). return nil, err @@ -2093,19 +2092,19 @@ func (a *Array) splitRoot() error { extraData := a.root.RemoveExtraData() // Save root node id - rootID := a.root.ID() + rootID := a.root.SlabID() - // Assign a new storage id to old root before splitting it. - sID, err := a.Storage.GenerateStorageID(a.Address()) + // Assign a new slab ID to old root before splitting it. + sID, err := a.Storage.GenerateSlabID(a.Address()) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID for address 0x%x", a.Address())) + fmt.Sprintf("failed to generate slab ID for address 0x%x", a.Address())) } oldRoot := a.root - oldRoot.SetID(sID) + oldRoot.SetSlabID(sID) // Split old root leftSlab, rightSlab, err := oldRoot.Split(a.Storage) @@ -2117,12 +2116,12 @@ func (a *Array) splitRoot() error { left := leftSlab.(ArraySlab) right := rightSlab.(ArraySlab) - // Create new ArrayMetaDataSlab with the old root's storage ID + // Create new ArrayMetaDataSlab with the old root's slab ID newRoot := &ArrayMetaDataSlab{ header: ArraySlabHeader{ - id: rootID, - count: left.Header().count + right.Header().count, - size: arrayMetaDataSlabPrefixSize + arraySlabHeaderSize*2, + slabID: rootID, + count: left.Header().count + right.Header().count, + size: arrayMetaDataSlabPrefixSize + arraySlabHeaderSize*2, }, childrenHeaders: []ArraySlabHeader{left.Header(), right.Header()}, childrenCountSum: []uint32{left.Header().count, left.Header().count + right.Header().count}, @@ -2131,26 +2130,26 @@ func (a *Array) splitRoot() error { a.root = newRoot - err = a.Storage.Store(left.ID(), left) + err = a.Storage.Store(left.SlabID(), left) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.SlabID())) } - err = a.Storage.Store(right.ID(), right) + err = a.Storage.Store(right.SlabID(), right) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.SlabID())) } - err = a.Storage.Store(a.root.ID(), a.root) + err = a.Storage.Store(a.root.SlabID(), a.root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.root.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.root.SlabID())) } return nil } -func (a *Array) promoteChildAsNewRoot(childID StorageID) error { +func (a *Array) promoteChildAsNewRoot(childID SlabID) error { child, err := getArraySlab(a.Storage, childID) if err != nil { @@ -2166,11 +2165,11 @@ func (a *Array) promoteChildAsNewRoot(childID StorageID) error { extraData := a.root.RemoveExtraData() - rootID := a.root.ID() + rootID := a.root.SlabID() a.root = child - a.root.SetID(rootID) + a.root.SetSlabID(rootID) a.root.SetExtraData(extraData) @@ -2192,7 +2191,7 @@ var emptyArrayIterator = &ArrayIterator{} type ArrayIterator struct { storage SlabStorage - id StorageID + id SlabID dataSlab *ArrayDataSlab index int remainingCount int @@ -2204,7 +2203,7 @@ func (i *ArrayIterator) Next() (Value, error) { } if i.dataSlab == nil { - if i.id == StorageIDUndefined { + if i.id == SlabIDUndefined { return nil, nil } @@ -2252,7 +2251,7 @@ func (a *Array) Iterator() (*ArrayIterator, error) { return &ArrayIterator{ storage: a.Storage, - id: slab.ID(), + id: slab.SlabID(), dataSlab: slab, remainingCount: int(a.Count()), }, nil @@ -2301,7 +2300,7 @@ func (a *Array) RangeIterator(startIndex uint64, endIndex uint64) (*ArrayIterato return &ArrayIterator{ storage: a.Storage, - id: dataSlab.ID(), + id: dataSlab.SlabID(), dataSlab: dataSlab, index: int(index), remainingCount: int(numberOfElements), @@ -2369,16 +2368,16 @@ func (a *Array) Count() uint64 { return uint64(a.root.Header().count) } -func (a *Array) StorageID() StorageID { - return a.root.ID() +func (a *Array) SlabID() SlabID { + return a.root.SlabID() } -func (a *Array) ID() ID { - sid := a.StorageID() +func (a *Array) ValueID() ValueID { + sid := a.SlabID() - var id ID - copy(id[:], sid.Address[:]) - copy(id[8:], sid.Index[:]) + var id ValueID + copy(id[:], sid.address[:]) + copy(id[8:], sid.index[:]) return id } @@ -2411,7 +2410,7 @@ func (a *Array) String() string { return fmt.Sprintf("[%s]", strings.Join(elemsStr, " ")) } -func getArraySlab(storage SlabStorage, id StorageID) (ArraySlab, error) { +func getArraySlab(storage SlabStorage, id SlabID) (ArraySlab, error) { slab, found, err := storage.Retrieve(id) if err != nil { // err can be an external error because storage is an interface. @@ -2432,7 +2431,7 @@ func firstArrayDataSlab(storage SlabStorage, slab ArraySlab) (*ArrayDataSlab, er return slab.(*ArrayDataSlab), nil } meta := slab.(*ArrayMetaDataSlab) - firstChildID := meta.childrenHeaders[0].id + firstChildID := meta.childrenHeaders[0].slabID firstChild, err := getArraySlab(storage, firstChildID) if err != nil { // Don't need to wrap error as external error because err is already categorized by getArraySlab(). @@ -2481,24 +2480,24 @@ func (a *Array) PopIterate(fn ArrayPopIterationFunc) error { return err } - rootID := a.root.ID() + rootID := a.root.SlabID() extraData := a.root.ExtraData() // Set root to empty data slab a.root = &ArrayDataSlab{ header: ArraySlabHeader{ - id: rootID, - size: arrayRootDataSlabPrefixSize, + slabID: rootID, + size: arrayRootDataSlabPrefixSize, }, extraData: extraData, } // Save root slab - err = a.Storage.Store(a.root.ID(), a.root) + err = a.Storage.Store(a.root.SlabID(), a.root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.root.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.root.SlabID())) } return nil @@ -2510,18 +2509,18 @@ func NewArrayFromBatchData(storage SlabStorage, address Address, typeInfo TypeIn var slabs []ArraySlab - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return nil, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } dataSlab := &ArrayDataSlab{ header: ArraySlabHeader{ - id: id, - size: arrayDataSlabPrefixSize, + slabID: id, + size: arrayDataSlabPrefixSize, }, } @@ -2540,15 +2539,15 @@ func NewArrayFromBatchData(storage SlabStorage, address Address, typeInfo TypeIn if dataSlab.header.size >= uint32(targetThreshold) { // Generate storge id for next data slab - nextID, err := storage.GenerateStorageID(address) + nextID, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return nil, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } - // Save next slab's storage id in data slab + // Save next slab's slab ID in data slab dataSlab.next = nextID // Append data slab to dataSlabs @@ -2557,8 +2556,8 @@ func NewArrayFromBatchData(storage SlabStorage, address Address, typeInfo TypeIn // Create next data slab dataSlab = &ArrayDataSlab{ header: ArraySlabHeader{ - id: nextID, - size: arrayDataSlabPrefixSize, + slabID: nextID, + size: arrayDataSlabPrefixSize, }, } @@ -2622,12 +2621,12 @@ func NewArrayFromBatchData(storage SlabStorage, address Address, typeInfo TypeIn // Store all slabs for _, slab := range slabs { - err = storage.Store(slab.ID(), slab) + err = storage.Store(slab.SlabID(), slab) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return nil, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to store slab %s", slab.ID())) + fmt.Sprintf("failed to store slab %s", slab.SlabID())) } } @@ -2654,10 +2653,10 @@ func NewArrayFromBatchData(storage SlabStorage, address Address, typeInfo TypeIn root.SetExtraData(extraData) // Store root - err = storage.Store(root.ID(), root) + err = storage.Store(root.SlabID(), root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.ID())) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.SlabID())) } return &Array{ @@ -2676,18 +2675,18 @@ func nextLevelArraySlabs(storage SlabStorage, address Address, slabs []ArraySlab nextLevelSlabsIndex := 0 // Generate storge id - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return nil, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } metaSlab := &ArrayMetaDataSlab{ header: ArraySlabHeader{ - id: id, - size: arrayMetaDataSlabPrefixSize, + slabID: id, + size: arrayMetaDataSlabPrefixSize, }, } @@ -2699,18 +2698,18 @@ func nextLevelArraySlabs(storage SlabStorage, address Address, slabs []ArraySlab nextLevelSlabsIndex++ // Generate storge id for next meta data slab - id, err = storage.GenerateStorageID(address) + id, err = storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. return nil, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } metaSlab = &ArrayMetaDataSlab{ header: ArraySlabHeader{ - id: id, - size: arrayMetaDataSlabPrefixSize, + slabID: id, + size: arrayMetaDataSlabPrefixSize, }, } } diff --git a/array_bench_test.go b/array_bench_test.go index 04578845..572abff8 100644 --- a/array_bench_test.go +++ b/array_bench_test.go @@ -204,7 +204,7 @@ func setupArray(b *testing.B, r *rand.Rand, storage *PersistentSlabStorage, init err = storage.Commit() require.NoError(b, err) - arrayID := array.StorageID() + arrayID := array.SlabID() storage.DropCache() diff --git a/array_benchmark_test.go b/array_benchmark_test.go index d141425a..2c6b3918 100644 --- a/array_benchmark_test.go +++ b/array_benchmark_test.go @@ -99,7 +99,7 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) { require.NoError(b, storage.Commit()) b.ResetTimer() - arrayID := array.StorageID() + arrayID := array.SlabID() // append storage.DropCache() diff --git a/array_debug.go b/array_debug.go index a6e076a0..18012557 100644 --- a/array_debug.go +++ b/array_debug.go @@ -47,13 +47,13 @@ func GetArrayStats(a *Array) (ArrayStats, error) { dataSlabCount := uint64(0) storableSlabCount := uint64(0) - nextLevelIDs := []StorageID{a.StorageID()} + nextLevelIDs := []SlabID{a.SlabID()} for len(nextLevelIDs) > 0 { ids := nextLevelIDs - nextLevelIDs = []StorageID(nil) + nextLevelIDs = []SlabID(nil) for _, id := range ids { @@ -68,7 +68,7 @@ func GetArrayStats(a *Array) (ArrayStats, error) { childStorables := slab.ChildStorables() for _, s := range childStorables { - if _, ok := s.(StorageIDStorable); ok { + if _, ok := s.(SlabIDStorable); ok { storableSlabCount++ } } @@ -76,11 +76,11 @@ func GetArrayStats(a *Array) (ArrayStats, error) { metaDataSlabCount++ for _, storable := range slab.ChildStorables() { - id, ok := storable.(StorageIDStorable) + id, ok := storable.(SlabIDStorable) if !ok { - return ArrayStats{}, NewFatalError(fmt.Errorf("metadata slab's child storables are not of type StorageIDStorable")) + return ArrayStats{}, NewFatalError(fmt.Errorf("metadata slab's child storables are not of type SlabIDStorable")) } - nextLevelIDs = append(nextLevelIDs, StorageID(id)) + nextLevelIDs = append(nextLevelIDs, SlabID(id)) } } } @@ -111,16 +111,16 @@ func PrintArray(a *Array) { func DumpArraySlabs(a *Array) ([]string, error) { var dumps []string - nextLevelIDs := []StorageID{a.StorageID()} + nextLevelIDs := []SlabID{a.SlabID()} - var overflowIDs []StorageID + var overflowIDs []SlabID level := 0 for len(nextLevelIDs) > 0 { ids := nextLevelIDs - nextLevelIDs = []StorageID(nil) + nextLevelIDs = []SlabID(nil) for _, id := range ids { @@ -136,8 +136,8 @@ func DumpArraySlabs(a *Array) ([]string, error) { childStorables := dataSlab.ChildStorables() for _, e := range childStorables { - if id, ok := e.(StorageIDStorable); ok { - overflowIDs = append(overflowIDs, StorageID(id)) + if id, ok := e.(SlabIDStorable); ok { + overflowIDs = append(overflowIDs, SlabID(id)) } } @@ -146,11 +146,11 @@ func DumpArraySlabs(a *Array) ([]string, error) { dumps = append(dumps, fmt.Sprintf("level %d, %s", level+1, meta)) for _, storable := range slab.ChildStorables() { - id, ok := storable.(StorageIDStorable) + id, ok := storable.(SlabIDStorable) if !ok { - return nil, NewFatalError(errors.New("metadata slab's child storables are not of type StorageIDStorable")) + return nil, NewFatalError(errors.New("metadata slab's child storables are not of type SlabIDStorable")) } - nextLevelIDs = append(nextLevelIDs, StorageID(id)) + nextLevelIDs = append(nextLevelIDs, SlabID(id)) } } } @@ -167,7 +167,7 @@ func DumpArraySlabs(a *Array) ([]string, error) { if !found { return nil, NewSlabNotFoundErrorf(id, "slab not found during array slab dump") } - dumps = append(dumps, fmt.Sprintf("overflow: %s", slab)) + dumps = append(dumps, slab.String()) } return dumps, nil @@ -179,21 +179,21 @@ func ValidArray(a *Array, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInp extraData := a.root.ExtraData() if extraData == nil { - return NewFatalError(fmt.Errorf("root slab %d doesn't have extra data", a.root.ID())) + return NewFatalError(fmt.Errorf("root slab %d doesn't have extra data", a.root.SlabID())) } // Verify that extra data has correct type information if typeInfo != nil && !tic(extraData.TypeInfo, typeInfo) { return NewFatalError(fmt.Errorf( "root slab %d type information %v is wrong, want %v", - a.root.ID(), + a.root.SlabID(), extraData.TypeInfo, typeInfo, )) } computedCount, dataSlabIDs, nextDataSlabIDs, err := - validArraySlab(tic, hip, a.Storage, a.root.Header().id, 0, nil, []StorageID{}, []StorageID{}) + validArraySlab(tic, hip, a.Storage, a.root.Header().slabID, 0, nil, []SlabID{}, []SlabID{}) if err != nil { // Don't need to wrap error as external error because err is already categorized by validArraySlab(). return err @@ -201,7 +201,7 @@ func ValidArray(a *Array, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInp // Verify array count if computedCount != uint32(a.Count()) { - return NewFatalError(fmt.Errorf("root slab %d count %d is wrong, want %d", a.root.ID(), a.Count(), computedCount)) + return NewFatalError(fmt.Errorf("root slab %d count %d is wrong, want %d", a.root.SlabID(), a.Count(), computedCount)) } // Verify next data slab ids @@ -217,15 +217,15 @@ func validArraySlab( tic TypeInfoComparator, hip HashInputProvider, storage SlabStorage, - id StorageID, + id SlabID, level int, headerFromParentSlab *ArraySlabHeader, - dataSlabIDs []StorageID, - nextDataSlabIDs []StorageID, + dataSlabIDs []SlabID, + nextDataSlabIDs []SlabID, ) ( elementCount uint32, - _dataSlabIDs []StorageID, - _nextDataSlabIDs []StorageID, + _dataSlabIDs []SlabID, + _nextDataSlabIDs []SlabID, err error, ) { @@ -296,7 +296,7 @@ func validArraySlab( dataSlabIDs = append(dataSlabIDs, id) - if dataSlab.next != StorageIDUndefined { + if dataSlab.next != SlabIDUndefined { nextDataSlabIDs = append(nextDataSlabIDs, dataSlab.next) } @@ -354,7 +354,7 @@ func validArraySlab( // Verify child slabs var count uint32 count, dataSlabIDs, nextDataSlabIDs, err = - validArraySlab(tic, hip, storage, h.id, level+1, &h, dataSlabIDs, nextDataSlabIDs) + validArraySlab(tic, hip, storage, h.slabID, level+1, &h, dataSlabIDs, nextDataSlabIDs) if err != nil { // Don't need to wrap error as external error because err is already categorized by validArraySlab(). return 0, nil, nil, err @@ -399,7 +399,7 @@ func ValidArraySerialization( ) error { return validArraySlabSerialization( a.Storage, - a.root.ID(), + a.root.SlabID(), cborDecMode, cborEncMode, decodeStorable, @@ -410,7 +410,7 @@ func ValidArraySerialization( func validArraySlabSerialization( storage SlabStorage, - id StorageID, + id SlabID, cborDecMode cbor.DecMode, cborEncMode cbor.EncMode, decodeStorable StorableDecoder, @@ -516,7 +516,7 @@ func validArraySlabSerialization( // Verify child slabs err = validArraySlabSerialization( storage, - h.id, + h.slabID, cborDecMode, cborEncMode, decodeStorable, @@ -574,11 +574,11 @@ func arrayDataSlabEqual( } // Compare nested element - if idStorable, ok := ee.(StorageIDStorable); ok { + if idStorable, ok := ee.(SlabIDStorable); ok { ev, err := idStorable.StoredValue(storage) if err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageIDStorable.StoredValue(). + // Don't need to wrap error as external error because err is already categorized by SlabIDStorable.StoredValue(). return err } diff --git a/array_test.go b/array_test.go index 7931cfcb..66b06d08 100644 --- a/array_test.go +++ b/array_test.go @@ -100,12 +100,12 @@ func verifyArray( rootIDSet, err := CheckStorageHealth(storage, 1) require.NoError(t, err) - rootIDs := make([]StorageID, 0, len(rootIDSet)) + rootIDs := make([]SlabID, 0, len(rootIDSet)) for id := range rootIDSet { rootIDs = append(rootIDs, id) } require.Equal(t, 1, len(rootIDs)) - require.Equal(t, array.StorageID(), rootIDs[0]) + require.Equal(t, array.SlabID(), rootIDs[0]) if !hasNestedArrayMapElement { // Need to call Commit before calling storage.Count() for PersistentSlabStorage. @@ -483,8 +483,8 @@ func TestArrayRemove(t *testing.T) { valueEqual(t, typeInfoComparator, values[i], existingValue) - if id, ok := existingStorable.(StorageIDStorable); ok { - err = array.Storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = array.Storage.Remove(SlabID(id)) require.NoError(t, err) } @@ -530,8 +530,8 @@ func TestArrayRemove(t *testing.T) { valueEqual(t, typeInfoComparator, values[i], existingValue) - if id, ok := existingStorable.(StorageIDStorable); ok { - err = array.Storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = array.Storage.Remove(SlabID(id)) require.NoError(t, err) } @@ -580,8 +580,8 @@ func TestArrayRemove(t *testing.T) { valueEqual(t, typeInfoComparator, v, existingValue) - if id, ok := existingStorable.(StorageIDStorable); ok { - err = array.Storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = array.Storage.Remove(SlabID(id)) require.NoError(t, err) } @@ -1025,7 +1025,7 @@ func TestArrayIterateRange(t *testing.T) { }) } -func TestArrayRootStorageID(t *testing.T) { +func TestArrayRootSlabID(t *testing.T) { SetThreshold(256) defer SetThreshold(1024) @@ -1038,14 +1038,14 @@ func TestArrayRootStorageID(t *testing.T) { array, err := NewArray(storage, address, typeInfo) require.NoError(t, err) - savedRootID := array.StorageID() - require.NotEqual(t, StorageIDUndefined, savedRootID) + savedRootID := array.SlabID() + require.NotEqual(t, SlabIDUndefined, savedRootID) // Append elements for i := uint64(0); i < arraySize; i++ { err := array.Append(Uint64Value(i)) require.NoError(t, err) - require.Equal(t, savedRootID, array.StorageID()) + require.Equal(t, savedRootID, array.SlabID()) } require.True(t, typeInfoComparator(typeInfo, array.Type())) @@ -1057,13 +1057,13 @@ func TestArrayRootStorageID(t *testing.T) { storable, err := array.Remove(0) require.NoError(t, err) require.Equal(t, Uint64Value(i), storable) - require.Equal(t, savedRootID, array.StorageID()) + require.Equal(t, savedRootID, array.SlabID()) } require.True(t, typeInfoComparator(typeInfo, array.Type())) require.Equal(t, address, array.Address()) require.Equal(t, uint64(0), array.Count()) - require.Equal(t, savedRootID, array.StorageID()) + require.Equal(t, savedRootID, array.SlabID()) } func TestArraySetRandomValues(t *testing.T) { @@ -1232,8 +1232,8 @@ func TestArrayRemoveRandomValues(t *testing.T) { copy(values[k:], values[k+1:]) values = values[:len(values)-1] - if id, ok := existingStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } } @@ -1297,8 +1297,8 @@ func testArrayAppendSetInsertRemoveRandomValues( require.NoError(t, err) valueEqual(t, typeInfoComparator, oldV, existingValue) - if id, ok := existingStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } @@ -1330,8 +1330,8 @@ func testArrayAppendSetInsertRemoveRandomValues( copy(values[k:], values[k+1:]) values = values[:len(values)-1] - if id, ok := existingStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } } @@ -1545,13 +1545,13 @@ func TestArrayEncodeDecode(t *testing.T) { slabData, err := storage.Encode() require.NoError(t, err) require.Equal(t, 1, len(slabData)) - require.Equal(t, expectedData, slabData[array.StorageID()]) + require.Equal(t, expectedData, slabData[array.SlabID()]) // Decode data to new storage storage2 := newTestPersistentStorageWithData(t, slabData) // Test new array from storage2 - array2, err := NewArrayWithRootID(storage2, array.StorageID()) + array2, err := NewArrayWithRootID(storage2, array.SlabID()) require.NoError(t, err) verifyEmptyArray(t, storage2, typeInfo, address, array2) @@ -1594,13 +1594,13 @@ func TestArrayEncodeDecode(t *testing.T) { slabData, err := storage.Encode() require.NoError(t, err) require.Equal(t, 1, len(slabData)) - require.Equal(t, expectedData, slabData[array.StorageID()]) + require.Equal(t, expectedData, slabData[array.SlabID()]) // Decode data to new storage storage2 := newTestPersistentStorageWithData(t, slabData) // Test new array from storage2 - array2, err := NewArrayWithRootID(storage2, array.StorageID()) + array2, err := NewArrayWithRootID(storage2, array.SlabID()) require.NoError(t, err) verifyArray(t, storage2, typeInfo, address, array2, values, false) @@ -1639,13 +1639,13 @@ func TestArrayEncodeDecode(t *testing.T) { require.Equal(t, uint64(arraySize), array.Count()) require.Equal(t, uint64(1), nestedArray.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} - id2 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}} - id3 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 3}} - id4 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 4}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}} + id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}} + id4 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}} - // Expected serialized slab data with storage id - expected := map[StorageID][]byte{ + // Expected serialized slab data with slab id + expected := map[SlabID][]byte{ // (metadata slab) headers: [{id:2 size:228 count:9} {id:3 size:270 count:11} ] id1: { @@ -1665,7 +1665,7 @@ func TestArrayEncodeDecode(t *testing.T) { 0x81, // child header count 0x00, 0x02, - // child header 1 (storage id, count, size) + // child header 1 (slab id, count, size) 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0xe4, @@ -1681,7 +1681,7 @@ func TestArrayEncodeDecode(t *testing.T) { 0x00, // array data slab flag 0x00, - // next storage id + // next slab id 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // CBOR encoded array head (fixed size 3 byte) 0x99, 0x00, 0x09, @@ -1697,13 +1697,13 @@ func TestArrayEncodeDecode(t *testing.T) { 0x76, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, }, - // (data slab) next: 0, data: [aaaaaaaaaaaaaaaaaaaaaa ... StorageID(...)] + // (data slab) next: 0, data: [aaaaaaaaaaaaaaaaaaaaaa ... SlabID(...)] id3: { // version 0x00, // array data slab flag 0x40, - // next storage id + // next slab id 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // CBOR encoded array head (fixed size 3 byte) 0x99, 0x00, 0x0b, @@ -1756,7 +1756,7 @@ func TestArrayEncodeDecode(t *testing.T) { storage2 := newTestPersistentStorageWithData(t, m) // Test new array from storage2 - array2, err := NewArrayWithRootID(storage2, array.StorageID()) + array2, err := NewArrayWithRootID(storage2, array.SlabID()) require.NoError(t, err) verifyArray(t, storage2, typeInfo, address, array2, values, false) @@ -1784,7 +1784,7 @@ func TestArrayEncodeDecodeRandomValues(t *testing.T) { storage2 := newTestPersistentStorageWithBaseStorage(t, storage.baseStorage) // Test new array from storage2 - array2, err := NewArrayWithRootID(storage2, array.StorageID()) + array2, err := NewArrayWithRootID(storage2, array.SlabID()) require.NoError(t, err) verifyArray(t, storage2, typeInfo, address, array2, values, false) @@ -1956,7 +1956,7 @@ func TestArrayStoredValue(t *testing.T) { require.NoError(t, err) } - rootID := array.StorageID() + rootID := array.SlabID() slabIterator, err := storage.SlabIterator() require.NoError(t, err) @@ -1964,7 +1964,7 @@ func TestArrayStoredValue(t *testing.T) { for { id, slab := slabIterator() - if id == StorageIDUndefined { + if id == SlabIDUndefined { break } @@ -2102,7 +2102,7 @@ func TestArrayFromBatchData(t *testing.T) { return iter.Next() }) require.NoError(t, err) - require.NotEqual(t, copied.StorageID(), array.StorageID()) + require.NotEqual(t, copied.SlabID(), array.SlabID()) verifyEmptyArray(t, storage, typeInfo, address, copied) }) @@ -2143,7 +2143,7 @@ func TestArrayFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, copied.StorageID(), array.StorageID()) + require.NotEqual(t, copied.SlabID(), array.SlabID()) verifyArray(t, storage, typeInfo, address, copied, values, false) }) @@ -2186,7 +2186,7 @@ func TestArrayFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, array.StorageID(), copied.StorageID()) + require.NotEqual(t, array.SlabID(), copied.SlabID()) verifyArray(t, storage, typeInfo, address, copied, values, false) }) @@ -2236,7 +2236,7 @@ func TestArrayFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, array.StorageID(), copied.StorageID()) + require.NotEqual(t, array.SlabID(), copied.SlabID()) verifyArray(t, storage, typeInfo, address, copied, values, false) }) @@ -2286,7 +2286,7 @@ func TestArrayFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, array.StorageID(), copied.StorageID()) + require.NotEqual(t, array.SlabID(), copied.SlabID()) verifyArray(t, storage, typeInfo, address, copied, values, false) }) @@ -2333,7 +2333,7 @@ func TestArrayFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, array.StorageID(), copied.StorageID()) + require.NotEqual(t, array.SlabID(), copied.SlabID()) verifyArray(t, storage, typeInfo, address, copied, values, false) }) @@ -2387,7 +2387,7 @@ func TestArrayFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, array.StorageID(), copied.StorageID()) + require.NotEqual(t, array.SlabID(), copied.SlabID()) verifyArray(t, storage, typeInfo, address, copied, values, false) }) @@ -2445,9 +2445,9 @@ func TestArrayMaxInlineElement(t *testing.T) { require.True(t, array.root.IsData()) // Size of root data slab with two elements of max inlined size is target slab size minus - // storage id size (next storage id is omitted in root slab), and minus 1 byte + // slab id size (next slab id is omitted in root slab), and minus 1 byte // (for rounding when computing max inline array element size). - require.Equal(t, targetThreshold-storageIDSize-1, uint64(array.root.Header().size)) + require.Equal(t, targetThreshold-slabIDSize-1, uint64(array.root.Header().size)) verifyArray(t, storage, typeInfo, address, array, values, false) } @@ -2562,8 +2562,8 @@ func TestArraySlabDump(t *testing.T) { require.NoError(t, err) want := []string{ - "level 1, ArrayDataSlab id:0x102030405060708.1 size:24 count:1 elements: [StorageIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]})]", - "overflow: &{0x102030405060708.2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}", + "level 1, ArrayDataSlab id:0x102030405060708.1 size:24 count:1 elements: [SlabIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]})]", + "StorableSlab id:0x102030405060708.2 storable:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", } dumps, err := DumpArraySlabs(array) @@ -2598,9 +2598,9 @@ func TestArrayID(t *testing.T) { array, err := NewArray(storage, address, typeInfo) require.NoError(t, err) - sid := array.StorageID() - id := array.ID() + sid := array.SlabID() + id := array.ValueID() - require.Equal(t, sid.Address[:], id[:8]) - require.Equal(t, sid.Index[:], id[8:]) + require.Equal(t, sid.address[:], id[:8]) + require.Equal(t, sid.index[:], id[8:]) } diff --git a/basicarray.go b/basicarray.go index 5aed95a8..7e806847 100644 --- a/basicarray.go +++ b/basicarray.go @@ -50,7 +50,7 @@ func (a *BasicArray) Storable(_ SlabStorage, _ Address, _ uint64) (Storable, err } func newBasicArrayDataSlabFromData( - id StorageID, + id SlabID, data []byte, decMode cbor.DecMode, decodeStorable StorableDecoder, @@ -80,7 +80,7 @@ func newBasicArrayDataSlabFromData( elements := make([]Storable, elemCount) for i := 0; i < int(elemCount); i++ { - storable, err := decodeStorable(cborDec, StorageIDUndefined) + storable, err := decodeStorable(cborDec, SlabIDUndefined) if err != nil { // Wrap err as external error (if needed) because err is returned by StorableDecoder callback. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode array element") @@ -89,7 +89,7 @@ func newBasicArrayDataSlabFromData( } return &BasicArrayDataSlab{ - header: ArraySlabHeader{id: id, size: uint32(len(data)), count: uint32(elemCount)}, + header: ArraySlabHeader{slabID: id, size: uint32(len(data)), count: uint32(elemCount)}, elements: elements, }, nil } @@ -155,10 +155,10 @@ func (a *BasicArrayDataSlab) Set(storage SlabStorage, index uint64, v Storable) oldElem.ByteSize() + v.ByteSize() - err := storage.Store(a.header.id, a) + err := storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil @@ -180,10 +180,10 @@ func (a *BasicArrayDataSlab) Insert(storage SlabStorage, index uint64, v Storabl a.header.count++ a.header.size += v.ByteSize() - err := storage.Store(a.header.id, a) + err := storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return nil @@ -209,10 +209,10 @@ func (a *BasicArrayDataSlab) Remove(storage SlabStorage, index uint64) (Storable a.header.count-- a.header.size -= v.ByteSize() - err := storage.Store(a.header.id, a) + err := storage.Store(a.header.slabID, a) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", a.header.slabID)) } return v, nil @@ -230,8 +230,8 @@ func (a *BasicArrayDataSlab) ByteSize() uint32 { return a.header.size } -func (a *BasicArrayDataSlab) ID() StorageID { - return a.header.id +func (a *BasicArrayDataSlab) SlabID() SlabID { + return a.header.slabID } func (a *BasicArrayDataSlab) String() string { @@ -255,16 +255,16 @@ func (a *BasicArrayDataSlab) BorrowFromRight(_ Slab) error { } func NewBasicArray(storage SlabStorage, address Address) (*BasicArray, error) { - sID, err := storage.GenerateStorageID(address) + sID, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } root := &BasicArrayDataSlab{ header: ArraySlabHeader{ - id: sID, - size: basicArrayDataSlabPrefixSize, + slabID: sID, + size: basicArrayDataSlabPrefixSize, }, } @@ -274,17 +274,17 @@ func NewBasicArray(storage SlabStorage, address Address) (*BasicArray, error) { }, nil } -func (a *BasicArray) StorageID() StorageID { - return a.root.ID() +func (a *BasicArray) SlabID() SlabID { + return a.root.SlabID() } func (a *BasicArray) Address() Address { - return a.StorageID().Address + return a.SlabID().address } -func NewBasicArrayWithRootID(storage SlabStorage, id StorageID) (*BasicArray, error) { - if id == StorageIDUndefined { - return nil, NewStorageIDErrorf("cannot create BasicArray from undefined storage id") +func NewBasicArrayWithRootID(storage SlabStorage, id SlabID) (*BasicArray, error) { + if id == SlabIDUndefined { + return nil, NewSlabIDErrorf("cannot create BasicArray from undefined slab ID") } slab, found, err := storage.Retrieve(id) if err != nil { diff --git a/basicarray_benchmark_test.go b/basicarray_benchmark_test.go index 47126467..e6ee564e 100644 --- a/basicarray_benchmark_test.go +++ b/basicarray_benchmark_test.go @@ -88,7 +88,7 @@ func benchmarkBasicArray(b *testing.B, initialArraySize, numberOfElements int) { require.NoError(b, storage.Commit()) b.ResetTimer() - arrayID := array.StorageID() + arrayID := array.SlabID() // append storage.DropCache() diff --git a/basicarray_test.go b/basicarray_test.go index de305d51..7c9dfdc4 100644 --- a/basicarray_test.go +++ b/basicarray_test.go @@ -427,7 +427,7 @@ func TestBasicArrayDecodeEncodeRandomData(t *testing.T) { require.NoError(t, err) } - rootID := array.root.Header().id + rootID := array.root.Header().slabID // Encode slabs with random data of mixed types m1, err := storage.Encode() diff --git a/cmd/main/main.go b/cmd/main/main.go index 66d60e5b..3e0cf470 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -86,15 +86,15 @@ func (i testTypeInfo) Equal(other atree.TypeInfo) bool { return ok } -func decodeStorable(dec *cbor.StreamDecoder, _ atree.StorageID) (atree.Storable, error) { +func decodeStorable(dec *cbor.StreamDecoder, _ atree.SlabID) (atree.Storable, error) { tagNumber, err := dec.DecodeTagNumber() if err != nil { return nil, err } switch tagNumber { - case atree.CBORTagStorageID: - return atree.DecodeStorageIDStorable(dec) + case atree.CBORTagSlabID: + return atree.DecodeSlabIDStorable(dec) case cborTagUInt64Value: n, err := dec.DecodeUint64() diff --git a/cmd/stress/array.go b/cmd/stress/array.go index 68d7f59d..699bf60a 100644 --- a/cmd/stress/array.go +++ b/cmd/stress/array.go @@ -163,7 +163,7 @@ func testArray( storage.DropCache() // Load root slab from storage and cache it in read cache - rootID := array.StorageID() + rootID := array.SlabID() array, err = atree.NewArrayWithRootID(storage, rootID) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create array from root id %s: %s", rootID, err) @@ -393,15 +393,15 @@ func testArray( fmt.Fprintln(os.Stderr, err) return } - ids := make([]atree.StorageID, 0, len(rootIDs)) + ids := make([]atree.SlabID, 0, len(rootIDs)) for id := range rootIDs { // filter out root ids with empty address - if id.Address != atree.AddressUndefined { + if !id.HasTempAddress() { ids = append(ids, id) } } - if len(ids) != 1 || ids[0] != array.StorageID() { - fmt.Fprintf(os.Stderr, "root storage ids %v in storage, want %s\n", ids, array.StorageID()) + if len(ids) != 1 || ids[0] != array.SlabID() { + fmt.Fprintf(os.Stderr, "root slab ids %v in storage, want %s\n", ids, array.SlabID()) return } } diff --git a/cmd/stress/map.go b/cmd/stress/map.go index f552cea2..13f222c2 100644 --- a/cmd/stress/map.go +++ b/cmd/stress/map.go @@ -151,7 +151,7 @@ func testMap( elements = make(map[atree.Value]atree.Value, maxLength) // Load root slab from storage and cache it in read cache - rootID := m.StorageID() + rootID := m.SlabID() m, err = atree.NewMapWithRootID(storage, rootID, atree.NewDefaultDigesterBuilder()) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create map from root id %s: %s", rootID, err) @@ -366,15 +366,15 @@ func testMap( fmt.Fprintln(os.Stderr, err) return } - ids := make([]atree.StorageID, 0, len(rootIDs)) + ids := make([]atree.SlabID, 0, len(rootIDs)) for id := range rootIDs { // filter out root ids with empty address - if id.Address != atree.AddressUndefined { + if !id.HasTempAddress() { ids = append(ids, id) } } - if len(ids) != 1 || ids[0] != m.StorageID() { - fmt.Fprintf(os.Stderr, "root storage ids %v in storage, want %s\n", ids, m.StorageID()) + if len(ids) != 1 || ids[0] != m.SlabID() { + fmt.Fprintf(os.Stderr, "root slab ids %v in storage, want %s\n", ids, m.SlabID()) return } } diff --git a/cmd/stress/storable.go b/cmd/stress/storable.go index 29ad7df7..b3fba90a 100644 --- a/cmd/stress/storable.go +++ b/cmd/stress/storable.go @@ -348,30 +348,11 @@ func (v StringValue) StoredValue(_ atree.SlabStorage) (atree.Value, error) { } func (v StringValue) Storable(storage atree.SlabStorage, address atree.Address, maxInlineSize uint64) (atree.Storable, error) { - if uint64(v.ByteSize()) > maxInlineSize { - - // Create StorableSlab - id, err := storage.GenerateStorageID(address) - if err != nil { - return nil, err - } - - slab := &atree.StorableSlab{ - StorageID: id, - Storable: v, - } - - // Store StorableSlab in storage - err = storage.Store(id, slab) - if err != nil { - return nil, err - } - - // Return storage id as storable - return atree.StorageIDStorable(id), nil + if uint64(v.ByteSize()) <= maxInlineSize { + return v, nil } - return v, nil + return atree.NewStorableSlab(storage, address, v) } func (v StringValue) Encode(enc *atree.Encoder) error { @@ -432,7 +413,7 @@ func (v StringValue) String() string { return v.str } -func decodeStorable(dec *cbor.StreamDecoder, _ atree.StorageID) (atree.Storable, error) { +func decodeStorable(dec *cbor.StreamDecoder, _ atree.SlabID) (atree.Storable, error) { t, err := dec.NextType() if err != nil { return nil, err @@ -454,8 +435,8 @@ func decodeStorable(dec *cbor.StreamDecoder, _ atree.StorageID) (atree.Storable, switch tagNumber { - case atree.CBORTagStorageID: - return atree.DecodeStorageIDStorable(dec) + case atree.CBORTagSlabID: + return atree.DecodeSlabIDStorable(dec) case cborTagUInt8Value: n, err := dec.DecodeUint64() diff --git a/cmd/stress/utils.go b/cmd/stress/utils.go index 6aa9621a..c75296fe 100644 --- a/cmd/stress/utils.go +++ b/cmd/stress/utils.go @@ -184,20 +184,20 @@ func copyMap(storage *atree.PersistentSlabStorage, address atree.Address, m *atr func removeValue(storage *atree.PersistentSlabStorage, value atree.Value) error { switch v := value.(type) { case *atree.Array: - return removeStorable(storage, atree.StorageIDStorable(v.StorageID())) + return removeStorable(storage, atree.SlabIDStorable(v.SlabID())) case *atree.OrderedMap: - return removeStorable(storage, atree.StorageIDStorable(v.StorageID())) + return removeStorable(storage, atree.SlabIDStorable(v.SlabID())) } return nil } func removeStorable(storage *atree.PersistentSlabStorage, storable atree.Storable) error { - sid, ok := storable.(atree.StorageIDStorable) + sid, ok := storable.(atree.SlabIDStorable) if !ok { return nil } - id := atree.StorageID(sid) + id := atree.SlabID(sid) value, err := storable.StoredValue(storage) if err != nil { @@ -458,8 +458,8 @@ func newMap(storage *atree.PersistentSlabStorage, address atree.Address, length } type InMemBaseStorage struct { - segments map[atree.StorageID][]byte - storageIndex map[atree.Address]atree.StorageIndex + segments map[atree.SlabID][]byte + storageIndex map[atree.Address]atree.SlabIndex bytesRetrieved int bytesStored int } @@ -468,40 +468,40 @@ var _ atree.BaseStorage = &InMemBaseStorage{} func NewInMemBaseStorage() *InMemBaseStorage { return NewInMemBaseStorageFromMap( - make(map[atree.StorageID][]byte), + make(map[atree.SlabID][]byte), ) } -func NewInMemBaseStorageFromMap(segments map[atree.StorageID][]byte) *InMemBaseStorage { +func NewInMemBaseStorageFromMap(segments map[atree.SlabID][]byte) *InMemBaseStorage { return &InMemBaseStorage{ segments: segments, - storageIndex: make(map[atree.Address]atree.StorageIndex), + storageIndex: make(map[atree.Address]atree.SlabIndex), } } -func (s *InMemBaseStorage) Retrieve(id atree.StorageID) ([]byte, bool, error) { +func (s *InMemBaseStorage) Retrieve(id atree.SlabID) ([]byte, bool, error) { seg, ok := s.segments[id] s.bytesRetrieved += len(seg) return seg, ok, nil } -func (s *InMemBaseStorage) Store(id atree.StorageID, data []byte) error { +func (s *InMemBaseStorage) Store(id atree.SlabID, data []byte) error { s.segments[id] = data s.bytesStored += len(data) return nil } -func (s *InMemBaseStorage) Remove(id atree.StorageID) error { +func (s *InMemBaseStorage) Remove(id atree.SlabID) error { delete(s.segments, id) return nil } -func (s *InMemBaseStorage) GenerateStorageID(address atree.Address) (atree.StorageID, error) { +func (s *InMemBaseStorage) GenerateSlabID(address atree.Address) (atree.SlabID, error) { index := s.storageIndex[address] nextIndex := index.Next() s.storageIndex[address] = nextIndex - return atree.NewStorageID(address, nextIndex), nil + return atree.NewSlabID(address, nextIndex), nil } func (s *InMemBaseStorage) SegmentCounts() int { diff --git a/encode.go b/encode.go index be5cb49b..fb84d49a 100644 --- a/encode.go +++ b/encode.go @@ -42,14 +42,14 @@ func NewEncoder(w io.Writer, encMode cbor.EncMode) *Encoder { type StorableDecoder func( decoder *cbor.StreamDecoder, - storableSlabStorageID StorageID, + storableSlabID SlabID, ) ( Storable, error, ) func DecodeSlab( - id StorageID, + id SlabID, data []byte, decMode cbor.DecMode, decodeStorable StorableDecoder, @@ -100,9 +100,9 @@ func DecodeSlab( // Wrap err as external error (if needed) because err is returned by StorableDecoder callback. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode slab storable") } - return StorableSlab{ - StorageID: id, - Storable: storable, + return &StorableSlab{ + slabID: id, + storable: storable, }, nil default: diff --git a/errors.go b/errors.go index 7ab6088f..80c51359 100644 --- a/errors.go +++ b/errors.go @@ -145,11 +145,11 @@ func (e *IndexOutOfBoundsError) Error() string { // NotValueError is returned when we try to create Value objects from non-root slabs. type NotValueError struct { - id StorageID + id SlabID } // NewNotValueError constructs a NotValueError. -func NewNotValueError(id StorageID) error { +func NewNotValueError(id SlabID) error { return NewFatalError(&NotValueError{id: id}) } @@ -210,43 +210,43 @@ func (e *HashError) Error() string { return fmt.Sprintf("hasher error: %s", e.err.Error()) } -// StorageIDError is returned when storage id can't be created or it's invalid. -type StorageIDError struct { +// SlabIDError is returned when slab id can't be created or it's invalid. +type SlabIDError struct { msg string } -// NewStorageIDError constructs a fatal error of StorageIDError. -func NewStorageIDError(msg string) error { - return NewFatalError(&StorageIDError{msg: msg}) +// NewSlabIDError constructs a fatal error of SlabIDError. +func NewSlabIDError(msg string) error { + return NewFatalError(&SlabIDError{msg: msg}) } -// NewStorageIDErrorf constructs a fatal error of StorageIDError. -func NewStorageIDErrorf(msg string, args ...interface{}) error { - return NewStorageIDError(fmt.Sprintf(msg, args...)) +// NewSlabIDErrorf constructs a fatal error of SlabIDError. +func NewSlabIDErrorf(msg string, args ...interface{}) error { + return NewSlabIDError(fmt.Sprintf(msg, args...)) } -func (e *StorageIDError) Error() string { - return fmt.Sprintf("storage id error: %s", e.msg) +func (e *SlabIDError) Error() string { + return fmt.Sprintf("slab id error: %s", e.msg) } // SlabNotFoundError is always a fatal error returned when an slab is not found type SlabNotFoundError struct { - storageID StorageID - err error + slabID SlabID + err error } // NewSlabNotFoundError constructs a SlabNotFoundError -func NewSlabNotFoundError(storageID StorageID, err error) error { - return NewFatalError(&SlabNotFoundError{storageID: storageID, err: err}) +func NewSlabNotFoundError(slabID SlabID, err error) error { + return NewFatalError(&SlabNotFoundError{slabID: slabID, err: err}) } // NewSlabNotFoundErrorf constructs a new SlabNotFoundError with error formating -func NewSlabNotFoundErrorf(storageID StorageID, msg string, args ...interface{}) error { - return NewSlabNotFoundError(storageID, fmt.Errorf(msg, args...)) +func NewSlabNotFoundErrorf(slabID SlabID, msg string, args ...interface{}) error { + return NewSlabNotFoundError(slabID, fmt.Errorf(msg, args...)) } func (e *SlabNotFoundError) Error() string { - return fmt.Sprintf("slab (%s) not found: %s", e.storageID.String(), e.err.Error()) + return fmt.Sprintf("slab (%s) not found: %s", e.slabID.String(), e.err.Error()) } // SlabSplitError is always a fatal error returned when splitting an slab has failed diff --git a/map.go b/map.go index c601013e..26d72b59 100644 --- a/map.go +++ b/map.go @@ -49,14 +49,14 @@ const ( // CBOR array header (1 byte) + encoded level (1 byte) + hkeys byte string header (1 bytes) + elements array header (9 bytes) singleElementsPrefixSize = 1 + 1 + 1 + 9 - // slab header size: storage id (16 bytes) + size (4 bytes) + first digest (8 bytes) - mapSlabHeaderSize = storageIDSize + 4 + digestSize + // slab header size: slab ID (16 bytes) + size (4 bytes) + first digest (8 bytes) + mapSlabHeaderSize = slabIDSize + 4 + digestSize // meta data slab prefix size: version (1 byte) + flag (1 byte) + child header count (2 bytes) mapMetaDataSlabPrefixSize = 1 + 1 + 2 // version (1 byte) + flag (1 byte) + next id (16 bytes) - mapDataSlabPrefixSize = 2 + storageIDSize + mapDataSlabPrefixSize = 2 + slabIDSize // version (1 byte) + flag (1 byte) mapRootDataSlabPrefixSize = 2 @@ -195,8 +195,8 @@ var _ element = &inlineCollisionGroup{} var _ elementGroup = &inlineCollisionGroup{} type externalCollisionGroup struct { - id StorageID - size uint32 + slabID SlabID + size uint32 } var _ element = &externalCollisionGroup{} @@ -220,9 +220,9 @@ type singleElements struct { var _ elements = &singleElements{} type MapSlabHeader struct { - id StorageID // id is used to retrieve slab from storage - size uint32 // size is used to split and merge; leaf: size of all element; internal: size of all headers - firstKey Digest // firstKey (first hashed key) is used to lookup value + slabID SlabID // id is used to retrieve slab from storage + size uint32 // size is used to split and merge; leaf: size of all element; internal: size of all headers + firstKey Digest // firstKey (first hashed key) is used to lookup value } type MapExtraData struct { @@ -234,7 +234,7 @@ type MapExtraData struct { // MapDataSlab is leaf node, implementing MapSlab. // anySize is true for data slab that isn't restricted by size requirement. type MapDataSlab struct { - next StorageID + next SlabID header MapSlabHeader elements @@ -263,7 +263,6 @@ var _ MapSlab = &MapMetaDataSlab{} type MapSlab interface { Slab - fmt.Stringer Get(storage SlabStorage, digester Digester, level uint, hkey Digest, comparator ValueComparator, key Value) (MapValue, error) Set(storage SlabStorage, b DigesterBuilder, digester Digester, level uint, hkey Digest, comparator ValueComparator, hip HashInputProvider, key Value, value Value) (existingValue MapValue, err error) @@ -276,7 +275,7 @@ type MapSlab interface { CanLendToLeft(size uint32) bool CanLendToRight(size uint32) bool - SetID(StorageID) + SetSlabID(SlabID) Header() MapSlabHeader @@ -466,12 +465,12 @@ func newSingleElement(storage SlabStorage, address Address, key Value, value Val } var keyPointer bool - if _, ok := ks.(StorageIDStorable); ok { + if _, ok := ks.(SlabIDStorable); ok { keyPointer = true } var valuePointer bool - if _, ok := vs.(StorageIDStorable); ok { + if _, ok := vs.(SlabIDStorable); ok { valuePointer = true } @@ -494,25 +493,25 @@ func newSingleElementFromData(cborDec *cbor.StreamDecoder, decodeStorable Storab return nil, NewDecodingError(fmt.Errorf("failed to decode single element: expect array of 2 elements, got %d elements", elemCount)) } - key, err := decodeStorable(cborDec, StorageIDUndefined) + key, err := decodeStorable(cborDec, SlabIDUndefined) if err != nil { // Wrap err as external error (if needed) because err is returned by StorableDecoder callback. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode key's storable") } - value, err := decodeStorable(cborDec, StorageIDUndefined) + value, err := decodeStorable(cborDec, SlabIDUndefined) if err != nil { // Wrap err as external error (if needed) because err is returned by StorableDecoder callback. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode value's storable") } var keyPointer bool - if _, ok := key.(StorageIDStorable); ok { + if _, ok := key.(SlabIDStorable); ok { keyPointer = true } var valuePointer bool - if _, ok := value.(StorageIDStorable); ok { + if _, ok := value.(SlabIDStorable); ok { valuePointer = true } @@ -605,7 +604,7 @@ func (e *singleElement) Set( } valuePointer := false - if _, ok := valueStorable.(StorageIDStorable); ok { + if _, ok := valueStorable.(SlabIDStorable); ok { valuePointer = true } @@ -781,18 +780,18 @@ func (e *inlineCollisionGroup) Set( // for first level collision. if e.Size() > uint32(maxInlineMapElementSize) { - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(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 storage ID for address 0x%x", address)) + fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } // Create MapDataSlab slab := &MapDataSlab{ header: MapSlabHeader{ - id: id, + slabID: id, size: mapDataSlabPrefixSize + e.elements.Size(), firstKey: e.elements.firstKey(), }, @@ -809,8 +808,8 @@ func (e *inlineCollisionGroup) Set( // Create and return externalCollisionGroup (wrapper of newly created MapDataSlab) return &externalCollisionGroup{ - id: id, - size: externalCollisionGroupPrefixSize + StorageIDStorable(id).ByteSize(), + slabID: id, + size: externalCollisionGroupPrefixSize + SlabIDStorable(id).ByteSize(), }, existingValue, nil } } @@ -881,26 +880,26 @@ func (e *inlineCollisionGroup) String() string { func newExternalCollisionGroupFromData(cborDec *cbor.StreamDecoder, decodeStorable StorableDecoder) (*externalCollisionGroup, error) { - storable, err := decodeStorable(cborDec, StorageIDUndefined) + storable, err := decodeStorable(cborDec, SlabIDUndefined) if err != nil { // Wrap err as external error (if needed) because err is returned by StorableDecoder callback. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode Storable") } - idStorable, ok := storable.(StorageIDStorable) + idStorable, ok := storable.(SlabIDStorable) if !ok { - return nil, NewDecodingError(fmt.Errorf("failed to decode external collision group: expect storage id, got %T", storable)) + return nil, NewDecodingError(fmt.Errorf("failed to decode external collision group: expect slab ID, got %T", storable)) } return &externalCollisionGroup{ - id: StorageID(idStorable), - size: externalCollisionGroupPrefixSize + idStorable.ByteSize(), + slabID: SlabID(idStorable), + size: externalCollisionGroupPrefixSize + idStorable.ByteSize(), }, nil } // Encode encodes externalCollisionGroup to the given encoder. // -// CBOR tag (number: CBORTagExternalCollisionGroup, content: storage ID) +// CBOR tag (number: CBORTagExternalCollisionGroup, content: slab ID) func (e *externalCollisionGroup) Encode(enc *Encoder) error { err := enc.CBOR.EncodeRawBytes([]byte{ // tag number CBORTagExternalCollisionGroup @@ -910,9 +909,9 @@ func (e *externalCollisionGroup) Encode(enc *Encoder) error { return NewEncodingError(err) } - err = StorageIDStorable(e.id).Encode(enc) + err = SlabIDStorable(e.slabID).Encode(enc) if err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageIDStorable.Encode(). + // Don't need to wrap error as external error because err is already categorized by SlabIDStorable.Encode(). return err } @@ -926,7 +925,7 @@ func (e *externalCollisionGroup) Encode(enc *Encoder) error { } func (e *externalCollisionGroup) Get(storage SlabStorage, digester Digester, level uint, _ Digest, comparator ValueComparator, key Value) (MapValue, error) { - slab, err := getMapSlab(storage, e.id) + slab, err := getMapSlab(storage, e.slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by getMapSlab(). return nil, err @@ -945,7 +944,7 @@ func (e *externalCollisionGroup) Get(storage SlabStorage, digester Digester, lev } func (e *externalCollisionGroup) Set(storage SlabStorage, _ Address, b DigesterBuilder, digester Digester, level uint, _ Digest, comparator ValueComparator, hip HashInputProvider, key Value, value Value) (element, MapValue, error) { - slab, err := getMapSlab(storage, e.id) + slab, err := getMapSlab(storage, e.slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by getMapSlab(). return nil, nil, err @@ -971,18 +970,18 @@ func (e *externalCollisionGroup) Set(storage SlabStorage, _ Address, b DigesterB // TODO: updated element can be inlineCollisionGroup if size < maxInlineMapElementSize. func (e *externalCollisionGroup) Remove(storage SlabStorage, digester Digester, level uint, _ Digest, comparator ValueComparator, key Value) (MapKey, MapValue, element, error) { - slab, found, err := storage.Retrieve(e.id) + slab, found, err := storage.Retrieve(e.slabID) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to retrieve slab %s", e.id)) + return nil, nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to retrieve slab %s", e.slabID)) } if !found { - return nil, nil, nil, NewSlabNotFoundErrorf(e.id, "external collision slab not found") + return nil, nil, nil, NewSlabNotFoundErrorf(e.slabID, "external collision slab not found") } dataSlab, ok := slab.(*MapDataSlab) if !ok { - return nil, nil, nil, NewSlabDataErrorf("slab %s isn't MapDataSlab", e.id) + return nil, nil, nil, NewSlabDataErrorf("slab %s isn't MapDataSlab", e.slabID) } // Adjust level and hkey for collision group @@ -1008,10 +1007,10 @@ func (e *externalCollisionGroup) Remove(storage SlabStorage, digester Digester, return nil, nil, nil, err } if _, ok := elem.(elementGroup); !ok { - err := storage.Remove(e.id) + err := storage.Remove(e.slabID) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", e.id)) + return nil, nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", e.slabID)) } return k, v, elem, nil } @@ -1033,14 +1032,14 @@ func (e *externalCollisionGroup) Inline() bool { } func (e *externalCollisionGroup) Elements(storage SlabStorage) (elements, error) { - slab, err := getMapSlab(storage, e.id) + slab, err := getMapSlab(storage, e.slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by getMapSlab(). return nil, err } dataSlab, ok := slab.(*MapDataSlab) if !ok { - return nil, NewSlabDataErrorf("slab %s isn't MapDataSlab", e.id) + return nil, NewSlabDataErrorf("slab %s isn't MapDataSlab", e.slabID) } return dataSlab.elements, nil } @@ -1067,16 +1066,16 @@ func (e *externalCollisionGroup) PopIterate(storage SlabStorage, fn MapPopIterat return err } - err = storage.Remove(e.id) + err = storage.Remove(e.slabID) 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", e.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", e.slabID)) } return nil } func (e *externalCollisionGroup) String() string { - return fmt.Sprintf("external(%s)", e.id) + return fmt.Sprintf("external(%s)", e.slabID) } func newElementsFromData(cborDec *cbor.StreamDecoder, decodeStorable StorableDecoder) (elements, error) { @@ -2068,7 +2067,7 @@ func (e *singleElements) String() string { } func newMapDataSlabFromData( - id StorageID, + id SlabID, data []byte, decMode cbor.DecMode, decodeStorable StorableDecoder, @@ -2121,22 +2120,22 @@ func newMapDataSlabFromData( ) } - var next StorageID + var next SlabID var contentOffset int if !isRootSlab { - // Decode next storage ID - const nextStorageIDOffset = versionAndFlagSize + // Decode next slab ID + const nextSlabIDOffset = versionAndFlagSize var err error - next, err = NewStorageIDFromRawBytes(data[nextStorageIDOffset:]) + next, err = NewSlabIDFromRawBytes(data[nextSlabIDOffset:]) if err != nil { - // Don't need to wrap error as external error because err is already categorized by NewStorageIDFromRawBytes(). + // Don't need to wrap error as external error because err is already categorized by NewSlabIDFromRawBytes(). return nil, err } - contentOffset = nextStorageIDOffset + storageIDSize + contentOffset = nextSlabIDOffset + slabIDSize } else { contentOffset = versionAndFlagSize @@ -2151,7 +2150,7 @@ func newMapDataSlabFromData( } header := MapSlabHeader{ - id: id, + slabID: id, size: uint32(len(data)), firstKey: elements.firstKey(), } @@ -2170,9 +2169,9 @@ func newMapDataSlabFromData( // // Header (18 bytes): // -// +-------------------------------+--------------------------------+ -// | slab version + flag (2 bytes) | next sib storage ID (16 bytes) | -// +-------------------------------+--------------------------------+ +// +-------------------------------+-----------------------------+ +// | slab version + flag (2 bytes) | next sib slab ID (16 bytes) | +// +-------------------------------+-----------------------------+ // // Content (for now): // @@ -2219,15 +2218,15 @@ func (m *MapDataSlab) Encode(enc *Encoder) error { if m.extraData == nil { - // Encode next storage ID to scratch - const nextStorageIDOffset = versionAndFlagSize - _, err := m.next.ToRawBytes(enc.Scratch[nextStorageIDOffset:]) + // Encode next slab ID to scratch + const nextSlabIDOffset = versionAndFlagSize + _, err := m.next.ToRawBytes(enc.Scratch[nextSlabIDOffset:]) if err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageID.ToRawBytes(). + // Don't need to wrap error as external error because err is already categorized by SlabID.ToRawBytes(). return err } - totalSize = nextStorageIDOffset + storageIDSize + totalSize = nextSlabIDOffset + slabIDSize } else { @@ -2287,7 +2286,7 @@ func elementStorables(e element, childStorables []Storable) []Storable { switch v := e.(type) { case *externalCollisionGroup: - return append(childStorables, StorageIDStorable(v.id)) + return append(childStorables, SlabIDStorable(v.slabID)) case *inlineCollisionGroup: return elementsStorables(v.elements, childStorables) @@ -2301,7 +2300,7 @@ func elementStorables(e element, childStorables []Storable) []Storable { func (m *MapDataSlab) StoredValue(storage SlabStorage) (Value, error) { if m.extraData == nil { - return nil, NewNotValueError(m.ID()) + return nil, NewNotValueError(m.SlabID()) } digestBuilder := NewDefaultDigesterBuilder() @@ -2317,7 +2316,7 @@ func (m *MapDataSlab) StoredValue(storage SlabStorage) (Value, error) { func (m *MapDataSlab) Set(storage SlabStorage, b DigesterBuilder, digester Digester, level uint, hkey Digest, comparator ValueComparator, hip HashInputProvider, key Value, value Value) (MapValue, error) { - existingValue, err := m.elements.Set(storage, m.ID().Address, b, digester, level, hkey, comparator, hip, key, value) + existingValue, err := m.elements.Set(storage, m.SlabID().address, b, digester, level, hkey, comparator, hip, key, value) if err != nil { // Don't need to wrap error as external error because err is already categorized by elements.Set(). return nil, err @@ -2334,10 +2333,10 @@ func (m *MapDataSlab) Set(storage SlabStorage, b DigesterBuilder, digester Diges } // Store modified slab - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return existingValue, nil @@ -2362,10 +2361,10 @@ func (m *MapDataSlab) Remove(storage SlabStorage, digester Digester, level uint, } // Store modified slab - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) 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 store slab %s", m.header.id)) + return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return k, v, nil @@ -2374,7 +2373,7 @@ func (m *MapDataSlab) Remove(storage SlabStorage, digester Digester, level uint, func (m *MapDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { if m.elements.Count() < 2 { // Can't split slab with less than two elements - return nil, nil, NewSlabSplitErrorf("MapDataSlab (%s) has less than 2 elements", m.header.id) + return nil, nil, NewSlabSplitErrorf("MapDataSlab (%s) has less than 2 elements", m.header.slabID) } leftElements, rightElements, err := m.elements.Split() @@ -2383,16 +2382,16 @@ func (m *MapDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { return nil, nil, err } - sID, err := storage.GenerateStorageID(m.ID().Address) + sID, err := storage.GenerateSlabID(m.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 storage ID for address 0x%x", m.ID().Address)) + return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.SlabID().address)) } // Create new right slab rightSlab := &MapDataSlab{ header: MapSlabHeader{ - id: sID, + slabID: sID, size: mapDataSlabPrefixSize + rightElements.Size(), firstKey: rightElements.firstKey(), }, @@ -2403,7 +2402,7 @@ func (m *MapDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { // Modify left (original) slab m.header.size = mapDataSlabPrefixSize + leftElements.Size() - m.next = rightSlab.header.id + m.next = rightSlab.header.slabID m.elements = leftElements return m, rightSlab, nil @@ -2517,8 +2516,8 @@ func (m *MapDataSlab) CanLendToRight(size uint32) bool { return m.elements.CanLendToRight(size) } -func (m *MapDataSlab) SetID(id StorageID) { - m.header.id = id +func (m *MapDataSlab) SetSlabID(id SlabID) { + m.header.slabID = id } func (m *MapDataSlab) Header() MapSlabHeader { @@ -2529,8 +2528,8 @@ func (m *MapDataSlab) IsData() bool { return true } -func (m *MapDataSlab) ID() StorageID { - return m.header.id +func (m *MapDataSlab) SlabID() SlabID { + return m.header.slabID } func (m *MapDataSlab) ByteSize() uint32 { @@ -2566,7 +2565,7 @@ func (m *MapDataSlab) PopIterate(storage SlabStorage, fn MapPopIterationFunc) er func (m *MapDataSlab) String() string { return fmt.Sprintf("MapDataSlab id:%s size:%d firstkey:%d elements: [%s]", - m.header.id, + m.header.slabID, m.header.size, m.header.firstKey, m.elements.String(), @@ -2574,7 +2573,7 @@ func (m *MapDataSlab) String() string { } func newMapMetaDataSlabFromData( - id StorageID, + id SlabID, data []byte, decMode cbor.DecMode, decodeTypeInfo TypeInfoDecoder, @@ -2630,20 +2629,20 @@ func newMapMetaDataSlabFromData( offset := childHeaderCountOffset + 2 for i := 0; i < int(childHeaderCount); i++ { - storageID, err := NewStorageIDFromRawBytes(data[offset:]) + slabID, err := NewSlabIDFromRawBytes(data[offset:]) if err != nil { - // Don't need to wrap error as external error because err is already categorized by NewStorageIDFromRawBytes(). + // Don't need to wrap error as external error because err is already categorized by NewSlabIDFromRawBytes(). return nil, err } - firstKeyOffset := offset + storageIDSize + firstKeyOffset := offset + slabIDSize firstKey := binary.BigEndian.Uint64(data[firstKeyOffset:]) sizeOffset := firstKeyOffset + digestSize size := binary.BigEndian.Uint32(data[sizeOffset:]) childrenHeaders[i] = MapSlabHeader{ - id: storageID, + slabID: slabID, size: size, firstKey: Digest(firstKey), } @@ -2657,7 +2656,7 @@ func newMapMetaDataSlabFromData( } header := MapSlabHeader{ - id: id, + slabID: id, size: uint32(len(data)), firstKey: firstKey, } @@ -2679,7 +2678,7 @@ func newMapMetaDataSlabFromData( // // Content (n * 28 bytes): // -// [[storage id, first key, size], ...] +// [[slab ID, first key, size], ...] // // If this is root slab, extra data section is prepended to slab's encoded content. // See MapExtraData.Encode() for extra data section format. @@ -2722,13 +2721,13 @@ func (m *MapMetaDataSlab) Encode(enc *Encoder) error { // Encode children headers for _, h := range m.childrenHeaders { - _, err := h.id.ToRawBytes(enc.Scratch[:]) + _, err := h.slabID.ToRawBytes(enc.Scratch[:]) if err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageID.ToRawBytes(). + // Don't need to wrap error as external error because err is already categorized by SlabID.ToRawBytes(). return err } - const firstKeyOffset = storageIDSize + const firstKeyOffset = slabIDSize binary.BigEndian.PutUint64(enc.Scratch[firstKeyOffset:], uint64(h.firstKey)) const sizeOffset = firstKeyOffset + digestSize @@ -2746,7 +2745,7 @@ func (m *MapMetaDataSlab) Encode(enc *Encoder) error { func (m *MapMetaDataSlab) StoredValue(storage SlabStorage) (Value, error) { if m.extraData == nil { - return nil, NewNotValueError(m.ID()) + return nil, NewNotValueError(m.SlabID()) } digestBuilder := NewDefaultDigesterBuilder() @@ -2764,7 +2763,7 @@ func (m *MapMetaDataSlab) ChildStorables() []Storable { childIDs := make([]Storable, len(m.childrenHeaders)) for i, h := range m.childrenHeaders { - childIDs[i] = StorageIDStorable(h.id) + childIDs[i] = SlabIDStorable(h.slabID) } return childIDs @@ -2790,7 +2789,7 @@ func (m *MapMetaDataSlab) Get(storage SlabStorage, digester Digester, level uint childHeaderIndex := ans - childID := m.childrenHeaders[childHeaderIndex].id + childID := m.childrenHeaders[childHeaderIndex].slabID child, err := getMapSlab(storage, childID) if err != nil { @@ -2818,7 +2817,7 @@ func (m *MapMetaDataSlab) Set(storage SlabStorage, b DigesterBuilder, digester D childHeaderIndex := ans - childID := m.childrenHeaders[childHeaderIndex].id + childID := m.childrenHeaders[childHeaderIndex].slabID child, err := getMapSlab(storage, childID) if err != nil { @@ -2857,10 +2856,10 @@ func (m *MapMetaDataSlab) Set(storage SlabStorage, b DigesterBuilder, digester D return existingValue, nil } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return existingValue, nil } @@ -2885,7 +2884,7 @@ func (m *MapMetaDataSlab) Remove(storage SlabStorage, digester Digester, level u childHeaderIndex := ans - childID := m.childrenHeaders[childHeaderIndex].id + childID := m.childrenHeaders[childHeaderIndex].slabID child, err := getMapSlab(storage, childID) if err != nil { @@ -2924,10 +2923,10 @@ func (m *MapMetaDataSlab) Remove(storage SlabStorage, digester Digester, level u return k, v, nil } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) 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 store slab %s", m.header.id)) + return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return k, v, nil } @@ -2954,22 +2953,22 @@ func (m *MapMetaDataSlab) SplitChildSlab(storage SlabStorage, child MapSlab, chi m.header.size += mapSlabHeaderSize // Store modified slabs - err = storage.Store(left.ID(), left) + err = storage.Store(left.SlabID(), left) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.SlabID())) } - err = storage.Store(right.ID(), right) + err = storage.Store(right.SlabID(), right) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return nil @@ -2998,7 +2997,7 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( // Retrieve left sibling of the same parent. var leftSib MapSlab if childHeaderIndex > 0 { - leftSibID := m.childrenHeaders[childHeaderIndex-1].id + leftSibID := m.childrenHeaders[childHeaderIndex-1].slabID var err error leftSib, err = getMapSlab(storage, leftSibID) @@ -3011,7 +3010,7 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( // Retrieve right siblings of the same parent. var rightSib MapSlab if childHeaderIndex < len(m.childrenHeaders)-1 { - rightSibID := m.childrenHeaders[childHeaderIndex+1].id + rightSibID := m.childrenHeaders[childHeaderIndex+1].slabID var err error rightSib, err = getMapSlab(storage, rightSibID) @@ -3045,22 +3044,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( } // Store modified slabs - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(rightSib.ID(), rightSib) + err = storage.Store(rightSib.SlabID(), rightSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return nil } @@ -3078,22 +3077,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( m.childrenHeaders[childHeaderIndex] = child.Header() // Store modified slabs - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return nil } @@ -3111,22 +3110,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( m.childrenHeaders[childHeaderIndex] = child.Header() // Store modified slabs - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return nil } else { @@ -3147,22 +3146,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( } // Store modified slabs - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(rightSib.ID(), rightSib) + err = storage.Store(rightSib.SlabID(), rightSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", rightSib.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } return nil } @@ -3193,22 +3192,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( } // Store modified slabs in storage - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } // Remove right sib from storage - err = storage.Remove(rightSib.ID()) + err = storage.Remove(rightSib.SlabID()) 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", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", rightSib.SlabID())) } return nil } @@ -3231,22 +3230,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( m.header.size -= mapSlabHeaderSize // Store modified slabs in storage - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } // Remove child from storage - err = storage.Remove(child.ID()) + err = storage.Remove(child.SlabID()) 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", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", child.SlabID())) } return nil } @@ -3268,22 +3267,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( m.header.size -= mapSlabHeaderSize // Store modified slabs in storage - err = storage.Store(leftSib.ID(), leftSib) + err = storage.Store(leftSib.SlabID(), leftSib) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", leftSib.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } // Remove child from storage - err = storage.Remove(child.ID()) + err = storage.Remove(child.SlabID()) 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", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", child.SlabID())) } return nil } else { @@ -3309,22 +3308,22 @@ func (m *MapMetaDataSlab) MergeOrRebalanceChildSlab( } // Store modified slabs in storage - err = storage.Store(child.ID(), child) + err = storage.Store(child.SlabID(), child) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", child.SlabID())) } - err = storage.Store(m.header.id, m) + err = storage.Store(m.header.slabID, m) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.id)) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.header.slabID)) } // Remove rightSib from storage - err = storage.Remove(rightSib.ID()) + err = storage.Remove(rightSib.SlabID()) 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", rightSib.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to remove slab %s", rightSib.SlabID())) } return nil } @@ -3342,22 +3341,22 @@ func (m *MapMetaDataSlab) Merge(slab Slab) error { func (m *MapMetaDataSlab) Split(storage SlabStorage) (Slab, Slab, error) { if len(m.childrenHeaders) < 2 { // Can't split meta slab with less than 2 headers - return nil, nil, NewSlabSplitErrorf("MapMetaDataSlab (%s) has less than 2 child headers", m.header.id) + return nil, nil, NewSlabSplitErrorf("MapMetaDataSlab (%s) has less than 2 child headers", m.header.slabID) } leftChildrenCount := int(math.Ceil(float64(len(m.childrenHeaders)) / 2)) leftSize := leftChildrenCount * mapSlabHeaderSize - sID, err := storage.GenerateStorageID(m.ID().Address) + sID, err := storage.GenerateSlabID(m.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 storage ID for address 0x%x", m.ID().Address)) + return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.SlabID().address)) } // Construct right slab rightSlab := &MapMetaDataSlab{ header: MapSlabHeader{ - id: sID, + slabID: sID, size: m.header.size - uint32(leftSize), firstKey: m.childrenHeaders[leftChildrenCount].firstKey, }, @@ -3445,8 +3444,8 @@ func (m MapMetaDataSlab) IsData() bool { return false } -func (m *MapMetaDataSlab) SetID(id StorageID) { - m.header.id = id +func (m *MapMetaDataSlab) SetSlabID(id SlabID) { + m.header.slabID = id } func (m *MapMetaDataSlab) Header() MapSlabHeader { @@ -3457,8 +3456,8 @@ func (m *MapMetaDataSlab) ByteSize() uint32 { return m.header.size } -func (m *MapMetaDataSlab) ID() StorageID { - return m.header.id +func (m *MapMetaDataSlab) SlabID() SlabID { + return m.header.slabID } func (m *MapMetaDataSlab) ExtraData() *MapExtraData { @@ -3480,7 +3479,7 @@ func (m *MapMetaDataSlab) PopIterate(storage SlabStorage, fn MapPopIterationFunc // Iterate child slabs backwards for i := len(m.childrenHeaders) - 1; i >= 0; i-- { - childID := m.childrenHeaders[i].id + childID := m.childrenHeaders[i].slabID child, err := getMapSlab(storage, childID) if err != nil { @@ -3515,11 +3514,11 @@ func (m *MapMetaDataSlab) PopIterate(storage SlabStorage, fn MapPopIterationFunc func (m *MapMetaDataSlab) String() string { elemsStr := make([]string, len(m.childrenHeaders)) for i, h := range m.childrenHeaders { - elemsStr[i] = fmt.Sprintf("{id:%s size:%d firstKey:%d}", h.id, h.size, h.firstKey) + elemsStr[i] = fmt.Sprintf("{id:%s size:%d firstKey:%d}", h.slabID, h.size, h.firstKey) } return fmt.Sprintf("MapMetaDataSlab id:%s size:%d firstKey:%d children: [%s]", - m.header.id, + m.header.slabID, m.header.size, m.header.firstKey, strings.Join(elemsStr, " "), @@ -3528,11 +3527,11 @@ func (m *MapMetaDataSlab) String() string { func NewMap(storage SlabStorage, address Address, digestBuilder DigesterBuilder, typeInfo TypeInfo) (*OrderedMap, error) { - // Create root storage id - sID, err := storage.GenerateStorageID(address) + // Create root slab ID + sID, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } // Create seed for non-crypto hash algos (CircleHash64, SipHash) to use. @@ -3545,8 +3544,8 @@ func NewMap(storage SlabStorage, address Address, digestBuilder DigesterBuilder, // This is for creating the seed, so the seed used here is OK to be 0. // LittleEndian is needed for compatibility (same digest from []byte and // two uint64). - a := binary.LittleEndian.Uint64(sID.Address[:]) - b := binary.LittleEndian.Uint64(sID.Index[:]) + a := binary.LittleEndian.Uint64(sID.address[:]) + b := binary.LittleEndian.Uint64(sID.index[:]) k0 := circlehash.Hash64Uint64x2(a, b, uint64(0)) // To save storage space, only store 64-bits of the seed. @@ -3560,17 +3559,17 @@ func NewMap(storage SlabStorage, address Address, digestBuilder DigesterBuilder, root := &MapDataSlab{ header: MapSlabHeader{ - id: sID, - size: mapRootDataSlabPrefixSize + hkeyElementsPrefixSize, + slabID: sID, + size: mapRootDataSlabPrefixSize + hkeyElementsPrefixSize, }, elements: newHkeyElements(0), extraData: extraData, } - err = storage.Store(root.header.id, root) + err = storage.Store(root.header.slabID, root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.header.id)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.header.slabID)) } return &OrderedMap{ @@ -3580,9 +3579,9 @@ func NewMap(storage SlabStorage, address Address, digestBuilder DigesterBuilder, }, nil } -func NewMapWithRootID(storage SlabStorage, rootID StorageID, digestBuilder DigesterBuilder) (*OrderedMap, error) { - if rootID == StorageIDUndefined { - return nil, NewStorageIDErrorf("cannot create OrderedMap from undefined storage id") +func NewMapWithRootID(storage SlabStorage, rootID SlabID, digestBuilder DigesterBuilder) (*OrderedMap, error) { + if rootID == SlabIDUndefined { + return nil, NewSlabIDErrorf("cannot create OrderedMap from undefined slab ID") } root, err := getMapSlab(storage, rootID) @@ -3686,7 +3685,7 @@ func (m *OrderedMap) Set(comparator ValueComparator, hip HashInputProvider, key // Set root to its child slab if root has one child slab. root := m.root.(*MapMetaDataSlab) if len(root.childrenHeaders) == 1 { - err := m.promoteChildAsNewRoot(root.childrenHeaders[0].id) + err := m.promoteChildAsNewRoot(root.childrenHeaders[0].slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by OrderedMap.promoteChildAsNewRoot(). return nil, err @@ -3735,7 +3734,7 @@ func (m *OrderedMap) Remove(comparator ValueComparator, hip HashInputProvider, k // Set root to its child slab if root has one child slab. root := m.root.(*MapMetaDataSlab) if len(root.childrenHeaders) == 1 { - err := m.promoteChildAsNewRoot(root.childrenHeaders[0].id) + err := m.promoteChildAsNewRoot(root.childrenHeaders[0].slabID) if err != nil { // Don't need to wrap error as external error because err is already categorized by OrderedMap.promoteChildAsNewRoot(). return nil, nil, err @@ -3767,17 +3766,17 @@ func (m *OrderedMap) splitRoot() error { extraData := m.root.RemoveExtraData() // Save root node id - rootID := m.root.ID() + rootID := m.root.SlabID() - // Assign a new storage id to old root before splitting it. - sID, err := m.Storage.GenerateStorageID(m.Address()) + // Assign a new slab ID to old root before splitting it. + sID, err := m.Storage.GenerateSlabID(m.Address()) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", m.Address())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.Address())) } oldRoot := m.root - oldRoot.SetID(sID) + oldRoot.SetSlabID(sID) // Split old root leftSlab, rightSlab, err := oldRoot.Split(m.Storage) @@ -3789,10 +3788,10 @@ func (m *OrderedMap) splitRoot() error { left := leftSlab.(MapSlab) right := rightSlab.(MapSlab) - // Create new MapMetaDataSlab with the old root's storage ID + // Create new MapMetaDataSlab with the old root's slab ID newRoot := &MapMetaDataSlab{ header: MapSlabHeader{ - id: rootID, + slabID: rootID, size: mapMetaDataSlabPrefixSize + mapSlabHeaderSize*2, firstKey: left.Header().firstKey, }, @@ -3802,25 +3801,25 @@ func (m *OrderedMap) splitRoot() error { m.root = newRoot - err = m.Storage.Store(left.ID(), left) + err = m.Storage.Store(left.SlabID(), left) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", left.SlabID())) } - err = m.Storage.Store(right.ID(), right) + err = m.Storage.Store(right.SlabID(), right) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", right.SlabID())) } - err = m.Storage.Store(m.root.ID(), m.root) + err = m.Storage.Store(m.root.SlabID(), m.root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.root.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.root.SlabID())) } return nil } -func (m *OrderedMap) promoteChildAsNewRoot(childID StorageID) error { +func (m *OrderedMap) promoteChildAsNewRoot(childID SlabID) error { child, err := getMapSlab(m.Storage, childID) if err != nil { @@ -3836,11 +3835,11 @@ func (m *OrderedMap) promoteChildAsNewRoot(childID StorageID) error { extraData := m.root.RemoveExtraData() - rootID := m.root.ID() + rootID := m.root.SlabID() m.root = child - m.root.SetID(rootID) + m.root.SetSlabID(rootID) m.root.SetExtraData(extraData) @@ -3858,16 +3857,16 @@ func (m *OrderedMap) promoteChildAsNewRoot(childID StorageID) error { return nil } -func (m *OrderedMap) StorageID() StorageID { - return m.root.Header().id +func (m *OrderedMap) SlabID() SlabID { + return m.root.SlabID() } -func (m *OrderedMap) ID() ID { - sid := m.StorageID() +func (m *OrderedMap) ValueID() ValueID { + sid := m.SlabID() - var id ID - copy(id[:], sid.Address[:]) - copy(id[8:], sid.Index[:]) + var id ValueID + copy(id[:], sid.address[:]) + copy(id[8:], sid.index[:]) return id } @@ -3877,7 +3876,7 @@ func (m *OrderedMap) StoredValue(_ SlabStorage) (Value, error) { } func (m *OrderedMap) Storable(_ SlabStorage, _ Address, _ uint64) (Storable, error) { - return StorageIDStorable(m.StorageID()), nil + return SlabIDStorable(m.SlabID()), nil } func (m *OrderedMap) Count() uint64 { @@ -3885,7 +3884,7 @@ func (m *OrderedMap) Count() uint64 { } func (m *OrderedMap) Address() Address { - return m.root.ID().Address + return m.root.SlabID().address } func (m *OrderedMap) Type() TypeInfo { @@ -3916,7 +3915,7 @@ func (m *OrderedMap) String() string { return fmt.Sprintf("[%s]", strings.Join(elemsStr, " ")) } -func getMapSlab(storage SlabStorage, id StorageID) (MapSlab, error) { +func getMapSlab(storage SlabStorage, id SlabID) (MapSlab, error) { slab, found, err := storage.Retrieve(id) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. @@ -3937,7 +3936,7 @@ func firstMapDataSlab(storage SlabStorage, slab MapSlab) (MapSlab, error) { return slab, nil } meta := slab.(*MapMetaDataSlab) - firstChildID := meta.childrenHeaders[0].id + firstChildID := meta.childrenHeaders[0].slabID firstChild, err := getMapSlab(storage, firstChildID) if err != nil { // Don't need to wrap error as external error because err is already categorized by getMapSlab(). @@ -4017,13 +4016,13 @@ type MapElementIterationFunc func(Value) (resume bool, err error) type MapIterator struct { storage SlabStorage - id StorageID + id SlabID elemIterator *MapElementIterator } func (i *MapIterator) Next() (key Value, value Value, err error) { if i.elemIterator == nil { - if i.id == StorageIDUndefined { + if i.id == SlabIDUndefined { return nil, nil, nil } @@ -4064,7 +4063,7 @@ func (i *MapIterator) Next() (key Value, value Value, err error) { func (i *MapIterator) NextKey() (key Value, err error) { if i.elemIterator == nil { - if i.id == StorageIDUndefined { + if i.id == SlabIDUndefined { return nil, nil } @@ -4099,7 +4098,7 @@ func (i *MapIterator) NextKey() (key Value, err error) { func (i *MapIterator) NextValue() (value Value, err error) { if i.elemIterator == nil { - if i.id == StorageIDUndefined { + if i.id == SlabIDUndefined { return nil, nil } @@ -4275,7 +4274,7 @@ func (m *OrderedMap) PopIterate(fn MapPopIterationFunc) error { return err } - rootID := m.root.ID() + rootID := m.root.SlabID() // Set map count to 0 in extraData extraData := m.root.ExtraData() @@ -4284,18 +4283,18 @@ func (m *OrderedMap) PopIterate(fn MapPopIterationFunc) error { // Set root to empty data slab m.root = &MapDataSlab{ header: MapSlabHeader{ - id: rootID, - size: mapRootDataSlabPrefixSize + hkeyElementsPrefixSize, + slabID: rootID, + size: mapRootDataSlabPrefixSize + hkeyElementsPrefixSize, }, elements: newHkeyElements(0), extraData: extraData, } // Save root slab - err = m.Storage.Store(m.root.ID(), m.root) + err = m.Storage.Store(m.root.SlabID(), m.root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.root.ID())) + return wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", m.root.SlabID())) } return nil } @@ -4336,10 +4335,10 @@ func NewMapFromBatchData( var slabs []MapSlab - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } elements := &hkeyElements{ @@ -4425,16 +4424,16 @@ func NewMapFromBatchData( currentSlabSize+newElementSize > uint32(maxThreshold) { // Generate storge id for next data slab - nextID, err := storage.GenerateStorageID(address) + nextID, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } // Create data slab dataSlab := &MapDataSlab{ header: MapSlabHeader{ - id: id, + slabID: id, size: mapDataSlabPrefixSize + elements.Size(), firstKey: elements.firstKey(), }, @@ -4469,7 +4468,7 @@ func NewMapFromBatchData( // Create last data slab dataSlab := &MapDataSlab{ header: MapSlabHeader{ - id: id, + slabID: id, size: mapDataSlabPrefixSize + elements.Size(), firstKey: elements.firstKey(), }, @@ -4522,10 +4521,10 @@ func NewMapFromBatchData( // Store all slabs for _, slab := range slabs { - err = storage.Store(slab.ID(), slab) + err = storage.Store(slab.SlabID(), slab) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", slab.ID())) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", slab.SlabID())) } } @@ -4552,10 +4551,10 @@ func NewMapFromBatchData( root.SetExtraData(extraData) // Store root - err = storage.Store(root.ID(), root) + err = storage.Store(root.SlabID(), root) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.ID())) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", root.SlabID())) } return &OrderedMap{ @@ -4575,10 +4574,10 @@ func nextLevelMapSlabs(storage SlabStorage, address Address, slabs []MapSlab) ([ nextLevelSlabsIndex := 0 // Generate storge id - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } childrenCount := maxNumberOfHeadersInMetaSlab @@ -4588,7 +4587,7 @@ func nextLevelMapSlabs(storage SlabStorage, address Address, slabs []MapSlab) ([ metaSlab := &MapMetaDataSlab{ header: MapSlabHeader{ - id: id, + slabID: id, size: mapMetaDataSlabPrefixSize, firstKey: slabs[0].Header().firstKey, }, @@ -4609,15 +4608,15 @@ func nextLevelMapSlabs(storage SlabStorage, address Address, slabs []MapSlab) ([ } // Generate storge id for next meta data slab - id, err = storage.GenerateStorageID(address) + id, err = storage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by SlabStorage interface. - return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } metaSlab = &MapMetaDataSlab{ header: MapSlabHeader{ - id: id, + slabID: id, size: mapMetaDataSlabPrefixSize, firstKey: slab.Header().firstKey, }, diff --git a/map_debug.go b/map_debug.go index 426d6df9..0b7e3054 100644 --- a/map_debug.go +++ b/map_debug.go @@ -50,13 +50,13 @@ func GetMapStats(m *OrderedMap) (MapStats, error) { collisionDataSlabCount := uint64(0) storableDataSlabCount := uint64(0) - nextLevelIDs := []StorageID{m.StorageID()} + nextLevelIDs := []SlabID{m.SlabID()} for len(nextLevelIDs) > 0 { ids := nextLevelIDs - nextLevelIDs = []StorageID(nil) + nextLevelIDs = []SlabID(nil) for _, id := range ids { @@ -101,10 +101,10 @@ func GetMapStats(m *OrderedMap) (MapStats, error) { } else { e := elem.(*singleElement) - if _, ok := e.key.(StorageIDStorable); ok { + if _, ok := e.key.(SlabIDStorable); ok { storableDataSlabCount++ } - if _, ok := e.value.(StorageIDStorable); ok { + if _, ok := e.value.(SlabIDStorable); ok { storableDataSlabCount++ } } @@ -116,11 +116,11 @@ func GetMapStats(m *OrderedMap) (MapStats, error) { metaDataSlabCount++ for _, storable := range slab.ChildStorables() { - id, ok := storable.(StorageIDStorable) + id, ok := storable.(SlabIDStorable) if !ok { - return MapStats{}, NewFatalError(fmt.Errorf("metadata slab's child storables are not of type StorageIDStorable")) + return MapStats{}, NewFatalError(fmt.Errorf("metadata slab's child storables are not of type SlabIDStorable")) } - nextLevelIDs = append(nextLevelIDs, StorageID(id)) + nextLevelIDs = append(nextLevelIDs, SlabID(id)) } } } @@ -150,17 +150,17 @@ func PrintMap(m *OrderedMap) { func DumpMapSlabs(m *OrderedMap) ([]string, error) { var dumps []string - nextLevelIDs := []StorageID{m.StorageID()} + nextLevelIDs := []SlabID{m.SlabID()} - var overflowIDs []StorageID - var collisionSlabIDs []StorageID + var overflowIDs []SlabID + var collisionSlabIDs []SlabID level := 0 for len(nextLevelIDs) > 0 { ids := nextLevelIDs - nextLevelIDs = []StorageID(nil) + nextLevelIDs = []SlabID(nil) for _, id := range ids { @@ -183,15 +183,15 @@ func DumpMapSlabs(m *OrderedMap) ([]string, error) { if group, ok := elem.(elementGroup); ok { if !group.Inline() { extSlab := group.(*externalCollisionGroup) - collisionSlabIDs = append(collisionSlabIDs, extSlab.id) + collisionSlabIDs = append(collisionSlabIDs, extSlab.slabID) } } } childStorables := dataSlab.ChildStorables() for _, e := range childStorables { - if id, ok := e.(StorageIDStorable); ok { - overflowIDs = append(overflowIDs, StorageID(id)) + if id, ok := e.(SlabIDStorable); ok { + overflowIDs = append(overflowIDs, SlabID(id)) } } @@ -200,11 +200,11 @@ func DumpMapSlabs(m *OrderedMap) ([]string, error) { dumps = append(dumps, fmt.Sprintf("level %d, %s", level+1, meta)) for _, storable := range slab.ChildStorables() { - id, ok := storable.(StorageIDStorable) + id, ok := storable.(SlabIDStorable) if !ok { - return nil, NewFatalError(errors.New("metadata slab's child storables are not of type StorageIDStorable")) + return nil, NewFatalError(errors.New("metadata slab's child storables are not of type SlabIDStorable")) } - nextLevelIDs = append(nextLevelIDs, StorageID(id)) + nextLevelIDs = append(nextLevelIDs, SlabID(id)) } } } @@ -241,7 +241,7 @@ func DumpMapSlabs(m *OrderedMap) ([]string, error) { if !found { return nil, NewSlabNotFoundErrorf(id, "slab not found during map slab dump") } - dumps = append(dumps, fmt.Sprintf("overflow: %s", slab)) + dumps = append(dumps, slab.String()) } return dumps, nil @@ -251,7 +251,7 @@ func ValidMap(m *OrderedMap, typeInfo TypeInfo, tic TypeInfoComparator, hip Hash extraData := m.root.ExtraData() if extraData == nil { - return NewFatalError(fmt.Errorf("root slab %d doesn't have extra data", m.root.ID())) + return NewFatalError(fmt.Errorf("root slab %d doesn't have extra data", m.root.SlabID())) } // Verify that extra data has correct type information @@ -259,7 +259,7 @@ func ValidMap(m *OrderedMap, typeInfo TypeInfo, tic TypeInfoComparator, hip Hash return NewFatalError( fmt.Errorf( "root slab %d type information %v, want %v", - m.root.ID(), + m.root.SlabID(), extraData.TypeInfo, typeInfo, )) @@ -267,11 +267,11 @@ func ValidMap(m *OrderedMap, typeInfo TypeInfo, tic TypeInfoComparator, hip Hash // Verify that extra data has seed if extraData.Seed == 0 { - return NewFatalError(fmt.Errorf("root slab %d seed is uninitialized", m.root.ID())) + return NewFatalError(fmt.Errorf("root slab %d seed is uninitialized", m.root.SlabID())) } computedCount, dataSlabIDs, nextDataSlabIDs, firstKeys, err := validMapSlab( - m.Storage, m.digesterBuilder, tic, hip, m.root.ID(), 0, nil, []StorageID{}, []StorageID{}, []Digest{}) + m.Storage, m.digesterBuilder, tic, hip, m.root.SlabID(), 0, nil, []SlabID{}, []SlabID{}, []Digest{}) if err != nil { // Don't need to wrap error as external error because err is already categorized by validMapSlab(). return err @@ -282,7 +282,7 @@ func ValidMap(m *OrderedMap, typeInfo TypeInfo, tic TypeInfoComparator, hip Hash return NewFatalError( fmt.Errorf( "root slab %d count %d is wrong, want %d", - m.root.ID(), + m.root.SlabID(), extraData.Count, computedCount, )) @@ -320,16 +320,16 @@ func validMapSlab( digesterBuilder DigesterBuilder, tic TypeInfoComparator, hip HashInputProvider, - id StorageID, + id SlabID, level int, headerFromParentSlab *MapSlabHeader, - dataSlabIDs []StorageID, - nextDataSlabIDs []StorageID, + dataSlabIDs []SlabID, + nextDataSlabIDs []SlabID, firstKeys []Digest, ) ( elementCount uint64, - _dataSlabIDs []StorageID, - _nextDataSlabIDs []StorageID, + _dataSlabIDs []SlabID, + _nextDataSlabIDs []SlabID, _firstKeys []Digest, err error, ) { @@ -417,7 +417,7 @@ func validMapSlab( dataSlabIDs = append(dataSlabIDs, id) - if dataSlab.next != StorageIDUndefined { + if dataSlab.next != SlabIDUndefined { nextDataSlabIDs = append(nextDataSlabIDs, dataSlab.next) } @@ -447,7 +447,7 @@ func validMapSlab( // Verify child slabs count := uint64(0) count, dataSlabIDs, nextDataSlabIDs, firstKeys, err = - validMapSlab(storage, digesterBuilder, tic, hip, h.id, level+1, &h, dataSlabIDs, nextDataSlabIDs, firstKeys) + validMapSlab(storage, digesterBuilder, tic, hip, h.slabID, level+1, &h, dataSlabIDs, nextDataSlabIDs, firstKeys) if err != nil { // Don't need to wrap error as external error because err is already categorized by validMapSlab(). return 0, nil, nil, nil, err @@ -500,7 +500,7 @@ func validMapElements( db DigesterBuilder, tic TypeInfoComparator, hip HashInputProvider, - id StorageID, + id SlabID, elements elements, digestLevel uint, hkeyPrefixes []Digest, @@ -525,7 +525,7 @@ func validMapHkeyElements( db DigesterBuilder, tic TypeInfoComparator, hip HashInputProvider, - id StorageID, + id SlabID, elements *hkeyElements, digestLevel uint, hkeyPrefixes []Digest, @@ -660,7 +660,7 @@ func validMapSingleElements( db DigesterBuilder, tic TypeInfoComparator, hip HashInputProvider, - id StorageID, + id SlabID, elements *singleElements, digestLevel uint, hkeyPrefixes []Digest, @@ -727,7 +727,7 @@ func validSingleElement( ) { // Verify key pointer - if _, keyPointer := e.key.(StorageIDStorable); e.keyPointer != keyPointer { + if _, keyPointer := e.key.(SlabIDStorable); e.keyPointer != keyPointer { return 0, 0, NewFatalError(fmt.Errorf("element %s keyPointer %t is wrong, want %t", e, e.keyPointer, keyPointer)) } @@ -745,7 +745,7 @@ func validSingleElement( } // Verify value pointer - if _, valuePointer := e.value.(StorageIDStorable); e.valuePointer != valuePointer { + if _, valuePointer := e.value.(SlabIDStorable); e.valuePointer != valuePointer { return 0, 0, NewFatalError(fmt.Errorf("element %s valuePointer %t is wrong, want %t", e, e.valuePointer, valuePointer)) } @@ -812,7 +812,7 @@ func ValidMapSerialization( ) error { return validMapSlabSerialization( m.Storage, - m.root.ID(), + m.root.SlabID(), cborDecMode, cborEncMode, decodeStorable, @@ -823,7 +823,7 @@ func ValidMapSerialization( func validMapSlabSerialization( storage SlabStorage, - id StorageID, + id SlabID, cborDecMode cbor.DecMode, cborEncMode cbor.EncMode, decodeStorable StorableDecoder, @@ -931,7 +931,7 @@ func validMapSlabSerialization( // Verify child slabs err = validMapSlabSerialization( storage, - h.id, + h.slabID, cborDecMode, cborEncMode, decodeStorable, @@ -1235,14 +1235,14 @@ func mapExternalCollisionElementsEqual( return NewFatalError(fmt.Errorf("externalCollisionGroup size %d is wrong, want %d", actual.size, expected.size)) } - if expected.id != actual.id { - return NewFatalError(fmt.Errorf("externalCollisionGroup id %d is wrong, want %d", actual.id, expected.id)) + if expected.slabID != actual.slabID { + return NewFatalError(fmt.Errorf("externalCollisionGroup id %d is wrong, want %d", actual.slabID, expected.slabID)) } // Compare external collision slab err := validMapSlabSerialization( storage, - expected.id, + expected.slabID, cborDecMode, cborEncMode, decodeStorable, @@ -1285,11 +1285,11 @@ func mapSingleElementEqual( } // Compare key stored in a separate slab - if idStorable, ok := expected.key.(StorageIDStorable); ok { + if idStorable, ok := expected.key.(SlabIDStorable); ok { v, err := idStorable.StoredValue(storage) if err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageIDStorable.StoredValue(). + // Don't need to wrap error as external error because err is already categorized by SlabIDStorable.StoredValue(). return err } @@ -1312,11 +1312,11 @@ func mapSingleElementEqual( } // Compare value stored in a separate slab - if idStorable, ok := expected.value.(StorageIDStorable); ok { + if idStorable, ok := expected.value.(SlabIDStorable); ok { v, err := idStorable.StoredValue(storage) if err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageIDStorable.StoredValue(). + // Don't need to wrap error as external error because err is already categorized by SlabIDStorable.StoredValue(). return err } diff --git a/map_test.go b/map_test.go index 3ab71180..d31f1172 100644 --- a/map_test.go +++ b/map_test.go @@ -171,12 +171,12 @@ func verifyMap( rootIDSet, err := CheckStorageHealth(storage, 1) require.NoError(t, err) - rootIDs := make([]StorageID, 0, len(rootIDSet)) + rootIDs := make([]SlabID, 0, len(rootIDSet)) for id := range rootIDSet { rootIDs = append(rootIDs, id) } require.Equal(t, 1, len(rootIDs)) - require.Equal(t, m.StorageID(), rootIDs[0]) + require.Equal(t, m.SlabID(), rootIDs[0]) if !hasNestedArrayMapElement { // Need to call Commit before calling storage.Count() for PersistentSlabStorage. @@ -688,13 +688,13 @@ func testMapRemoveElement(t *testing.T, m *OrderedMap, k Value, expectedV Value) require.NoError(t, err) valueEqual(t, typeInfoComparator, expectedV, removedValue) - if id, ok := removedKeyStorable.(StorageIDStorable); ok { - err = m.Storage.Remove(StorageID(id)) + if id, ok := removedKeyStorable.(SlabIDStorable); ok { + err = m.Storage.Remove(SlabID(id)) require.NoError(t, err) } - if id, ok := removedValueStorable.(StorageIDStorable); ok { - err = m.Storage.Remove(StorageID(id)) + if id, ok := removedValueStorable.(SlabIDStorable); ok { + err = m.Storage.Remove(SlabID(id)) require.NoError(t, err) } @@ -1292,13 +1292,13 @@ func testMapDeterministicHashCollision(t *testing.T, r *rand.Rand, maxDigestLeve require.NoError(t, err) valueEqual(t, typeInfoComparator, v, removedValue) - if id, ok := removedKeyStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := removedKeyStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } - if id, ok := removedValueStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := removedValueStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } } @@ -1360,13 +1360,13 @@ func testMapRandomHashCollision(t *testing.T, r *rand.Rand, maxDigestLevel int) require.NoError(t, err) valueEqual(t, typeInfoComparator, v, removedValue) - if id, ok := removedKeyStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := removedKeyStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } - if id, ok := removedValueStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := removedValueStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } } @@ -1457,8 +1457,8 @@ func testMapSetRemoveRandomValues( require.NoError(t, err) valueEqual(t, typeInfoComparator, oldv, existingValue) - if id, ok := existingStorable.(StorageIDStorable); ok { - err = storage.Remove(StorageID(id)) + if id, ok := existingStorable.(SlabIDStorable); ok { + err = storage.Remove(SlabID(id)) require.NoError(t, err) } } else { @@ -1484,13 +1484,13 @@ func testMapSetRemoveRandomValues( require.NoError(t, err) valueEqual(t, typeInfoComparator, keyValues[k], removedValue) - if id, ok := removedKeyStorable.(StorageIDStorable); ok { - err := storage.Remove(StorageID(id)) + if id, ok := removedKeyStorable.(SlabIDStorable); ok { + err := storage.Remove(SlabID(id)) require.NoError(t, err) } - if id, ok := removedValueStorable.(StorageIDStorable); ok { - err := storage.Remove(StorageID(id)) + if id, ok := removedValueStorable.(SlabIDStorable); ok { + err := storage.Remove(SlabID(id)) require.NoError(t, err) } @@ -1537,9 +1537,9 @@ func TestMapEncodeDecode(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(0), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} - expected := map[StorageID][]byte{ + expected := map[SlabID][]byte{ id1: { // extra data // version @@ -1623,10 +1623,10 @@ func TestMapEncodeDecode(t *testing.T) { require.Equal(t, uint64(mapSize), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} - // Expected serialized slab data with storage id - expected := map[StorageID][]byte{ + // Expected serialized slab data with slab id + expected := map[SlabID][]byte{ id1: { // extra data @@ -1742,13 +1742,13 @@ func TestMapEncodeDecode(t *testing.T) { require.Equal(t, uint64(mapSize), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} - id2 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}} - id3 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 3}} - id4 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 4}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}} + id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}} + id4 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}} - // Expected serialized slab data with storage id - expected := map[StorageID][]byte{ + // Expected serialized slab data with slab id + expected := map[SlabID][]byte{ // metadata slab id1: { @@ -1772,7 +1772,7 @@ func TestMapEncodeDecode(t *testing.T) { 0x89, // child header count 0x00, 0x02, - // child header 1 (storage id, first key, size) + // child header 1 (slab id, first key, size) 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, @@ -1788,7 +1788,7 @@ func TestMapEncodeDecode(t *testing.T) { 0x00, // flag: map data 0x08, - // next storage id + // next slab id 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // the following encoded data is valid CBOR @@ -1837,7 +1837,7 @@ func TestMapEncodeDecode(t *testing.T) { 0x00, // flag: has pointer + map data 0x48, - // next storage id + // next slab id 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // the following encoded data is valid CBOR @@ -1874,7 +1874,7 @@ func TestMapEncodeDecode(t *testing.T) { 0x82, 0x76, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x76, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, 0x67, - // element: [hhhhhhhhhhhhhhhhhhhhhh:StorageID(1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,4)] + // element: [hhhhhhhhhhhhhhhhhhhhhh:SlabID(1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,4)] 0x82, 0x76, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0xd8, 0xff, 0x50, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, @@ -1961,10 +1961,10 @@ func TestMapEncodeDecode(t *testing.T) { require.Equal(t, uint64(mapSize), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} - // Expected serialized slab data with storage id - expected := map[StorageID][]byte{ + // Expected serialized slab data with slab id + expected := map[SlabID][]byte{ // map metadata slab id1: { @@ -2154,10 +2154,10 @@ func TestMapEncodeDecode(t *testing.T) { require.Equal(t, uint64(mapSize), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} - // Expected serialized slab data with storage id - expected := map[StorageID][]byte{ + // Expected serialized slab data with slab id + expected := map[SlabID][]byte{ // map metadata slab id1: { @@ -2397,12 +2397,12 @@ func TestMapEncodeDecode(t *testing.T) { require.Equal(t, uint64(mapSize), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} - id2 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}} - id3 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 3}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}} + id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}} - // Expected serialized slab data with storage id - expected := map[StorageID][]byte{ + // Expected serialized slab data with slab id + expected := map[SlabID][]byte{ // map data slab id1: { @@ -2445,14 +2445,14 @@ func TestMapEncodeDecode(t *testing.T) { // external collision group corresponding to hkey 0 // (tag number CBORTagExternalCollisionGroup) 0xd8, 0xfe, - // (tag content: storage id) + // (tag content: slab id) 0xd8, 0xff, 0x50, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, // external collision group corresponding to hkey 1 // (tag number CBORTagExternalCollisionGroup) 0xd8, 0xfe, - // (tag content: storage id) + // (tag content: slab id) 0xd8, 0xff, 0x50, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, }, @@ -2463,7 +2463,7 @@ func TestMapEncodeDecode(t *testing.T) { 0x00, // flag: any size + collision group 0x2b, - // next storage id + // next slab id 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // the following encoded data is valid CBOR @@ -2528,7 +2528,7 @@ func TestMapEncodeDecode(t *testing.T) { 0x00, // flag: any size + collision group 0x2b, - // next storage id + // next slab id 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // the following encoded data is valid CBOR @@ -2627,7 +2627,7 @@ func TestMapEncodeDecode(t *testing.T) { require.Equal(t, uint64(1), m.Count()) - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} expectedNoPointer := []byte{ @@ -2720,9 +2720,9 @@ func TestMapEncodeDecode(t *testing.T) { // elements (array of 1 elements) // each element is encoded as CBOR array of 2 elements (key, value) 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, - // element: [uint64(0), storage id] + // element: [uint64(0), slab id] 0x82, 0xd8, 0xa4, 0x00, - // (tag content: storage id) + // (tag content: slab id) 0xd8, 0xff, 0x50, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, } @@ -2753,7 +2753,7 @@ func TestMapEncodeDecodeRandomValues(t *testing.T) { storage2 := newTestPersistentStorageWithBaseStorage(t, storage.baseStorage) // Create new map from new storage - m2, err := NewMapWithRootID(storage2, m.StorageID(), m.digesterBuilder) + m2, err := NewMapWithRootID(storage2, m.SlabID(), m.digesterBuilder) require.NoError(t, err) verifyMap(t, storage2, typeInfo, address, m2, keyValues, nil, false) @@ -2786,7 +2786,7 @@ func TestMapStoredValue(t *testing.T) { require.Nil(t, existingStorable) } - rootID := m.StorageID() + rootID := m.SlabID() slabIterator, err := storage.SlabIterator() require.NoError(t, err) @@ -2794,7 +2794,7 @@ func TestMapStoredValue(t *testing.T) { for { id, slab := slabIterator() - if id == StorageIDUndefined { + if id == SlabIDUndefined { break } @@ -3115,7 +3115,7 @@ func TestMapFromBatchData(t *testing.T) { return iter.Next() }) require.NoError(t, err) - require.NotEqual(t, copied.StorageID(), m.StorageID()) + require.NotEqual(t, copied.SlabID(), m.SlabID()) verifyEmptyMap(t, storage, typeInfo, address, copied) }) @@ -3176,7 +3176,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, copied.StorageID(), m.StorageID()) + require.NotEqual(t, copied.SlabID(), m.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3235,7 +3235,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, m.StorageID(), copied.StorageID()) + require.NotEqual(t, m.SlabID(), copied.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3300,7 +3300,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, m.StorageID(), copied.StorageID()) + require.NotEqual(t, m.SlabID(), copied.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3369,7 +3369,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, m.StorageID(), copied.StorageID()) + require.NotEqual(t, m.SlabID(), copied.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3432,7 +3432,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, m.StorageID(), copied.StorageID()) + require.NotEqual(t, m.SlabID(), copied.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3514,7 +3514,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, m.StorageID(), copied.StorageID()) + require.NotEqual(t, m.SlabID(), copied.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3596,7 +3596,7 @@ func TestMapFromBatchData(t *testing.T) { }) require.NoError(t, err) - require.NotEqual(t, m.StorageID(), copied.StorageID()) + require.NotEqual(t, m.SlabID(), copied.SlabID()) verifyMap(t, storage, typeInfo, address, copied, keyValues, sortedKeys, false) }) @@ -3700,8 +3700,8 @@ func TestMapMaxInlineElement(t *testing.T) { // Size of root data slab with two elements (key+value pairs) of // max inlined size is target slab size minus - // storage id size (next storage id is omitted in root slab) - require.Equal(t, targetThreshold-storageIDSize, uint64(m.root.Header().size)) + // slab id size (next slab id is omitted in root slab) + require.Equal(t, targetThreshold-slabIDSize, uint64(m.root.Header().size)) verifyMap(t, storage, typeInfo, address, m, keyValues, nil, false) } @@ -3908,8 +3908,8 @@ func TestMapSlabDump(t *testing.T) { require.Nil(t, existingStorable) want := []string{ - "level 1, MapDataSlab id:0x102030405060708.1 size:102 firstkey:0 elements: [0:StorageIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]}):bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]", - "overflow: &{0x102030405060708.2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}", + "level 1, MapDataSlab id:0x102030405060708.1 size:102 firstkey:0 elements: [0:SlabIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]}):bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]", + "StorableSlab id:0x102030405060708.2 storable:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", } dumps, err := DumpMapSlabs(m) require.NoError(t, err) @@ -3935,8 +3935,8 @@ func TestMapSlabDump(t *testing.T) { require.Nil(t, existingStorable) want := []string{ - "level 1, MapDataSlab id:0x102030405060708.1 size:100 firstkey:0 elements: [0:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:StorageIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]})]", - "overflow: &{0x102030405060708.2 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb}", + "level 1, MapDataSlab id:0x102030405060708.1 size:100 firstkey:0 elements: [0:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:SlabIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]})]", + "StorableSlab id:0x102030405060708.2 storable:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", } dumps, err := DumpMapSlabs(m) require.NoError(t, err) @@ -4180,7 +4180,7 @@ func TestMaxInlineMapValueSize(t *testing.T) { t.Run("large key", func(t *testing.T) { // Value has larger max inline size when key is more than max map key size because // when key size exceeds max map key size, it is stored in a separate storable slab, - // and StorageIDStorable is stored as key in the map, which is 19 bytes. + // and SlabIDStorable is stored as key in the map, which is 19 bytes. SetThreshold(256) defer SetThreshold(1024) @@ -4226,9 +4226,9 @@ func TestMapID(t *testing.T) { m, err := NewMap(storage, address, newBasicDigesterBuilder(), typeInfo) require.NoError(t, err) - sid := m.StorageID() - id := m.ID() + sid := m.SlabID() + id := m.ValueID() - require.Equal(t, sid.Address[:], id[:8]) - require.Equal(t, sid.Index[:], id[8:]) + require.Equal(t, sid.address[:], id[:8]) + require.Equal(t, sid.index[:], id[8:]) } diff --git a/slab.go b/slab.go index be33b341..fdfc17ad 100644 --- a/slab.go +++ b/slab.go @@ -18,10 +18,13 @@ package atree +import "fmt" + type Slab interface { Storable + fmt.Stringer - ID() StorageID + SlabID() SlabID Split(SlabStorage) (Slab, Slab, error) Merge(Slab) error // LendToRight rebalances slabs by moving elements from left to right diff --git a/storable.go b/storable.go index 3f3942c9..1ee513a9 100644 --- a/storable.go +++ b/storable.go @@ -41,21 +41,21 @@ const ( CBORTagInlineCollisionGroup = 253 CBORTagExternalCollisionGroup = 254 - CBORTagStorageID = 255 + CBORTagSlabID = 255 ) -type StorageIDStorable StorageID +type SlabIDStorable SlabID -var _ Storable = StorageIDStorable{} +var _ Storable = SlabIDStorable{} -func (v StorageIDStorable) ChildStorables() []Storable { +func (v SlabIDStorable) ChildStorables() []Storable { return nil } -func (v StorageIDStorable) StoredValue(storage SlabStorage) (Value, error) { - id := StorageID(v) +func (v SlabIDStorable) StoredValue(storage SlabStorage) (Value, error) { + id := SlabID(v) if err := id.Valid(); err != nil { - // Don't need to wrap error as external error because err is already categorized by StorageID.Valid(). + // Don't need to wrap error as external error because err is already categorized by SlabID.Valid(). return nil, err } @@ -75,25 +75,25 @@ func (v StorageIDStorable) StoredValue(storage SlabStorage) (Value, error) { return value, nil } -// Encode encodes StorageIDStorable as +// Encode encodes SlabIDStorable as // // cbor.Tag{ -// Number: cborTagStorageID, +// Number: cborTagSlabID, // Content: byte(v), // } -func (v StorageIDStorable) Encode(enc *Encoder) error { +func (v SlabIDStorable) Encode(enc *Encoder) error { err := enc.CBOR.EncodeRawBytes([]byte{ // tag number - 0xd8, CBORTagStorageID, + 0xd8, CBORTagSlabID, }) if err != nil { return NewEncodingError(err) } - copy(enc.Scratch[:], v.Address[:]) - copy(enc.Scratch[8:], v.Index[:]) + copy(enc.Scratch[:], v.address[:]) + copy(enc.Scratch[8:], v.index[:]) - err = enc.CBOR.EncodeBytes(enc.Scratch[:storageIDSize]) + err = enc.CBOR.EncodeBytes(enc.Scratch[:slabIDSize]) if err != nil { return NewEncodingError(err) } @@ -101,13 +101,13 @@ func (v StorageIDStorable) Encode(enc *Encoder) error { return nil } -func (v StorageIDStorable) ByteSize() uint32 { - // tag number (2 bytes) + byte string header (1 byte) + storage id (16 bytes) - return 2 + 1 + storageIDSize +func (v SlabIDStorable) ByteSize() uint32 { + // tag number (2 bytes) + byte string header (1 byte) + slab id (16 bytes) + return 2 + 1 + slabIDSize } -func (v StorageIDStorable) String() string { - return fmt.Sprintf("StorageIDStorable(%d)", v) +func (v SlabIDStorable) String() string { + return fmt.Sprintf("SlabIDStorable(%d)", v) } // Encode is a wrapper for Storable.Encode() @@ -129,17 +129,17 @@ func Encode(storable Storable, encMode cbor.EncMode) ([]byte, error) { return buf.Bytes(), nil } -func DecodeStorageIDStorable(dec *cbor.StreamDecoder) (Storable, error) { +func DecodeSlabIDStorable(dec *cbor.StreamDecoder) (Storable, error) { b, err := dec.DecodeBytes() if err != nil { return nil, NewDecodingError(err) } - id, err := NewStorageIDFromRawBytes(b) + id, err := NewSlabIDFromRawBytes(b) if err != nil { - // Don't need to wrap error as external error because err is already categorized by NewStorageIDFromRawBytes(). + // Don't need to wrap error as external error because err is already categorized by NewSlabIDFromRawBytes(). return nil, err } - return StorageIDStorable(id), nil + return SlabIDStorable(id), nil } diff --git a/storable_slab.go b/storable_slab.go index b1cdf4a6..3a24618a 100644 --- a/storable_slab.go +++ b/storable_slab.go @@ -18,23 +18,56 @@ package atree +import "fmt" + // StorableSlab allows storing storables (CBOR encoded data) directly in a slab. // Eventually we will only have a dictionary at the account storage root, // so this won't be needed, but during the refactor we have the need to store // other non-dictionary values (e.g. strings, integers, etc.) directly in accounts // (i.e. directly in slabs aka registers) type StorableSlab struct { - StorageID StorageID - Storable Storable + slabID SlabID + storable Storable } -var _ Slab = StorableSlab{} +var _ Slab = &StorableSlab{} + +func NewStorableSlab(storage SlabStorage, address Address, storable Storable) (Storable, error) { + id, err := storage.GenerateSlabID(address) + if err != nil { + // Wrap err as external error (if needed) because err is returned by SlabStorage interface. + return nil, wrapErrorfAsExternalErrorIfNeeded( + err, + fmt.Sprintf( + "failed to generate slab ID for address 0x%x", + address, + ), + ) + } + + slab := &StorableSlab{ + slabID: id, + storable: storable, + } + + err = storage.Store(id, slab) + if err != nil { + // Wrap err as external error (if needed) because err is returned by SlabStorage interface. + return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to store slab %s", id)) + } + + return SlabIDStorable(id), nil +} + +func (s *StorableSlab) String() string { + return fmt.Sprintf("StorableSlab id:%s storable:%s", s.slabID, s.storable) +} -func (s StorableSlab) ChildStorables() []Storable { - return []Storable{s.Storable} +func (s *StorableSlab) ChildStorables() []Storable { + return []Storable{s.storable} } -func (s StorableSlab) Encode(enc *Encoder) error { +func (s *StorableSlab) Encode(enc *Encoder) error { // Encode version enc.Scratch[0] = 0 @@ -42,7 +75,7 @@ func (s StorableSlab) Encode(enc *Encoder) error { flag := maskStorable flag = setNoSizeLimit(flag) - if _, ok := s.Storable.(StorageIDStorable); ok { + if _, ok := s.storable.(SlabIDStorable); ok { flag = setHasPointers(flag) } @@ -53,7 +86,7 @@ func (s StorableSlab) Encode(enc *Encoder) error { return NewEncodingError(err) } - err = s.Storable.Encode(enc) + err = s.storable.Encode(enc) if err != nil { // Wrap err as external error (if needed) because err is returned by Storable interface. return wrapErrorfAsExternalErrorIfNeeded(err, "failed to encode storable") @@ -62,16 +95,16 @@ func (s StorableSlab) Encode(enc *Encoder) error { return nil } -func (s StorableSlab) ByteSize() uint32 { - return versionAndFlagSize + s.Storable.ByteSize() +func (s *StorableSlab) ByteSize() uint32 { + return versionAndFlagSize + s.storable.ByteSize() } -func (s StorableSlab) ID() StorageID { - return s.StorageID +func (s *StorableSlab) SlabID() SlabID { + return s.slabID } -func (s StorableSlab) StoredValue(storage SlabStorage) (Value, error) { - value, err := s.Storable.StoredValue(storage) +func (s *StorableSlab) StoredValue(storage SlabStorage) (Value, error) { + value, err := s.storable.StoredValue(storage) if err != nil { // Wrap err as external error (if needed) because err is returned by Storable interface. return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to get storable's stored value") @@ -79,18 +112,18 @@ func (s StorableSlab) StoredValue(storage SlabStorage) (Value, error) { return value, nil } -func (StorableSlab) Split(_ SlabStorage) (Slab, Slab, error) { +func (*StorableSlab) Split(_ SlabStorage) (Slab, Slab, error) { return nil, nil, NewNotApplicableError("StorableSlab", "Slab", "Split") } -func (StorableSlab) Merge(_ Slab) error { +func (*StorableSlab) Merge(_ Slab) error { return NewNotApplicableError("StorableSlab", "Slab", "Merge") } -func (StorableSlab) LendToRight(_ Slab) error { +func (*StorableSlab) LendToRight(_ Slab) error { return NewNotApplicableError("StorableSlab", "Slab", "LendToRight") } -func (StorableSlab) BorrowFromRight(_ Slab) error { +func (*StorableSlab) BorrowFromRight(_ Slab) error { return NewNotApplicableError("StorableSlab", "Slab", "BorrowFromRight") } diff --git a/storable_test.go b/storable_test.go index 6fe3ef05..be725054 100644 --- a/storable_test.go +++ b/storable_test.go @@ -349,14 +349,14 @@ func (v StringValue) Storable(storage SlabStorage, address Address, maxInlineSiz if uint64(v.ByteSize()) > maxInlineSize { // Create StorableSlab - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) if err != nil { return nil, err } slab := &StorableSlab{ - StorageID: id, - Storable: v, + slabID: id, + storable: v, } // Store StorableSlab in storage @@ -365,8 +365,8 @@ func (v StringValue) Storable(storage SlabStorage, address Address, maxInlineSiz return nil, err } - // Return storage id as storable - return StorageIDStorable(id), nil + // Return slab id as storable + return SlabIDStorable(id), nil } return v, nil @@ -430,7 +430,7 @@ func (v StringValue) String() string { return v.str } -func decodeStorable(dec *cbor.StreamDecoder, id StorageID) (Storable, error) { +func decodeStorable(dec *cbor.StreamDecoder, id SlabID) (Storable, error) { t, err := dec.NextType() if err != nil { return nil, err @@ -451,8 +451,8 @@ func decodeStorable(dec *cbor.StreamDecoder, id StorageID) (Storable, error) { } switch tagNumber { - case CBORTagStorageID: - return DecodeStorageIDStorable(dec) + case CBORTagSlabID: + return DecodeSlabIDStorable(dec) case cborTagUInt8Value: n, err := dec.DecodeUint64() diff --git a/storage.go b/storage.go index 48e3e4af..d36d0237 100644 --- a/storage.go +++ b/storage.go @@ -31,94 +31,106 @@ import ( const LedgerBaseStorageSlabPrefix = "$" -type ID [16]byte +// ValueID identifies Array and OrderedMap. +type ValueID [16]byte type ( - Address [8]byte - StorageIndex [8]byte + Address [8]byte + SlabIndex [8]byte - StorageID struct { - Address Address - Index StorageIndex + // SlabID identifies slab in storage. + // SlabID should only be used to retrieve, + // store, and remove slab in storage. + SlabID struct { + address Address + index SlabIndex } ) var ( - AddressUndefined = Address{} - StorageIndexUndefined = StorageIndex{} - StorageIDUndefined = StorageID{} + AddressUndefined = Address{} + SlabIndexUndefined = SlabIndex{} + SlabIDUndefined = SlabID{} ) -// Next returns new StorageIndex with index+1 value. +// Next returns new SlabIndex with index+1 value. // The caller is responsible for preventing overflow // by checking if the index value is valid before // calling this function. -func (index StorageIndex) Next() StorageIndex { +func (index SlabIndex) Next() SlabIndex { i := binary.BigEndian.Uint64(index[:]) - var next StorageIndex + var next SlabIndex binary.BigEndian.PutUint64(next[:], i+1) return next } -func NewStorageID(address Address, index StorageIndex) StorageID { - return StorageID{address, index} +func NewSlabID(address Address, index SlabIndex) SlabID { + return SlabID{address, index} } -func NewStorageIDFromRawBytes(b []byte) (StorageID, error) { - if len(b) < storageIDSize { - return StorageID{}, NewStorageIDErrorf("incorrect storage id buffer length %d", len(b)) +func NewSlabIDFromRawBytes(b []byte) (SlabID, error) { + if len(b) < slabIDSize { + return SlabID{}, NewSlabIDErrorf("incorrect slab ID buffer length %d", len(b)) } var address Address copy(address[:], b) - var index StorageIndex + var index SlabIndex copy(index[:], b[8:]) - return StorageID{address, index}, nil + return SlabID{address, index}, nil } -func (id StorageID) ToRawBytes(b []byte) (int, error) { - if len(b) < storageIDSize { - return 0, NewStorageIDErrorf("incorrect storage id buffer length %d", len(b)) +func (id SlabID) ToRawBytes(b []byte) (int, error) { + if len(b) < slabIDSize { + return 0, NewSlabIDErrorf("incorrect slab ID buffer length %d", len(b)) } - copy(b, id.Address[:]) - copy(b[8:], id.Index[:]) - return storageIDSize, nil + copy(b, id.address[:]) + copy(b[8:], id.index[:]) + return slabIDSize, nil } -func (id StorageID) String() string { +func (id SlabID) String() string { return fmt.Sprintf( "0x%x.%d", - binary.BigEndian.Uint64(id.Address[:]), - binary.BigEndian.Uint64(id.Index[:]), + binary.BigEndian.Uint64(id.address[:]), + binary.BigEndian.Uint64(id.index[:]), ) } -func (id StorageID) AddressAsUint64() uint64 { - return binary.BigEndian.Uint64(id.Address[:]) +func (id SlabID) AddressAsUint64() uint64 { + return binary.BigEndian.Uint64(id.address[:]) } -func (id StorageID) IndexAsUint64() uint64 { - return binary.BigEndian.Uint64(id.Index[:]) +func (id SlabID) IndexAsUint64() uint64 { + return binary.BigEndian.Uint64(id.index[:]) } -func (id StorageID) Valid() error { - if id == StorageIDUndefined { - return NewStorageIDError("undefined storage id") +func (id SlabID) HasTempAddress() bool { + return id.address == AddressUndefined +} + +func (id SlabID) Index() SlabIndex { + return id.index +} + +func (id SlabID) Valid() error { + if id == SlabIDUndefined { + return NewSlabIDError("undefined slab ID") } - if id.Index == StorageIndexUndefined { - return NewStorageIDError("undefined storage index") + if id.index == SlabIndexUndefined { + return NewSlabIDError("undefined slab index") } return nil } -func (id StorageID) Compare(other StorageID) int { - result := bytes.Compare(id.Address[:], other.Address[:]) +func (id SlabID) Compare(other SlabID) int { + result := bytes.Compare(id.address[:], other.address[:]) if result == 0 { - return bytes.Compare(id.Index[:], other.Index[:]) + return bytes.Compare(id.index[:], other.index[:]) } return result } @@ -133,10 +145,10 @@ type BaseStorageUsageReporter interface { } type BaseStorage interface { - Store(StorageID, []byte) error - Retrieve(StorageID) ([]byte, bool, error) - Remove(StorageID) error - GenerateStorageID(Address) (StorageID, error) + Store(SlabID, []byte) error + Retrieve(SlabID) ([]byte, bool, error) + Remove(SlabID) error + GenerateSlabID(Address) (SlabID, error) SegmentCounts() int // number of segments stored in the storage Size() int // total byte size stored BaseStorageUsageReporter @@ -149,8 +161,8 @@ type Ledger interface { SetValue(owner, key, value []byte) (err error) // ValueExists returns true if the given key exists in the storage, owned by the given account. ValueExists(owner, key []byte) (exists bool, err error) - // AllocateStorageIndex allocates a new storage index under the given account. - AllocateStorageIndex(owner []byte) (StorageIndex, error) + // AllocateSlabIndex allocates a new slab index under the given account. + AllocateSlabIndex(owner []byte) (SlabIndex, error) } type LedgerBaseStorage struct { @@ -169,8 +181,8 @@ func NewLedgerBaseStorage(ledger Ledger) *LedgerBaseStorage { } } -func (s *LedgerBaseStorage) Retrieve(id StorageID) ([]byte, bool, error) { - v, err := s.ledger.GetValue(id.Address[:], SlabIndexToLedgerKey(id.Index)) +func (s *LedgerBaseStorage) Retrieve(id SlabID) ([]byte, bool, error) { + v, err := s.ledger.GetValue(id.address[:], SlabIndexToLedgerKey(id.index)) s.bytesRetrieved += len(v) if err != nil { @@ -181,9 +193,9 @@ func (s *LedgerBaseStorage) Retrieve(id StorageID) ([]byte, bool, error) { return v, len(v) > 0, nil } -func (s *LedgerBaseStorage) Store(id StorageID, data []byte) error { +func (s *LedgerBaseStorage) Store(id SlabID, data []byte) error { s.bytesStored += len(data) - err := s.ledger.SetValue(id.Address[:], SlabIndexToLedgerKey(id.Index), data) + err := s.ledger.SetValue(id.address[:], SlabIndexToLedgerKey(id.index), data) if err != nil { // Wrap err as external error (if needed) because err is returned by Ledger interface. @@ -193,8 +205,8 @@ func (s *LedgerBaseStorage) Store(id StorageID, data []byte) error { return nil } -func (s *LedgerBaseStorage) Remove(id StorageID) error { - err := s.ledger.SetValue(id.Address[:], SlabIndexToLedgerKey(id.Index), nil) +func (s *LedgerBaseStorage) Remove(id SlabID) error { + err := s.ledger.SetValue(id.address[:], SlabIndexToLedgerKey(id.index), nil) if err != nil { // Wrap err as external error (if needed) because err is returned by Ledger interface. @@ -204,22 +216,22 @@ func (s *LedgerBaseStorage) Remove(id StorageID) error { return nil } -func (s *LedgerBaseStorage) GenerateStorageID(address Address) (StorageID, error) { - idx, err := s.ledger.AllocateStorageIndex(address[:]) +func (s *LedgerBaseStorage) GenerateSlabID(address Address) (SlabID, error) { + idx, err := s.ledger.AllocateSlabIndex(address[:]) if err != nil { // Wrap err as external error (if needed) because err is returned by Ledger interface. - return StorageID{}, + return SlabID{}, wrapErrorfAsExternalErrorIfNeeded( err, - fmt.Sprintf("failed to generate storage ID with address 0x%x", address), + fmt.Sprintf("failed to generate slab ID with address 0x%x", address), ) } - return NewStorageID(address, idx), nil + return NewSlabID(address, idx), nil } -func SlabIndexToLedgerKey(ind StorageIndex) []byte { +func SlabIndexToLedgerKey(ind SlabIndex) []byte { return []byte(LedgerBaseStorageSlabPrefix + string(ind[:])) } @@ -265,20 +277,20 @@ func (s *LedgerBaseStorage) ResetReporter() { s.bytesRetrieved = 0 } -type SlabIterator func() (StorageID, Slab) +type SlabIterator func() (SlabID, Slab) type SlabStorage interface { - Store(StorageID, Slab) error - Retrieve(StorageID) (Slab, bool, error) - Remove(StorageID) error - GenerateStorageID(address Address) (StorageID, error) + Store(SlabID, Slab) error + Retrieve(SlabID) (Slab, bool, error) + Remove(SlabID) error + GenerateSlabID(address Address) (SlabID, error) Count() int SlabIterator() (SlabIterator, error) } type BasicSlabStorage struct { - Slabs map[StorageID]Slab - storageIndex map[Address]StorageIndex + Slabs map[SlabID]Slab + slabIndex map[Address]SlabIndex DecodeStorable StorableDecoder DecodeTypeInfo TypeInfoDecoder cborEncMode cbor.EncMode @@ -294,8 +306,8 @@ func NewBasicSlabStorage( decodeTypeInfo TypeInfoDecoder, ) *BasicSlabStorage { return &BasicSlabStorage{ - Slabs: make(map[StorageID]Slab), - storageIndex: make(map[Address]StorageIndex), + Slabs: make(map[SlabID]Slab), + slabIndex: make(map[Address]SlabIndex), cborEncMode: cborEncMode, cborDecMode: cborDecMode, DecodeStorable: decodeStorable, @@ -303,25 +315,25 @@ func NewBasicSlabStorage( } } -func (s *BasicSlabStorage) GenerateStorageID(address Address) (StorageID, error) { - index := s.storageIndex[address] +func (s *BasicSlabStorage) GenerateSlabID(address Address) (SlabID, error) { + index := s.slabIndex[address] nextIndex := index.Next() - s.storageIndex[address] = nextIndex - return NewStorageID(address, nextIndex), nil + s.slabIndex[address] = nextIndex + return NewSlabID(address, nextIndex), nil } -func (s *BasicSlabStorage) Retrieve(id StorageID) (Slab, bool, error) { +func (s *BasicSlabStorage) Retrieve(id SlabID) (Slab, bool, error) { slab, ok := s.Slabs[id] return slab, ok, nil } -func (s *BasicSlabStorage) Store(id StorageID, slab Slab) error { +func (s *BasicSlabStorage) Store(id SlabID, slab Slab) error { s.Slabs[id] = slab return nil } -func (s *BasicSlabStorage) Remove(id StorageID) error { +func (s *BasicSlabStorage) Remove(id SlabID) error { delete(s.Slabs, id) return nil } @@ -330,18 +342,18 @@ func (s *BasicSlabStorage) Count() int { return len(s.Slabs) } -func (s *BasicSlabStorage) StorageIDs() []StorageID { - result := make([]StorageID, 0, len(s.Slabs)) - for storageID := range s.Slabs { - result = append(result, storageID) +func (s *BasicSlabStorage) SlabIDs() []SlabID { + result := make([]SlabID, 0, len(s.Slabs)) + for slabID := range s.Slabs { + result = append(result, slabID) } return result } // Encode returns serialized slabs in storage. // This is currently used for testing. -func (s *BasicSlabStorage) Encode() (map[StorageID][]byte, error) { - m := make(map[StorageID][]byte) +func (s *BasicSlabStorage) Encode() (map[SlabID][]byte, error) { + m := make(map[SlabID][]byte) for id, slab := range s.Slabs { b, err := Encode(slab, s.cborEncMode) if err != nil { @@ -355,7 +367,7 @@ func (s *BasicSlabStorage) Encode() (map[StorageID][]byte, error) { func (s *BasicSlabStorage) SlabIterator() (SlabIterator, error) { type slabEntry struct { - StorageID + SlabID Slab } @@ -367,32 +379,32 @@ func (s *BasicSlabStorage) SlabIterator() (SlabIterator, error) { for id, slab := range s.Slabs { slabs = append(slabs, slabEntry{ - StorageID: id, - Slab: slab, + SlabID: id, + Slab: slab, }) } var i int - return func() (StorageID, Slab) { + return func() (SlabID, Slab) { if i >= len(slabs) { - return StorageIDUndefined, nil + return SlabIDUndefined, nil } slabEntry := slabs[i] i++ - return slabEntry.StorageID, slabEntry.Slab + return slabEntry.SlabID, slabEntry.Slab }, nil } // CheckStorageHealth checks for the health of slab storage. // It traverses the slabs and checks these factors: // - All non-root slabs only has a single parent reference (no double referencing) -// - Every child of a parent shares the same ownership (childStorageID.Address == parentStorageID.Address) +// - Every child of a parent shares the same ownership (childSlabID.Address == parentSlabID.Address) // - The number of root slabs are equal to the expected number (skipped if expectedNumberOfRootSlabs is -1) // This should be used for testing purposes only, as it might be slow to process -func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map[StorageID]struct{}, error) { - parentOf := make(map[StorageID]StorageID) - leaves := make([]StorageID, 0) +func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map[SlabID]struct{}, error) { + parentOf := make(map[SlabID]SlabID) + leaves := make([]SlabID, 0) slabIterator, err := storage.SlabIterator() if err != nil { @@ -400,11 +412,11 @@ func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to create slab iterator") } - slabs := map[StorageID]Slab{} + slabs := map[SlabID]Slab{} for { id, slab := slabIterator() - if id == StorageIDUndefined { + if id == SlabIDUndefined { break } @@ -422,8 +434,8 @@ func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map for _, s := range childStorables { - if sids, ok := s.(StorageIDStorable); ok { - sid := StorageID(sids) + if sids, ok := s.(SlabIDStorable); ok { + sid := SlabID(sids) if _, found := parentOf[sid]; found { return nil, NewFatalError(fmt.Errorf("two parents are captured for the slab %s", sid)) } @@ -442,9 +454,9 @@ func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map } } - rootsMap := make(map[StorageID]struct{}) - visited := make(map[StorageID]struct{}) - var id StorageID + rootsMap := make(map[SlabID]struct{}) + visited := make(map[SlabID]struct{}) + var id SlabID for _, leaf := range leaves { id = leaf if _, ok := visited[id]; ok { @@ -478,8 +490,8 @@ func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to retrieve parent slab %s", parentID)) } - childOwner := childSlab.ID().Address - parentOwner := parentSlab.ID().Address + childOwner := childSlab.SlabID().address + parentOwner := parentSlab.SlabID().address if childOwner != parentOwner { return nil, NewFatalError( @@ -495,7 +507,7 @@ func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map if len(visited) != len(slabs) { - var unreachableID StorageID + var unreachableID SlabID var unreachableSlab Slab for id, slab := range slabs { @@ -527,14 +539,14 @@ func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map } type PersistentSlabStorage struct { - baseStorage BaseStorage - cache map[StorageID]Slab - deltas map[StorageID]Slab - tempStorageIndex uint64 - DecodeStorable StorableDecoder - DecodeTypeInfo TypeInfoDecoder - cborEncMode cbor.EncMode - cborDecMode cbor.DecMode + baseStorage BaseStorage + cache map[SlabID]Slab + deltas map[SlabID]Slab + tempSlabIndex uint64 + DecodeStorable StorableDecoder + DecodeTypeInfo TypeInfoDecoder + cborEncMode cbor.EncMode + cborDecMode cbor.DecMode } var _ SlabStorage = &PersistentSlabStorage{} @@ -542,7 +554,7 @@ var _ SlabStorage = &PersistentSlabStorage{} func (s *PersistentSlabStorage) SlabIterator() (SlabIterator, error) { var slabs []struct { - StorageID + SlabID Slab } @@ -555,12 +567,12 @@ func (s *PersistentSlabStorage) SlabIterator() (SlabIterator, error) { for _, childStorable := range childStorables { - storageIDStorable, ok := childStorable.(StorageIDStorable) + slabIDStorable, ok := childStorable.(SlabIDStorable) if !ok { continue } - id := StorageID(storageIDStorable) + id := SlabID(slabIDStorable) if _, ok := s.deltas[id]; ok { continue @@ -580,11 +592,11 @@ func (s *PersistentSlabStorage) SlabIterator() (SlabIterator, error) { } slabs = append(slabs, struct { - StorageID + SlabID Slab }{ - StorageID: id, - Slab: slab, + SlabID: id, + Slab: slab, }) nextChildStorables = append( @@ -599,13 +611,13 @@ func (s *PersistentSlabStorage) SlabIterator() (SlabIterator, error) { return nil } - appendSlab := func(id StorageID, slab Slab) error { + appendSlab := func(id SlabID, slab Slab) error { slabs = append(slabs, struct { - StorageID + SlabID Slab }{ - StorageID: id, - Slab: slab, + SlabID: id, + Slab: slab, }) return appendChildStorables(slab) @@ -625,7 +637,7 @@ func (s *PersistentSlabStorage) SlabIterator() (SlabIterator, error) { // Create a temporary copy of all the cached IDs, // as s.cache will get mutated inside the for-loop - cached := make([]StorageID, 0, len(s.cache)) + cached := make([]SlabID, 0, len(s.cache)) for id := range s.cache { cached = append(cached, id) } @@ -649,13 +661,13 @@ func (s *PersistentSlabStorage) SlabIterator() (SlabIterator, error) { var i int - return func() (StorageID, Slab) { + return func() (SlabID, Slab) { if i >= len(slabs) { - return StorageIDUndefined, nil + return SlabIDUndefined, nil } slabEntry := slabs[i] i++ - return slabEntry.StorageID, slabEntry.Slab + return slabEntry.SlabID, slabEntry.Slab }, nil } @@ -669,9 +681,10 @@ func NewPersistentSlabStorage( decodeTypeInfo TypeInfoDecoder, opts ...StorageOption, ) *PersistentSlabStorage { - storage := &PersistentSlabStorage{baseStorage: base, - cache: make(map[StorageID]Slab), - deltas: make(map[StorageID]Slab), + storage := &PersistentSlabStorage{ + baseStorage: base, + cache: make(map[SlabID]Slab), + deltas: make(map[SlabID]Slab), cborEncMode: cborEncMode, cborDecMode: cborDecMode, DecodeStorable: decodeStorable, @@ -685,26 +698,26 @@ func NewPersistentSlabStorage( return storage } -func (s *PersistentSlabStorage) GenerateStorageID(address Address) (StorageID, error) { +func (s *PersistentSlabStorage) GenerateSlabID(address Address) (SlabID, error) { if address == AddressUndefined { - var idx StorageIndex - s.tempStorageIndex++ - binary.BigEndian.PutUint64(idx[:], s.tempStorageIndex) - return NewStorageID(address, idx), nil + var idx SlabIndex + s.tempSlabIndex++ + binary.BigEndian.PutUint64(idx[:], s.tempSlabIndex) + return NewSlabID(address, idx), nil } - id, err := s.baseStorage.GenerateStorageID(address) + id, err := s.baseStorage.GenerateSlabID(address) if err != nil { // Wrap err as external error (if needed) because err is returned by BaseStorage interface. - return StorageID{}, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate storage ID for address 0x%x", address)) + return SlabID{}, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", address)) } return id, nil } -func (s *PersistentSlabStorage) sortedOwnedDeltaKeys() []StorageID { - keysWithOwners := make([]StorageID, 0, len(s.deltas)) +func (s *PersistentSlabStorage) sortedOwnedDeltaKeys() []SlabID { + keysWithOwners := make([]SlabID, 0, len(s.deltas)) for k := range s.deltas { // ignore the ones that are not owned by accounts - if k.Address != AddressUndefined { + if k.address != AddressUndefined { keysWithOwners = append(keysWithOwners, k) } } @@ -712,7 +725,7 @@ func (s *PersistentSlabStorage) sortedOwnedDeltaKeys() []StorageID { sort.Slice(keysWithOwners, func(i, j int) bool { a := keysWithOwners[i] b := keysWithOwners[j] - if a.Address == b.Address { + if a.address == b.address { return a.IndexAsUint64() < b.IndexAsUint64() } return a.AddressAsUint64() < b.AddressAsUint64() @@ -786,16 +799,16 @@ func (s *PersistentSlabStorage) FastCommit(numWorkers int) error { } // construct job queue - jobs := make(chan StorageID, len(keysWithOwners)) + jobs := make(chan SlabID, len(keysWithOwners)) for _, id := range keysWithOwners { jobs <- id } close(jobs) type encodedSlabs struct { - storageID StorageID - data []byte - err error + slabID SlabID + data []byte + err error } // construct result queue @@ -803,7 +816,7 @@ func (s *PersistentSlabStorage) FastCommit(numWorkers int) error { // define encoders (workers) and launch them // encoders encodes slabs in parallel - encoder := func(wg *sync.WaitGroup, done <-chan struct{}, jobs <-chan StorageID, results chan<- *encodedSlabs) { + encoder := func(wg *sync.WaitGroup, done <-chan struct{}, jobs <-chan SlabID, results chan<- *encodedSlabs) { defer wg.Done() for id := range jobs { @@ -817,18 +830,18 @@ func (s *PersistentSlabStorage) FastCommit(numWorkers int) error { slab := s.deltas[id] if slab == nil { results <- &encodedSlabs{ - storageID: id, - data: nil, - err: nil, + slabID: id, + data: nil, + err: nil, } continue } // serialize data, err := Encode(slab, s.cborEncMode) results <- &encodedSlabs{ - storageID: id, - data: data, - err: err, + slabID: id, + data: data, + err: err, } } } @@ -855,7 +868,7 @@ func (s *PersistentSlabStorage) FastCommit(numWorkers int) error { // process the results while encoders are working // we need to capture them inside a map // again so we can apply them in order of keys - encSlabByID := make(map[StorageID][]byte) + encSlabByID := make(map[SlabID][]byte) for i := 0; i < len(keysWithOwners); i++ { result := <-results // if any error return @@ -865,7 +878,7 @@ func (s *PersistentSlabStorage) FastCommit(numWorkers int) error { // result.err is already categorized by Encode(). return result.err } - encSlabByID[result.storageID] = result.data + encSlabByID[result.slabID] = result.data } // at this stage all results has been processed @@ -909,14 +922,14 @@ func (s *PersistentSlabStorage) FastCommit(numWorkers int) error { } func (s *PersistentSlabStorage) DropDeltas() { - s.deltas = make(map[StorageID]Slab) + s.deltas = make(map[SlabID]Slab) } func (s *PersistentSlabStorage) DropCache() { - s.cache = make(map[StorageID]Slab) + s.cache = make(map[SlabID]Slab) } -func (s *PersistentSlabStorage) RetrieveIgnoringDeltas(id StorageID) (Slab, bool, error) { +func (s *PersistentSlabStorage) RetrieveIgnoringDeltas(id SlabID) (Slab, bool, error) { // check the read cache next if slab, ok := s.cache[id]; ok { @@ -945,7 +958,7 @@ func (s *PersistentSlabStorage) RetrieveIgnoringDeltas(id StorageID) (Slab, bool return slab, ok, nil } -func (s *PersistentSlabStorage) Retrieve(id StorageID) (Slab, bool, error) { +func (s *PersistentSlabStorage) Retrieve(id SlabID) (Slab, bool, error) { // check deltas first if slab, ok := s.deltas[id]; ok { return slab, slab != nil, nil @@ -955,13 +968,13 @@ func (s *PersistentSlabStorage) Retrieve(id StorageID) (Slab, bool, error) { return s.RetrieveIgnoringDeltas(id) } -func (s *PersistentSlabStorage) Store(id StorageID, slab Slab) error { +func (s *PersistentSlabStorage) Store(id SlabID, slab Slab) error { // add to deltas s.deltas[id] = slab return nil } -func (s *PersistentSlabStorage) Remove(id StorageID) error { +func (s *PersistentSlabStorage) Remove(id SlabID) error { // add to nil to deltas under that id s.deltas[id] = nil return nil @@ -982,7 +995,7 @@ func (s *PersistentSlabStorage) DeltasWithoutTempAddresses() uint { deltas := uint(0) for k := range s.deltas { // exclude the ones that are not owned by accounts - if k.Address != AddressUndefined { + if k.address != AddressUndefined { deltas++ } } @@ -994,7 +1007,7 @@ func (s *PersistentSlabStorage) DeltasSizeWithoutTempAddresses() uint64 { size := uint64(0) for k, slab := range s.deltas { // Exclude slabs that are not owned by accounts. - if k.Address == AddressUndefined || slab == nil { + if k.address == AddressUndefined || slab == nil { continue } diff --git a/storage_test.go b/storage_test.go index d91cc557..413bbbeb 100644 --- a/storage_test.go +++ b/storage_test.go @@ -30,214 +30,217 @@ import ( ) func TestStorageIndexNext(t *testing.T) { - index := StorageIndex{0, 0, 0, 0, 0, 0, 0, 1} - want := StorageIndex{0, 0, 0, 0, 0, 0, 0, 2} + index := SlabIndex{0, 0, 0, 0, 0, 0, 0, 1} + want := SlabIndex{0, 0, 0, 0, 0, 0, 0, 2} require.Equal(t, want, index.Next()) } -func TestNewStorageID(t *testing.T) { +func TestNewSlabID(t *testing.T) { t.Run("temp address", func(t *testing.T) { - want := StorageID{Address: Address{}, Index: StorageIndex{1}} - require.Equal(t, want, NewStorageID(Address{}, StorageIndex{1})) + want := SlabID{address: Address{}, index: SlabIndex{1}} + require.Equal(t, want, NewSlabID(Address{}, SlabIndex{1})) }) t.Run("perm address", func(t *testing.T) { - want := StorageID{Address: Address{1}, Index: StorageIndex{1}} - require.Equal(t, want, NewStorageID(Address{1}, StorageIndex{1})) + want := SlabID{address: Address{1}, index: SlabIndex{1}} + require.Equal(t, want, NewSlabID(Address{1}, SlabIndex{1})) }) } -func TestNewStorageIDFromRawBytes(t *testing.T) { - t.Run("data length < storage id size", func(t *testing.T) { +func TestNewSlabIDFromRawBytes(t *testing.T) { + t.Run("data length < slab id size", func(t *testing.T) { var fatalError *FatalError - var storageIDError *StorageIDError + var slabIDError *SlabIDError - id, err := NewStorageIDFromRawBytes(nil) - require.Equal(t, StorageIDUndefined, id) + id, err := NewSlabIDFromRawBytes(nil) + require.Equal(t, SlabIDUndefined, id) require.Equal(t, 1, errorCategorizationCount(err)) require.ErrorAs(t, err, &fatalError) - require.ErrorAs(t, err, &storageIDError) - require.ErrorAs(t, fatalError, &storageIDError) + require.ErrorAs(t, err, &slabIDError) + require.ErrorAs(t, fatalError, &slabIDError) - id, err = NewStorageIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2}) - require.Equal(t, StorageIDUndefined, id) + id, err = NewSlabIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2}) + require.Equal(t, SlabIDUndefined, id) require.Equal(t, 1, errorCategorizationCount(err)) require.ErrorAs(t, err, &fatalError) - require.ErrorAs(t, err, &storageIDError) - require.ErrorAs(t, fatalError, &storageIDError) + require.ErrorAs(t, err, &slabIDError) + require.ErrorAs(t, fatalError, &slabIDError) }) - t.Run("data length == storage id size", func(t *testing.T) { - id, err := NewStorageIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2}) + t.Run("data length == slab id size", func(t *testing.T) { + id, err := NewSlabIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2}) - want := StorageID{ - Address: Address{0, 0, 0, 0, 0, 0, 0, 1}, - Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}, + want := SlabID{ + address: Address{0, 0, 0, 0, 0, 0, 0, 1}, + index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}, } require.Equal(t, want, id) require.NoError(t, err) }) - t.Run("data length > storage id size", func(t *testing.T) { - id, err := NewStorageIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 3, 4, 5, 6, 7, 8}) + t.Run("data length > slab id size", func(t *testing.T) { + id, err := NewSlabIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 3, 4, 5, 6, 7, 8}) - want := StorageID{ - Address: Address{0, 0, 0, 0, 0, 0, 0, 1}, - Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}, + want := SlabID{ + address: Address{0, 0, 0, 0, 0, 0, 0, 1}, + index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}, } require.Equal(t, want, id) require.NoError(t, err) }) } -func TestStorageIDToRawBytes(t *testing.T) { +func TestSlabIDToRawBytes(t *testing.T) { t.Run("buffer nil", func(t *testing.T) { var fatalError *FatalError - var storageIDError *StorageIDError + var slabIDError *SlabIDError - size, err := StorageIDUndefined.ToRawBytes(nil) + size, err := SlabIDUndefined.ToRawBytes(nil) require.Equal(t, 0, size) require.Equal(t, 1, errorCategorizationCount(err)) require.ErrorAs(t, err, &fatalError) - require.ErrorAs(t, err, &storageIDError) - require.ErrorAs(t, fatalError, &storageIDError) + require.ErrorAs(t, err, &slabIDError) + require.ErrorAs(t, fatalError, &slabIDError) }) t.Run("buffer too short", func(t *testing.T) { var fatalError *FatalError - var storageIDError *StorageIDError + var slabIDError *SlabIDError b := make([]byte, 8) - size, err := StorageIDUndefined.ToRawBytes(b) + size, err := SlabIDUndefined.ToRawBytes(b) require.Equal(t, 0, size) require.Equal(t, 1, errorCategorizationCount(err)) require.ErrorAs(t, err, &fatalError) - require.ErrorAs(t, err, &storageIDError) - require.ErrorAs(t, fatalError, &storageIDError) + require.ErrorAs(t, err, &slabIDError) + require.ErrorAs(t, fatalError, &slabIDError) }) t.Run("undefined", func(t *testing.T) { - b := make([]byte, storageIDSize) - size, err := StorageIDUndefined.ToRawBytes(b) + b := make([]byte, slabIDSize) + size, err := SlabIDUndefined.ToRawBytes(b) require.NoError(t, err) want := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} require.Equal(t, want, b) - require.Equal(t, storageIDSize, size) + require.Equal(t, slabIDSize, size) }) t.Run("temp address", func(t *testing.T) { - id := NewStorageID(Address{0, 0, 0, 0, 0, 0, 0, 0}, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}) - b := make([]byte, storageIDSize) + id := NewSlabID(Address{0, 0, 0, 0, 0, 0, 0, 0}, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}) + b := make([]byte, slabIDSize) size, err := id.ToRawBytes(b) require.NoError(t, err) want := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} require.Equal(t, want, b) - require.Equal(t, storageIDSize, size) + require.Equal(t, slabIDSize, size) }) t.Run("temp index", func(t *testing.T) { - id := NewStorageID(Address{0, 0, 0, 0, 0, 0, 0, 1}, StorageIndex{0, 0, 0, 0, 0, 0, 0, 0}) - b := make([]byte, storageIDSize) + id := NewSlabID(Address{0, 0, 0, 0, 0, 0, 0, 1}, SlabIndex{0, 0, 0, 0, 0, 0, 0, 0}) + b := make([]byte, slabIDSize) size, err := id.ToRawBytes(b) require.NoError(t, err) want := []byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0} require.Equal(t, want, b) - require.Equal(t, storageIDSize, size) + require.Equal(t, slabIDSize, size) }) t.Run("perm", func(t *testing.T) { - id := NewStorageID(Address{0, 0, 0, 0, 0, 0, 0, 1}, StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}) - b := make([]byte, storageIDSize) + id := NewSlabID(Address{0, 0, 0, 0, 0, 0, 0, 1}, SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}) + b := make([]byte, slabIDSize) size, err := id.ToRawBytes(b) require.NoError(t, err) want := []byte{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2} require.Equal(t, want, b) - require.Equal(t, storageIDSize, size) + require.Equal(t, slabIDSize, size) }) } -func TestStorageIDAddressAsUint64(t *testing.T) { +func TestSlabIDAddressAsUint64(t *testing.T) { t.Run("temp", func(t *testing.T) { - id := NewStorageID(Address{}, StorageIndex{1}) + id := NewSlabID(Address{}, SlabIndex{1}) require.Equal(t, uint64(0), id.AddressAsUint64()) }) t.Run("perm", func(t *testing.T) { - id := NewStorageID(Address{0, 0, 0, 0, 0, 0, 0, 1}, StorageIndex{1}) + id := NewSlabID(Address{0, 0, 0, 0, 0, 0, 0, 1}, SlabIndex{1}) require.Equal(t, uint64(1), id.AddressAsUint64()) }) } -func TestStorageIDIndexAsUint64(t *testing.T) { +func TestSlabIDIndexAsUint64(t *testing.T) { t.Run("temp", func(t *testing.T) { - id := NewStorageID(Address{}, StorageIndex{}) + id := NewSlabID(Address{}, SlabIndex{}) require.Equal(t, uint64(0), id.IndexAsUint64()) }) t.Run("perm", func(t *testing.T) { - id := NewStorageID(Address{}, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}) + id := NewSlabID(Address{}, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}) require.Equal(t, uint64(1), id.IndexAsUint64()) }) } -func TestStorageIDValid(t *testing.T) { +func TestSlabIDValid(t *testing.T) { t.Run("undefined", func(t *testing.T) { - id := StorageIDUndefined + id := SlabIDUndefined err := id.Valid() var fatalError *FatalError - var storageIDError *StorageIDError + var slabIDError *SlabIDError require.Equal(t, 1, errorCategorizationCount(err)) require.ErrorAs(t, err, &fatalError) - require.ErrorAs(t, err, &storageIDError) - require.ErrorAs(t, fatalError, &storageIDError) + require.ErrorAs(t, err, &slabIDError) + require.ErrorAs(t, fatalError, &slabIDError) }) + t.Run("temp index", func(t *testing.T) { - id := StorageID{Address: Address{1}, Index: StorageIndexUndefined} + id := SlabID{address: Address{1}, index: SlabIndexUndefined} err := id.Valid() var fatalError *FatalError - var storageIDError *StorageIDError + var slabIDError *SlabIDError require.Equal(t, 1, errorCategorizationCount(err)) require.ErrorAs(t, err, &fatalError) - require.ErrorAs(t, err, &storageIDError) - require.ErrorAs(t, fatalError, &storageIDError) + require.ErrorAs(t, err, &slabIDError) + require.ErrorAs(t, fatalError, &slabIDError) }) + t.Run("temp address", func(t *testing.T) { - id := StorageID{Address: AddressUndefined, Index: StorageIndex{1}} + id := SlabID{address: AddressUndefined, index: SlabIndex{1}} require.NoError(t, id.Valid()) }) + t.Run("valid", func(t *testing.T) { - id := StorageID{Address: Address{1}, Index: StorageIndex{2}} + id := SlabID{address: Address{1}, index: SlabIndex{2}} require.NoError(t, id.Valid()) }) } -func TestStorageIDCompare(t *testing.T) { +func TestSlabIDCompare(t *testing.T) { t.Run("same", func(t *testing.T) { - id1 := NewStorageID(Address{1}, StorageIndex{1}) - id2 := NewStorageID(Address{1}, StorageIndex{1}) + id1 := NewSlabID(Address{1}, SlabIndex{1}) + id2 := NewSlabID(Address{1}, SlabIndex{1}) require.Equal(t, 0, id1.Compare(id2)) require.Equal(t, 0, id2.Compare(id1)) }) t.Run("different address", func(t *testing.T) { - id1 := NewStorageID(Address{1}, StorageIndex{1}) - id2 := NewStorageID(Address{2}, StorageIndex{1}) + id1 := NewSlabID(Address{1}, SlabIndex{1}) + id2 := NewSlabID(Address{2}, SlabIndex{1}) require.Equal(t, -1, id1.Compare(id2)) require.Equal(t, 1, id2.Compare(id1)) }) t.Run("different index", func(t *testing.T) { - id1 := NewStorageID(Address{1}, StorageIndex{1}) - id2 := NewStorageID(Address{1}, StorageIndex{2}) + id1 := NewSlabID(Address{1}, SlabIndex{1}) + id2 := NewSlabID(Address{1}, SlabIndex{2}) require.Equal(t, -1, id1.Compare(id2)) require.Equal(t, 1, id2.Compare(id1)) }) t.Run("different address and index", func(t *testing.T) { - id1 := NewStorageID(Address{1}, StorageIndex{1}) - id2 := NewStorageID(Address{2}, StorageIndex{2}) + id1 := NewSlabID(Address{1}, SlabIndex{1}) + id2 := NewSlabID(Address{2}, SlabIndex{2}) require.Equal(t, -1, id1.Compare(id2)) require.Equal(t, 1, id2.Compare(id1)) }) @@ -248,9 +251,9 @@ func TestLedgerBaseStorageStore(t *testing.T) { baseStorage := NewLedgerBaseStorage(ledger) bytesStored := 0 - values := map[StorageID][]byte{ - {Address{1}, StorageIndex{1}}: {1, 2, 3}, - {Address{1}, StorageIndex{2}}: {4, 5, 6}, + values := map[SlabID][]byte{ + {Address{1}, SlabIndex{1}}: {1, 2, 3}, + {Address{1}, SlabIndex{2}}: {4, 5, 6}, } // Store values @@ -279,9 +282,9 @@ func TestLedgerBaseStorageStore(t *testing.T) { if owner == nil { break } - var id StorageID - copy(id.Address[:], owner) - copy(id.Index[:], key[1:]) + var id SlabID + copy(id.address[:], owner) + copy(id.index[:], key[1:]) require.True(t, LedgerKeyIsSlabKey(string(key))) require.Equal(t, values[id], value) @@ -295,7 +298,7 @@ func TestLedgerBaseStorageRetrieve(t *testing.T) { ledger := newTestLedger() baseStorage := NewLedgerBaseStorage(ledger) - id := StorageID{Address: Address{1}, Index: StorageIndex{1}} + id := SlabID{address: Address{1}, index: SlabIndex{1}} value := []byte{1, 2, 3} bytesStored := 0 bytesRetrieved := 0 @@ -318,7 +321,7 @@ func TestLedgerBaseStorageRetrieve(t *testing.T) { require.Equal(t, value, b) // Retrieve non-existent value - id = StorageID{Address: Address{1}, Index: StorageIndex{2}} + id = SlabID{address: Address{1}, index: SlabIndex{2}} b, found, err = baseStorage.Retrieve(id) require.NoError(t, err) require.False(t, found) @@ -332,7 +335,7 @@ func TestLedgerBaseStorageRemove(t *testing.T) { ledger := newTestLedger() baseStorage := NewLedgerBaseStorage(ledger) - id := StorageID{Address: Address{1}, Index: StorageIndex{1}} + id := SlabID{address: Address{1}, index: SlabIndex{1}} value := []byte{1, 2, 3} // Remove value from empty storage @@ -351,7 +354,7 @@ func TestLedgerBaseStorageRemove(t *testing.T) { require.NoError(t, err) // Remove non-existent value - err = baseStorage.Remove(StorageID{Address: id.Address, Index: id.Index.Next()}) + err = baseStorage.Remove(SlabID{address: id.address, index: id.index.Next()}) require.NoError(t, err) // Retrieve removed value @@ -368,9 +371,9 @@ func TestLedgerBaseStorageRemove(t *testing.T) { if owner == nil { break } - var id StorageID - copy(id.Address[:], owner) - copy(id.Index[:], key[1:]) + var id SlabID + copy(id.address[:], owner) + copy(id.index[:], key[1:]) require.True(t, LedgerKeyIsSlabKey(string(key))) require.Nil(t, value) @@ -380,27 +383,27 @@ func TestLedgerBaseStorageRemove(t *testing.T) { require.Equal(t, 2, count) } -func TestLedgerBaseStorageGenerateStorageID(t *testing.T) { +func TestLedgerBaseStorageGenerateSlabID(t *testing.T) { ledger := newTestLedger() baseStorage := NewLedgerBaseStorage(ledger) address1 := Address{1} address2 := Address{2} - id, err := baseStorage.GenerateStorageID(address1) + id, err := baseStorage.GenerateSlabID(address1) require.NoError(t, err) - require.Equal(t, address1, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.Index) + require.Equal(t, address1, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.index) - id, err = baseStorage.GenerateStorageID(address1) + id, err = baseStorage.GenerateSlabID(address1) require.NoError(t, err) - require.Equal(t, address1, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.Index) + require.Equal(t, address1, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.index) - id, err = baseStorage.GenerateStorageID(address2) + id, err = baseStorage.GenerateSlabID(address2) require.NoError(t, err) - require.Equal(t, address2, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.Index) + require.Equal(t, address2, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.index) } func TestBasicSlabStorageStore(t *testing.T) { @@ -408,9 +411,9 @@ func TestBasicSlabStorageStore(t *testing.T) { r := newRand(t) address := Address{1} - slabs := map[StorageID]Slab{ - {address, StorageIndex{1}}: generateRandomSlab(address, r), - {address, StorageIndex{2}}: generateRandomSlab(address, r), + slabs := map[SlabID]Slab{ + {address, SlabIndex{1}}: generateRandomSlab(address, r), + {address, SlabIndex{2}}: generateRandomSlab(address, r), } // Store values @@ -421,7 +424,7 @@ func TestBasicSlabStorageStore(t *testing.T) { // Overwrite stored values for id := range slabs { - slab := generateRandomSlab(id.Address, r) + slab := generateRandomSlab(id.address, r) slabs[id] = slab err := storage.Store(id, slab) require.NoError(t, err) @@ -442,8 +445,8 @@ func TestBasicSlabStorageRetrieve(t *testing.T) { storage := NewBasicSlabStorage(nil, nil, nil, nil) r := newRand(t) - id := StorageID{Address{1}, StorageIndex{1}} - slab := generateRandomSlab(id.Address, r) + id := SlabID{Address{1}, SlabIndex{1}} + slab := generateRandomSlab(id.address, r) // Retrieve value from empty storage retrievedSlab, found, err := storage.Retrieve(id) @@ -461,7 +464,7 @@ func TestBasicSlabStorageRetrieve(t *testing.T) { require.Equal(t, slab, retrievedSlab) // Retrieve non-existent value - id = StorageID{Address: Address{1}, Index: StorageIndex{2}} + id = SlabID{address: Address{1}, index: SlabIndex{2}} retrievedSlab, found, err = storage.Retrieve(id) require.NoError(t, err) require.False(t, found) @@ -472,8 +475,8 @@ func TestBasicSlabStorageRemove(t *testing.T) { storage := NewBasicSlabStorage(nil, nil, nil, nil) r := newRand(t) - id := StorageID{Address{1}, StorageIndex{1}} - slab := generateRandomSlab(id.Address, r) + id := SlabID{Address{1}, SlabIndex{1}} + slab := generateRandomSlab(id.address, r) // Remove value from empty storage err := storage.Remove(id) @@ -491,7 +494,7 @@ func TestBasicSlabStorageRemove(t *testing.T) { require.NoError(t, err) // Remove non-existent value - err = storage.Remove(StorageID{Address: id.Address, Index: id.Index.Next()}) + err = storage.Remove(SlabID{address: id.address, index: id.index.Next()}) require.NoError(t, err) // Retrieve removed value @@ -503,52 +506,52 @@ func TestBasicSlabStorageRemove(t *testing.T) { require.Equal(t, 0, storage.Count()) } -func TestBasicSlabStorageGenerateStorageID(t *testing.T) { +func TestBasicSlabStorageGenerateSlabID(t *testing.T) { storage := NewBasicSlabStorage(nil, nil, nil, nil) address1 := Address{1} address2 := Address{2} - id, err := storage.GenerateStorageID(address1) + id, err := storage.GenerateSlabID(address1) require.NoError(t, err) - require.Equal(t, address1, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.Index) + require.Equal(t, address1, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.index) - id, err = storage.GenerateStorageID(address1) + id, err = storage.GenerateSlabID(address1) require.NoError(t, err) - require.Equal(t, address1, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.Index) + require.Equal(t, address1, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.index) - id, err = storage.GenerateStorageID(address2) + id, err = storage.GenerateSlabID(address2) require.NoError(t, err) - require.Equal(t, address2, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.Index) + require.Equal(t, address2, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.index) } -func TestBasicSlabStorageStorageIDs(t *testing.T) { +func TestBasicSlabStorageSlabIDs(t *testing.T) { r := newRand(t) address := Address{1} - index := StorageIndex{0, 0, 0, 0, 0, 0, 0, 0} - wantIDs := map[StorageID]bool{ - {Address: address, Index: index.Next()}: true, - {Address: address, Index: index.Next()}: true, - {Address: address, Index: index.Next()}: true, + index := SlabIndex{0, 0, 0, 0, 0, 0, 0, 0} + wantIDs := map[SlabID]bool{ + {address: address, index: index.Next()}: true, + {address: address, index: index.Next()}: true, + {address: address, index: index.Next()}: true, } storage := NewBasicSlabStorage(nil, nil, nil, nil) - // Get storage ids from empty storgae - ids := storage.StorageIDs() + // Get slab ids from empty storgae + ids := storage.SlabIDs() require.Equal(t, 0, len(ids)) // Store values for id := range wantIDs { - err := storage.Store(id, generateRandomSlab(id.Address, r)) + err := storage.Store(id, generateRandomSlab(id.address, r)) require.NoError(t, err) } - // Get storage ids from non-empty storgae - ids = storage.StorageIDs() + // Get slab ids from non-empty storgae + ids = storage.SlabIDs() require.Equal(t, len(wantIDs), len(ids)) for _, id := range ids { @@ -559,16 +562,16 @@ func TestBasicSlabStorageStorageIDs(t *testing.T) { func TestBasicSlabStorageSlabIterat(t *testing.T) { r := newRand(t) address := Address{1} - index := StorageIndex{0, 0, 0, 0, 0, 0, 0, 0} + index := SlabIndex{0, 0, 0, 0, 0, 0, 0, 0} - id1 := StorageID{Address: address, Index: index.Next()} - id2 := StorageID{Address: address, Index: index.Next()} - id3 := StorageID{Address: address, Index: index.Next()} + id1 := SlabID{address: address, index: index.Next()} + id2 := SlabID{address: address, index: index.Next()} + id3 := SlabID{address: address, index: index.Next()} - want := map[StorageID]Slab{ - id1: generateRandomSlab(id1.Address, r), - id2: generateRandomSlab(id2.Address, r), - id3: generateRandomSlab(id3.Address, r), + want := map[SlabID]Slab{ + id1: generateRandomSlab(id1.address, r), + id2: generateRandomSlab(id2.address, r), + id3: generateRandomSlab(id3.address, r), } storage := NewBasicSlabStorage(nil, nil, nil, nil) @@ -585,7 +588,7 @@ func TestBasicSlabStorageSlabIterat(t *testing.T) { count := 0 for { id, slab := iterator() - if id == StorageIDUndefined { + if id == SlabIDUndefined { break } require.NotNil(t, want[id]) @@ -607,17 +610,17 @@ func TestPersistentStorage(t *testing.T) { baseStorage := NewInMemBaseStorage() storage := NewPersistentSlabStorage(baseStorage, encMode, decMode, nil, nil) - tempStorageID, err := NewStorageIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) + tempSlabID, err := NewSlabIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) require.NoError(t, err) - permStorageID, err := NewStorageIDFromRawBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) + permSlabID, err := NewSlabIDFromRawBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) require.NoError(t, err) - _, found, err := storage.Retrieve(tempStorageID) + _, found, err := storage.Retrieve(tempSlabID) require.NoError(t, err) require.False(t, found) - _, found, err = storage.Retrieve(permStorageID) + _, found, err = storage.Retrieve(permSlabID) require.NoError(t, err) require.False(t, found) @@ -633,21 +636,21 @@ func TestPersistentStorage(t *testing.T) { baseStorage := NewInMemBaseStorage() storage := NewPersistentSlabStorage(baseStorage, encMode, decMode, nil, nil) - tempStorageID, err := NewStorageIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) + tempSlabID, err := NewSlabIDFromRawBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) require.NoError(t, err) - permStorageID, err := NewStorageIDFromRawBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) + permSlabID, err := NewSlabIDFromRawBytes([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}) require.NoError(t, err) - slab1 := generateRandomSlab(tempStorageID.Address, r) - slab2 := generateRandomSlab(permStorageID.Address, r) + slab1 := generateRandomSlab(tempSlabID.address, r) + slab2 := generateRandomSlab(permSlabID.address, r) // no temp ids should be in the base storage - err = storage.Store(tempStorageID, slab1) + err = storage.Store(tempSlabID, slab1) require.NoError(t, err) require.Equal(t, uint64(0), storage.DeltasSizeWithoutTempAddresses()) - err = storage.Store(permStorageID, slab2) + err = storage.Store(permSlabID, slab2) require.NoError(t, err) require.Equal(t, uint(1), storage.DeltasWithoutTempAddresses()) @@ -662,32 +665,32 @@ func TestPersistentStorage(t *testing.T) { require.Equal(t, uint(1), storage.Deltas()) require.Equal(t, uint64(0), storage.DeltasSizeWithoutTempAddresses()) - // Slab with temp storage id is NOT persisted in base storage. - _, found, err := baseStorage.Retrieve(tempStorageID) + // Slab with temp slab id is NOT persisted in base storage. + _, found, err := baseStorage.Retrieve(tempSlabID) require.NoError(t, err) require.False(t, found) - // Slab with temp storage id is cached in storage. - _, found, err = storage.Retrieve(tempStorageID) + // Slab with temp slab id is cached in storage. + _, found, err = storage.Retrieve(tempSlabID) require.NoError(t, err) require.True(t, found) - // Slab with perm storage id is persisted in base storage. - _, found, err = baseStorage.Retrieve(permStorageID) + // Slab with perm slab id is persisted in base storage. + _, found, err = baseStorage.Retrieve(permSlabID) require.NoError(t, err) require.True(t, found) - // Slab with perm storage id is cached in storage. - _, found, err = storage.Retrieve(permStorageID) + // Slab with perm slab id is cached in storage. + _, found, err = storage.Retrieve(permSlabID) require.NoError(t, err) require.True(t, found) - // Remove slab with perm storage id - err = storage.Remove(permStorageID) + // Remove slab with perm slab id + err = storage.Remove(permSlabID) require.NoError(t, err) - // Remove slab with temp storage id - err = storage.Remove(tempStorageID) + // Remove slab with temp slab id + err = storage.Remove(tempSlabID) require.NoError(t, err) require.Equal(t, uint(1), storage.DeltasWithoutTempAddresses()) @@ -696,18 +699,18 @@ func TestPersistentStorage(t *testing.T) { err = storage.Commit() require.NoError(t, err) - // Slab with perm storage id is removed from base storage. - _, found, err = baseStorage.Retrieve(permStorageID) + // Slab with perm slab id is removed from base storage. + _, found, err = baseStorage.Retrieve(permSlabID) require.NoError(t, err) require.False(t, found) - // Slab with perm storage id is removed from cache in storage. - _, found, err = storage.Retrieve(permStorageID) + // Slab with perm slab id is removed from cache in storage. + _, found, err = storage.Retrieve(permSlabID) require.NoError(t, err) require.False(t, found) - // Slab with temp storage id is removed from cache in storage. - _, found, err = storage.Retrieve(tempStorageID) + // Slab with temp slab id is removed from cache in storage. + _, found, err = storage.Retrieve(tempSlabID) require.NoError(t, err) require.False(t, found) @@ -726,7 +729,7 @@ func TestPersistentStorage(t *testing.T) { baseStorage2 := newAccessOrderTrackerBaseStorage() storageWithFastCommit := NewPersistentSlabStorage(baseStorage2, encMode, decMode, nil, nil) - simpleMap := make(map[StorageID][]byte) + simpleMap := make(map[SlabID][]byte) slabSize := uint64(0) // test random updates apply commit and check the order of committed values for i := 0; i < numberOfAccounts; i++ { @@ -735,18 +738,18 @@ func TestPersistentStorage(t *testing.T) { slab := generateRandomSlab(addr, r) slabSize += uint64(slab.ByteSize()) - storageID, err := storage.GenerateStorageID(addr) + slabID, err := storage.GenerateSlabID(addr) require.NoError(t, err) - err = storage.Store(storageID, slab) + err = storage.Store(slabID, slab) require.NoError(t, err) - storageID2, err := storageWithFastCommit.GenerateStorageID(addr) + slabID2, err := storageWithFastCommit.GenerateSlabID(addr) require.NoError(t, err) - err = storageWithFastCommit.Store(storageID2, slab) + err = storageWithFastCommit.Store(slabID2, slab) require.NoError(t, err) // capture data for accuracy testing - simpleMap[storageID], err = Encode(slab, encMode) + simpleMap[slabID], err = Encode(slab, encMode) require.NoError(t, err) } } @@ -844,14 +847,14 @@ func TestPersistentStorage(t *testing.T) { address := Address{1} - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) require.NoError(t, err) err = storage.Store(id, slabWithNonStorable) require.NoError(t, err) for i := 0; i < 500; i++ { - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) require.NoError(t, err) err = storage.Store(id, slabWithSlowStorable) @@ -878,7 +881,7 @@ func TestPersistentStorageSlabIterator(t *testing.T) { count := 0 for { id, _ := iterator() - if id == StorageIDUndefined { + if id == SlabIDUndefined { break } count++ @@ -889,12 +892,12 @@ func TestPersistentStorageSlabIterator(t *testing.T) { t.Run("not-empty storage", func(t *testing.T) { address := Address{1, 2, 3, 4, 5, 6, 7, 8} - id1 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}} - id2 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}} - id3 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 3}} - id4 := StorageID{Address: address, Index: StorageIndex{0, 0, 0, 0, 0, 0, 0, 4}} + id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}} + id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}} + id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}} + id4 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}} - data := map[StorageID][]byte{ + data := map[SlabID][]byte{ // (metadata slab) headers: [{id:2 size:228 count:9} {id:3 size:270 count:11} ] id1: { // extra data @@ -913,7 +916,7 @@ func TestPersistentStorageSlabIterator(t *testing.T) { 0x81, // child header count 0x00, 0x02, - // child header 1 (storage id, count, size) + // child header 1 (slab id, count, size) 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0xe4, @@ -929,7 +932,7 @@ func TestPersistentStorageSlabIterator(t *testing.T) { 0x00, // array data slab flag 0x00, - // next storage id + // next slab id 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // CBOR encoded array head (fixed size 3 byte) 0x99, 0x00, 0x09, @@ -945,13 +948,13 @@ func TestPersistentStorageSlabIterator(t *testing.T) { 0x76, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, }, - // (data slab) next: 0, data: [aaaaaaaaaaaaaaaaaaaaaa ... StorageID(...)] + // (data slab) next: 0, data: [aaaaaaaaaaaaaaaaaaaaaa ... SlabID(...)] id3: { // version 0x00, // array data slab flag 0x40, - // next storage id + // next slab id 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // CBOR encoded array head (fixed size 3 byte) 0x99, 0x00, 0x0b, @@ -1003,7 +1006,7 @@ func TestPersistentStorageSlabIterator(t *testing.T) { count := 0 for { id, slab := iterator() - if id == StorageIDUndefined { + if id == SlabIDUndefined { break } @@ -1017,35 +1020,35 @@ func TestPersistentStorageSlabIterator(t *testing.T) { }) } -func TestPersistentStorageGenerateStorageID(t *testing.T) { +func TestPersistentStorageGenerateSlabID(t *testing.T) { baseStorage := NewInMemBaseStorage() storage := NewPersistentSlabStorage(baseStorage, nil, nil, nil, nil) t.Run("temp address", func(t *testing.T) { address := Address{} - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) require.NoError(t, err) - require.Equal(t, address, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.Index) + require.Equal(t, address, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.index) - id, err = storage.GenerateStorageID(address) + id, err = storage.GenerateSlabID(address) require.NoError(t, err) - require.Equal(t, address, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.Index) + require.Equal(t, address, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.index) }) t.Run("perm address", func(t *testing.T) { address := Address{1} - id, err := storage.GenerateStorageID(address) + id, err := storage.GenerateSlabID(address) require.NoError(t, err) - require.Equal(t, address, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.Index) + require.Equal(t, address, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}, id.index) - id, err = storage.GenerateStorageID(address) + id, err = storage.GenerateSlabID(address) require.NoError(t, err) - require.Equal(t, address, id.Address) - require.Equal(t, StorageIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.Index) + require.Equal(t, address, id.address) + require.Equal(t, SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}, id.index) }) } @@ -1054,9 +1057,9 @@ func generateRandomSlab(address Address, r *rand.Rand) Slab { return &ArrayDataSlab{ header: ArraySlabHeader{ - id: NewStorageID(address, StorageIndex{1}), - size: arrayRootDataSlabPrefixSize + storable.ByteSize(), - count: 1, + slabID: NewSlabID(address, SlabIndex{1}), + size: arrayRootDataSlabPrefixSize + storable.ByteSize(), + count: 1, }, elements: []Storable{storable}, } @@ -1069,51 +1072,51 @@ func generateRandomAddress(r *rand.Rand) Address { } type accessOrderTrackerBaseStorage struct { - segTouchOrder []StorageID + segTouchOrder []SlabID indexReqOrder []Address - segments map[StorageID][]byte - storageIndex map[Address]StorageIndex + segments map[SlabID][]byte + storageIndex map[Address]SlabIndex } func newAccessOrderTrackerBaseStorage() *accessOrderTrackerBaseStorage { return &accessOrderTrackerBaseStorage{ - segTouchOrder: make([]StorageID, 0), + segTouchOrder: make([]SlabID, 0), indexReqOrder: make([]Address, 0), - segments: make(map[StorageID][]byte), - storageIndex: make(map[Address]StorageIndex), + segments: make(map[SlabID][]byte), + storageIndex: make(map[Address]SlabIndex), } } -func (s *accessOrderTrackerBaseStorage) SegTouchOrder() []StorageID { +func (s *accessOrderTrackerBaseStorage) SegTouchOrder() []SlabID { return s.segTouchOrder } -func (s *accessOrderTrackerBaseStorage) Retrieve(id StorageID) ([]byte, bool, error) { +func (s *accessOrderTrackerBaseStorage) Retrieve(id SlabID) ([]byte, bool, error) { s.segTouchOrder = append(s.segTouchOrder, id) seg, ok := s.segments[id] return seg, ok, nil } -func (s *accessOrderTrackerBaseStorage) Store(id StorageID, data []byte) error { +func (s *accessOrderTrackerBaseStorage) Store(id SlabID, data []byte) error { s.segTouchOrder = append(s.segTouchOrder, id) s.segments[id] = data return nil } -func (s *accessOrderTrackerBaseStorage) Remove(id StorageID) error { +func (s *accessOrderTrackerBaseStorage) Remove(id SlabID) error { s.segTouchOrder = append(s.segTouchOrder, id) delete(s.segments, id) return nil } -func (s *accessOrderTrackerBaseStorage) GenerateStorageID(address Address) (StorageID, error) { +func (s *accessOrderTrackerBaseStorage) GenerateSlabID(address Address) (SlabID, error) { s.indexReqOrder = append(s.indexReqOrder, address) index := s.storageIndex[address] nextIndex := index.Next() s.storageIndex[address] = nextIndex - return NewStorageID(address, nextIndex), nil + return NewSlabID(address, nextIndex), nil } func (s *accessOrderTrackerBaseStorage) SegmentCounts() int { return len(s.segments) } @@ -1134,7 +1137,7 @@ func (s *accessOrderTrackerBaseStorage) ResetReporter() {} type testLedger struct { values map[string][]byte - index map[string]StorageIndex + index map[string]SlabIndex } var _ Ledger = &testLedger{} @@ -1142,7 +1145,7 @@ var _ Ledger = &testLedger{} func newTestLedger() *testLedger { return &testLedger{ values: make(map[string][]byte), - index: make(map[string]StorageIndex), + index: make(map[string]SlabIndex), } } @@ -1161,7 +1164,7 @@ func (l *testLedger) ValueExists(owner, key []byte) (exists bool, err error) { return len(value) > 0, nil } -func (l *testLedger) AllocateStorageIndex(owner []byte) (StorageIndex, error) { +func (l *testLedger) AllocateSlabIndex(owner []byte) (SlabIndex, error) { index := l.index[string(owner)] next := index.Next() l.index[string(owner)] = next diff --git a/utils_test.go b/utils_test.go index 56271275..a40a3599 100644 --- a/utils_test.go +++ b/utils_test.go @@ -127,7 +127,7 @@ func newTestPersistentStorage(t testing.TB) *PersistentSlabStorage { ) } -func newTestPersistentStorageWithData(t testing.TB, data map[StorageID][]byte) *PersistentSlabStorage { +func newTestPersistentStorageWithData(t testing.TB, data map[SlabID][]byte) *PersistentSlabStorage { baseStorage := NewInMemBaseStorage() baseStorage.segments = data return newTestPersistentStorageWithBaseStorage(t, baseStorage) @@ -166,34 +166,34 @@ func newTestBasicStorage(t testing.TB) *BasicSlabStorage { } type InMemBaseStorage struct { - segments map[StorageID][]byte - storageIndex map[Address]StorageIndex + segments map[SlabID][]byte + slabIndex map[Address]SlabIndex bytesRetrieved int bytesStored int - segmentsReturned map[StorageID]struct{} - segmentsUpdated map[StorageID]struct{} - segmentsTouched map[StorageID]struct{} + segmentsReturned map[SlabID]struct{} + segmentsUpdated map[SlabID]struct{} + segmentsTouched map[SlabID]struct{} } var _ BaseStorage = &InMemBaseStorage{} func NewInMemBaseStorage() *InMemBaseStorage { return NewInMemBaseStorageFromMap( - make(map[StorageID][]byte), + make(map[SlabID][]byte), ) } -func NewInMemBaseStorageFromMap(segments map[StorageID][]byte) *InMemBaseStorage { +func NewInMemBaseStorageFromMap(segments map[SlabID][]byte) *InMemBaseStorage { return &InMemBaseStorage{ segments: segments, - storageIndex: make(map[Address]StorageIndex), - segmentsReturned: make(map[StorageID]struct{}), - segmentsUpdated: make(map[StorageID]struct{}), - segmentsTouched: make(map[StorageID]struct{}), + slabIndex: make(map[Address]SlabIndex), + segmentsReturned: make(map[SlabID]struct{}), + segmentsUpdated: make(map[SlabID]struct{}), + segmentsTouched: make(map[SlabID]struct{}), } } -func (s *InMemBaseStorage) Retrieve(id StorageID) ([]byte, bool, error) { +func (s *InMemBaseStorage) Retrieve(id SlabID) ([]byte, bool, error) { seg, ok := s.segments[id] s.bytesRetrieved += len(seg) s.segmentsReturned[id] = struct{}{} @@ -201,7 +201,7 @@ func (s *InMemBaseStorage) Retrieve(id StorageID) ([]byte, bool, error) { return seg, ok, nil } -func (s *InMemBaseStorage) Store(id StorageID, data []byte) error { +func (s *InMemBaseStorage) Store(id SlabID, data []byte) error { s.segments[id] = data s.bytesStored += len(data) s.segmentsUpdated[id] = struct{}{} @@ -209,19 +209,19 @@ func (s *InMemBaseStorage) Store(id StorageID, data []byte) error { return nil } -func (s *InMemBaseStorage) Remove(id StorageID) error { +func (s *InMemBaseStorage) Remove(id SlabID) error { s.segmentsUpdated[id] = struct{}{} s.segmentsTouched[id] = struct{}{} delete(s.segments, id) return nil } -func (s *InMemBaseStorage) GenerateStorageID(address Address) (StorageID, error) { - index := s.storageIndex[address] +func (s *InMemBaseStorage) GenerateSlabID(address Address) (SlabID, error) { + index := s.slabIndex[address] nextIndex := index.Next() - s.storageIndex[address] = nextIndex - return NewStorageID(address, nextIndex), nil + s.slabIndex[address] = nextIndex + return NewSlabID(address, nextIndex), nil } func (s *InMemBaseStorage) SegmentCounts() int { @@ -259,9 +259,9 @@ func (s *InMemBaseStorage) SegmentsTouched() int { func (s *InMemBaseStorage) ResetReporter() { s.bytesStored = 0 s.bytesRetrieved = 0 - s.segmentsReturned = make(map[StorageID]struct{}) - s.segmentsUpdated = make(map[StorageID]struct{}) - s.segmentsTouched = make(map[StorageID]struct{}) + s.segmentsReturned = make(map[SlabID]struct{}) + s.segmentsUpdated = make(map[SlabID]struct{}) + s.segmentsTouched = make(map[SlabID]struct{}) } func valueEqual(t *testing.T, tic TypeInfoComparator, a Value, b Value) { @@ -285,7 +285,7 @@ func arrayEqual(t *testing.T, tic TypeInfoComparator, a Value, b Value) { require.True(t, tic(array1.Type(), array2.Type())) require.Equal(t, array1.Address(), array2.Address()) require.Equal(t, array1.Count(), array2.Count()) - require.Equal(t, array1.StorageID(), array2.StorageID()) + require.Equal(t, array1.SlabID(), array2.SlabID()) iterator1, err := array1.Iterator() require.NoError(t, err) @@ -318,7 +318,7 @@ func mapEqual(t *testing.T, tic TypeInfoComparator, a Value, b Value) { require.True(t, tic(m1.Type(), m2.Type())) require.Equal(t, m1.Address(), m2.Address()) require.Equal(t, m1.Count(), m2.Count()) - require.Equal(t, m1.StorageID(), m2.StorageID()) + require.Equal(t, m1.SlabID(), m2.SlabID()) iterator1, err := m1.Iterator() require.NoError(t, err)