Skip to content

Commit

Permalink
Return if element was deleted from Hashmap (#2271)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Laine <[email protected]>
  • Loading branch information
dhrubabasu and Dan Laine authored Nov 8, 2023
1 parent 1329a59 commit 93d88c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
12 changes: 7 additions & 5 deletions utils/linkedhashmap/linkedhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _ LinkedHashmap[int, struct{}] = (*linkedHashmap[int, struct{}])(nil)
type Hashmap[K, V any] interface {
Put(key K, val V)
Get(key K) (val V, exists bool)
Delete(key K)
Delete(key K) (deleted bool)
Len() int
}

Expand Down Expand Up @@ -63,11 +63,11 @@ func (lh *linkedHashmap[K, V]) Get(key K) (V, bool) {
return lh.get(key)
}

func (lh *linkedHashmap[K, V]) Delete(key K) {
func (lh *linkedHashmap[K, V]) Delete(key K) bool {
lh.lock.Lock()
defer lh.lock.Unlock()

lh.delete(key)
return lh.delete(key)
}

func (lh *linkedHashmap[K, V]) Len() int {
Expand Down Expand Up @@ -114,11 +114,13 @@ func (lh *linkedHashmap[K, V]) get(key K) (V, bool) {
return utils.Zero[V](), false
}

func (lh *linkedHashmap[K, V]) delete(key K) {
if e, ok := lh.entryMap[key]; ok {
func (lh *linkedHashmap[K, V]) delete(key K) bool {
e, ok := lh.entryMap[key]
if ok {
lh.entryList.Remove(e)
delete(lh.entryMap, key)
}
return ok
}

func (lh *linkedHashmap[K, V]) len() int {
Expand Down
8 changes: 4 additions & 4 deletions utils/linkedhashmap/linkedhashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestLinkedHashmap(t *testing.T) {
require.Equal(key1, rkey1, "wrong key")
require.Equal(1, val1, "wrong value")

lh.Delete(key0)
require.True(lh.Delete(key0))
require.Equal(1, lh.Len(), "wrong hashmap length")

_, exists = lh.Get(key0)
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestIterator(t *testing.T) {
// Should be empty
require.False(iter.Next())
// Delete id1
lh.Delete(id1)
require.True(lh.Delete(id1))
iter = lh.NewIterator()
require.NotNil(iter)
// Should immediately be exhausted
Expand Down Expand Up @@ -169,8 +169,8 @@ func TestIterator(t *testing.T) {
iter := lh.NewIterator()
require.True(iter.Next())
require.True(iter.Next())
lh.Delete(id1)
lh.Delete(id2)
require.True(lh.Delete(id1))
require.True(lh.Delete(id2))
require.True(iter.Next())
require.Equal(id3, iter.Key())
require.Equal(3, iter.Value())
Expand Down
14 changes: 5 additions & 9 deletions vms/avm/txs/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ type Mempool interface {
Get(txID ids.ID) *txs.Tx
Remove(txs []*txs.Tx)

// Peek returns the next first tx that was added to the mempool whose size
// is less than or equal to maxTxSize.
// Peek returns the first tx in the mempool whose size is <= [maxTxSize].
Peek(maxTxSize int) *txs.Tx

// RequestBuildBlock notifies the consensus engine that a block should be
Expand Down Expand Up @@ -162,23 +161,20 @@ func (m *mempool) Has(txID ids.ID) bool {
}

func (m *mempool) Get(txID ids.ID) *txs.Tx {
unissuedTxs, _ := m.unissuedTxs.Get(txID)
return unissuedTxs
tx, _ := m.unissuedTxs.Get(txID)
return tx
}

func (m *mempool) Remove(txsToRemove []*txs.Tx) {
for _, tx := range txsToRemove {
txID := tx.ID()
if _, ok := m.unissuedTxs.Get(txID); !ok {
// If tx isn't in the mempool, there is nothing to do.
if !m.unissuedTxs.Delete(txID) {
continue
}

txBytes := tx.Bytes()
m.bytesAvailable += len(txBytes)
m.bytesAvailable += len(tx.Bytes())
m.bytesAvailableMetric.Set(float64(m.bytesAvailable))

m.unissuedTxs.Delete(txID)
m.numTxs.Dec()

inputs := tx.Unsigned.InputIDs()
Expand Down

0 comments on commit 93d88c0

Please sign in to comment.