2014-09-25 06:44:07 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-03-12 06:38:21 +03:00
|
|
|
"github.com/siddontang/go/log"
|
2014-09-25 06:44:07 +04:00
|
|
|
"github.com/siddontang/ledisdb/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNestTx = errors.New("nest transaction not supported")
|
|
|
|
ErrTxDone = errors.New("Transaction has already been committed or rolled back")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tx struct {
|
|
|
|
*DB
|
|
|
|
|
|
|
|
tx *store.Tx
|
|
|
|
|
2014-11-10 07:40:19 +03:00
|
|
|
data *store.BatchData
|
2014-09-25 06:44:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) IsTransaction() bool {
|
|
|
|
return db.status == DBInTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin a transaction, it will block all other write operations before calling Commit or Rollback.
|
|
|
|
// You must be very careful to prevent long-time transaction.
|
|
|
|
func (db *DB) Begin() (*Tx, error) {
|
2015-03-12 06:38:21 +03:00
|
|
|
log.Warn("Transaction support will be removed later, use your own risk!!!")
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
if db.IsTransaction() {
|
|
|
|
return nil, ErrNestTx
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := new(Tx)
|
|
|
|
|
2014-11-10 07:40:19 +03:00
|
|
|
tx.data = &store.BatchData{}
|
2014-09-25 06:44:07 +04:00
|
|
|
|
|
|
|
tx.DB = new(DB)
|
|
|
|
tx.DB.l = db.l
|
|
|
|
|
|
|
|
tx.l.wLock.Lock()
|
|
|
|
|
|
|
|
tx.DB.sdb = db.sdb
|
|
|
|
|
|
|
|
var err error
|
|
|
|
tx.tx, err = db.sdb.Begin()
|
|
|
|
if err != nil {
|
|
|
|
tx.l.wLock.Unlock()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.DB.bucket = tx.tx
|
|
|
|
|
|
|
|
tx.DB.status = DBInTransaction
|
|
|
|
|
|
|
|
tx.DB.index = db.index
|
|
|
|
|
|
|
|
tx.DB.kvBatch = tx.newBatch()
|
|
|
|
tx.DB.listBatch = tx.newBatch()
|
|
|
|
tx.DB.hashBatch = tx.newBatch()
|
|
|
|
tx.DB.zsetBatch = tx.newBatch()
|
|
|
|
tx.DB.setBatch = tx.newBatch()
|
|
|
|
|
2014-10-16 13:51:52 +04:00
|
|
|
tx.DB.lbkeys = db.lbkeys
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Commit() error {
|
|
|
|
if tx.tx == nil {
|
|
|
|
return ErrTxDone
|
|
|
|
}
|
|
|
|
|
2014-11-10 07:40:19 +03:00
|
|
|
err := tx.l.handleCommit(tx.data, tx.tx)
|
|
|
|
tx.data.Reset()
|
2014-09-25 06:44:07 +04:00
|
|
|
|
|
|
|
tx.tx = nil
|
|
|
|
|
|
|
|
tx.l.wLock.Unlock()
|
|
|
|
|
|
|
|
tx.DB.bucket = nil
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Rollback() error {
|
|
|
|
if tx.tx == nil {
|
|
|
|
return ErrTxDone
|
|
|
|
}
|
|
|
|
|
|
|
|
err := tx.tx.Rollback()
|
2014-11-10 07:40:19 +03:00
|
|
|
tx.data.Reset()
|
2014-09-25 06:44:07 +04:00
|
|
|
tx.tx = nil
|
|
|
|
|
|
|
|
tx.l.wLock.Unlock()
|
|
|
|
tx.DB.bucket = nil
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) newBatch() *batch {
|
|
|
|
return tx.l.newBatch(tx.tx.NewWriteBatch(), &txBatchLocker{}, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Select(index int) error {
|
2015-03-04 04:15:28 +03:00
|
|
|
if index < 0 || index >= int(tx.l.cfg.Databases) {
|
2014-09-25 06:44:07 +04:00
|
|
|
return fmt.Errorf("invalid db index %d", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.DB.index = uint8(index)
|
|
|
|
return nil
|
|
|
|
}
|