diff --git a/cmd/statusd-prune/main.go b/cmd/statusd-prune/main.go index e4fc1c980..d1ec84f40 100644 --- a/cmd/statusd-prune/main.go +++ b/cmd/statusd-prune/main.go @@ -6,8 +6,8 @@ import ( "log" "os" + "github.com/status-im/status-go/db" "github.com/status-im/status-go/mailserver" - "github.com/syndtr/goleveldb/leveldb" ) var ( @@ -47,7 +47,7 @@ func init() { } func main() { - db, err := leveldb.OpenFile(*dbPath, nil) + db, err := db.Open(*dbPath, nil) if err != nil { log.Fatal(err) } diff --git a/db/db.go b/db/db.go index 608a8a47d..6f8a728b8 100644 --- a/db/db.go +++ b/db/db.go @@ -45,14 +45,15 @@ func Create(path, dbName string) (*leveldb.DB, error) { } path = filepath.Join(path, dbName) - opts := &opt.Options{OpenFilesCacheCapacity: 5} - db, err := leveldb.OpenFile(path, opts) + return Open(path, &opt.Options{OpenFilesCacheCapacity: 5}) +} + +// 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 { log.Info("database is corrupted trying to recover", "path", path) db, err = leveldb.RecoverFile(path, nil) } - if err != nil { - return nil, err - } - return db, err + return } diff --git a/mailserver/mailserver.go b/mailserver/mailserver.go index 61b95e291..2aff17b48 100644 --- a/mailserver/mailserver.go +++ b/mailserver/mailserver.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/rlp" whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" + "github.com/status-im/status-go/db" "github.com/status-im/status-go/params" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -119,7 +120,7 @@ func (s *WMailServer) Init(shh *whisper.Whisper, config *params.WhisperConfig) e return errPasswordNotProvided } - db, err := leveldb.OpenFile(config.DataDir, nil) + db, err := db.Open(config.DataDir, nil) if err != nil { return fmt.Errorf("open DB: %s", err) }