Skip to content

Commit

Permalink
Adding Delete() method
Browse files Browse the repository at this point in the history
  • Loading branch information
djherbis committed Feb 20, 2016
1 parent 8bf601c commit 4539295
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 16 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,19 @@ func (s *Store) DeleteAll() error {
return tx.DeleteBucket(s.bucket)
})
}

// Delete will remove the item with the specified key from the store.
// It returns nil if the item was not found (like BoltDB).
func (s *Store) Delete(key interface{}) error {
keyBytes, err := s.toBytes(key)
if err != nil {
return err
}
return s.db.Update(func(tx *bolt.Tx) error {
objects := tx.Bucket(s.bucket)
if objects == nil {
return nil
}
return objects.Delete(keyBytes)
})
}
12 changes: 10 additions & 2 deletions stow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,20 @@ func testStore(t testing.TB, store *Store) {
t.Errorf("Should have been NotFound!")
}

store.DeleteAll()
store.Delete("hello")

var name4 MyType
err = store.Pull([]byte("hello"), &name4)
if err != ErrNotFound {
t.Errorf("DeleteAll failed!")
t.Errorf("Delete failed!")
}

if err := store.DeleteAll(); err != nil {
t.Errorf("DeleteAll should have returned nil err %s", err.Error())
}

if err := store.Delete("hello"); err != nil {
t.Errorf("Delete should have returned nil err %s", err.Error())
}
}

Expand Down

0 comments on commit 4539295

Please sign in to comment.