storage: remove Batch from Store
We never use it for real underlying stores, so these implementations are useless (everything goes though PutChangeSet now).
This commit is contained in:
parent
35bdfc5eca
commit
17a43b19e0
4 changed files with 2 additions and 117 deletions
|
@ -81,12 +81,6 @@ func (s *BoltDBStore) Delete(key []byte) error {
|
|||
})
|
||||
}
|
||||
|
||||
// PutBatch implements the Store interface.
|
||||
func (s *BoltDBStore) PutBatch(batch Batch) error {
|
||||
memBatch := batch.(*MemoryBatch)
|
||||
return s.PutChangeSet(memBatch.mem, memBatch.stor)
|
||||
}
|
||||
|
||||
// PutChangeSet implements the Store interface.
|
||||
func (s *BoltDBStore) PutChangeSet(puts map[string][]byte, stores map[string][]byte) error {
|
||||
var err error
|
||||
|
@ -168,12 +162,6 @@ func boltSeek(txopener func(func(*bbolt.Tx) error) error, rng SeekRange, f func(
|
|||
})
|
||||
}
|
||||
|
||||
// Batch implements the Batch interface and returns a boltdb
|
||||
// compatible Batch.
|
||||
func (s *BoltDBStore) Batch() Batch {
|
||||
return newMemoryBatch()
|
||||
}
|
||||
|
||||
// Close releases all db resources.
|
||||
func (s *BoltDBStore) Close() error {
|
||||
return s.db.Close()
|
||||
|
|
|
@ -55,12 +55,6 @@ func (s *LevelDBStore) Delete(key []byte) error {
|
|||
return s.db.Delete(key, nil)
|
||||
}
|
||||
|
||||
// PutBatch implements the Store interface.
|
||||
func (s *LevelDBStore) PutBatch(batch Batch) error {
|
||||
lvldbBatch := batch.(*leveldb.Batch)
|
||||
return s.db.Write(lvldbBatch, nil)
|
||||
}
|
||||
|
||||
// PutChangeSet implements the Store interface.
|
||||
func (s *LevelDBStore) PutChangeSet(puts map[string][]byte, stores map[string][]byte) error {
|
||||
tx, err := s.db.OpenTransaction()
|
||||
|
@ -133,12 +127,6 @@ func (s *LevelDBStore) seek(iter iterator.Iterator, backwards bool, f func(k, v
|
|||
iter.Release()
|
||||
}
|
||||
|
||||
// Batch implements the Batch interface and returns a leveldb
|
||||
// compatible Batch.
|
||||
func (s *LevelDBStore) Batch() Batch {
|
||||
return new(leveldb.Batch)
|
||||
}
|
||||
|
||||
// Close implements the Store interface.
|
||||
func (s *LevelDBStore) Close() error {
|
||||
return s.db.Close()
|
||||
|
|
|
@ -85,11 +85,9 @@ type (
|
|||
// Store is anything that can persist and retrieve the blockchain.
|
||||
// information.
|
||||
Store interface {
|
||||
Batch() Batch
|
||||
Delete(k []byte) error
|
||||
Get([]byte) ([]byte, error)
|
||||
Put(k, v []byte) error
|
||||
PutBatch(Batch) error
|
||||
// PutChangeSet allows to push prepared changeset to the Store.
|
||||
PutChangeSet(puts map[string][]byte, stor map[string][]byte) error
|
||||
// Seek can guarantee that provided key (k) and value (v) are the only valid until the next call to f.
|
||||
|
|
|
@ -37,27 +37,6 @@ func testStoreGetNonExistent(t *testing.T, s Store) {
|
|||
assert.Equal(t, err, ErrKeyNotFound)
|
||||
}
|
||||
|
||||
func testStorePutBatch(t *testing.T, s Store) {
|
||||
var (
|
||||
key = []byte("foo")
|
||||
value = []byte("bar")
|
||||
batch = s.Batch()
|
||||
)
|
||||
// Test that key and value are copied when batching.
|
||||
keycopy := slice.Copy(key)
|
||||
valuecopy := slice.Copy(value)
|
||||
|
||||
batch.Put(keycopy, valuecopy)
|
||||
copy(valuecopy, key)
|
||||
copy(keycopy, value)
|
||||
|
||||
require.NoError(t, s.PutBatch(batch))
|
||||
newVal, err := s.Get(key)
|
||||
assert.Nil(t, err)
|
||||
require.Equal(t, value, newVal)
|
||||
assert.Equal(t, value, newVal)
|
||||
}
|
||||
|
||||
func testStoreSeek(t *testing.T, s Store) {
|
||||
// Use the same set of kvs to test Seek with different prefix/start values.
|
||||
kvs := []KeyValue{
|
||||
|
@ -254,74 +233,6 @@ func testStorePutAndDelete(t *testing.T, s Store) {
|
|||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func testStorePutBatchWithDelete(t *testing.T, s Store) {
|
||||
var (
|
||||
toBeStored = map[string][]byte{
|
||||
"foo": []byte("bar"),
|
||||
"bar": []byte("baz"),
|
||||
}
|
||||
deletedInBatch = map[string][]byte{
|
||||
"edc": []byte("rfv"),
|
||||
"tgb": []byte("yhn"),
|
||||
}
|
||||
readdedToBatch = map[string][]byte{
|
||||
"yhn": []byte("ujm"),
|
||||
}
|
||||
toBeDeleted = map[string][]byte{
|
||||
"qaz": []byte("wsx"),
|
||||
"qwe": []byte("123"),
|
||||
}
|
||||
toStay = map[string][]byte{
|
||||
"key": []byte("val"),
|
||||
"faa": []byte("bra"),
|
||||
}
|
||||
)
|
||||
for k, v := range toBeDeleted {
|
||||
require.NoError(t, s.Put([]byte(k), v))
|
||||
}
|
||||
for k, v := range toStay {
|
||||
require.NoError(t, s.Put([]byte(k), v))
|
||||
}
|
||||
batch := s.Batch()
|
||||
for k, v := range toBeStored {
|
||||
batch.Put([]byte(k), v)
|
||||
}
|
||||
for k := range toBeDeleted {
|
||||
batch.Delete([]byte(k))
|
||||
}
|
||||
for k, v := range readdedToBatch {
|
||||
batch.Put([]byte(k), v)
|
||||
}
|
||||
for k, v := range deletedInBatch {
|
||||
batch.Put([]byte(k), v)
|
||||
}
|
||||
for k := range deletedInBatch {
|
||||
batch.Delete([]byte(k))
|
||||
}
|
||||
for k := range readdedToBatch {
|
||||
batch.Delete([]byte(k))
|
||||
}
|
||||
for k, v := range readdedToBatch {
|
||||
batch.Put([]byte(k), v)
|
||||
}
|
||||
require.NoError(t, s.PutBatch(batch))
|
||||
toBe := []map[string][]byte{toStay, toBeStored, readdedToBatch}
|
||||
notToBe := []map[string][]byte{deletedInBatch, toBeDeleted}
|
||||
for _, kvs := range toBe {
|
||||
for k, v := range kvs {
|
||||
value, err := s.Get([]byte(k))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, value, v)
|
||||
}
|
||||
}
|
||||
for _, kvs := range notToBe {
|
||||
for k, v := range kvs {
|
||||
_, err := s.Get([]byte(k))
|
||||
assert.Equal(t, ErrKeyNotFound, err, "%s:%s", k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testStoreSeekGC(t *testing.T, s Store) {
|
||||
kvs := []KeyValue{
|
||||
{[]byte("10"), []byte("bar")},
|
||||
|
@ -365,9 +276,9 @@ func TestAllDBs(t *testing.T) {
|
|||
{"Memory", newMemoryStoreForTesting},
|
||||
}
|
||||
var tests = []dbTestFunction{testStorePutAndGet,
|
||||
testStoreGetNonExistent, testStorePutBatch, testStoreSeek,
|
||||
testStoreGetNonExistent, testStoreSeek,
|
||||
testStoreDeleteNonExistent, testStorePutAndDelete,
|
||||
testStorePutBatchWithDelete, testStoreSeekGC}
|
||||
testStoreSeekGC}
|
||||
for _, db := range DBs {
|
||||
for _, test := range tests {
|
||||
s := db.create(t)
|
||||
|
|
Loading…
Reference in a new issue