Skip to content

Commit

Permalink
feat(bucket): add list method
Browse files Browse the repository at this point in the history
  • Loading branch information
xtlsoft committed Sep 15, 2020
1 parent 8e0a755 commit eef6ea3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package unikv

import (
"encoding/gob"
"fmt"
"strconv"
"strings"
)
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions drivers/memory/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memorydriver

import (
"reflect"
"sync"

"github.com/apiles/unikv"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eef6ea3

Please sign in to comment.