From fa4b4dece17746952ec26fdd7689b076eed059b8 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 24 Nov 2015 13:15:56 +0100 Subject: [PATCH] Compact database on startup (ref #2400) --- cmd/syncthing/main.go | 7 +++++++ lib/db/leveldb_dbinstance.go | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 391ee3fc1..d21e03d7d 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -639,6 +639,13 @@ func syncthingMain() { } } + // Pack and optimize the database + if err := ldb.Compact(); err != nil { + // I don't think this is fatal, but who knows. If it is, we'll surely + // get an error when trying to write to the db later. + l.Infoln("Compacting database:", err) + } + m := model.NewModel(cfg, myID, myName, "syncthing", Version, ldb, protectedFiles) cfg.Subscribe(m) diff --git a/lib/db/leveldb_dbinstance.go b/lib/db/leveldb_dbinstance.go index f7996497a..9814fdd74 100644 --- a/lib/db/leveldb_dbinstance.go +++ b/lib/db/leveldb_dbinstance.go @@ -65,6 +65,10 @@ func newDBInstance(db *leveldb.DB) *Instance { } } +func (db *Instance) Compact() error { + return db.CompactRange(util.Range{}) +} + func (db *Instance) genericReplace(folder, device []byte, fs []protocol.FileInfo, localSize, globalSize *sizeTracker, deleteFn deletionHandler) int64 { sort.Sort(fileList(fs)) // sort list on name, same as in the database