2018-04-18 17:55:28 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-04-18 16:34:15 +01:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2018-06-29 21:06:25 +01:00
|
|
|
"fmt"
|
2018-04-18 16:34:15 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
2018-06-13 19:22:32 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/storage"
|
2018-04-18 16:34:15 +01:00
|
|
|
)
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
var (
|
|
|
|
// Error is a redis error
|
|
|
|
Error = errs.Class("redis error")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultNodeExpiration = 61 * time.Minute
|
2018-08-01 15:15:38 +01:00
|
|
|
maxKeyLookup = 100
|
2018-06-19 19:03:46 +01:00
|
|
|
)
|
2018-04-18 16:34:15 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
// Client is the entrypoint into Redis
|
|
|
|
type Client struct {
|
2018-06-13 19:22:32 +01:00
|
|
|
db *redis.Client
|
|
|
|
TTL time.Duration
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// NewClient returns a configured Client instance, verifying a sucessful connection to redis
|
2018-08-01 15:15:38 +01:00
|
|
|
func NewClient(address, password string, db int) (*Client, error) {
|
|
|
|
c := &Client{
|
2018-06-13 19:22:32 +01:00
|
|
|
db: redis.NewClient(&redis.Options{
|
2018-04-18 16:34:15 +01:00
|
|
|
Addr: address,
|
|
|
|
Password: password,
|
|
|
|
DB: db,
|
|
|
|
}),
|
2018-06-13 19:22:32 +01:00
|
|
|
TTL: defaultNodeExpiration,
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// ping here to verify we are able to connect to redis with the initialized client.
|
|
|
|
if err := c.db.Ping().Err(); err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return nil, Error.New("ping failed: %v", err)
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// Get looks up the provided key from redis returning either an error or the result.
|
2018-08-01 15:15:38 +01:00
|
|
|
func (c *Client) Get(key storage.Key) (storage.Value, error) {
|
2018-06-19 19:03:46 +01:00
|
|
|
b, err := c.db.Get(string(key)).Bytes()
|
2018-08-14 16:22:29 +01:00
|
|
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil, storage.ErrKeyNotFound.New(key.String())
|
|
|
|
}
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
if err != nil {
|
2018-06-29 21:06:25 +01:00
|
|
|
if err.Error() == "redis: nil" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: log
|
2018-08-22 07:39:57 +01:00
|
|
|
return nil, Error.New("get error: %v", err)
|
2018-06-19 19:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put adds a value to the provided key in redis, returning an error on failure.
|
2018-08-01 15:15:38 +01:00
|
|
|
func (c *Client) Put(key storage.Key, value storage.Value) error {
|
2018-06-13 19:22:32 +01:00
|
|
|
v, err := value.MarshalBinary()
|
|
|
|
|
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return Error.New("put error: %v", err)
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 19:03:46 +01:00
|
|
|
err = c.db.Set(key.String(), v, c.TTL).Err()
|
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return Error.New("put error: %v", err)
|
2018-06-19 19:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
// List returns either a list of keys for which boltdb has values or an error.
|
|
|
|
func (c *Client) List(startingKey storage.Key, limit storage.Limit) (storage.Keys, error) {
|
2018-06-29 21:06:25 +01:00
|
|
|
var noOrderKeys []string
|
|
|
|
if startingKey != nil {
|
|
|
|
_, cursor, err := c.db.Scan(0, fmt.Sprintf("%s", startingKey), int64(limit)).Result()
|
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return nil, Error.New("list error with starting key: %v", err)
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
keys, _, err := c.db.Scan(cursor, "", int64(limit)).Result()
|
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return nil, Error.New("list error with starting key: %v", err)
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
noOrderKeys = keys
|
|
|
|
} else if startingKey == nil {
|
|
|
|
keys, _, err := c.db.Scan(0, "", int64(limit)).Result()
|
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return nil, Error.New("list error without starting key: %v", err)
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
noOrderKeys = keys
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
listKeys := make(storage.Keys, len(noOrderKeys))
|
|
|
|
for i, k := range noOrderKeys {
|
|
|
|
listKeys[i] = storage.Key(k)
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:06:25 +01:00
|
|
|
return listKeys, nil
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
2018-04-18 16:34:15 +01:00
|
|
|
|
2018-08-26 04:00:49 +01:00
|
|
|
//ListV2 is the new definition and will replace `List` definition
|
|
|
|
func (c *Client) ListV2(opts storage.ListOptions) (storage.Items, storage.More, error) {
|
|
|
|
//TODO write the implementation
|
|
|
|
panic("to do")
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
// ReverseList returns either a list of keys for which redis has values or an error.
|
|
|
|
// Starts from startingKey and iterates backwards
|
2018-08-01 15:15:38 +01:00
|
|
|
func (c *Client) ReverseList(startingKey storage.Key, limit storage.Limit) (storage.Keys, error) {
|
2018-07-30 20:25:18 +01:00
|
|
|
//TODO
|
|
|
|
return storage.Keys{}, nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// Delete deletes a key/value pair from redis, for a given the key
|
2018-08-01 15:15:38 +01:00
|
|
|
func (c *Client) Delete(key storage.Key) error {
|
2018-06-19 19:03:46 +01:00
|
|
|
err := c.db.Del(key.String()).Err()
|
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
return Error.New("delete error: %v", err)
|
2018-06-19 19:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// Close closes a redis client
|
2018-08-01 15:15:38 +01:00
|
|
|
func (c *Client) Close() error {
|
2018-06-13 19:22:32 +01:00
|
|
|
return c.db.Close()
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
2018-08-01 15:15:38 +01:00
|
|
|
|
|
|
|
// GetAll is the bulk method for gets from the redis data store
|
|
|
|
// The maximum keys returned will be 100. If more than that is requested an
|
|
|
|
// error will be returned
|
|
|
|
func (c *Client) GetAll(keys storage.Keys) (storage.Values, error) {
|
|
|
|
lk := len(keys)
|
|
|
|
if lk > maxKeyLookup {
|
|
|
|
return nil, Error.New(fmt.Sprintf("requested %d keys, maximum is %d", lk, maxKeyLookup))
|
|
|
|
}
|
|
|
|
|
|
|
|
ks := make([]string, lk)
|
|
|
|
for i, v := range keys {
|
|
|
|
ks[i] = v.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
vs, err := c.db.MGet(ks...).Result()
|
|
|
|
if err != nil {
|
|
|
|
return []storage.Value{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
values := []storage.Value{}
|
|
|
|
for _, v := range vs {
|
2018-08-20 19:21:41 +01:00
|
|
|
values = append(values, storage.Value([]byte(v.(string))))
|
2018-08-01 15:15:38 +01:00
|
|
|
}
|
|
|
|
return values, nil
|
|
|
|
}
|