Refactor database code to support standalone batches, transactions.

This is spin-off of changes from #459.

Transactions are not being used yet, but batches are updated to work
with the new API.

`database/` package was refactored to split abstract interfaces and
implementation via goleveldb. This should make it easier to implement
new database types.
This commit is contained in:
Andrey Smirnov
2019-08-02 00:10:36 +03:00
committed by Andrey Smirnov
parent 26098f6c8d
commit 67e38955ae
23 changed files with 539 additions and 315 deletions
+69
View File
@@ -0,0 +1,69 @@
// Package database provides KV database for meta-information
package database
import "errors"
// Errors for Storage
var (
ErrNotFound = errors.New("key not found")
)
// StorageProcessor is a function to process one single storage entry
type StorageProcessor func(key []byte, value []byte) error
// Reader provides KV read calls
type Reader interface {
Get(key []byte) ([]byte, error)
}
// PrefixReader provides prefixed operations
type PrefixReader interface {
HasPrefix(prefix []byte) bool
ProcessByPrefix(prefix []byte, proc StorageProcessor) error
KeysByPrefix(prefix []byte) [][]byte
FetchByPrefix(prefix []byte) [][]byte
}
// Writer provides KV update/delete calls
type Writer interface {
Put(key []byte, value []byte) error
Delete(key []byte) error
}
// Storage is an interface to KV storage
type Storage interface {
Reader
Writer
PrefixReader
CreateBatch() Batch
OpenTransaction() (Transaction, error)
CreateTemporary() (Storage, error)
Open() error
Close() error
CompactDB() error
Drop() error
}
// Batch provides a way to pack many writes.
type Batch interface {
Writer
// Write closes batch and send accumulated writes to the database
Write() error
}
// Transaction provides isolated atomic way to perform updates.
//
// Transactions might be expensive.
// Transaction should always finish with either Discard() or Commit()
type Transaction interface {
Reader
Writer
Commit() error
Discard()
}