cosmos-sdk/store/types.go

133 lines
3.4 KiB
Go
Raw Normal View History

2017-10-31 13:45:57 -07:00
package store
import (
"github.com/tendermint/go-wire/data"
)
type CommitID struct {
2017-12-01 08:52:54 -08:00
Version int64
2017-10-31 13:45:57 -07:00
Hash []byte
}
2017-12-01 09:24:02 -08:00
func (cid CommitID) IsZero() bool {
return cid.Version == 0 && len(cid.Hash) == 0
2017-12-01 08:52:54 -08:00
}
2017-10-31 13:45:57 -07:00
type Committer interface {
// Commit persists the state to disk.
Commit() CommitID
}
type CommitterLoader func(id CommitID) (Committer, error)
// CacheWriter is returned from CacheWrap and knows how to
// write its cached changes to its parent
type CacheWriter interface {
// Write must write to the
Write() error
}
// CacheWrappable is anything that can be wrapped with a cache.
type CacheWrappable interface {
// CacheWrap() wraps a thing with a cache. After calling
// .Write() on the CacheWrap, all previous CacheWraps on the
// object expire.
//
// CacheWrap() should not return a Committer, since Commit() on
// CacheWraps make no sense. It can return KVStore, IterKVStore,
// etc.
//
// NOTE: https://dave.cheney.net/2017/07/22/should-go-2-0-support-generics.
// The returned object may or may not implement CacheWrap() as well.
CacheWrap() CacheWriter
}
2017-10-31 13:45:57 -07:00
// KVStore is a simple interface to get/set data
type KVStore interface {
Set(key, value []byte) (prev []byte)
Get(key []byte) (value []byte, exists bool)
Has(key []byte) (exists bool)
Remove(key []byte) (prev []byte, removed bool)
2017-12-01 09:24:02 -08:00
// CacheKVStore() wraps a thing with a cache. After
// calling .Write() on the CacheKVStore, all previous
// CacheWraps on the object expire.
CacheKVStore() CacheKVStore
}
type CacheKVStore interface {
KVStore
Write() // Writes operations to underlying KVStore
2017-10-31 13:45:57 -07:00
}
// IterKVStore can be iterated on
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
type IterKVStore interface {
2017-12-01 09:24:02 -08:00
KVStore
2017-10-31 13:45:57 -07:00
Iterator(start, end []byte) Iterator
ReverseIterator(start, end []byte) Iterator
First(start, end []byte) (kv KVPair, ok bool)
Last(start, end []byte) (kv KVPair, ok bool)
2017-12-01 09:24:02 -08:00
// CacheIterKVStore() wraps a thing with a cache.
// After calling .Write() on the CacheIterKVStore, all
// previous CacheWraps on the object expire.
CacheIterKVStore() CacheIterKVStore
}
type CacheIterKVStore interface {
IterKVStore
Write() // Writes operations to underlying KVStore
2017-10-31 13:45:57 -07:00
}
type KVPair struct {
Key data.Bytes
Value data.Bytes
}
/*
Usage:
for itr := kvm.Iterator(start, end); itr.Valid(); itr.Next() {
k, v := itr.Key(); itr.Value()
....
}
*/
type Iterator interface {
// The start & end (exclusive) limits to iterate over.
// If end < start, then the Iterator goes in reverse order.
// A domain of ([]byte{12, 13}, []byte{12, 14}) will iterate
// over anything with the prefix []byte{12, 13}
Domain() (start []byte, end []byte)
// Returns if the current position is valid.
Valid() bool
// Next moves the iterator to the next key/value pair.
//
// If Valid returns false, this method will panic.
Next()
// Key returns the key of the current key/value pair, or nil if done.
// The caller should not modify the contents of the returned slice, and
// its contents may change after calling Next().
//
// If Valid returns false, this method will panic.
Key() []byte
// Value returns the key of the current key/value pair, or nil if done.
// The caller should not modify the contents of the returned slice, and
// its contents may change after calling Next().
//
// If Valid returns false, this method will panic.
Value() []byte
// Releases any resources and iteration-locks
Release()
}