1
0
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:
James Mills 2019-03-21 10:41:56 +10:00
parent 01cb269a51
commit aaea7273c3
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
3 changed files with 16 additions and 2 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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)
}()