diff --git a/bucket.go b/bucket.go index d586b16..f18b43c 100644 --- a/bucket.go +++ b/bucket.go @@ -2,6 +2,7 @@ package unikv import ( "encoding/gob" + "fmt" "strconv" "strings" ) @@ -25,6 +26,26 @@ func (b *Bucket) PutString(key interface{}, str string) error { return b.Driver.Put(NewKey(key).String(), str) } +// List lists keys +func (b *Bucket) List() ([]Key, error) { + ks, err := b.Driver.List() + if err != nil { + return nil, err + } + switch ks.(type) { + case []Key: + return ks.([]Key), nil + case []string: + ksa := ks.([]string) + sl := make([]Key, len(ksa)) + for i, v := range ksa { + sl[i] = NewKey(v) + } + return sl, nil + } + return nil, fmt.Errorf("Unknown driver error") +} + // Unset unsets a value func (b *Bucket) Unset(key interface{}) error { return b.Driver.Unset(NewKey(key).String()) diff --git a/drivers.go b/drivers.go index e270cfc..3da908c 100644 --- a/drivers.go +++ b/drivers.go @@ -7,6 +7,7 @@ type Driver interface { Get(key string) (string, error) Put(key string, data string) error Unset(key string) error + List() (interface{}, error) // interface{} should be []unikv.Key or []string Close() error } diff --git a/drivers/memory/driver.go b/drivers/memory/driver.go index 1a0d781..ad41643 100644 --- a/drivers/memory/driver.go +++ b/drivers/memory/driver.go @@ -1,6 +1,7 @@ package memorydriver import ( + "reflect" "sync" "github.com/apiles/unikv" @@ -32,6 +33,11 @@ func (d *Driver) Unset(key string) error { return nil } +// List lists the keys +func (d *Driver) List() (interface{}, error) { + return reflect.ValueOf(d.data).MapKeys(), nil +} + // Close closes driver func (d *Driver) Close() error { return nil