diff --git a/kvstore/badger/errors.go b/kvstore/badger/errors.go index 6759b30..6bdb083 100644 --- a/kvstore/badger/errors.go +++ b/kvstore/badger/errors.go @@ -6,7 +6,7 @@ import ( var ( // ErrBadgerOpeningStore is returned when the badger store cannot be opened - // or an error occurs while opening/creating the KVStore + // or an error occurs while opening/creating the BadgerKVStore ErrBadgerOpeningStore = errors.New("error opening the store") // ErrBadgerUnableToSetValue is returned when the badger store fails to // set a value diff --git a/kvstore/badger/interface.go b/kvstore/badger/interface.go index 6780a5f..5691868 100644 --- a/kvstore/badger/interface.go +++ b/kvstore/badger/interface.go @@ -6,27 +6,46 @@ import ( "github.com/pokt-network/smt/kvstore" ) -// Ensure the KVStore can be used as an SMT node store -var _ kvstore.MapStore = (KVStore)(nil) +// Ensure the BadgerKVStore can be used as an SMT node store +var _ kvstore.MapStore = (BadgerKVStore)(nil) -// KVStore is an interface that defines a key-value store +// BadgerKVStore is an interface that defines a key-value store // that can be used standalone or as the node store for an SMT. -type KVStore interface { - // Store methods +// This is a superset of the MapStore interface that offers more +// features and can be used as a standalone key-value store. +type BadgerKVStore interface { + // --- Store methods --- + + // Get returns the value for a given key Get(key []byte) ([]byte, error) + // Set sets/updates the value for a given key Set(key, value []byte) error + // Delete removes a key Delete(key []byte) error - // Lifecycle methods + // --- Lifecycle methods --- + + // Stop closes the database connection, disabling any access to the store Stop() error - // Data methods + // --- Data methods --- + + // Backup creates a full backup of the store written to the provided writer Backup(writer io.Writer, incremental bool) error + // Restore loads the store from a backup in the reader provided Restore(io.Reader) error - // Accessors + // --- Accessors --- + + // GetAll returns all keys and values with the given prefix in the specified order GetAll(prefixKey []byte, descending bool) (keys, values [][]byte, err error) + // Exists returns true if the key exists Exists(key []byte) (bool, error) - ClearAll() error + // Len returns the number of key-value pairs in the store Len() int + + // --- Data management --- + + // ClearAll deletes all key-value pairs in the store + ClearAll() error } diff --git a/kvstore/badger/kvstore.go b/kvstore/badger/kvstore.go index 2d8edd4..07e2caf 100644 --- a/kvstore/badger/kvstore.go +++ b/kvstore/badger/kvstore.go @@ -11,16 +11,16 @@ const ( maxPendingWrites = 16 // used in backup restoration ) -var _ KVStore = &badgerKVStore{} +var _ BadgerKVStore = &badgerKVStore{} type badgerKVStore struct { db *badgerv4.DB lastBackup uint64 // timestamp of the most recent backup } -// NewKVStore creates a new KVStore using badger as the underlying database +// NewKVStore creates a new BadgerKVStore using badger as the underlying database // if no path for a persistence database is provided it will create one in-memory -func NewKVStore(path string) (KVStore, error) { +func NewKVStore(path string) (BadgerKVStore, error) { var db *badgerv4.DB var err error if path == "" { diff --git a/kvstore/badger/kvstore_test.go b/kvstore/badger/kvstore_test.go index bac9d3d..0f41b6d 100644 --- a/kvstore/badger/kvstore_test.go +++ b/kvstore/badger/kvstore_test.go @@ -393,7 +393,7 @@ func TestBadger_KVStore_Len(t *testing.T) { } } -func setupStore(t *testing.T, store badger.KVStore) { +func setupStore(t *testing.T, store badger.BadgerKVStore) { t.Helper() err := store.Set([]byte("foo"), []byte("bar")) require.NoError(t, err) diff --git a/kvstore/interfaces.go b/kvstore/interfaces.go index 0d0ac53..e0c92a4 100644 --- a/kvstore/interfaces.go +++ b/kvstore/interfaces.go @@ -5,11 +5,18 @@ package kvstore // store requires in order to back an SM(S)T. type MapStore interface { // --- Accessors --- + + // Get returns the value for a given key Get(key []byte) ([]byte, error) + // Set sets/updates the value for a given key Set(key, value []byte) error + // Delete removes a key Delete(key []byte) error + // Len returns the number of key-value pairs in the store Len() int // --- Debug --- + + // ClearAll deletes all key-value pairs in the store ClearAll() error }