2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-04 15:02:55 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/siddontang/golib/leveldb"
|
2014-05-06 09:32:38 +04:00
|
|
|
"sync"
|
2014-05-04 15:02:55 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type tx struct {
|
2014-05-06 09:32:38 +04:00
|
|
|
m sync.Mutex
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
app *App
|
|
|
|
|
|
|
|
wb *leveldb.WriteBatch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) newTx() *tx {
|
|
|
|
t := new(tx)
|
|
|
|
|
|
|
|
t.app = app
|
|
|
|
|
|
|
|
t.wb = app.db.NewWriteBatch()
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
func (t *tx) Close() {
|
|
|
|
t.wb.Close()
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
func (t *tx) Put(key []byte, value []byte) {
|
|
|
|
t.wb.Put(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tx) Delete(key []byte) {
|
|
|
|
t.wb.Delete(key)
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
func (t *tx) Lock() {
|
|
|
|
t.m.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tx) Unlock() {
|
|
|
|
t.wb.Rollback()
|
|
|
|
t.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
func (t *tx) Commit() error {
|
|
|
|
err := t.wb.Commit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-05 07:37:44 +04:00
|
|
|
func (t *tx) Rollback() {
|
|
|
|
t.wb.Rollback()
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|