Centralize leveldb.OpenFile allowing Recover corrupted db everywhere (#1072)
This commit is contained in:
parent
8aa046e893
commit
237aeea7b2
|
@ -6,8 +6,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/status-im/status-go/db"
|
||||||
"github.com/status-im/status-go/mailserver"
|
"github.com/status-im/status-go/mailserver"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -47,7 +47,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db, err := leveldb.OpenFile(*dbPath, nil)
|
db, err := db.Open(*dbPath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
13
db/db.go
13
db/db.go
|
@ -45,14 +45,15 @@ func Create(path, dbName string) (*leveldb.DB, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
path = filepath.Join(path, dbName)
|
path = filepath.Join(path, dbName)
|
||||||
opts := &opt.Options{OpenFilesCacheCapacity: 5}
|
return Open(path, &opt.Options{OpenFilesCacheCapacity: 5})
|
||||||
db, err := leveldb.OpenFile(path, opts)
|
}
|
||||||
|
|
||||||
|
// Open opens an existing leveldb database
|
||||||
|
func Open(path string, opts *opt.Options) (db *leveldb.DB, err error) {
|
||||||
|
db, err = leveldb.OpenFile(path, opts)
|
||||||
if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
|
if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
|
||||||
log.Info("database is corrupted trying to recover", "path", path)
|
log.Info("database is corrupted trying to recover", "path", path)
|
||||||
db, err = leveldb.RecoverFile(path, nil)
|
db, err = leveldb.RecoverFile(path, nil)
|
||||||
}
|
}
|
||||||
if err != nil {
|
return
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return db, err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
||||||
|
"github.com/status-im/status-go/db"
|
||||||
"github.com/status-im/status-go/params"
|
"github.com/status-im/status-go/params"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||||
|
@ -119,7 +120,7 @@ func (s *WMailServer) Init(shh *whisper.Whisper, config *params.WhisperConfig) e
|
||||||
return errPasswordNotProvided
|
return errPasswordNotProvided
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := leveldb.OpenFile(config.DataDir, nil)
|
db, err := db.Open(config.DataDir, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("open DB: %s", err)
|
return fmt.Errorf("open DB: %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue