2014-09-02 13:55:12 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
2014-09-24 05:46:36 +04:00
|
|
|
"github.com/siddontang/go/log"
|
2014-09-22 13:50:51 +04:00
|
|
|
"github.com/siddontang/ledisdb/rpl"
|
2014-09-02 13:55:12 +04:00
|
|
|
"github.com/siddontang/ledisdb/store"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type batch struct {
|
|
|
|
l *Ledis
|
|
|
|
|
|
|
|
store.WriteBatch
|
|
|
|
|
|
|
|
sync.Locker
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
tx *Tx
|
|
|
|
|
2014-09-17 13:54:04 +04:00
|
|
|
eb *eventBatch
|
2014-09-02 13:55:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batch) Commit() error {
|
2014-09-22 13:50:51 +04:00
|
|
|
if b.l.IsReadOnly() {
|
|
|
|
return ErrWriteInROnly
|
2014-09-17 19:06:42 +04:00
|
|
|
}
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
if b.tx == nil {
|
|
|
|
return b.l.handleCommit(b.eb, b.WriteBatch)
|
2014-09-22 13:50:51 +04:00
|
|
|
} else {
|
2014-09-25 06:44:07 +04:00
|
|
|
if b.l.r != nil {
|
|
|
|
b.tx.eb.Write(b.eb.Bytes())
|
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
return b.WriteBatch.Commit()
|
2014-09-17 19:06:42 +04:00
|
|
|
}
|
2014-09-02 13:55:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batch) Lock() {
|
|
|
|
b.Locker.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batch) Unlock() {
|
2014-09-17 19:06:42 +04:00
|
|
|
b.eb.Reset()
|
2014-09-22 13:50:51 +04:00
|
|
|
|
2014-09-02 13:55:12 +04:00
|
|
|
b.WriteBatch.Rollback()
|
|
|
|
b.Locker.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batch) Put(key []byte, value []byte) {
|
2014-09-22 13:50:51 +04:00
|
|
|
if b.l.r != nil {
|
2014-09-17 19:06:42 +04:00
|
|
|
b.eb.Put(key, value)
|
2014-09-02 13:55:12 +04:00
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
|
2014-09-02 13:55:12 +04:00
|
|
|
b.WriteBatch.Put(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batch) Delete(key []byte) {
|
2014-09-22 13:50:51 +04:00
|
|
|
if b.l.r != nil {
|
|
|
|
b.Delete(key)
|
2014-09-02 13:55:12 +04:00
|
|
|
}
|
2014-09-17 13:54:04 +04:00
|
|
|
|
2014-09-02 13:55:12 +04:00
|
|
|
b.WriteBatch.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbBatchLocker struct {
|
|
|
|
l *sync.Mutex
|
|
|
|
wrLock *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *dbBatchLocker) Lock() {
|
|
|
|
l.wrLock.RLock()
|
|
|
|
l.l.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *dbBatchLocker) Unlock() {
|
|
|
|
l.l.Unlock()
|
|
|
|
l.wrLock.RUnlock()
|
|
|
|
}
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
type txBatchLocker struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *txBatchLocker) Lock() {}
|
|
|
|
func (l *txBatchLocker) Unlock() {}
|
|
|
|
|
2014-09-02 13:55:12 +04:00
|
|
|
type multiBatchLocker struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *multiBatchLocker) Lock() {}
|
|
|
|
func (l *multiBatchLocker) Unlock() {}
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
func (l *Ledis) newBatch(wb store.WriteBatch, locker sync.Locker, tx *Tx) *batch {
|
2014-09-02 13:55:12 +04:00
|
|
|
b := new(batch)
|
|
|
|
b.l = l
|
|
|
|
b.WriteBatch = wb
|
|
|
|
|
|
|
|
b.Locker = locker
|
|
|
|
|
2014-09-25 06:44:07 +04:00
|
|
|
b.tx = tx
|
2014-09-22 13:50:51 +04:00
|
|
|
b.eb = new(eventBatch)
|
2014-09-17 13:54:04 +04:00
|
|
|
|
2014-09-02 13:55:12 +04:00
|
|
|
return b
|
|
|
|
}
|
2014-09-25 06:44:07 +04:00
|
|
|
|
|
|
|
type commiter interface {
|
|
|
|
Commit() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) handleCommit(eb *eventBatch, c commiter) error {
|
|
|
|
l.commitLock.Lock()
|
|
|
|
defer l.commitLock.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if l.r != nil {
|
|
|
|
var rl *rpl.Log
|
|
|
|
if rl, err = l.r.Log(eb.Bytes()); err != nil {
|
|
|
|
log.Fatal("write wal error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.propagate(rl)
|
|
|
|
|
|
|
|
if err = c.Commit(); err != nil {
|
|
|
|
log.Fatal("commit error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = l.r.UpdateCommitID(rl.ID); err != nil {
|
|
|
|
log.Fatal("update commit id error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return c.Commit()
|
|
|
|
}
|
|
|
|
}
|