ipld-eth-server/vendor/github.com/ipfs/go-datastore/batch.go
Elizabeth Engelman 36533f7c3f Update vendor directory and make necessary code changes
Fixes for new geth version
2019-09-25 16:32:27 -05:00

53 lines
949 B
Go

package datastore
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 {
ops map[Key]op
target Datastore
}
func NewBasicBatch(ds Datastore) Batch {
return &basicBatch{
ops: make(map[Key]op),
target: ds,
}
}
func (bt *basicBatch) Put(key Key, val []byte) error {
bt.ops[key] = op{value: val}
return nil
}
func (bt *basicBatch) Delete(key Key) error {
bt.ops[key] = op{delete: true}
return nil
}
func (bt *basicBatch) Commit() error {
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)
}
if err != nil {
break
}
}
return err
}