2014-04-18 10:50:29 +04:00
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmhodges/levigo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Snapshot struct {
|
|
|
|
db *DB
|
|
|
|
s *levigo.Snapshot
|
|
|
|
readOpts *levigo.ReadOptions
|
|
|
|
iteratorOpts *levigo.ReadOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSnapshot(db *DB) *Snapshot {
|
|
|
|
s := new(Snapshot)
|
|
|
|
s.db = db
|
|
|
|
s.s = db.db.NewSnapshot()
|
|
|
|
|
|
|
|
s.readOpts = levigo.NewReadOptions()
|
|
|
|
s.readOpts.SetSnapshot(s.s)
|
|
|
|
|
|
|
|
s.iteratorOpts = levigo.NewReadOptions()
|
|
|
|
s.iteratorOpts.SetSnapshot(s.s)
|
|
|
|
s.iteratorOpts.SetFillCache(false)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) Close() {
|
|
|
|
s.db.db.ReleaseSnapshot(s.s)
|
|
|
|
|
|
|
|
s.iteratorOpts.Close()
|
|
|
|
s.readOpts.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) Get(key []byte) ([]byte, error) {
|
|
|
|
return s.db.db.Get(s.readOpts, key)
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:53:24 +04:00
|
|
|
func (s *Snapshot) Iterator(min []byte, max []byte, rangeType uint8, offset int, limit int) *Iterator {
|
|
|
|
return newIterator(s.db, s.iteratorOpts, NewRange(min, max, rangeType), offset, limit, IteratorForward)
|
2014-04-18 10:50:29 +04:00
|
|
|
}
|
|
|
|
|
2014-05-07 18:53:24 +04:00
|
|
|
func (s *Snapshot) RevIterator(min []byte, max []byte, rangeType uint8, offset int, limit int) *Iterator {
|
|
|
|
return newIterator(s.db, s.iteratorOpts, NewRange(min, max, rangeType), offset, limit, IteratorBackward)
|
2014-04-18 10:50:29 +04:00
|
|
|
}
|
2014-05-05 13:00:11 +04:00
|
|
|
|
|
|
|
func (s *Snapshot) GetInt(key []byte) (int64, error) {
|
2014-05-05 13:09:05 +04:00
|
|
|
return Int(s.Get(key))
|
2014-05-05 13:00:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) GetUInt(key []byte) (uint64, error) {
|
2014-05-05 13:09:05 +04:00
|
|
|
return Uint(s.Get(key))
|
2014-05-05 13:00:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) GetFloat(key []byte) (float64, error) {
|
2014-05-05 13:09:05 +04:00
|
|
|
return Float(s.Get(key))
|
2014-05-05 13:00:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) GetString(key []byte) (string, error) {
|
2014-05-05 13:09:05 +04:00
|
|
|
return String(s.Get(key))
|
2014-05-05 13:00:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) GetSlice(key []byte) ([]byte, error) {
|
2014-05-05 13:09:05 +04:00
|
|
|
return Slice(s.Get(key))
|
2014-05-05 13:00:11 +04:00
|
|
|
}
|