mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
Add DeleteAll function (#116)
This commit is contained in:
parent
be3fd71ebe
commit
ca06e332d6
17
bitcask.go
17
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.
|
||||
|
@ -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 ++ {
|
||||
|
Loading…
x
Reference in New Issue
Block a user