storage: simplify MemoryBatch
It's used a lot and it looks a lot like MemoryStore, it just needs not to return errors from Put and Delete, so make it use MemoryStore internally with adjusted interface.
This commit is contained in:
parent
13bf2618ef
commit
78861485b6
3 changed files with 7 additions and 18 deletions
|
@ -83,7 +83,7 @@ func (s *BoltDBStore) Delete(key []byte) error {
|
||||||
func (s *BoltDBStore) PutBatch(batch Batch) error {
|
func (s *BoltDBStore) PutBatch(batch Batch) error {
|
||||||
return s.db.Batch(func(tx *bbolt.Tx) error {
|
return s.db.Batch(func(tx *bbolt.Tx) error {
|
||||||
b := tx.Bucket(Bucket)
|
b := tx.Bucket(Bucket)
|
||||||
for k, v := range batch.(*MemoryBatch).m {
|
for k, v := range batch.(*MemoryBatch).mem {
|
||||||
err := b.Put([]byte(k), v)
|
err := b.Put([]byte(k), v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -16,25 +16,17 @@ type MemoryStore struct {
|
||||||
|
|
||||||
// MemoryBatch a in-memory batch compatible with MemoryStore.
|
// MemoryBatch a in-memory batch compatible with MemoryStore.
|
||||||
type MemoryBatch struct {
|
type MemoryBatch struct {
|
||||||
m map[string][]byte
|
MemoryStore
|
||||||
// A map, not a slice, to avoid duplicates.
|
|
||||||
del map[string]bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put implements the Batch interface.
|
// Put implements the Batch interface.
|
||||||
func (b *MemoryBatch) Put(k, v []byte) {
|
func (b *MemoryBatch) Put(k, v []byte) {
|
||||||
vcopy := make([]byte, len(v))
|
_ = b.MemoryStore.Put(k, v)
|
||||||
copy(vcopy, v)
|
|
||||||
kcopy := string(k)
|
|
||||||
b.m[kcopy] = vcopy
|
|
||||||
delete(b.del, kcopy)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete implements Batch interface.
|
// Delete implements Batch interface.
|
||||||
func (b *MemoryBatch) Delete(k []byte) {
|
func (b *MemoryBatch) Delete(k []byte) {
|
||||||
kcopy := string(k)
|
_ = b.MemoryStore.Delete(k)
|
||||||
delete(b.m, kcopy)
|
|
||||||
b.del[kcopy] = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMemoryStore creates a new MemoryStore object.
|
// NewMemoryStore creates a new MemoryStore object.
|
||||||
|
@ -97,7 +89,7 @@ func (s *MemoryStore) PutBatch(batch Batch) error {
|
||||||
for k := range b.del {
|
for k := range b.del {
|
||||||
s.drop(k)
|
s.drop(k)
|
||||||
}
|
}
|
||||||
for k, v := range b.m {
|
for k, v := range b.mem {
|
||||||
s.put(k, v)
|
s.put(k, v)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -119,10 +111,7 @@ func (s *MemoryStore) Batch() Batch {
|
||||||
|
|
||||||
// newMemoryBatch returns new memory batch.
|
// newMemoryBatch returns new memory batch.
|
||||||
func newMemoryBatch() *MemoryBatch {
|
func newMemoryBatch() *MemoryBatch {
|
||||||
return &MemoryBatch{
|
return &MemoryBatch{MemoryStore: *NewMemoryStore()}
|
||||||
m: make(map[string][]byte),
|
|
||||||
del: make(map[string]bool),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Persist flushes all the MemoryStore contents into the (supposedly) persistent
|
// Persist flushes all the MemoryStore contents into the (supposedly) persistent
|
||||||
|
|
|
@ -63,7 +63,7 @@ func (s *RedisStore) Put(k, v []byte) error {
|
||||||
// PutBatch implements the Store interface.
|
// PutBatch implements the Store interface.
|
||||||
func (s *RedisStore) PutBatch(b Batch) error {
|
func (s *RedisStore) PutBatch(b Batch) error {
|
||||||
pipe := s.client.Pipeline()
|
pipe := s.client.Pipeline()
|
||||||
for k, v := range b.(*MemoryBatch).m {
|
for k, v := range b.(*MemoryBatch).mem {
|
||||||
pipe.Set(k, v, 0)
|
pipe.Set(k, v, 0)
|
||||||
}
|
}
|
||||||
for k := range b.(*MemoryBatch).del {
|
for k := range b.(*MemoryBatch).del {
|
||||||
|
|
Loading…
Reference in a new issue