diff --git a/datastore/memory/memory_test.go b/datastore/memory/memory_test.go index 9ce4a34d41..81a9b9ccff 100644 --- a/datastore/memory/memory_test.go +++ b/datastore/memory/memory_test.go @@ -94,6 +94,20 @@ func TestDeleteOperation(t *testing.T) { require.ErrorIs(t, err, ds.ErrNotFound) } +func TestDeleteOperation2(t *testing.T) { + ctx := context.Background() + s := NewDatastore(ctx) + + err := s.Put(ctx, testKey1, testValue1) + require.NoError(t, err) + + err = s.Delete(ctx, testKey1) + require.NoError(t, err) + + _, err = s.Get(ctx, testKey1) + require.ErrorIs(t, err, ds.ErrNotFound) +} + func TestGetSizeOperation(t *testing.T) { ctx := context.Background() s := newLoadedDatastore(ctx) diff --git a/datastore/memory/txn.go b/datastore/memory/txn.go index 19df770454..65a5bd8771 100644 --- a/datastore/memory/txn.go +++ b/datastore/memory/txn.go @@ -39,6 +39,24 @@ func (t *basicTxn) getTxnVersion() uint64 { return atomic.LoadUint64(t.txnVersion) } +// Delete implements ds.Delete +func (t *basicTxn) Delete(ctx context.Context, key ds.Key) error { + if t.discarded { + return ErrTxnDiscarded + } + if t.readOnly { + return ErrReadOnlyTxn + } + + item := t.get(ctx, key) + if item.key == "" || item.isDeleted { + return nil + } + + t.ops.Set(dsItem{key: key.String(), version: t.getTxnVersion(), isDeleted: true}) + return nil +} + func (t *basicTxn) get(ctx context.Context, key ds.Key) dsItem { result := dsItem{} t.ops.Descend(dsItem{key: key.String(), version: t.getTxnVersion()}, func(item dsItem) bool { @@ -174,24 +192,6 @@ func setEntry(key string, value []byte, q dsq.Query) dsq.Entry { return e } -// Delete implements ds.Delete -func (t *basicTxn) Delete(ctx context.Context, key ds.Key) error { - if t.discarded { - return ErrTxnDiscarded - } - if t.readOnly { - return ErrReadOnlyTxn - } - - item := t.get(ctx, key) - if item.key == "" || item.isDeleted { - return ds.ErrNotFound - } - - t.ops.Set(dsItem{key: key.String(), version: t.getTxnVersion(), isDeleted: true}) - return nil -} - // Discard removes all the operations added to the transaction func (t *basicTxn) Discard(ctx context.Context) { if t.discarded {