Skip to content

Commit

Permalink
feat(bucket): implement list method
Browse files Browse the repository at this point in the history
  • Loading branch information
xtlsoft committed Oct 1, 2020
1 parent eef6ea3 commit 41ddfe2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
8 changes: 8 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package unikv
import (
"encoding/gob"
"fmt"
"reflect"
"strconv"
"strings"
)
Expand Down Expand Up @@ -42,6 +43,13 @@ func (b *Bucket) List() ([]Key, error) {
sl[i] = NewKey(v)
}
return sl, nil
case []reflect.Value:
ksa := ks.([]reflect.Value)
sl := make([]Key, len(ksa))
for i, v := range ksa {
sl[i] = NewKey(v.String())
}
return sl, nil
}
return nil, fmt.Errorf("Unknown driver error")
}
Expand Down
8 changes: 6 additions & 2 deletions drivers/memory/driver.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package memorydriver

import (
"reflect"
"sync"

"github.com/apiles/unikv"
Expand Down Expand Up @@ -35,7 +34,12 @@ func (d *Driver) Unset(key string) error {

// List lists the keys
func (d *Driver) List() (interface{}, error) {
return reflect.ValueOf(d.data).MapKeys(), nil
var rslt []string
d.data.Range(func(k, _v interface{}) bool {
rslt = append(rslt, k.(string))
return true
})
return rslt, nil
}

// Close closes driver
Expand Down
6 changes: 6 additions & 0 deletions drivers/persistent-memory/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package persistentmemorydriver
import (
"encoding/gob"
"os"
"reflect"
"sync"

"github.com/apiles/unikv"
Expand Down Expand Up @@ -65,6 +66,11 @@ func (d *Driver) Put(key string, value string) error {
return nil
}

// List lists the keys
func (d *Driver) List() (interface{}, error) {
return reflect.ValueOf(d.data).MapKeys(), nil
}

// Unset unsets data
func (d *Driver) Unset(key string) error {
key = unikv.ConcatPrefix(d.prefix, key)
Expand Down
20 changes: 20 additions & 0 deletions drivers/redis/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ func (d *Driver) Unset(key string) error {
return err
}

// List lists the keys
func (d *Driver) List() (interface{}, error) {
data, err := d.conn.Do("KEYS", "*")
if err != nil {
if err == redis.ErrNil {
return "", unikv.ErrNotFound
}
if !d.reconnected {
d.connect()
d.reconnected = true
return d.List()
}
return "", err
}
if d.reconnected {
d.reconnected = false
}
return data, err
}

// Close closes driver
func (d *Driver) Close() error {
return d.conn.Close()
Expand Down

0 comments on commit 41ddfe2

Please sign in to comment.