ledisdb/store/mdb/tx.go

91 lines
1.6 KiB
Go
Raw Normal View History

2015-03-11 06:54:16 +03:00
// +build lmdb
2014-08-15 20:08:01 +04:00
2014-07-29 13:29:51 +04:00
package mdb
import (
"github.com/siddontang/ledisdb/store/driver"
2014-08-04 04:48:06 +04:00
mdb "github.com/szferi/gomdb"
2014-07-29 13:29:51 +04:00
)
type Tx struct {
db mdb.DBI
tx *mdb.Txn
}
func newTx(db MDB) (*Tx, error) {
tx, err := db.env.BeginTxn(nil, uint(0))
if err != nil {
return nil, err
}
return &Tx{db.db, tx}, nil
}
func (t *Tx) Get(key []byte) ([]byte, error) {
2014-08-25 10:18:23 +04:00
v, err := t.tx.Get(t.db, key)
if err == mdb.NotFound {
return nil, nil
}
return v, err
2014-07-29 13:29:51 +04:00
}
func (t *Tx) Put(key []byte, value []byte) error {
return t.tx.Put(t.db, key, value, mdb.NODUPDATA)
}
func (t *Tx) Delete(key []byte) error {
return t.tx.Del(t.db, key, nil)
}
func (t *Tx) NewIterator() driver.IIterator {
return t.newIterator()
}
func (t *Tx) newIterator() *MDBIterator {
c, err := t.tx.CursorOpen(t.db)
if err != nil {
return &MDBIterator{nil, nil, nil, nil, false, err, false}
}
return &MDBIterator{nil, nil, c, t.tx, true, nil, false}
}
func (t *Tx) NewWriteBatch() driver.IWriteBatch {
2014-08-02 11:16:16 +04:00
return driver.NewWriteBatch(t)
2014-07-29 13:29:51 +04:00
}
2014-08-02 11:16:16 +04:00
func (t *Tx) BatchPut(writes []driver.Write) error {
2014-07-29 13:29:51 +04:00
itr := t.newIterator()
for _, w := range writes {
if w.Value == nil {
2014-08-04 04:48:06 +04:00
itr.key, itr.value, itr.err = itr.c.Get(w.Key, nil, mdb.SET)
2014-07-29 13:29:51 +04:00
if itr.err == nil {
itr.err = itr.c.Del(0)
}
} else {
itr.err = itr.c.Put(w.Key, w.Value, 0)
}
if itr.err != nil && itr.err != mdb.NotFound {
break
}
}
itr.setState()
return itr.Close()
2014-10-09 09:05:55 +04:00
}
2014-07-29 13:29:51 +04:00
2014-10-09 09:05:55 +04:00
func (t *Tx) SyncBatchPut(writes []driver.Write) error {
return t.BatchPut(writes)
2014-07-29 13:29:51 +04:00
}
func (t *Tx) Rollback() error {
t.tx.Abort()
return nil
}
func (t *Tx) Commit() error {
return t.tx.Commit()
}