mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
Add Keys() to exported API (extended API)
This commit is contained in:
parent
01cb269a51
commit
aaea7273c3
@ -125,6 +125,10 @@ func (b *Bitcask) Scan(prefix string, f func(key string) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bitcask) Keys() chan string {
|
||||
return b.keydir.Keys()
|
||||
}
|
||||
|
||||
func (b *Bitcask) Fold(f func(key string) error) error {
|
||||
for key := range b.keydir.Keys() {
|
||||
if err := f(key); err != nil {
|
||||
|
@ -44,6 +44,14 @@ func TestAll(t *testing.T) {
|
||||
assert.True(db.Has("foo"))
|
||||
})
|
||||
|
||||
t.Run("Keys", func(t *testing.T) {
|
||||
keys := make([]string, 0)
|
||||
for key := range db.Keys() {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
assert.Equal([]string{"foo"}, keys)
|
||||
})
|
||||
|
||||
t.Run("Fold", func(t *testing.T) {
|
||||
var (
|
||||
keys []string
|
||||
|
@ -55,8 +55,10 @@ func (k *Keydir) Delete(key string) {
|
||||
func (k *Keydir) Keys() chan string {
|
||||
ch := make(chan string)
|
||||
go func() {
|
||||
for k := range k.kv {
|
||||
ch <- k
|
||||
k.RLock()
|
||||
defer k.RUnlock()
|
||||
for key := range k.kv {
|
||||
ch <- key
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
|
Loading…
x
Reference in New Issue
Block a user