Skip to content

Commit

Permalink
make delete on non existing document a no-op
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Dec 15, 2022
1 parent 0a39dd9 commit aceed6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
14 changes: 14 additions & 0 deletions datastore/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 18 additions & 18 deletions datastore/memory/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit aceed6e

Please sign in to comment.