Skip to content

Commit

Permalink
fix(bucket): trim prefix when listing
Browse files Browse the repository at this point in the history
  • Loading branch information
xtlsoft committed Oct 2, 2020
1 parent 41ddfe2 commit 8c38565
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/unikvd-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ When you POST this address, the body will be the data.

When you DELETE this address, the key will be deleted.

When KEY is set to `_list`, a list of all the keys will be returned in JSON format.

## System Namespace

When you get `/v1/_system/*`, you are visiting system namespace.
Expand Down
13 changes: 12 additions & 1 deletion drivers/persistent-memory/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ func (d *Driver) Put(key string, value string) error {

// List lists the keys
func (d *Driver) List() (interface{}, error) {
return reflect.ValueOf(d.data).MapKeys(), nil
if d.loadWhenGet {
err := d.Load()
if err != nil {
return nil, err
}
}
keys := reflect.ValueOf(d.data).MapKeys()
var rslt []string = make([]string, len(keys))
for k, v := range keys {
rslt[k] = unikv.TrimPrefix(d.prefix, v.String())
}
return rslt, nil
}

// Unset unsets data
Expand Down
32 changes: 22 additions & 10 deletions drivers/redis/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ func (d *Driver) Get(key string) (string, error) {
func (d *Driver) Put(key string, value string) error {
key = unikv.ConcatPrefix(d.prefix, key)
_, err := d.conn.Do("SET", key, value)
if !d.reconnected {
d.connect()
d.reconnected = true
return d.Put(key, value)
if err != nil {
if !d.reconnected {
d.connect()
d.reconnected = true
return d.Put(key, value)
}
}
if d.reconnected {
d.reconnected = false
Expand All @@ -61,10 +63,12 @@ func (d *Driver) Put(key string, value string) error {
func (d *Driver) Unset(key string) error {
key = unikv.ConcatPrefix(d.prefix, key)
_, err := d.conn.Do("SET", key, "EX", "1")
if !d.reconnected {
d.connect()
d.reconnected = true
return d.Unset(key)
if err != nil {
if !d.reconnected {
d.connect()
d.reconnected = true
return d.Unset(key)
}
}
if d.reconnected {
d.reconnected = false
Expand All @@ -74,7 +78,7 @@ func (d *Driver) Unset(key string) error {

// List lists the keys
func (d *Driver) List() (interface{}, error) {
data, err := d.conn.Do("KEYS", "*")
data, err := d.conn.Do("KEYS", d.prefix+"*")
if err != nil {
if err == redis.ErrNil {
return "", unikv.ErrNotFound
Expand All @@ -89,7 +93,15 @@ func (d *Driver) List() (interface{}, error) {
if d.reconnected {
d.reconnected = false
}
return data, err
dat, ok := data.([]interface{})
if !ok {
return nil, fmt.Errorf("Invalid response from redis server")
}
var rslt []string = make([]string, len(dat))
for k, v := range dat {
rslt[k] = unikv.TrimPrefix(d.prefix, string(v.([]byte)))
}
return rslt, err
}

// Close closes driver
Expand Down
11 changes: 10 additions & 1 deletion unikvd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var namespaces map[string]*unikv.Namespace
var enableLog = true

func main() {
if len(os.Args) <= 2 {
if len(os.Args) < 2 {
fmt.Println(`USAGE: unikvd [LISTEN_ADDR] [--disable-log]`)
os.Exit(0)
}
Expand Down Expand Up @@ -171,6 +171,15 @@ func handle(rw http.ResponseWriter, r *http.Request) {
}
switch r.Method {
case "GET":
if key == "_list" {
rslt, err := bucket.List()
if err != nil {
resp500(rw, err.Error())
return
}
resp200(rw, rslt)
return
}
rslt, err := bucket.GetString(key)
if err == unikv.ErrNotFound {
resp404(rw)
Expand Down
10 changes: 9 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package unikv

import "encoding/json"
import (
"encoding/json"
"strings"
)

// ConcatPrefix concats two prefixes together
func ConcatPrefix(prefix string, str string) string {
Expand All @@ -14,6 +17,11 @@ func concatPrefix(prefix string, str string) string {
return ConcatPrefix(prefix, str)
}

// TrimPrefix trims the prefix of the given string
func TrimPrefix(prefix string, str string) string {
return strings.TrimPrefix(str, prefix)
}

type temporaryStringWriter struct {
buffer string
}
Expand Down

0 comments on commit 8c38565

Please sign in to comment.