diff --git a/bitcask.go b/bitcask.go index 604ba0d..ddb9e01 100644 --- a/bitcask.go +++ b/bitcask.go @@ -193,6 +193,23 @@ func (b *Bitcask) Delete(key []byte) error { return nil } +// DeleteAll deletes all the keys. If an I/O error occurs the error is returned. +func (b *Bitcask) DeleteAll() (err error) { + b.mu.RLock() + defer b.mu.RUnlock() + + b.trie.ForEach(func(node art.Node) bool { + _, _, err = b.put(node.Key(), []byte{}) + if err != nil { + return false + } + return true + }) + b.trie = art.New() + + return +} + // Scan performs a prefix scan of keys matching the given prefix and calling // the function `f` with the keys found. If the function returns an error // no further keys are processed and the first error returned. diff --git a/bitcask_test.go b/bitcask_test.go index 7416ffe..b345e2a 100644 --- a/bitcask_test.go +++ b/bitcask_test.go @@ -133,6 +133,25 @@ func TestAll(t *testing.T) { }) } +func TestDeleteAll(t *testing.T) { + assert := assert.New(t) + testdir, _ := ioutil.TempDir("", "bitcask") + db, _ := Open(testdir) + _ = db.Put([]byte("foo"), []byte("foo")) + _ = db.Put([]byte("bar"), []byte("bar")) + _ = db.Put([]byte("baz"), []byte("baz")) + assert.Equal(3, db.Len()) + err := db.DeleteAll() + assert.NoError(err) + assert.Equal(0, db.Len()) + _, err = db.Get([]byte("foo")) + assert.Equal(ErrKeyNotFound, err) + _, err = db.Get([]byte("bar")) + assert.Equal(ErrKeyNotFound, err) + _, err = db.Get([]byte("baz")) + assert.Equal(ErrKeyNotFound, err) +} + func TestReopen1(t *testing.T) { assert := assert.New(t) for i := 0; i < 10; i ++ {