Skip to content

Commit

Permalink
Try to recover DB on corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Dec 19, 2020
1 parent dc513dd commit ce2ee99
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down

0 comments on commit ce2ee99

Please sign in to comment.