Remove RWTransaction.Bucket().

Add an reference to the RWTransaction onto Transaction so that calls to
Transaction.Bucket() and Transaction.Buckets() return writable buckets
when attached to a writabe transaction.
master
Ben Johnson 2014-02-25 08:31:04 -07:00
parent 488785df09
commit 41fb285e37
2 changed files with 20 additions and 32 deletions

View File

@ -18,34 +18,13 @@ type RWTransaction struct {
// init initializes the transaction.
func (t *RWTransaction) init(db *DB) {
t.Transaction.init(db)
t.Transaction.rwtransaction = t
t.pages = make(map[pgid]*page)
// Increment the transaction id.
t.meta.txnid += txnid(1)
}
// Bucket retrieves a writable bucket by name.
// Returns nil if the bucket does not exist.
func (t *RWTransaction) Bucket(name string) *Bucket {
b := t.Transaction.Bucket(name)
if b == nil {
return nil
}
b.rwtransaction = t
return b
}
// Buckets retrieves a list of all buckets.
// All returned buckets are writable.
func (t *RWTransaction) Buckets() []*Bucket {
buckets := t.Transaction.Buckets()
for _, b := range buckets {
b.rwtransaction = t
}
return buckets
}
// CreateBucket creates a new bucket.
// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.
func (t *RWTransaction) CreateBucket(name string) error {

View File

@ -8,10 +8,11 @@ package bolt
// can not be reclaimed by the writer until no more transactions are using them.
// A long running read transaction can cause the database to quickly grow.
type Transaction struct {
db *DB
meta *meta
buckets *buckets
pages map[pgid]*page
db *DB
rwtransaction *RWTransaction
meta *meta
buckets *buckets
pages map[pgid]*page
}
// txnid represents the internal transaction identifier.
@ -38,6 +39,9 @@ func (t *Transaction) id() txnid {
// Close closes the transaction and releases any pages it is using.
func (t *Transaction) Close() {
if t.rwtransaction != nil {
t.rwtransaction.Rollback()
}
t.db.removeTransaction(t)
}
@ -46,7 +50,7 @@ func (t *Transaction) DB() *DB {
return t.db
}
// Bucket retrieves a read-only bucket by name.
// Bucket retrieves a bucket by name.
// Returns nil if the bucket does not exist.
func (t *Transaction) Bucket(name string) *Bucket {
b := t.buckets.get(name)
@ -55,18 +59,23 @@ func (t *Transaction) Bucket(name string) *Bucket {
}
return &Bucket{
bucket: b,
name: name,
transaction: t,
bucket: b,
name: name,
transaction: t,
rwtransaction: t.rwtransaction,
}
}
// Buckets retrieves a list of all buckets.
// All returned buckets are read-only.
func (t *Transaction) Buckets() []*Bucket {
buckets := make([]*Bucket, 0, len(t.buckets.items))
for name, b := range t.buckets.items {
bucket := &Bucket{bucket: b, transaction: t, name: name}
bucket := &Bucket{
bucket: b,
name: name,
transaction: t,
rwtransaction: t.rwtransaction,
}
buckets = append(buckets, bucket)
}
return buckets