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"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2018-09-05 17:10:35 +01:00
|
|
|
"go.uber.org/zap"
|
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
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Logger implements a zap.Logger for storage.KeyValueStore.
|
2018-09-05 17:10:35 +01:00
|
|
|
type Logger struct {
|
|
|
|
log *zap.Logger
|
|
|
|
store storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// New creates a new Logger with log and store.
|
2018-09-05 17:10:35 +01:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:00:46 +00:00
|
|
|
// LookupLimit returns the maximum limit that is allowed.
|
|
|
|
func (store *Logger) LookupLimit() int { return store.store.LookupLimit() }
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DeleteMultiple deletes keys ignoring missing keys.
|
2020-01-28 20:52:04 +00:00
|
|
|
func (store *Logger) DeleteMultiple(ctx context.Context, keys []storage.Key) (_ storage.Items, err error) {
|
|
|
|
defer mon.Task()(&ctx, len(keys))(&err)
|
|
|
|
store.log.Debug("DeleteMultiple", zap.Any("keys", keys))
|
|
|
|
return store.store.DeleteMultiple(ctx, keys)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// 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),
|
|
|
|
)
|
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)
|
2020-05-05 07:51:24 +01:00
|
|
|
if ok {
|
|
|
|
store.log.Debug(" ",
|
|
|
|
zap.ByteString("key", item.Key),
|
|
|
|
zap.Int("value length", len(item.Value)),
|
|
|
|
zap.Binary("truncated value", truncate(item.Value)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateWithoutLookupLimit calls the callback with an iterator over the keys, but doesn't enforce default limit on opts.
|
|
|
|
func (store *Logger) IterateWithoutLookupLimit(ctx context.Context, opts storage.IterateOptions, fn func(context.Context, storage.Iterator) error) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
store.log.Debug("IterateWithoutLookupLimit",
|
|
|
|
zap.ByteString("prefix", opts.Prefix),
|
|
|
|
zap.ByteString("first", opts.First),
|
|
|
|
zap.Bool("recurse", opts.Recurse),
|
|
|
|
)
|
|
|
|
return store.store.IterateWithoutLookupLimit(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
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes the store.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (store *Logger) Close() error {
|
|
|
|
store.log.Debug("Close")
|
|
|
|
return store.store.Close()
|
|
|
|
}
|
2019-01-09 11:18:23 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// CompareAndSwap atomically compares and swaps oldValue with newValue.
|
2019-07-23 20:46:33 +01:00
|
|
|
func (store *Logger) CompareAndSwap(ctx context.Context, key storage.Key, oldValue, newValue storage.Value) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
store.log.Debug("CompareAndSwap", zap.ByteString("key", key),
|
|
|
|
zap.Int("old value length", len(oldValue)), zap.Int("new value length", len(newValue)),
|
|
|
|
zap.Binary("truncated old value", truncate(oldValue)), zap.Binary("truncated new value", truncate(newValue)))
|
|
|
|
return store.store.CompareAndSwap(ctx, key, oldValue, newValue)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|