update lmdv

This commit is contained in:
siddontang 2014-07-25 19:41:04 +08:00
parent 7a237661da
commit 4f5e47e907
5 changed files with 39 additions and 10 deletions

View File

@ -7,4 +7,4 @@ go get github.com/siddontang/go-snappy/snappy
go get github.com/siddontang/copier go get github.com/siddontang/copier
go get github.com/syndtr/goleveldb/leveldb go get github.com/syndtr/goleveldb/leveldb
go get github.com/szferi/gomdb go get github.com/influxdb/gomdb

View File

@ -5,10 +5,13 @@ type Config struct {
Path string Path string
//for leveldb, goleveldb, rocksdb //for leveldb, goleveldb
Compression bool Compression bool
BlockSize int BlockSize int
WriteBufferSize int WriteBufferSize int
CacheSize int CacheSize int
MaxOpenFiles int MaxOpenFiles int
//for lmdb
MapSize int
} }

View File

@ -6,27 +6,27 @@ type Write struct {
} }
type WriteBatch struct { type WriteBatch struct {
db MDB db *MDB
wb []Write wb []Write
} }
func (w WriteBatch) Close() error { func (w *WriteBatch) Close() error {
return nil return nil
} }
func (w WriteBatch) Put(key, value []byte) { func (w *WriteBatch) Put(key, value []byte) {
w.wb = append(w.wb, Write{key, value}) w.wb = append(w.wb, Write{key, value})
} }
func (w WriteBatch) Delete(key []byte) { func (w *WriteBatch) Delete(key []byte) {
w.wb = append(w.wb, Write{key, nil}) w.wb = append(w.wb, Write{key, nil})
} }
func (w WriteBatch) Commit() error { func (w *WriteBatch) Commit() error {
return w.db.BatchPut(w.wb) return w.db.BatchPut(w.wb)
} }
func (w WriteBatch) Rollback() error { func (w *WriteBatch) Rollback() error {
w.wb = []Write{} w.wb = []Write{}
return nil return nil
} }

View File

@ -20,7 +20,7 @@ type MDB struct {
func Open(c *Config) (MDB, error) { func Open(c *Config) (MDB, error) {
path := c.Path path := c.Path
if c.MapSize == 0 { if c.MapSize == 0 {
c.MapSize = 1 * 1024 * 1024 * 1024 c.MapSize = 10 * 1024 * 1024
} }
env, err := mdb.NewEnv() env, err := mdb.NewEnv()
@ -245,5 +245,5 @@ func (db MDB) NewIterator() driver.IIterator {
} }
func (db MDB) NewWriteBatch() driver.IWriteBatch { func (db MDB) NewWriteBatch() driver.IWriteBatch {
return WriteBatch{db, []Write{}} return &WriteBatch{&db, []Write{}}
} }

View File

@ -137,6 +137,16 @@ func TestIterator(t *testing.T) {
db.Put(key, value) db.Put(key, value)
} }
i = db.NewIterator()
i.SeekToFirst()
if !i.Valid() {
t.Fatal("must valid")
} else if string(i.Key()) != "key_0" {
t.Fatal(string(i.Key()))
}
i.Close()
var it *RangeLimitIterator var it *RangeLimitIterator
k := func(i int) []byte { k := func(i int) []byte {
@ -147,51 +157,67 @@ func TestIterator(t *testing.T) {
if err := checkIterator(it, 1, 2, 3, 4, 5); err != nil { if err := checkIterator(it, 1, 2, 3, 4, 5); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RangeLimitIterator(k(1), k(5), RangeClose, 0, -1)
if err := checkIterator(it, 1, 2, 3, 4, 5); err != nil {
t.Fatal(err)
}
it.Close()
it = db.RangeLimitIterator(k(1), k(5), RangeClose, 1, 3) it = db.RangeLimitIterator(k(1), k(5), RangeClose, 1, 3)
if err := checkIterator(it, 2, 3, 4); err != nil { if err := checkIterator(it, 2, 3, 4); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RangeLimitIterator(k(1), k(5), RangeLOpen, 0, -1) it = db.RangeLimitIterator(k(1), k(5), RangeLOpen, 0, -1)
if err := checkIterator(it, 2, 3, 4, 5); err != nil { if err := checkIterator(it, 2, 3, 4, 5); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RangeLimitIterator(k(1), k(5), RangeROpen, 0, -1) it = db.RangeLimitIterator(k(1), k(5), RangeROpen, 0, -1)
if err := checkIterator(it, 1, 2, 3, 4); err != nil { if err := checkIterator(it, 1, 2, 3, 4); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RangeLimitIterator(k(1), k(5), RangeOpen, 0, -1) it = db.RangeLimitIterator(k(1), k(5), RangeOpen, 0, -1)
if err := checkIterator(it, 2, 3, 4); err != nil { if err := checkIterator(it, 2, 3, 4); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RevRangeLimitIterator(k(1), k(5), RangeClose, 0, -1) it = db.RevRangeLimitIterator(k(1), k(5), RangeClose, 0, -1)
if err := checkIterator(it, 5, 4, 3, 2, 1); err != nil { if err := checkIterator(it, 5, 4, 3, 2, 1); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RevRangeLimitIterator(k(1), k(5), RangeClose, 1, 3) it = db.RevRangeLimitIterator(k(1), k(5), RangeClose, 1, 3)
if err := checkIterator(it, 4, 3, 2); err != nil { if err := checkIterator(it, 4, 3, 2); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RevRangeLimitIterator(k(1), k(5), RangeLOpen, 0, -1) it = db.RevRangeLimitIterator(k(1), k(5), RangeLOpen, 0, -1)
if err := checkIterator(it, 5, 4, 3, 2); err != nil { if err := checkIterator(it, 5, 4, 3, 2); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RevRangeLimitIterator(k(1), k(5), RangeROpen, 0, -1) it = db.RevRangeLimitIterator(k(1), k(5), RangeROpen, 0, -1)
if err := checkIterator(it, 4, 3, 2, 1); err != nil { if err := checkIterator(it, 4, 3, 2, 1); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
it = db.RevRangeLimitIterator(k(1), k(5), RangeOpen, 0, -1) it = db.RevRangeLimitIterator(k(1), k(5), RangeOpen, 0, -1)
if err := checkIterator(it, 4, 3, 2); err != nil { if err := checkIterator(it, 4, 3, 2); err != nil {
t.Fatal(err) t.Fatal(err)
} }
it.Close()
} }
func TestCloseMore(t *testing.T) { func TestCloseMore(t *testing.T) {