From ce2ee9956bef1ee6b503ede1cf749c490d041aa7 Mon Sep 17 00:00:00 2001 From: rkfg Date: Sat, 19 Dec 2020 15:30:59 +0300 Subject: [PATCH] Try to recover DB on corruption --- db.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/db.go b/db.go index d7af8eb..7d879cf 100644 --- a/db.go +++ b/db.go @@ -2,9 +2,11 @@ package main import ( "encoding/binary" + "log" "strings" "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/util" ) @@ -26,10 +28,11 @@ func commitOrDiscard(tx *leveldb.Transaction, err *error) { func openDB(dbPath string) (err error) { db, err = leveldb.OpenFile(dbPath, nil) - if err != nil { - return + if e, corrupted := err.(*errors.ErrCorrupted); corrupted { + log.Printf("WARNING: database corruption: %s. Attempting to recover...", e) + db, err = leveldb.RecoverFile(dbPath, nil) } - return nil + return } func closeDB() error {