2014-07-29 13:29:51 +04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/siddontang/ledisdb/store/driver"
|
|
|
|
)
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
type Tx struct {
|
2014-10-16 05:35:35 +04:00
|
|
|
tx driver.Tx
|
2014-10-15 06:18:20 +04:00
|
|
|
st *Stat
|
2014-07-29 13:29:51 +04:00
|
|
|
}
|
2014-08-25 10:18:23 +04:00
|
|
|
|
|
|
|
func (tx *Tx) NewIterator() *Iterator {
|
|
|
|
it := new(Iterator)
|
2014-10-16 05:35:35 +04:00
|
|
|
it.it = tx.tx.NewIterator()
|
2014-10-15 06:18:20 +04:00
|
|
|
it.st = tx.st
|
|
|
|
|
|
|
|
tx.st.IterNum.Add(1)
|
2014-08-25 10:18:23 +04:00
|
|
|
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2014-10-15 06:18:20 +04:00
|
|
|
func (tx *Tx) NewWriteBatch() *WriteBatch {
|
|
|
|
tx.st.BatchNum.Add(1)
|
|
|
|
|
|
|
|
wb := new(WriteBatch)
|
2014-10-16 05:35:35 +04:00
|
|
|
wb.wb = tx.tx.NewWriteBatch()
|
2014-10-15 06:18:20 +04:00
|
|
|
wb.st = tx.st
|
|
|
|
return wb
|
2014-08-25 10:18:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) RangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator {
|
|
|
|
return NewRangeLimitIterator(tx.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) RevRangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator {
|
|
|
|
return NewRevRangeLimitIterator(tx.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1})
|
|
|
|
}
|
|
|
|
|
|
|
|
//count < 0, unlimit.
|
|
|
|
//
|
|
|
|
//offset must >= 0, if < 0, will get nothing.
|
|
|
|
func (tx *Tx) RangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *RangeLimitIterator {
|
|
|
|
return NewRangeLimitIterator(tx.NewIterator(), &Range{min, max, rangeType}, &Limit{offset, count})
|
|
|
|
}
|
|
|
|
|
|
|
|
//count < 0, unlimit.
|
|
|
|
//
|
|
|
|
//offset must >= 0, if < 0, will get nothing.
|
|
|
|
func (tx *Tx) RevRangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *RangeLimitIterator {
|
|
|
|
return NewRevRangeLimitIterator(tx.NewIterator(), &Range{min, max, rangeType}, &Limit{offset, count})
|
|
|
|
}
|
2014-10-15 06:18:20 +04:00
|
|
|
|
|
|
|
func (tx *Tx) Get(key []byte) ([]byte, error) {
|
2014-10-16 05:35:35 +04:00
|
|
|
v, err := tx.tx.Get(key)
|
2014-10-15 06:18:20 +04:00
|
|
|
tx.st.statGet(v, err)
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
2014-10-29 15:17:12 +03:00
|
|
|
func (tx *Tx) GetSlice(key []byte) (Slice, error) {
|
|
|
|
if v, err := tx.Get(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return driver.GoSlice(v), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 06:18:20 +04:00
|
|
|
func (tx *Tx) Put(key []byte, value []byte) error {
|
|
|
|
tx.st.PutNum.Add(1)
|
2014-10-16 05:35:35 +04:00
|
|
|
return tx.tx.Put(key, value)
|
2014-10-15 06:18:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Delete(key []byte) error {
|
|
|
|
tx.st.DeleteNum.Add(1)
|
2014-10-16 05:35:35 +04:00
|
|
|
return tx.tx.Delete(key)
|
2014-10-15 06:18:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Commit() error {
|
|
|
|
tx.st.TxCommitNum.Add(1)
|
2014-10-16 05:35:35 +04:00
|
|
|
return tx.tx.Commit()
|
2014-10-15 06:18:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Rollback() error {
|
2014-10-16 05:35:35 +04:00
|
|
|
return tx.tx.Rollback()
|
2014-10-15 06:18:20 +04:00
|
|
|
}
|