diff --git a/cmd/db_cleanup.go b/cmd/db_cleanup.go index 13a17575..e43b9ce4 100644 --- a/cmd/db_cleanup.go +++ b/cmd/db_cleanup.go @@ -143,6 +143,10 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error { context.progress.Printf("Disk space freed: %.2f GiB...\n", float64(totalSize)/1024.0/1024.0/1024.0) } + + context.progress.Printf("Compacting database...\n") + err = context.database.CompactDB() + return err } diff --git a/database/leveldb.go b/database/leveldb.go index 5c42ec9f..deac8dea 100644 --- a/database/leveldb.go +++ b/database/leveldb.go @@ -7,6 +7,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/filter" "github.com/syndtr/goleveldb/leveldb/opt" + "github.com/syndtr/goleveldb/leveldb/util" ) // Errors for Storage @@ -24,6 +25,7 @@ type Storage interface { Close() error StartBatch() FinishBatch() error + CompactDB() error } type levelDB struct { @@ -49,6 +51,7 @@ func OpenDB(path string) (Storage, error) { return &levelDB{db: db}, nil } +// Get key value from database func (l *levelDB) Get(key []byte) ([]byte, error) { value, err := l.db.Get(key, nil) if err != nil { @@ -61,6 +64,7 @@ func (l *levelDB) Get(key []byte) ([]byte, error) { return value, nil } +// Put saves key to database, if key has the same value in DB already, it is not saved func (l *levelDB) Put(key []byte, value []byte) error { if l.batch != nil { l.batch.Put(key, value) @@ -79,6 +83,7 @@ func (l *levelDB) Put(key []byte, value []byte) error { return l.db.Put(key, value, nil) } +// Delete removes key from DB func (l *levelDB) Delete(key []byte) error { if l.batch != nil { l.batch.Delete(key) @@ -87,6 +92,7 @@ func (l *levelDB) Delete(key []byte) error { return l.db.Delete(key, nil) } +// KeysByPrefix returns all keys that start with prefix func (l *levelDB) KeysByPrefix(prefix []byte) [][]byte { result := make([][]byte, 0, 20) @@ -103,6 +109,7 @@ func (l *levelDB) KeysByPrefix(prefix []byte) [][]byte { return result } +// FetchByPrefix returns all values with keys that start with prefix func (l *levelDB) FetchByPrefix(prefix []byte) [][]byte { result := make([][]byte, 0, 20) @@ -119,10 +126,14 @@ func (l *levelDB) FetchByPrefix(prefix []byte) [][]byte { return result } +// Close finishes DB work func (l *levelDB) Close() error { return l.db.Close() } +// StartBatch starts batch processing of keys +// +// All subsequent Get, Put and Delete would work on batch func (l *levelDB) StartBatch() { if l.batch != nil { panic("batch already started") @@ -130,6 +141,7 @@ func (l *levelDB) StartBatch() { l.batch = new(leveldb.Batch) } +// FinishBatch finalizes the batch, saving operations func (l *levelDB) FinishBatch() error { if l.batch == nil { panic("no batch") @@ -138,3 +150,8 @@ func (l *levelDB) FinishBatch() error { l.batch = nil return err } + +// CompactDB compacts database by merging layers +func (l *levelDB) CompactDB() error { + return l.db.CompactRange(util.Range{}) +} diff --git a/database/leveldb_test.go b/database/leveldb_test.go index a93dc9aa..5863b456 100644 --- a/database/leveldb_test.go +++ b/database/leveldb_test.go @@ -122,3 +122,11 @@ func (s *LevelDBSuite) TestBatch(c *C) { s.db.StartBatch() c.Check(func() { s.db.StartBatch() }, Panics, "batch already started") } + +func (s *LevelDBSuite) TestCompactDB(c *C) { + s.db.Put([]byte{0x80, 0x01}, []byte{0x01}) + s.db.Put([]byte{0x80, 0x03}, []byte{0x03}) + s.db.Put([]byte{0x80, 0x02}, []byte{0x02}) + + c.Check(s.db.CompactDB(), IsNil) +} diff --git a/system/t08_db/CleanupDB1Test_gold b/system/t08_db/CleanupDB1Test_gold index 11a05b46..1816ebdd 100644 --- a/system/t08_db/CleanupDB1Test_gold +++ b/system/t08_db/CleanupDB1Test_gold @@ -4,3 +4,4 @@ Deleting unreferenced packages (0)... Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (0)... +Compacting database... diff --git a/system/t08_db/CleanupDB2Test_gold b/system/t08_db/CleanupDB2Test_gold index 061b8199..051152a6 100644 --- a/system/t08_db/CleanupDB2Test_gold +++ b/system/t08_db/CleanupDB2Test_gold @@ -4,3 +4,4 @@ Deleting unreferenced packages (73295)... Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (0)... +Compacting database... diff --git a/system/t08_db/CleanupDB3Test_gold b/system/t08_db/CleanupDB3Test_gold index 1b003922..82c8446e 100644 --- a/system/t08_db/CleanupDB3Test_gold +++ b/system/t08_db/CleanupDB3Test_gold @@ -5,3 +5,4 @@ Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (9)... Disk space freed: 0.01 GiB... +Compacting database... diff --git a/system/t08_db/CleanupDB4Test_gold b/system/t08_db/CleanupDB4Test_gold index 11a05b46..1816ebdd 100644 --- a/system/t08_db/CleanupDB4Test_gold +++ b/system/t08_db/CleanupDB4Test_gold @@ -4,3 +4,4 @@ Deleting unreferenced packages (0)... Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (0)... +Compacting database... diff --git a/system/t08_db/CleanupDB5Test_gold b/system/t08_db/CleanupDB5Test_gold index 1b003922..82c8446e 100644 --- a/system/t08_db/CleanupDB5Test_gold +++ b/system/t08_db/CleanupDB5Test_gold @@ -5,3 +5,4 @@ Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (9)... Disk space freed: 0.01 GiB... +Compacting database... diff --git a/system/t08_db/CleanupDB6Test_gold b/system/t08_db/CleanupDB6Test_gold index 11a05b46..1816ebdd 100644 --- a/system/t08_db/CleanupDB6Test_gold +++ b/system/t08_db/CleanupDB6Test_gold @@ -4,3 +4,4 @@ Deleting unreferenced packages (0)... Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (0)... +Compacting database... diff --git a/system/t08_db/CleanupDB7Test_gold b/system/t08_db/CleanupDB7Test_gold index 11a05b46..1816ebdd 100644 --- a/system/t08_db/CleanupDB7Test_gold +++ b/system/t08_db/CleanupDB7Test_gold @@ -4,3 +4,4 @@ Deleting unreferenced packages (0)... Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (0)... +Compacting database... diff --git a/system/t08_db/CleanupDB8Test_gold b/system/t08_db/CleanupDB8Test_gold index 8ab0e68b..4f43418e 100644 --- a/system/t08_db/CleanupDB8Test_gold +++ b/system/t08_db/CleanupDB8Test_gold @@ -5,3 +5,4 @@ Building list of files referenced by packages... Building list of files in package pool... Deleting unreferenced files (5)... Disk space freed: 0.00 GiB... +Compacting database...