storj/storage/redis/client.go
Natalie Villasana 80727ae90b adds netstate pagination (#95)
* adds netstate rpc server pagination, mocks pagination in test/util.go

* updates ns client example, combines ns client and server test to netstate_test, adds pagination to bolt client

* better organizes netstate test calls

* wip breaking netstate test into smaller tests

* wip modularizing netstate tests

* adds some test panics

* wip netstate test attempts

* testing bug in netstate TestDeleteAuth

* wip fixes global variable problem, still issues with list

* wip fixes get request params and args

* fixes bug in path when using MakePointers helper fn

* updates mockdb list func, adds test, changes Limit to int

* fixes merge conflicts

* fixes broken tests from merge

* remove unnecessary PointerEntry struct

* removes error when Get returns nil value from boltdb

* breaks boltdb client tests into smaller tests

* renames AssertNoErr test helper to HandleErr

* adds StartingKey and Limit parameters to redis list func, adds beginning of redis tests

* adds helper func for mockdb List function

* if no starting key provided for netstate List, the first value in storage will be used

* adds basic pagination for redis List function, adds tests

* adds list limit to call in overlay/server.go

* streamlines/fixes some nits from review

* removes use of obsolete EncryptedUnencryptedSize

* uses MockKeyValueStore instead of redis instance in redis client test

* changes test to expect nil returned for getting missing key

* remove error from `KeyValueStore#Get`

* fix bolt test

* Merge pull request #1 from bryanchriswhite/nat-pagination

remove error from `KeyValueStore#Get`

* adds Get returning error back to KeyValueStore interface and affected clients

* trying to appease travis: returns errors in Get calls in overlay/cache and cache_test

* handles redis get error when no key found
2018-06-29 16:06:25 -04:00

123 lines
2.8 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package redis
import (
"fmt"
"time"
"github.com/go-redis/redis"
"github.com/zeebo/errs"
"storj.io/storj/storage"
)
var (
// Error is a redis error
Error = errs.Class("redis error")
)
const (
defaultNodeExpiration = 61 * time.Minute
)
// redisClient is the entrypoint into Redis
type redisClient struct {
db *redis.Client
TTL time.Duration
}
// NewClient returns a configured Client instance, verifying a sucessful connection to redis
func NewClient(address, password string, db int) (storage.KeyValueStore, error) {
c := &redisClient{
db: redis.NewClient(&redis.Options{
Addr: address,
Password: password,
DB: db,
}),
TTL: defaultNodeExpiration,
}
// ping here to verify we are able to connect to redis with the initialized client.
if err := c.db.Ping().Err(); err != nil {
return nil, Error.New("ping failed", err)
}
return c, nil
}
// Get looks up the provided key from redis returning either an error or the result.
func (c *redisClient) Get(key storage.Key) (storage.Value, error) {
b, err := c.db.Get(string(key)).Bytes()
if err != nil {
if err.Error() == "redis: nil" {
return nil, nil
}
// TODO: log
return nil, Error.New("get error", err)
}
return b, nil
}
// Put adds a value to the provided key in redis, returning an error on failure.
func (c *redisClient) Put(key storage.Key, value storage.Value) error {
v, err := value.MarshalBinary()
if err != nil {
return Error.New("put error", err)
}
err = c.db.Set(key.String(), v, c.TTL).Err()
if err != nil {
return Error.New("put error", err)
}
return nil
}
// List returns either a list of keys for which boltdb has values or an error.
func (c *redisClient) List(startingKey storage.Key, limit storage.Limit) (storage.Keys, error) {
var noOrderKeys []string
if startingKey != nil {
_, cursor, err := c.db.Scan(0, fmt.Sprintf("%s", startingKey), int64(limit)).Result()
if err != nil {
return nil, Error.New("list error with starting key", err)
}
keys, _, err := c.db.Scan(cursor, "", int64(limit)).Result()
if err != nil {
return nil, Error.New("list error with starting key", err)
}
noOrderKeys = keys
} else if startingKey == nil {
keys, _, err := c.db.Scan(0, "", int64(limit)).Result()
if err != nil {
return nil, Error.New("list error without starting key", err)
}
noOrderKeys = keys
}
listKeys := make(storage.Keys, len(noOrderKeys))
for i, k := range noOrderKeys {
listKeys[i] = storage.Key(k)
}
return listKeys, nil
}
// Delete deletes a key/value pair from redis, for a given the key
func (c *redisClient) Delete(key storage.Key) error {
err := c.db.Del(key.String()).Err()
if err != nil {
return Error.New("delete error", err)
}
return err
}
// Close closes a redis client
func (c *redisClient) Close() error {
return c.db.Close()
}