2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-06-13 19:22:32 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
package kvstore
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-08-14 16:22:29 +01:00
|
|
|
import (
|
2018-09-05 17:10:35 +01:00
|
|
|
"bytes"
|
2019-06-05 15:23:10 +01:00
|
|
|
"context"
|
2018-09-05 17:10:35 +01:00
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2018-08-14 16:22:29 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// Delimiter separates nested paths in storage.
|
2018-09-05 17:10:35 +01:00
|
|
|
const Delimiter = '/'
|
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
// ErrKeyNotFound used when something doesn't exist.
|
2018-08-14 16:22:29 +01:00
|
|
|
var ErrKeyNotFound = errs.Class("key not found")
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// ErrEmptyKey is returned when an empty key is used in Put or in CompareAndSwap.
|
2018-11-15 15:31:33 +00:00
|
|
|
var ErrEmptyKey = errs.Class("empty key")
|
2018-09-05 17:10:35 +01:00
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// Key is the type for the keys in a `Store`.
|
2018-06-13 19:22:32 +01:00
|
|
|
type Key []byte
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// Value is the type for the values in a `ValueValueStore`.
|
2018-06-13 19:22:32 +01:00
|
|
|
type Value []byte
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// Keys is the type for a slice of keys in a `Store`.
|
2018-06-13 19:22:32 +01:00
|
|
|
type Keys []Key
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// Values is the type for a slice of Values in a `Store`.
|
2018-08-01 15:15:38 +01:00
|
|
|
type Values []Value
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// Items keeps all Item.
|
|
|
|
type Items []Item
|
2018-08-26 04:00:49 +01:00
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// Item returns Key, Value, IsPrefix.
|
|
|
|
type Item struct {
|
2018-08-26 04:00:49 +01:00
|
|
|
Key Key
|
|
|
|
Value Value
|
|
|
|
IsPrefix bool
|
|
|
|
}
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// Store describes key/value stores like redis and boltdb.
|
|
|
|
type Store interface {
|
2020-02-17 07:56:13 +00:00
|
|
|
// Put adds a value to store.
|
2019-06-05 15:23:10 +01:00
|
|
|
Put(context.Context, Key, Value) error
|
2020-02-17 07:56:13 +00:00
|
|
|
// Get gets a value to store.
|
2019-06-05 15:23:10 +01:00
|
|
|
Get(context.Context, Key) (Value, error)
|
2020-02-17 07:56:13 +00:00
|
|
|
// Delete deletes key and the value.
|
2019-06-05 15:23:10 +01:00
|
|
|
Delete(context.Context, Key) error
|
2023-04-06 10:53:25 +01:00
|
|
|
// Range iterates over all items in unspecified order.
|
|
|
|
// The Key and Value are valid only for the duration of callback.
|
|
|
|
Range(ctx context.Context, fn func(context.Context, Key, Value) error) error
|
2020-02-17 07:56:13 +00:00
|
|
|
// Close closes the store.
|
2018-06-13 19:22:32 +01:00
|
|
|
Close() error
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:44:23 +01:00
|
|
|
// IsZero returns true if the value struct is a zero value.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (value Value) IsZero() bool {
|
|
|
|
return len(value) == 0
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:44:23 +01:00
|
|
|
// IsZero returns true if the key struct is a zero value.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (key Key) IsZero() bool {
|
|
|
|
return len(key) == 0
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// MarshalBinary implements the encoding.BinaryMarshaler interface for the Value type.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (value Value) MarshalBinary() ([]byte, error) {
|
|
|
|
return value, nil
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// MarshalBinary implements the encoding.BinaryMarshaler interface for the Key type.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (key Key) MarshalBinary() ([]byte, error) {
|
|
|
|
return key, nil
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// ByteSlices converts a `Keys` struct to a slice of byte-slices (i.e. `[][]byte`).
|
2018-09-05 17:10:35 +01:00
|
|
|
func (keys Keys) ByteSlices() [][]byte {
|
|
|
|
result := make([][]byte, len(keys))
|
2018-06-13 19:22:32 +01:00
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
for key, val := range keys {
|
|
|
|
result[key] = []byte(val)
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// String implements the Stringer interface.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (key Key) String() string { return string(key) }
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// Strings returns everything as strings.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (keys Keys) Strings() []string {
|
|
|
|
strs := make([]string, 0, len(keys))
|
|
|
|
for _, key := range keys {
|
|
|
|
strs = append(strs, string(key))
|
|
|
|
}
|
|
|
|
return strs
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
2018-08-26 04:00:49 +01:00
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
// GetKeys gets all the Keys in []Item and converts them to Keys.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (items Items) GetKeys() Keys {
|
|
|
|
if len(items) == 0 {
|
2018-08-26 04:00:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var keys Keys
|
2018-09-05 17:10:35 +01:00
|
|
|
for _, item := range items {
|
2018-08-26 04:00:49 +01:00
|
|
|
keys = append(keys, item.Key)
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
// Len is the number of elements in the collection.
|
|
|
|
func (items Items) Len() int { return len(items) }
|
|
|
|
|
|
|
|
// Less reports whether the element with
|
|
|
|
// index i should sort before the element with index j.
|
|
|
|
func (items Items) Less(i, k int) bool { return items[i].Less(items[k]) }
|
|
|
|
|
|
|
|
// Swap swaps the elements with indexes i and j.
|
|
|
|
func (items Items) Swap(i, k int) { items[i], items[k] = items[k], items[i] }
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// Less returns whether item should be sorted before b.
|
2023-04-06 14:54:41 +01:00
|
|
|
func (item Item) Less(b Item) bool { return item.Key.Less(b.Key) }
|
2018-09-05 17:10:35 +01:00
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// Less returns whether key should be sorted before b.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (key Key) Less(b Key) bool { return bytes.Compare([]byte(key), []byte(b)) < 0 }
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// Equal returns whether key and b are equal.
|
2018-09-05 17:10:35 +01:00
|
|
|
func (key Key) Equal(b Key) bool { return bytes.Equal([]byte(key), []byte(b)) }
|