Skip to content

Commit

Permalink
Rename variables for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Oct 3, 2023
1 parent 903fc13 commit 8d02bbc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 67 deletions.
48 changes: 24 additions & 24 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type element interface {
hip HashInputProvider,
key Value,
value Value,
) (newElem element, keyStorable MapKey, existingValue MapValue, err error)
) (newElem element, keyStorable MapKey, existingMapValueStorable MapValue, err error)

// Remove returns matched key, value, and updated element.
// Updated element may be nil, modified, or a different type of element.
Expand Down Expand Up @@ -654,7 +654,7 @@ func (e *singleElement) Set(

// Key matches, overwrite existing value
if equal {
existingValue := e.value
existingMapValueStorable := e.value

valueStorable, err := value.Storable(storage, address, maxInlineMapValueSize(uint64(e.key.ByteSize())))
if err != nil {
Expand All @@ -664,7 +664,7 @@ func (e *singleElement) Set(

e.value = valueStorable
e.size = singleElementPrefixSize + e.key.ByteSize() + e.value.ByteSize()
return e, e.key, existingValue, nil
return e, e.key, existingMapValueStorable, nil
}

// Hash collision detected
Expand Down Expand Up @@ -822,7 +822,7 @@ func (e *inlineCollisionGroup) Set(
}
hkey, _ := digester.Digest(level)

keyStorable, existingValue, err := e.elements.Set(storage, address, b, digester, level, hkey, comparator, hip, key, value)
keyStorable, existingMapValueStorable, err := e.elements.Set(storage, 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, nil, nil, err
Expand Down Expand Up @@ -863,11 +863,11 @@ func (e *inlineCollisionGroup) Set(
return &externalCollisionGroup{
slabID: id,
size: externalCollisionGroupPrefixSize + SlabIDStorable(id).ByteSize(),
}, keyStorable, existingValue, nil
}, keyStorable, existingMapValueStorable, nil
}
}

return e, keyStorable, existingValue, nil
return e, keyStorable, existingMapValueStorable, nil
}

// Remove returns key, value, and updated element if key is found.
Expand Down Expand Up @@ -1021,12 +1021,12 @@ func (e *externalCollisionGroup) Set(
}
hkey, _ := digester.Digest(level)

keyStorable, existingValue, err := slab.Set(storage, b, digester, level, hkey, comparator, hip, key, value)
keyStorable, existingMapValueStorable, err := slab.Set(storage, 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 MapSlab.Set().
return nil, nil, nil, err
}
return e, keyStorable, existingValue, nil
return e, keyStorable, existingMapValueStorable, nil
}

// Remove returns key, value, and updated element if key is found.
Expand Down Expand Up @@ -1498,7 +1498,7 @@ func (e *hkeyElements) Set(
}
}

elem, keyStorable, existingValue, err := elem.Set(storage, address, b, digester, level, hkey, comparator, hip, key, value)
elem, keyStorable, existingMapValueStorable, err := elem.Set(storage, 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 element.Set().
return nil, nil, err
Expand All @@ -1515,7 +1515,7 @@ func (e *hkeyElements) Set(
}
e.size = size

return keyStorable, existingValue, nil
return keyStorable, existingMapValueStorable, nil
}

// No matching hkey
Expand Down Expand Up @@ -2017,8 +2017,8 @@ func (e *singleElements) Set(
}

if equal {
existingKey := elem.key
existingValue := elem.value
existingKeyStorable := elem.key
existingValueStorable := elem.value

vs, err := value.Storable(storage, address, maxInlineMapValueSize(uint64(elem.key.ByteSize())))
if err != nil {
Expand All @@ -2038,7 +2038,7 @@ func (e *singleElements) Set(
}
e.size = size

return existingKey, existingValue, nil
return existingKeyStorable, existingValueStorable, nil
}
}

Expand Down Expand Up @@ -3177,7 +3177,7 @@ func (m *MapDataSlab) Set(
value Value,
) (MapKey, MapValue, error) {

keyStorable, existingValue, err := m.elements.Set(storage, m.SlabID().address, b, digester, level, hkey, comparator, hip, key, value)
keyStorable, existingMapValueStorable, 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, nil, err
Expand All @@ -3198,7 +3198,7 @@ func (m *MapDataSlab) Set(
}
}

return keyStorable, existingValue, nil
return keyStorable, existingMapValueStorable, nil
}

func (m *MapDataSlab) Remove(storage SlabStorage, digester Digester, level uint, hkey Digest, comparator ValueComparator, key Value) (MapKey, MapValue, error) {
Expand Down Expand Up @@ -3888,7 +3888,7 @@ func (m *MapMetaDataSlab) Set(
return nil, nil, err
}

keyStorable, existingValue, err := child.Set(storage, b, digester, level, hkey, comparator, hip, key, value)
keyStorable, existingMapValueStorable, err := child.Set(storage, 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 MapSlab.Set().
return nil, nil, err
Expand All @@ -3907,7 +3907,7 @@ func (m *MapMetaDataSlab) Set(
// Don't need to wrap error as external error because err is already categorized by MapMetaDataSlab.SplitChildSlab().
return nil, nil, err
}
return keyStorable, existingValue, nil
return keyStorable, existingMapValueStorable, nil
}

if underflowSize, underflow := child.IsUnderflow(); underflow {
Expand All @@ -3916,15 +3916,15 @@ func (m *MapMetaDataSlab) Set(
// Don't need to wrap error as external error because err is already categorized by MapMetaDataSlab.MergeOrRebalanceChildSlab().
return nil, nil, err
}
return keyStorable, existingValue, nil
return keyStorable, existingMapValueStorable, nil
}

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.slabID))
}
return keyStorable, existingValue, nil
return keyStorable, existingMapValueStorable, nil
}

func (m *MapMetaDataSlab) Remove(storage SlabStorage, digester Digester, level uint, hkey Digest, comparator ValueComparator, key Value) (MapKey, MapValue, error) {
Expand Down Expand Up @@ -4902,13 +4902,13 @@ func (m *OrderedMap) set(comparator ValueComparator, hip HashInputProvider, key
return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to get map key digest at level %d", level))
}

keyStorable, existingValue, err := m.root.Set(m.Storage, m.digesterBuilder, keyDigest, level, hkey, comparator, hip, key, value)
keyStorable, existingMapValueStorable, err := m.root.Set(m.Storage, m.digesterBuilder, keyDigest, level, hkey, comparator, hip, key, value)
if err != nil {
// Don't need to wrap error as external error because err is already categorized by MapSlab.Set().
return nil, err
}

if existingValue == nil {
if existingMapValueStorable == nil {
m.root.ExtraData().incrementCount()
}

Expand Down Expand Up @@ -4957,7 +4957,7 @@ func (m *OrderedMap) set(comparator ValueComparator, hip HashInputProvider, key
maxInlineSize := maxInlineMapValueSize(uint64(keyStorable.ByteSize()))
m.setCallbackWithChild(comparator, hip, key, value, maxInlineSize)

return existingValue, nil
return existingMapValueStorable, nil
}

func (m *OrderedMap) Remove(comparator ValueComparator, hip HashInputProvider, key Value) (Storable, Storable, error) {
Expand Down Expand Up @@ -5724,12 +5724,12 @@ func NewMapFromBatchData(
prevElem := elements.elems[lastElementIndex]
prevElemSize := prevElem.Size()

elem, _, existingValue, err := prevElem.Set(storage, address, digesterBuilder, digester, 0, hkey, comparator, hip, key, value)
elem, _, existingMapValueStorable, err := prevElem.Set(storage, address, digesterBuilder, digester, 0, hkey, comparator, hip, key, value)
if err != nil {
// Don't need to wrap error as external error because err is already categorized by element.Set().
return nil, err
}
if existingValue != nil {
if existingMapValueStorable != nil {
return nil, NewDuplicateKeyError(key)
}

Expand Down
80 changes: 40 additions & 40 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7882,15 +7882,15 @@ func TestEmptyMap(t *testing.T) {
})

t.Run("remove", func(t *testing.T) {
existingKey, existingValue, err := m.Remove(compare, hashInputProvider, Uint64Value(0))
existingMapKeyStorable, existingMapValueStorable, err := m.Remove(compare, hashInputProvider, Uint64Value(0))
require.Equal(t, 1, errorCategorizationCount(err))
var userError *UserError
var keyNotFoundError *KeyNotFoundError
require.ErrorAs(t, err, &userError)
require.ErrorAs(t, err, &keyNotFoundError)
require.ErrorAs(t, userError, &keyNotFoundError)
require.Nil(t, existingKey)
require.Nil(t, existingValue)
require.Nil(t, existingMapKeyStorable)
require.Nil(t, existingMapValueStorable)
})

t.Run("iterate", func(t *testing.T) {
Expand Down Expand Up @@ -10882,10 +10882,10 @@ func TestChildMapInlinabilityInParentMap(t *testing.T) {
}

for _, k := range keys {
existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, k)

Expand Down Expand Up @@ -11041,10 +11041,10 @@ func TestChildMapInlinabilityInParentMap(t *testing.T) {
break
}

existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, aKey)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, aKey)
require.NoError(t, err)
require.Equal(t, aKey, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, aKey, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, aKey)

Expand Down Expand Up @@ -11084,10 +11084,10 @@ func TestChildMapInlinabilityInParentMap(t *testing.T) {
}

for _, k := range keys {
existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, k)

Expand Down Expand Up @@ -11231,10 +11231,10 @@ func TestChildMapInlinabilityInParentMap(t *testing.T) {
break
}

existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, aKey)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, aKey)
require.NoError(t, err)
require.Equal(t, aKey, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, aKey, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, aKey)

Expand Down Expand Up @@ -11268,10 +11268,10 @@ func TestChildMapInlinabilityInParentMap(t *testing.T) {
}

for _, k := range keys {
existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, k)

Expand Down Expand Up @@ -11515,10 +11515,10 @@ func TestNestedThreeLevelChildMapInlinabilityInParentMap(t *testing.T) {
}

for _, k := range gchildKeys {
existingKey, existingValue, err := gchildMap.Remove(compare, hashInputProvider, k)
existingMapKey, existingMapValueStorable, err := gchildMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKey)
require.NotNil(t, existingMapValueStorable)

delete(expectedGChildMapValues, k)

Expand Down Expand Up @@ -11782,10 +11782,10 @@ func TestNestedThreeLevelChildMapInlinabilityInParentMap(t *testing.T) {
// Remove all elements (large element first) to trigger grand child map being inlined again.
for _, k := range keys {

existingKey, existingValue, err := gchildMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := gchildMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedGChildMapValues, k)

Expand Down Expand Up @@ -12101,10 +12101,10 @@ func TestNestedThreeLevelChildMapInlinabilityInParentMap(t *testing.T) {
}

// Remove one element
existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, aKey)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, aKey)
require.NoError(t, err)
require.Equal(t, aKey, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, aKey, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, aKey)

Expand Down Expand Up @@ -12171,10 +12171,10 @@ func TestNestedThreeLevelChildMapInlinabilityInParentMap(t *testing.T) {

// Remove all elements, except grand child map
for _, k := range keys {
existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedChildMapValues, k)

Expand Down Expand Up @@ -12417,10 +12417,10 @@ func TestNestedThreeLevelChildMapInlinabilityInParentMap(t *testing.T) {
}

// Remove one element from grand child map
existingKey, existingValue, err := gchildMap.Remove(compare, hashInputProvider, aKey)
existingMapKeyStorable, existingMapValueStorable, err := gchildMap.Remove(compare, hashInputProvider, aKey)
require.NoError(t, err)
require.Equal(t, aKey, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, aKey, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedGChildMapValues, aKey)

Expand Down Expand Up @@ -12473,13 +12473,13 @@ func TestNestedThreeLevelChildMapInlinabilityInParentMap(t *testing.T) {

// Remove grand children
for _, k := range keys {
existingKey, existingValue, err := childMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := childMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.Equal(t, k, existingKey)
require.NotNil(t, existingValue)
require.Equal(t, k, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

// Grand child map is returned as SlabIDStorable, even if it was stored inlined in the parent.
id, ok := existingValue.(SlabIDStorable)
id, ok := existingMapValueStorable.(SlabIDStorable)
require.True(t, ok)

v, err := id.StoredValue(storage)
Expand Down Expand Up @@ -12659,10 +12659,10 @@ func TestChildMapWhenParentMapIsModified(t *testing.T) {

for _, k := range keysForNonChildMaps {

existingKey, existingValue, err := parentMap.Remove(compare, hashInputProvider, k)
existingMapKeyStorable, existingMapValueStorable, err := parentMap.Remove(compare, hashInputProvider, k)
require.NoError(t, err)
require.NotNil(t, existingKey)
require.NotNil(t, existingValue)
require.NotNil(t, existingMapKeyStorable)
require.NotNil(t, existingMapValueStorable)

delete(expectedKeyValues, k)

Expand Down
Loading

0 comments on commit 8d02bbc

Please sign in to comment.