diff --git a/store/boltdb/db.go b/store/boltdb/db.go index 8212d08..f2cb1f3 100644 --- a/store/boltdb/db.go +++ b/store/boltdb/db.go @@ -152,6 +152,10 @@ func (db *DB) BatchPut(writes []driver.Write) error { return err } +func (db *DB) Compact() error { + return nil +} + func init() { driver.Register(Store{}) } diff --git a/store/db.go b/store/db.go index ca57326..99e6ed2 100644 --- a/store/db.go +++ b/store/db.go @@ -6,6 +6,11 @@ import ( type DB struct { driver.IDB + name string +} + +func (db *DB) String() string { + return db.name } func (db *DB) NewIterator() *Iterator { @@ -29,6 +34,10 @@ func (db *DB) NewSnapshot() (*Snapshot, error) { return s, nil } +func (db *DB) Compact() error { + return db.IDB.Compact() +} + func (db *DB) RangeIterator(min []byte, max []byte, rangeType uint8) *RangeLimitIterator { return NewRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1}) } diff --git a/store/driver/driver.go b/store/driver/driver.go index 6da67df..859b840 100644 --- a/store/driver/driver.go +++ b/store/driver/driver.go @@ -23,6 +23,8 @@ type IDB interface { NewSnapshot() (ISnapshot, error) Begin() (Tx, error) + + Compact() error } type ISnapshot interface { diff --git a/store/goleveldb/db.go b/store/goleveldb/db.go index e873feb..2a13f50 100644 --- a/store/goleveldb/db.go +++ b/store/goleveldb/db.go @@ -6,6 +6,7 @@ import ( "github.com/siddontang/goleveldb/leveldb/filter" "github.com/siddontang/goleveldb/leveldb/opt" "github.com/siddontang/goleveldb/leveldb/storage" + "github.com/siddontang/goleveldb/leveldb/util" "github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/store/driver" @@ -180,6 +181,10 @@ func (db *DB) NewSnapshot() (driver.ISnapshot, error) { return s, nil } +func (db *DB) Compact() error { + return db.db.CompactRange(util.Range{nil, nil}) +} + func init() { driver.Register(Store{}) driver.Register(MemStore{}) diff --git a/store/hyperleveldb/db.go b/store/hyperleveldb/db.go index 6d0e176..d6d7aeb 100644 --- a/store/hyperleveldb/db.go +++ b/store/hyperleveldb/db.go @@ -270,6 +270,11 @@ func (db *DB) Begin() (driver.Tx, error) { return nil, driver.ErrTxSupport } +func (db *DB) Compact() error { + C.leveldb_compact_range(db.db, nil, 0, nil, 0) + return nil +} + func init() { driver.Register(Store{}) } diff --git a/store/leveldb/db.go b/store/leveldb/db.go index 43ee0c2..92a2419 100644 --- a/store/leveldb/db.go +++ b/store/leveldb/db.go @@ -270,6 +270,11 @@ func (db *DB) Begin() (driver.Tx, error) { return nil, driver.ErrTxSupport } +func (db *DB) Compact() error { + C.leveldb_compact_range(db.db, nil, 0, nil, 0) + return nil +} + func init() { driver.Register(Store{}) } diff --git a/store/mdb/mdb.go b/store/mdb/mdb.go index d5c3987..ca79706 100644 --- a/store/mdb/mdb.go +++ b/store/mdb/mdb.go @@ -240,9 +240,6 @@ func (db MDB) Path() string { return db.path } -func (db MDB) Compact() { -} - func (db MDB) iterator(rdonly bool) *MDBIterator { flags := uint(0) if rdonly { @@ -286,6 +283,10 @@ func (db MDB) NewSnapshot() (driver.ISnapshot, error) { return newSnapshot(db) } +func (db MDB) Compact() error { + return nil +} + func init() { driver.Register(Store{}) } diff --git a/store/rocksdb/db.go b/store/rocksdb/db.go index cc1ab81..1c79229 100644 --- a/store/rocksdb/db.go +++ b/store/rocksdb/db.go @@ -296,6 +296,11 @@ func (db *DB) Begin() (driver.Tx, error) { return nil, driver.ErrTxSupport } +func (db *DB) Compact() error { + C.rocksdb_compact_range(db.db, nil, 0, nil, 0) + return nil +} + func init() { driver.Register(Store{}) } diff --git a/store/store.go b/store/store.go index e2a6b85..d620c03 100644 --- a/store/store.go +++ b/store/store.go @@ -36,7 +36,7 @@ func Open(cfg *config.Config) (*DB, error) { return nil, err } - db := &DB{idb} + db := &DB{idb, s.String()} return db, nil } diff --git a/store/store_test.go b/store/store_test.go index 5fdd95d..d2f2ca6 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -148,6 +148,9 @@ func checkIterator(it *RangeLimitIterator, cv ...int) error { v := make([]string, 0, len(cv)) for ; it.Valid(); it.Next() { k := it.Key() + if string(it.Value()) != "value" { + return fmt.Errorf("invalid value") + } v = append(v, string(k)) } @@ -175,7 +178,7 @@ func testIterator(db *DB, t *testing.T) { for i := 0; i < 10; i++ { key := []byte(fmt.Sprintf("key_%d", i)) - value := []byte("") + value := []byte("value") db.Put(key, value) }