Skip to content

Commit

Permalink
chore: interface commennts
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law committed Jan 3, 2024
1 parent f927cf2 commit 6850a8a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion kvstore/badger/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 28 additions & 9 deletions kvstore/badger/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions kvstore/badger/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion kvstore/badger/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions kvstore/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6850a8a

Please sign in to comment.