github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/init.go (about) 1 package badger 2 3 import ( 4 "fmt" 5 6 "github.com/dgraph-io/badger/v2" 7 8 "github.com/onflow/flow-go/storage/badger/operation" 9 ) 10 11 // InitPublic initializes a public database by checking and setting the database 12 // type marker. If an existing, inconsistent type marker is set, this method will 13 // return an error. Once a database type marker has been set using these methods, 14 // the type cannot be changed. 15 func InitPublic(opts badger.Options) (*badger.DB, error) { 16 17 db, err := badger.Open(opts) 18 if err != nil { 19 return nil, fmt.Errorf("could not open db: %w", err) 20 } 21 err = db.Update(operation.InsertPublicDBMarker) 22 if err != nil { 23 return nil, fmt.Errorf("could not assert db type: %w", err) 24 } 25 26 return db, nil 27 } 28 29 // InitSecret initializes a secrets database by checking and setting the database 30 // type marker. If an existing, inconsistent type marker is set, this method will 31 // return an error. Once a database type marker has been set using these methods, 32 // the type cannot be changed. 33 func InitSecret(opts badger.Options) (*badger.DB, error) { 34 35 db, err := badger.Open(opts) 36 if err != nil { 37 return nil, fmt.Errorf("could not open db: %w", err) 38 } 39 err = db.Update(operation.InsertSecretDBMarker) 40 if err != nil { 41 return nil, fmt.Errorf("could not assert db type: %w", err) 42 } 43 44 return db, nil 45 }