frostfs-node/pkg/local_object_storage/kvio/kvio.go
Alejandro Lopez 00c8712dc6 [#421] Create common interface for kv repositories
Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
2023-06-20 12:54:53 +03:00

88 lines
2.6 KiB
Go

// Package kvio provides basic interfaces to key-value I/O primitives.
// It serves as a wrapper for exisiting implementations such as those
// provided by popular key-value stores (bbolt, badger, leveldb, etc.)
// and can serve to compose them as well.
package kvio
type (
Key []byte
Value []byte
)
// Writer is the interface that wraps the Write method for a key-value entry.
type Writer interface {
Write(Key, Value) error
}
// Reader is the interface that wraps the Read method for a key-value entry.
type Reader interface {
Read(Key, func(Value) error) error
}
// Deleter is the interface that wraps the Delete method for a key-value entry.
type Deleter interface {
Delete(Key) error
}
// KeyCursor is the interface of cursors that can iterate over a set of keys.
type KeyCursor interface {
// Key returns the key of the current entry, or nil if at the end of the cursor.
Key() Key
// Rewind rewinds the cursor to the first key in lexicographical order.
Rewind()
// Seek moves the cursor to the smallest key that is equal or greater than the
// provided key in lexicographical order.
Seek(Key)
// Next moves the cursor to the next key in lexicographical order.
Next()
// Close frees the resources allocated by the cursor and invalidates it.
Close()
}
// Cursor is the interface of cursors that can iterate over a set of key-value entries.
type Cursor interface {
KeyCursor
// Value calls the given function with the value of the current entry, or nil if
// at the end of the cursor.
Value(func(Value) error) error
}
// Iterator is the interface which provides cursors over sets of key-value entries.
type Iterator interface {
// IterateKeys provides a cursor to iterate the key set in lexicographical order.
IterateKeys() KeyCursor
// IterateKeys provides a cursor to iterate the key-value set in lexicographical order of keys.
Iterate() Cursor
}
// ReadOnlyTx is the interface of read-only transactions for key-value repositories.
type ReadOnlyTx interface {
Reader
Iterator
}
// WriteOnlyTx is the interface of write-only transactions for key-value repositories.
type WriteOnlyTx interface {
Writer
Deleter
}
// ReadWriteTx is the interface of read-write transactions for key-value repositories.
type ReadWriteTx interface {
ReadOnlyTx
WriteOnlyTx
}
// Stats are the common statistics for key-value repositories.
type Stats struct {
KeyCount uint64
}
// Repository is the interface of key-value repositories.
type Repository interface {
Read(func(ReadOnlyTx) error) error
Write(func(WriteOnlyTx) error) error
ReadWrite(func(ReadWriteTx) error) error
Stats() (Stats, error)
Close() error
}