Skip to content

Commit

Permalink
Force disable journaling if the Tx is not writable
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-petithory committed Feb 21, 2017
1 parent a721fe9 commit b238215
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ type W struct {
}

func RTx(tx *bolt.Tx) *Tx {
return &Tx{Tx: tx, j: &Journal{TxID: tx.ID(), Type: JournalTypeUpdate}}
return &Tx{Tx: tx, writable: tx.Writable(), j: &Journal{TxID: tx.ID(), Type: JournalTypeUpdate}}
}

type Tx struct {
*bolt.Tx

j *Journal
writable bool
j *Journal
}

func (tx *Tx) Journal() *Journal {
Expand All @@ -197,6 +198,9 @@ func (tx *Tx) log(op Op, path [][]byte, v []byte, cursorID int) {

func (tx *Tx) Bucket(name []byte) *Bucket {
b := tx.Tx.Bucket(name)
if !tx.writable {
return &Bucket{b: b, tx: tx}
}
n := cpb(name)
return &Bucket{b: b, tx: tx, path: [][]byte{n}}
}
Expand All @@ -206,6 +210,9 @@ func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {
if err != nil {
return nil, err
}
if !tx.writable {
return &Bucket{b: b, tx: tx}, nil
}
n := cpb(name)
tx.log(OpCreateBucket, [][]byte{n}, nil, 0)
return &Bucket{b: b, tx: tx, path: [][]byte{n}}, nil
Expand All @@ -216,6 +223,9 @@ func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
if err != nil {
return nil, err
}
if !tx.writable {
return &Bucket{b: b, tx: tx}, nil
}
n := cpb(name)
tx.log(OpCreateBucketIfNotExists, [][]byte{n}, nil, 0)
return &Bucket{b: b, tx: tx, path: [][]byte{n}}, nil
Expand All @@ -231,12 +241,20 @@ func (tx *Tx) DeleteBucket(name []byte) error {
if err != nil {
return err
}
if !tx.writable {
return nil
}
n := cpb(name)
tx.log(OpDeleteBucket, [][]byte{n}, nil, 0)
return nil
}

func (tx *Tx) ForEach(fn func([]byte, *Bucket) error) error {
if !tx.writable {
return tx.Tx.ForEach(func(name []byte, b *bolt.Bucket) error {
return fn(name, &Bucket{b: b, tx: tx})
})
}
return tx.Tx.ForEach(func(name []byte, b *bolt.Bucket) error {
n := cpb(name)
return fn(name, &Bucket{b: b, tx: tx, path: [][]byte{n}})
Expand All @@ -252,6 +270,9 @@ type Bucket struct {

func (b *Bucket) Bucket(name []byte) *Bucket {
sb := b.b.Bucket(name)
if !b.tx.writable {
return &Bucket{b: sb, tx: b.tx}
}
return &Bucket{b: sb, tx: b.tx, path: append(b.path, cpb(name))}
}

Expand All @@ -260,6 +281,9 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
if err != nil {
return nil, err
}
if !b.tx.writable {
return &Bucket{b: sb, tx: b.tx}, nil
}
p := append(b.path, cpb(key))
b.tx.log(OpCreateBucket, p, nil, 0)
return &Bucket{b: sb, tx: b.tx, path: p}, nil
Expand All @@ -270,6 +294,9 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
if err != nil {
return nil, err
}
if !b.tx.writable {
return &Bucket{b: sb, tx: b.tx}, nil
}
p := append(b.path, cpb(key))
b.tx.log(OpCreateBucketIfNotExists, p, nil, 0)
return &Bucket{b: sb, tx: b.tx, path: p}, nil
Expand All @@ -288,6 +315,9 @@ func (b *Bucket) Delete(key []byte) error {
if err != nil {
return err
}
if !b.tx.writable {
return nil
}
p := append(b.path, cpb(key))
b.tx.log(OpDelete, p, nil, 0)
return nil
Expand All @@ -298,6 +328,9 @@ func (b *Bucket) DeleteBucket(key []byte) error {
if err != nil {
return err
}
if !b.tx.writable {
return nil
}
p := append(b.path, cpb(key))
b.tx.log(OpDeleteBucket, p, nil, 0)
return nil
Expand All @@ -308,6 +341,9 @@ func (b *Bucket) Put(key []byte, value []byte) error {
if err != nil {
return err
}
if !b.tx.writable {
return nil
}
b.tx.log(OpPut, append(b.path, cpb(key)), cpb(value), 0)
return nil
}
Expand Down Expand Up @@ -339,32 +375,44 @@ func (c *Cursor) Bucket() *Bucket {
}
func (c *Cursor) Delete() error {
err := c.c.Delete()
c.tx.log(OpCursorDelete, c.b.path, nil, c.id)
if c.tx.writable {
c.tx.log(OpCursorDelete, c.b.path, nil, c.id)
}
return err
}
func (c *Cursor) First() (key []byte, value []byte) {
k, v := c.c.First()
c.tx.log(OpCursorFirst, c.b.path, nil, c.id)
if c.tx.writable {
c.tx.log(OpCursorFirst, c.b.path, nil, c.id)
}
return k, v
}
func (c *Cursor) Last() (key []byte, value []byte) {
k, v := c.c.Last()
c.tx.log(OpCursorLast, c.b.path, nil, c.id)
if c.tx.writable {
c.tx.log(OpCursorLast, c.b.path, nil, c.id)
}
return k, v
}
func (c *Cursor) Next() (key []byte, value []byte) {
k, v := c.c.Next()
c.tx.log(OpCursorNext, c.b.path, nil, c.id)
if c.tx.writable {
c.tx.log(OpCursorNext, c.b.path, nil, c.id)
}
return k, v
}
func (c *Cursor) Prev() (key []byte, value []byte) {
k, v := c.c.Prev()
c.tx.log(OpCursorPrev, c.b.path, nil, c.id)
if c.tx.writable {
c.tx.log(OpCursorPrev, c.b.path, nil, c.id)
}
return k, v
}
func (c *Cursor) Seek(seek []byte) (key []byte, value []byte) {
k, v := c.c.Seek(seek)
c.tx.log(OpCursorSeek, c.b.path, cpb(seek), c.id)
if c.tx.writable {
c.tx.log(OpCursorSeek, c.b.path, cpb(seek), c.id)
}
return k, v
}

Expand Down

0 comments on commit b238215

Please sign in to comment.