Commit bebd894c authored by Steven Allen's avatar Steven Allen

make BasicBatch handle concurrent puts/deletes

parent f24a416e
package datastore
type verb int
type op struct {
delete bool
value []byte
}
// basicBatch implements the transaction interface for datastores who do
// not have any sort of underlying transactional support
type basicBatch struct {
puts map[Key][]byte
deletes map[Key]struct{}
ops map[Key]op
target Datastore
}
func NewBasicBatch(ds Datastore) Batch {
return &basicBatch{
puts: make(map[Key][]byte),
deletes: make(map[Key]struct{}),
target: ds,
ops: make(map[Key]op),
target: ds,
}
}
func (bt *basicBatch) Put(key Key, val []byte) error {
bt.puts[key] = val
bt.ops[key] = op{value: val}
return nil
}
func (bt *basicBatch) Delete(key Key) error {
bt.deletes[key] = struct{}{}
bt.ops[key] = op{delete: true}
return nil
}
func (bt *basicBatch) Commit() error {
for k, val := range bt.puts {
if err := bt.target.Put(k, val); err != nil {
return err
var err error
for k, op := range bt.ops {
if op.delete {
err = bt.target.Delete(k)
// We could try to do something smarter but I really
// don't care. Delete should be idempotent anyways.
if err == ErrNotFound {
err = nil
}
} else {
err = bt.target.Put(k, op.value)
}
}
for k, _ := range bt.deletes {
if err := bt.target.Delete(k); err != nil {
return err
if err != nil {
break
}
}
return nil
return err
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment