ledisdb/store/mdb/snapshot.go

44 lines
778 B
Go
Raw Normal View History

2015-03-11 06:54:16 +03:00
// +build lmdb
2014-08-25 10:18:23 +04:00
package mdb
import (
"github.com/siddontang/ledisdb/store/driver"
mdb "github.com/siddontang/ledisdb/vendor/gomdb"
2014-08-25 10:18:23 +04:00
)
type Snapshot struct {
db mdb.DBI
tx *mdb.Txn
}
func newSnapshot(db MDB) (*Snapshot, error) {
tx, err := db.env.BeginTxn(nil, mdb.RDONLY)
if err != nil {
return nil, err
}
return &Snapshot{db.db, tx}, nil
}
func (s *Snapshot) Get(key []byte) ([]byte, error) {
v, err := s.tx.Get(s.db, key)
if err == mdb.NotFound {
return nil, nil
}
return v, err
}
func (s *Snapshot) NewIterator() driver.IIterator {
c, err := s.tx.CursorOpen(s.db)
if err != nil {
return &MDBIterator{nil, nil, nil, nil, false, err, false}
}
return &MDBIterator{nil, nil, c, s.tx, true, nil, false}
}
func (s *Snapshot) Close() {
s.tx.Commit()
}