Skip to content

Commit

Permalink
Create read-only factory functions for databases. (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador authored Apr 22, 2024
1 parent 570203f commit 60d9336
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
4 changes: 4 additions & 0 deletions db/base_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func MakeDefaultBaseDBFromBaseDB(db BaseDB) BaseDB {
return &baseDB{backend: db.getBackend()}
}

// NewReadOnlyBaseDB creates a new instance of read-only BaseDB.
func NewReadOnlyBaseDB(path string) (BaseDB, error) {
return newBaseDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}
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
5 changes: 5 additions & 0 deletions db/code_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func MakeDefaultCodeDBFromBaseDB(db BaseDB) CodeDB {
return &codeDB{&baseDB{backend: db.getBackend()}}
}

// NewReadOnlyCodeDB creates a new instance of read-only CodeDB.
func NewReadOnlyCodeDB(path string) (CodeDB, error) {
return newCodeDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

func newCodeDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*codeDB, error) {
base, err := newBaseDB(path, o, wo, ro)
if err != nil {
Expand Down
22 changes: 8 additions & 14 deletions db/destroyed_account_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"errors"
"fmt"
"log"

"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
Expand All @@ -17,29 +16,24 @@ type DestroyedAccountDB struct {
backend BaseDB
}

func NewDestroyedAccountDB(backend BaseDB) *DestroyedAccountDB {
return &DestroyedAccountDB{backend: backend}
}

func OpenDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return openDestroyedAccountDB(destroyedAccountDir, &opt.Options{ReadOnly: false}, nil, nil)
func NewDefaultDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return newDestroyedAccountDB(destroyedAccountDir, nil, nil, nil)
}

func OpenDestroyedAccountDBReadOnly(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return openDestroyedAccountDB(destroyedAccountDir, &opt.Options{ReadOnly: true}, nil, nil)
func MakeDefaultDestroyedAccountDBFromBaseDB(backend BaseDB) *DestroyedAccountDB {
return &DestroyedAccountDB{backend: backend}
}

func MakeDestroyedAccountDBFromBaseDB(db BaseDB) *DestroyedAccountDB {
return &DestroyedAccountDB{db}
func NewReadOnlyDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return newDestroyedAccountDB(destroyedAccountDir, &opt.Options{ReadOnly: true}, nil, nil)
}

func openDestroyedAccountDB(destroyedAccountDir string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*DestroyedAccountDB, error) {
log.Println("substate: OpenDestroyedAccountDB")
func newDestroyedAccountDB(destroyedAccountDir string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*DestroyedAccountDB, error) {
backend, err := newBaseDB(destroyedAccountDir, o, wo, ro)
if err != nil {
return nil, fmt.Errorf("error opening deletion-db %s: %w", destroyedAccountDir, err)
}
return NewDestroyedAccountDB(backend), nil
return MakeDefaultDestroyedAccountDBFromBaseDB(backend), nil
}

func (db *DestroyedAccountDB) Close() error {
Expand Down
7 changes: 6 additions & 1 deletion db/substate_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func MakeDefaultSubstateDBFromBaseDB(db BaseDB) SubstateDB {
return &substateDB{&codeDB{&baseDB{backend: db.getBackend()}}}
}

func MakeSubstateDb(db *leveldb.DB, wo *opt.WriteOptions, ro *opt.ReadOptions) SubstateDB {
// NewReadOnlySubstateDB creates a new instance of read-only SubstateDB.
func NewReadOnlySubstateDB(path string) (SubstateDB, error) {
return newSubstateDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

func MakeSubstateDB(db *leveldb.DB, wo *opt.WriteOptions, ro *opt.ReadOptions) SubstateDB {
return &substateDB{&codeDB{&baseDB{backend: db, wo: wo, ro: ro}}}
}

Expand Down
5 changes: 5 additions & 0 deletions db/update_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func MakeDefaultUpdateDBFromBaseDB(db BaseDB) UpdateDB {
return &updateDB{&codeDB{&baseDB{backend: db.getBackend()}}}
}

// NewReadOnlyUpdateDB creates a new instance of read-only UpdateDB.
func NewReadOnlyUpdateDB(path string) (UpdateDB, error) {
return newUpdateDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

func newUpdateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*updateDB, error) {
base, err := newCodeDB(path, o, wo, ro)
if err != nil {
Expand Down

0 comments on commit 60d9336

Please sign in to comment.