Update store types to new spec

This commit is contained in:
Jae Kwon 2017-12-01 09:24:02 -08:00
parent 0919737c00
commit 6a3a37d5a6
1 changed files with 23 additions and 21 deletions

View File

@ -9,7 +9,7 @@ type CommitID struct {
Hash []byte Hash []byte
} }
type (cid CommitID) IsZero() bool { func (cid CommitID) IsZero() bool {
return cid.Version == 0 && len(cid.Hash) == 0 return cid.Version == 0 && len(cid.Hash) == 0
} }
@ -21,42 +21,44 @@ type Committer interface {
type CommitterLoader func(id CommitID) (Committer, error) type CommitterLoader func(id CommitID) (Committer, error)
// A Store 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() interface{}
}
// KVStore is a simple interface to get/set data // KVStore is a simple interface to get/set data
type KVStore interface { type KVStore interface {
CacheWrappable // CacheWrap returns KVStore
Set(key, value []byte) (prev []byte) Set(key, value []byte) (prev []byte)
Get(key []byte) (value []byte, exists bool) Get(key []byte) (value []byte, exists bool)
Has(key []byte) (exists bool) Has(key []byte) (exists bool)
Remove(key []byte) (prev []byte, removed bool) Remove(key []byte) (prev []byte, removed bool)
// 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
} }
// IterKVStore can be iterated on // IterKVStore can be iterated on
// CONTRACT: No writes may happen within a domain while an iterator exists over it. // CONTRACT: No writes may happen within a domain while an iterator exists over it.
type IterKVStore interface { type IterKVStore interface {
KVStore // CacheWrap returns IterKVMap KVStore
Iterator(start, end []byte) Iterator Iterator(start, end []byte) Iterator
ReverseIterator(start, end []byte) Iterator ReverseIterator(start, end []byte) Iterator
First(start, end []byte) (kv KVPair, ok bool) First(start, end []byte) (kv KVPair, ok bool)
Last(start, end []byte) (kv KVPair, ok bool) Last(start, end []byte) (kv KVPair, ok bool)
// 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
} }
type KVPair struct { type KVPair struct {