0
0

single append for inmem KVStore batch

This commit is contained in:
Steve Yen 2015-01-29 11:14:02 -08:00
parent 5a30d36b17
commit 7d6a6aeaa8

View File

@ -13,10 +13,14 @@ import (
indexStore "github.com/blevesearch/bleve/index/store" indexStore "github.com/blevesearch/bleve/index/store"
) )
type op struct {
k []byte
v []byte
}
type Batch struct { type Batch struct {
store *Store store *Store
keys [][]byte ops []op
vals [][]byte
alreadyLocked bool alreadyLocked bool
merges map[string]indexStore.AssociativeMergeChain merges map[string]indexStore.AssociativeMergeChain
} }
@ -24,8 +28,7 @@ type Batch struct {
func newBatch(store *Store) *Batch { func newBatch(store *Store) *Batch {
rv := Batch{ rv := Batch{
store: store, store: store,
keys: make([][]byte, 0), ops: make([]op, 0, 100),
vals: make([][]byte, 0),
merges: make(map[string]indexStore.AssociativeMergeChain), merges: make(map[string]indexStore.AssociativeMergeChain),
} }
return &rv return &rv
@ -34,8 +37,7 @@ func newBatch(store *Store) *Batch {
func newBatchAlreadyLocked(store *Store) *Batch { func newBatchAlreadyLocked(store *Store) *Batch {
rv := Batch{ rv := Batch{
store: store, store: store,
keys: make([][]byte, 0), ops: make([]op, 0, 100),
vals: make([][]byte, 0),
alreadyLocked: true, alreadyLocked: true,
merges: make(map[string]indexStore.AssociativeMergeChain), merges: make(map[string]indexStore.AssociativeMergeChain),
} }
@ -43,13 +45,11 @@ func newBatchAlreadyLocked(store *Store) *Batch {
} }
func (i *Batch) Set(key, val []byte) { func (i *Batch) Set(key, val []byte) {
i.keys = append(i.keys, key) i.ops = append(i.ops, op{key, val})
i.vals = append(i.vals, val)
} }
func (i *Batch) Delete(key []byte) { func (i *Batch) Delete(key []byte) {
i.keys = append(i.keys, key) i.ops = append(i.ops, op{key, nil})
i.vals = append(i.vals, nil)
} }
func (i *Batch) Merge(key []byte, oper indexStore.AssociativeMerge) { func (i *Batch) Merge(key []byte, oper indexStore.AssociativeMerge) {
@ -90,15 +90,14 @@ func (i *Batch) Execute() error {
} }
} }
for index, key := range i.keys { for _, op := range i.ops {
val := i.vals[index] if op.v == nil {
if val == nil { err := i.store.deletelocked(op.k)
err := i.store.deletelocked(key)
if err != nil { if err != nil {
return err return err
} }
} else { } else {
err := i.store.setlocked(key, val) err := i.store.setlocked(op.k, op.v)
if err != nil { if err != nil {
return err return err
} }