Skip to content

Commit

Permalink
Open existing db (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador authored Jun 6, 2024
1 parent cc6c29f commit 0d28864
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 12 additions & 0 deletions db/base_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"fmt"
"io"
"os"

"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
Expand Down Expand Up @@ -87,6 +88,17 @@ func MakeDefaultBaseDBFromBaseDB(db BaseDB) BaseDB {
func NewReadOnlyBaseDB(path string) (BaseDB, error) {
return newBaseDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

// OpenBaseDB opens existing database. If it does not exists error is returned instead.
func OpenBaseDB(path string) (BaseDB, error) {
_, err := os.Stat(path)
if err != nil {
return nil, err
}

return NewDefaultBaseDB(path)
}

func newBaseDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*baseDB, error) {
b, err := leveldb.OpenFile(path, o)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions types/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
// Bytes gets the byte representation of the underlying hash.
func (h Hash) Bytes() []byte { return h[:] }

// Big converts a hash to a big integer.
func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }

// Compare two big int representations of h and h2.
func (h Hash) Compare(h2 Hash) int {
b1 := new(big.Int).SetBytes(h[:])
Expand Down

0 comments on commit 0d28864

Please sign in to comment.