2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-05 17:10:35 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storelogger
|
|
|
|
|
|
|
|
import (
|
2019-06-05 15:23:10 +01:00
|
|
|
"context"
|
2018-09-05 17:10:35 +01:00
|
|
|
"strconv"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2019-06-05 15:23:10 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-10-16 12:43:44 +01:00
|
|
|
|
|
|
|
"storj.io/storj/storage"
|
2018-09-05 17:10:35 +01:00
|
|
|
)
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
var id int64
|
|
|
|
|
|
|
|
// Logger implements a zap.Logger for storage.KeyValueStore
|
|
|
|
type Logger struct {
|
|
|
|
log *zap.Logger
|
|
|
|
store storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Logger with log and store
|
|
|
|
func New(log *zap.Logger, store storage.KeyValueStore) *Logger {
|
|
|
|
loggerid := atomic.AddInt64(&id, 1)
|
|
|
|
name := strconv.Itoa(int(loggerid))
|
|
|
|
return &Logger{log.Named(name), store}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put adds a value to store
|
2019-06-05 15:23:10 +01:00
|
|
|
func (store *Logger) Put(ctx context.Context, key storage.Key, value storage.Value) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-18 00:37:44 +01:00
|
|
|
store.log.Debug("Put", zap.ByteString("key", key), zap.Int("value length", len(value)), zap.Binary("truncated value", truncate(value)))
|
2019-06-05 15:23:10 +01:00
|
|
|
return store.store.Put(ctx, key, value)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets a value to store
|
2019-06-05 15:23:10 +01:00
|
|
|
func (store *Logger) Get(ctx context.Context, key storage.Key) (_ storage.Value, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-18 00:37:44 +01:00
|
|
|
store.log.Debug("Get", zap.ByteString("key", key))
|
2019-06-05 15:23:10 +01:00
|
|
|
return store.store.Get(ctx, key)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll gets all values from the store corresponding to keys
|
2019-06-05 15:23:10 +01:00
|
|
|
func (store *Logger) GetAll(ctx context.Context, keys storage.Keys) (_ storage.Values, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-05 17:10:35 +01:00
|
|
|
store.log.Debug("GetAll", zap.Any("keys", keys))
|
2019-06-05 15:23:10 +01:00
|
|
|
return store.store.GetAll(ctx, keys)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes key and the value
|
2019-06-05 15:23:10 +01:00
|
|
|
func (store *Logger) Delete(ctx context.Context, key storage.Key) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-18 00:37:44 +01:00
|
|
|
store.log.Debug("Delete", zap.ByteString("key", key))
|
2019-06-05 15:23:10 +01:00
|
|
|
return store.store.Delete(ctx, key)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List lists all keys starting from first and upto limit items
|
2019-06-05 15:23:10 +01:00
|
|
|
func (store *Logger) List(ctx context.Context, first storage.Key, limit int) (_ storage.Keys, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
keys, err := store.store.List(ctx, first, limit)
|
2019-06-18 00:37:44 +01:00
|
|
|
store.log.Debug("List", zap.ByteString("first", first), zap.Int("limit", limit), zap.Strings("keys", keys.Strings()))
|
2018-09-05 17:10:35 +01:00
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate iterates over items based on opts
|
2019-06-05 15:23:10 +01:00
|
|
|
func (store *Logger) Iterate(ctx context.Context, opts storage.IterateOptions, fn func(context.Context, storage.Iterator) error) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-05 17:10:35 +01:00
|
|
|
store.log.Debug("Iterate",
|
2019-06-18 00:37:44 +01:00
|
|
|
zap.ByteString("prefix", opts.Prefix),
|
|
|
|
zap.ByteString("first", opts.First),
|
2018-09-05 17:10:35 +01:00
|
|
|
zap.Bool("recurse", opts.Recurse),
|
|
|
|
zap.Bool("reverse", opts.Reverse),
|
|
|
|
)
|
2019-06-05 15:23:10 +01:00
|
|
|
return store.store.Iterate(ctx, opts, func(ctx context.Context, it storage.Iterator) error {
|
|
|
|
return fn(ctx, storage.IteratorFunc(func(ctx context.Context, item *storage.ListItem) bool {
|
|
|
|
ok := it.Next(ctx, item)
|
2018-09-05 17:10:35 +01:00
|
|
|
if ok {
|
2019-01-09 11:18:23 +00:00
|
|
|
store.log.Debug(" ",
|
2019-06-18 00:37:44 +01:00
|
|
|
zap.ByteString("key", item.Key),
|
2019-01-09 11:18:23 +00:00
|
|
|
zap.Int("value length", len(item.Value)),
|
|
|
|
zap.Binary("truncated value", truncate(item.Value)),
|
|
|
|
)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the store
|
|
|
|
func (store *Logger) Close() error {
|
|
|
|
store.log.Debug("Close")
|
|
|
|
return store.store.Close()
|
|
|
|
}
|
2019-01-09 11:18:23 +00:00
|
|
|
|
|
|
|
func truncate(v storage.Value) (t []byte) {
|
|
|
|
if len(v)-1 < 10 {
|
|
|
|
t = []byte(v)
|
|
|
|
} else {
|
|
|
|
t = v[:10]
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|