diff --git a/autobatch/autobatch.go b/autobatch/autobatch.go index aeca9a455bbfc842b2c067497a1c92d8731ccc1b..66d701e2bc5c90ba2357721cfd1770249819cbc3 100644 --- a/autobatch/autobatch.go +++ b/autobatch/autobatch.go @@ -75,10 +75,6 @@ func (d *Datastore) Flush() error { var err error if o.delete { err = b.Delete(k) - if err == ds.ErrNotFound { - // Ignore these, let delete be idempotent. - err = nil - } } else { err = b.Put(k, o.value) } diff --git a/basic_ds.go b/basic_ds.go index 19b74e26c9143ff4a9d7ff8c8c3b1876b3e8217d..08ae2af746b1ab349d01ad1768c33e1293deb260 100644 --- a/basic_ds.go +++ b/basic_ds.go @@ -53,9 +53,6 @@ func (d *MapDatastore) GetSize(key Key) (size int, err error) { // Delete implements Datastore.Delete func (d *MapDatastore) Delete(key Key) (err error) { - if _, found := d.values[key]; !found { - return ErrNotFound - } delete(d.values, key) return nil } diff --git a/batch.go b/batch.go index 57880dd2d39ef769ddd1d62dc3b7f8ab4ceb8cf5..41e23ffe44efad450599e6892639bff854d92f54 100644 --- a/batch.go +++ b/batch.go @@ -35,11 +35,6 @@ func (bt *basicBatch) Commit() error { for k, op := range bt.ops { if op.delete { err = bt.target.Delete(k) - // We could try to do something smarter but I really - // don't care. Delete should be idempotent anyways. - if err == ErrNotFound { - err = nil - } } else { err = bt.target.Put(k, op.value) } diff --git a/datastore.go b/datastore.go index 0cc3b9b97d3f9d57604fae3e74803b08843bb2a1..0a49d1fb5dfc60f4ce99f8778d60733bd80bef3f 100644 --- a/datastore.go +++ b/datastore.go @@ -50,7 +50,8 @@ type Write interface { // type-safe interface to your application, and do the checking up-front. Put(key Key, value []byte) error - // Delete removes the value for given `key`. + // Delete removes the value for given `key`. If the key is not in the + // datastore, this method returns no error. Delete(key Key) error } @@ -191,8 +192,8 @@ type TxnDatastore interface { // Errors -// ErrNotFound is returned by Get, Has, and Delete when a datastore does not -// map the given key to a value. +// ErrNotFound is returned by Get and GetSize when a datastore does not map the +// given key to a value. var ErrNotFound = errors.New("datastore: key not found") // ErrInvalidType is returned by Put when a given value is incopatible with diff --git a/examples/fs.go b/examples/fs.go index 94cefab1bc5748a7a941b9144617b06aefe7bca0..87369e811384a3d70dc3b38290d2cf18853b8fd6 100644 --- a/examples/fs.go +++ b/examples/fs.go @@ -86,10 +86,14 @@ func (d *Datastore) GetSize(key ds.Key) (size int, err error) { func (d *Datastore) Delete(key ds.Key) (err error) { fn := d.KeyFilename(key) if !isFile(fn) { - return ds.ErrNotFound + return nil } - return os.Remove(fn) + err = os.Remove(fn) + if os.IsNotExist(err) { + err = nil // idempotent + } + return err } // Query implements Datastore.Query diff --git a/mount/mount.go b/mount/mount.go index 326277a686def8a2da0754a9c410c3acf0e3d21f..71ed8e80b076bbed0228a48be3ba95ede1e4fd75 100644 --- a/mount/mount.go +++ b/mount/mount.go @@ -216,7 +216,7 @@ func (d *Datastore) GetSize(key ds.Key) (size int, err error) { func (d *Datastore) Delete(key ds.Key) error { cds, _, k := d.lookup(key) if cds == nil { - return ds.ErrNotFound + return nil } return cds.Delete(k) } diff --git a/mount/mount_test.go b/mount/mount_test.go index 5a3277254650652dc6f6e10e55209ab7065a39ff..36135363d3578de25b03d2426ffe62ac49f6e2aa 100644 --- a/mount/mount_test.go +++ b/mount/mount_test.go @@ -171,8 +171,8 @@ func TestDeleteNotFound(t *testing.T) { }) err := m.Delete(datastore.NewKey("/quux/thud")) - if g, e := err, datastore.ErrNotFound; g != e { - t.Fatalf("expected ErrNotFound, got: %v\n", g) + if err != nil { + t.Fatalf("expected nil, got: %v\n", err) } }