2014-09-25 06:44:07 +04:00
|
|
|
package ledis
|
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// import (
|
|
|
|
// "errors"
|
|
|
|
// "fmt"
|
|
|
|
// "github.com/siddontang/go/log"
|
|
|
|
// "github.com/siddontang/ledisdb/store"
|
|
|
|
// )
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// var (
|
|
|
|
// ErrNestTx = errors.New("nest transaction not supported")
|
|
|
|
// ErrTxDone = errors.New("Transaction has already been committed or rolled back")
|
|
|
|
// )
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// type Tx struct {
|
|
|
|
// *DB
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx *store.Tx
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// data *store.BatchData
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// func (db *DB) IsTransaction() bool {
|
|
|
|
// return db.status == DBInTransaction
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// // 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) {
|
|
|
|
// log.Warn("Transaction support will be removed later, use your own risk!!!")
|
2015-03-12 06:38:21 +03:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// if db.IsTransaction() {
|
|
|
|
// return nil, ErrNestTx
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx := new(Tx)
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.data = &store.BatchData{}
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB = new(DB)
|
|
|
|
// tx.DB.l = db.l
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.l.wLock.Lock()
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.sdb = db.sdb
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// var err error
|
|
|
|
// tx.tx, err = db.sdb.Begin()
|
|
|
|
// if err != nil {
|
|
|
|
// tx.l.wLock.Unlock()
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.bucket = tx.tx
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.status = DBInTransaction
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.index = db.index
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// 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-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.lbkeys = db.lbkeys
|
2014-10-16 13:51:52 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// return tx, nil
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// func (tx *Tx) Commit() error {
|
|
|
|
// if tx.tx == nil {
|
|
|
|
// return ErrTxDone
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// err := tx.l.handleCommit(tx.data, tx.tx)
|
|
|
|
// tx.data.Reset()
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.tx = nil
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.l.wLock.Unlock()
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.bucket = nil
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// return err
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// func (tx *Tx) Rollback() error {
|
|
|
|
// if tx.tx == nil {
|
|
|
|
// return ErrTxDone
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// err := tx.tx.Rollback()
|
|
|
|
// tx.data.Reset()
|
|
|
|
// tx.tx = nil
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.l.wLock.Unlock()
|
|
|
|
// tx.DB.bucket = nil
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// return err
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// func (tx *Tx) newBatch() *batch {
|
|
|
|
// return tx.l.newBatch(tx.tx.NewWriteBatch(), &txBatchLocker{}, tx)
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// func (tx *Tx) Select(index int) error {
|
|
|
|
// if index < 0 || index >= int(tx.l.cfg.Databases) {
|
|
|
|
// return fmt.Errorf("invalid db index %d", index)
|
|
|
|
// }
|
2014-09-25 06:44:07 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// tx.DB.index = uint8(index)
|
|
|
|
// return nil
|
|
|
|
// }
|