diff options
Diffstat (limited to 'rwtransaction.go')
-rw-r--r-- | rwtransaction.go | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/rwtransaction.go b/rwtransaction.go index 776cf55..0c670d9 100644 --- a/rwtransaction.go +++ b/rwtransaction.go @@ -5,29 +5,29 @@ import ( "unsafe" ) -// RWTransaction represents a transaction that can read and write data. +// RWTx represents a transaction that can read and write data. // Only one read/write transaction can be active for a database at a time. -// RWTransaction is composed of a read-only Transaction so it can also use -// functions provided by Transaction. -type RWTransaction struct { - Transaction +// RWTx is composed of a read-only transaction so it can also use +// functions provided by Tx. +type RWTx struct { + Tx pending []*node } // init initializes the transaction. -func (t *RWTransaction) init(db *DB) { - t.Transaction.init(db) - t.Transaction.rwtransaction = t +func (t *RWTx) init(db *DB) { + t.Tx.init(db) + t.Tx.rwtx = t t.pages = make(map[pgid]*page) t.nodes = make(map[pgid]*node) // Increment the transaction id. - t.meta.txnid += txnid(1) + t.meta.txid += txid(1) } // 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 { +func (t *RWTx) CreateBucket(name string) error { // Check if bucket already exists. if b := t.Bucket(name); b != nil { return ErrBucketExists @@ -52,7 +52,7 @@ func (t *RWTransaction) CreateBucket(name string) error { // CreateBucketIfNotExists creates a new bucket if it doesn't already exist. // Returns an error if the bucket name is blank, or if the bucket name is too long. -func (t *RWTransaction) CreateBucketIfNotExists(name string) error { +func (t *RWTx) CreateBucketIfNotExists(name string) error { err := t.CreateBucket(name) if err != nil && err != ErrBucketExists { return err @@ -62,7 +62,7 @@ func (t *RWTransaction) CreateBucketIfNotExists(name string) error { // DeleteBucket deletes a bucket. // Returns an error if the bucket cannot be found. -func (t *RWTransaction) DeleteBucket(name string) error { +func (t *RWTx) DeleteBucket(name string) error { b := t.Bucket(name) if b == nil { return ErrBucketNotFound @@ -81,7 +81,7 @@ func (t *RWTransaction) DeleteBucket(name string) error { // Commit writes all changes to disk and updates the meta page. // Returns an error if a disk write error occurs. -func (t *RWTransaction) Commit() error { +func (t *RWTx) Commit() error { if t.db == nil { return nil } @@ -118,11 +118,11 @@ func (t *RWTransaction) Commit() error { } // Rollback closes the transaction and ignores all previous updates. -func (t *RWTransaction) Rollback() { +func (t *RWTx) Rollback() { t.close() } -func (t *RWTransaction) close() { +func (t *RWTx) close() { if t.db != nil { t.db.rwlock.Unlock() t.db = nil @@ -130,7 +130,7 @@ func (t *RWTransaction) close() { } // allocate returns a contiguous block of memory starting at a given page. -func (t *RWTransaction) allocate(count int) (*page, error) { +func (t *RWTx) allocate(count int) (*page, error) { p, err := t.db.allocate(count) if err != nil { return nil, err @@ -143,14 +143,14 @@ func (t *RWTransaction) allocate(count int) (*page, error) { } // rebalance attempts to balance all nodes. -func (t *RWTransaction) rebalance() { +func (t *RWTx) rebalance() { for _, n := range t.nodes { n.rebalance() } } // spill writes all the nodes to dirty pages. -func (t *RWTransaction) spill() error { +func (t *RWTx) spill() error { // Keep track of the current root nodes. // We will update this at the end once all nodes are created. type root struct { @@ -182,7 +182,7 @@ func (t *RWTransaction) spill() error { // If this is a root node that split then create a parent node. if n.parent == nil && len(newNodes) > 1 { - n.parent = &node{transaction: t, isLeaf: false} + n.parent = &node{tx: t, isLeaf: false} nodes = append(nodes, n.parent) } @@ -233,7 +233,7 @@ func (t *RWTransaction) spill() error { } // write writes any dirty pages to disk. -func (t *RWTransaction) write() error { +func (t *RWTx) write() error { // Sort pages by id. pages := make(pages, 0, len(t.pages)) for _, p := range t.pages { @@ -258,7 +258,7 @@ func (t *RWTransaction) write() error { } // writeMeta writes the meta to the disk. -func (t *RWTransaction) writeMeta() error { +func (t *RWTx) writeMeta() error { // Create a temporary buffer for the meta page. buf := make([]byte, t.db.pageSize) p := t.db.pageInBuffer(buf, 0) @@ -271,14 +271,14 @@ func (t *RWTransaction) writeMeta() error { } // node creates a node from a page and associates it with a given parent. -func (t *RWTransaction) node(pgid pgid, parent *node) *node { +func (t *RWTx) node(pgid pgid, parent *node) *node { // Retrieve node if it has already been fetched. - if n := t.Transaction.node(pgid); n != nil { + if n := t.Tx.node(pgid); n != nil { return n } // Otherwise create a branch and cache it. - n := &node{transaction: t, parent: parent} + n := &node{tx: t, parent: parent} if n.parent != nil { n.depth = n.parent.depth + 1 } @@ -289,7 +289,7 @@ func (t *RWTransaction) node(pgid pgid, parent *node) *node { } // dereference removes all references to the old mmap. -func (t *RWTransaction) dereference() { +func (t *RWTx) dereference() { for _, n := range t.nodes { n.dereference() } |