Commit 1702774c authored by Jeromy's avatar Jeromy

rename Transaction to Batch

parent c3fa34b4
......@@ -63,8 +63,8 @@ func (d *MapDatastore) Query(q dsq.Query) (dsq.Results, error) {
return r, nil
}
func (d *MapDatastore) StartBatchOp() Transaction {
return NewBasicTransaction(d)
func (d *MapDatastore) Batch() Batch {
return NewBasicBatch(d)
}
// NullDatastore stores nothing, but conforms to the API.
......@@ -102,8 +102,8 @@ func (d *NullDatastore) Query(q dsq.Query) (dsq.Results, error) {
return dsq.ResultsWithEntries(q, nil), nil
}
func (d *NullDatastore) StartBatchOp() Transaction {
return NewBasicTransaction(d)
func (d *NullDatastore) Batch() Batch {
return NewBasicBatch(d)
}
// LogDatastore logs all accesses through the datastore.
......@@ -163,7 +163,7 @@ func (d *LogDatastore) Query(q dsq.Query) (dsq.Results, error) {
return d.child.Query(q)
}
func (d *LogDatastore) StartBatchOp() Transaction {
log.Printf("%s: StartBatchOp\n", d.Name)
return d.child.StartBatchOp()
func (d *LogDatastore) Batch() Batch {
log.Printf("%s: Batch\n", d.Name)
return d.child.Batch()
}
......@@ -41,7 +41,7 @@ func (c *Datastore) Query(q dsq.Query) (dsq.Results, error) {
return c.D.Query(q)
}
func (c *Datastore) StartBatchOp() ds.Transaction {
func (c *Datastore) Batch() ds.Batch {
c.F()
return c.D.StartBatchOp()
return c.D.Batch()
}
......@@ -125,6 +125,6 @@ func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
return d.child.Query(q)
}
func (d *datastore) StartBatchOp() ds.Transaction {
return ds.NewBasicTransaction(d)
func (d *datastore) Batch() ds.Batch {
return ds.NewBasicBatch(d)
}
......@@ -68,8 +68,8 @@ type Datastore interface {
//
Query(q query.Query) (query.Results, error)
// StartBatchOp begins a datastore transaction
StartBatchOp() Transaction
// Batch begins a datastore transaction
Batch() Batch
}
// ThreadSafeDatastore is an interface that all threadsafe datastore should
......@@ -108,7 +108,7 @@ func GetBackedHas(ds Datastore, key Key) (bool, error) {
}
}
type Transaction interface {
type Batch interface {
Put(key Key, val interface{}) error
Delete(key Key) error
......
......@@ -323,32 +323,32 @@ func (fs *Datastore) enumerateKeys(fi os.FileInfo, res []query.Entry) ([]query.E
return res, nil
}
type flatfsTransaction struct {
type flatfsBatch struct {
puts map[datastore.Key]interface{}
deletes map[datastore.Key]struct{}
ds *Datastore
}
func (fs *Datastore) StartBatchOp() datastore.Transaction {
return &flatfsTransaction{
func (fs *Datastore) Batch() datastore.Batch {
return &flatfsBatch{
puts: make(map[datastore.Key]interface{}),
deletes: make(map[datastore.Key]struct{}),
ds: fs,
}
}
func (bt *flatfsTransaction) Put(key datastore.Key, val interface{}) error {
func (bt *flatfsBatch) Put(key datastore.Key, val interface{}) error {
bt.puts[key] = val
return nil
}
func (bt *flatfsTransaction) Delete(key datastore.Key) error {
func (bt *flatfsBatch) Delete(key datastore.Key) error {
bt.deletes[key] = struct{}{}
return nil
}
func (bt *flatfsTransaction) Commit() error {
func (bt *flatfsBatch) Commit() error {
if err := bt.ds.putMany(bt.puts); err != nil {
return err
}
......
......@@ -130,10 +130,10 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) {
return r, nil
}
func (d *Datastore) StartBatchOp() ds.Transaction {
func (d *Datastore) Batch() ds.Batch {
// just use basic transaction for now, this datastore
// isnt really used in performant code yet
return ds.NewBasicTransaction(d)
return ds.NewBasicBatch(d)
}
// isDir returns whether given path is a directory
......
......@@ -74,27 +74,27 @@ func (d *ktds) Query(q dsq.Query) (dsq.Results, error) {
return dsq.DerivedResults(qr, ch), nil
}
func (d *ktds) StartBatchOp() ds.Transaction {
return &transformTransaction{
dst: d.child.StartBatchOp(),
func (d *ktds) Batch() ds.Batch {
return &transformBatch{
dst: d.child.Batch(),
f: d.ConvertKey,
}
}
type transformTransaction struct {
dst ds.Transaction
type transformBatch struct {
dst ds.Batch
f KeyMapping
}
func (t *transformTransaction) Put(key ds.Key, val interface{}) error {
func (t *transformBatch) Put(key ds.Key, val interface{}) error {
return t.dst.Put(t.f(key), val)
}
func (t *transformTransaction) Delete(key ds.Key) error {
func (t *transformBatch) Delete(key ds.Key) error {
return t.dst.Delete(t.f(key))
}
func (t *transformTransaction) Commit() error {
func (t *transformBatch) Commit() error {
return t.dst.Commit()
}
......@@ -105,7 +105,7 @@ type ldbBatch struct {
d *datastore
}
func (d *datastore) StartBatchOp() ds.Transaction {
func (d *datastore) Batch() ds.Batch {
return &ldbBatch{
b: new(leveldb.Batch),
d: d,
......
......@@ -55,6 +55,6 @@ func (d *Datastore) Query(q dsq.Query) (dsq.Results, error) {
return nil, errors.New("KeyList not implemented.")
}
func (d *Datastore) StartBatchOp() ds.Transaction {
return ds.NewBasicTransaction(d)
func (d *Datastore) Batch() ds.Batch {
return ds.NewBasicBatch(d)
}
......@@ -148,36 +148,36 @@ func (m *measure) Query(q query.Query) (query.Results, error) {
return res, err
}
type measuredTransaction struct {
type measuredBatch struct {
puts int
deletes int
putts datastore.Transaction
delts datastore.Transaction
putts datastore.Batch
delts datastore.Batch
m *measure
}
func (m *measure) StartBatchOp() datastore.Transaction {
return &measuredTransaction{
putts: m.backend.StartBatchOp(),
delts: m.backend.StartBatchOp(),
func (m *measure) Batch() datastore.Batch {
return &measuredBatch{
putts: m.backend.Batch(),
delts: m.backend.Batch(),
m: m,
}
}
func (mt *measuredTransaction) Put(key datastore.Key, val interface{}) error {
func (mt *measuredBatch) Put(key datastore.Key, val interface{}) error {
mt.puts++
return mt.putts.Put(key, val)
}
func (mt *measuredTransaction) Delete(key datastore.Key) error {
func (mt *measuredBatch) Delete(key datastore.Key) error {
mt.deletes++
return mt.delts.Delete(key)
}
func (mt *measuredTransaction) Commit() error {
func (mt *measuredBatch) Commit() error {
if mt.deletes > 0 {
before := time.Now()
err := mt.delts.Commit()
......
......@@ -115,42 +115,42 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) {
return r, nil
}
type mountTransaction struct {
mounts map[string]datastore.Transaction
type mountBatch struct {
mounts map[string]datastore.Batch
d *Datastore
}
func (d *Datastore) StartBatchOp() datastore.Transaction {
return &mountTransaction{
mounts: make(map[string]datastore.Transaction),
func (d *Datastore) Batch() datastore.Batch {
return &mountBatch{
mounts: make(map[string]datastore.Batch),
d: d,
}
}
func (mt *mountTransaction) Put(key datastore.Key, val interface{}) error {
func (mt *mountBatch) Put(key datastore.Key, val interface{}) error {
child, loc, rest := mt.d.lookup(key)
t, ok := mt.mounts[loc.String()]
if !ok {
t = child.StartBatchOp()
t = child.Batch()
mt.mounts[loc.String()] = t
}
return t.Put(rest, val)
}
func (mt *mountTransaction) Delete(key datastore.Key) error {
func (mt *mountBatch) Delete(key datastore.Key) error {
child, loc, rest := mt.d.lookup(key)
t, ok := mt.mounts[loc.String()]
if !ok {
t = child.StartBatchOp()
t = child.Batch()
mt.mounts[loc.String()] = t
}
return t.Delete(rest)
}
func (mt *mountTransaction) Commit() error {
func (mt *mountBatch) Commit() error {
for _, t := range mt.mounts {
err := t.Commit()
if err != nil {
......
......@@ -67,11 +67,11 @@ func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
return r, nil
}
type panicTransaction struct {
t ds.Transaction
type panicBatch struct {
t ds.Batch
}
func (p *panicTransaction) Put(key ds.Key, val interface{}) error {
func (p *panicBatch) Put(key ds.Key, val interface{}) error {
err := p.t.Put(key, val)
if err != nil {
fmt.Fprintf(os.Stdout, "panic datastore: %s", err)
......@@ -80,7 +80,7 @@ func (p *panicTransaction) Put(key ds.Key, val interface{}) error {
return nil
}
func (p *panicTransaction) Delete(key ds.Key) error {
func (p *panicBatch) Delete(key ds.Key) error {
err := p.t.Delete(key)
if err != nil {
fmt.Fprintf(os.Stdout, "panic datastore: %s", err)
......@@ -89,7 +89,7 @@ func (p *panicTransaction) Delete(key ds.Key) error {
return nil
}
func (p *panicTransaction) Commit() error {
func (p *panicBatch) Commit() error {
err := p.t.Commit()
if err != nil {
fmt.Fprintf(os.Stdout, "panic datastore: %s", err)
......@@ -98,6 +98,6 @@ func (p *panicTransaction) Commit() error {
return nil
}
func (d *datastore) StartBatchOp() ds.Transaction {
return &panicTransaction{d.child.StartBatchOp()}
func (d *datastore) Batch() ds.Batch {
return &panicBatch{d.child.Batch()}
}
......@@ -64,8 +64,8 @@ func (d *MutexDatastore) Query(q dsq.Query) (dsq.Results, error) {
return d.child.Query(q)
}
func (d *MutexDatastore) StartBatchOp() ds.Transaction {
func (d *MutexDatastore) Batch() ds.Batch {
d.RLock()
defer d.RUnlock()
return d.child.StartBatchOp()
return d.child.Batch()
}
......@@ -93,17 +93,17 @@ func (d tiered) Query(q dsq.Query) (dsq.Results, error) {
return d[len(d)-1].Query(q)
}
type tieredTransaction []ds.Transaction
type tieredBatch []ds.Batch
func (d tiered) StartBatchOp() ds.Transaction {
var out tieredTransaction
func (d tiered) Batch() ds.Batch {
var out tieredBatch
for _, ds := range d {
out = append(out, ds.StartBatchOp())
out = append(out, ds.Batch())
}
return out
}
func (t tieredTransaction) Put(key ds.Key, val interface{}) error {
func (t tieredBatch) Put(key ds.Key, val interface{}) error {
for _, ts := range t {
err := ts.Put(key, val)
if err != nil {
......@@ -113,7 +113,7 @@ func (t tieredTransaction) Put(key ds.Key, val interface{}) error {
return nil
}
func (t tieredTransaction) Delete(key ds.Key) error {
func (t tieredBatch) Delete(key ds.Key) error {
for _, ts := range t {
err := ts.Delete(key)
if err != nil {
......@@ -123,7 +123,7 @@ func (t tieredTransaction) Delete(key ds.Key) error {
return nil
}
func (t tieredTransaction) Commit() error {
func (t tieredBatch) Commit() error {
for _, ts := range t {
err := ts.Commit()
if err != nil {
......
......@@ -95,7 +95,7 @@ func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
return d.cache.Query(q)
}
func (d *datastore) StartBatchOp() ds.Transaction {
func (d *datastore) Batch() ds.Batch {
// sorry, being lazy here
return ds.NewBasicTransaction(d)
return ds.NewBasicBatch(d)
}
package datastore
// basicTransaction implements the transaction interface for datastores who do
// basicBatch implements the transaction interface for datastores who do
// not have any sort of underlying transactional support
type basicTransaction struct {
type basicBatch struct {
puts map[Key]interface{}
deletes map[Key]struct{}
target Datastore
}
func NewBasicTransaction(ds Datastore) Transaction {
return &basicTransaction{
func NewBasicBatch(ds Datastore) Batch {
return &basicBatch{
puts: make(map[Key]interface{}),
deletes: make(map[Key]struct{}),
target: ds,
}
}
func (bt *basicTransaction) Put(key Key, val interface{}) error {
func (bt *basicBatch) Put(key Key, val interface{}) error {
bt.puts[key] = val
return nil
}
func (bt *basicTransaction) Delete(key Key) error {
func (bt *basicBatch) Delete(key Key) error {
bt.deletes[key] = struct{}{}
return nil
}
func (bt *basicTransaction) Commit() error {
func (bt *basicBatch) Commit() error {
for k, val := range bt.puts {
if err := bt.target.Put(k, val); err != 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