MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / NewDatabase

Function NewDatabase

worker/db.go:93–221  ·  view source on GitHub ↗

NewDatabase create a single database instance using config.

(cfg *DBConfig, peers *proto.Peers,
	genesis *types.Block)

Source from the content-addressed store, hash-verified

91
92// NewDatabase create a single database instance using config.
93func NewDatabase(cfg *DBConfig, peers *proto.Peers,
94 genesis *types.Block) (db *Database, err error) {
95 // ensure dir exists
96 if err = os.MkdirAll(cfg.DataDir, 0755); err != nil {
97 return
98 }
99
100 if peers == nil || genesis == nil {
101 err = ErrInvalidDBConfig
102 return
103 }
104
105 // get private key
106 var privateKey *asymmetric.PrivateKey
107 if privateKey, err = kms.GetLocalPrivateKey(); err != nil {
108 return
109 }
110
111 var accountAddr proto.AccountAddress
112 if accountAddr, err = crypto.PubKeyHash(privateKey.PubKey()); err != nil {
113 return
114 }
115
116 // init database
117 db = &Database{
118 cfg: cfg,
119 dbID: cfg.DatabaseID,
120 mux: cfg.KayakMux,
121 connSeqEvictCh: make(chan uint64, 1),
122 privateKey: privateKey,
123 accountAddr: accountAddr,
124 }
125
126 defer func() {
127 // on error recycle all resources
128 if err != nil {
129 // stop kayak runtime
130 if db.kayakRuntime != nil {
131 db.kayakRuntime.Shutdown()
132 }
133
134 // close chain
135 if db.chain != nil {
136 db.chain.Stop()
137 }
138 }
139 }()
140
141 // init storage
142 storageFile := filepath.Join(cfg.DataDir, StorageFileName)
143 storageDSN, err := storage.NewDSN(storageFile)
144 if err != nil {
145 return
146 }
147
148 if cfg.EncryptionKey != "" {
149 storageDSN.AddParam("_crypto_key", cfg.EncryptionKey)
150 }

Callers 4

CreateMethod · 0.70
TestSingleDatabaseFunction · 0.70
TestInitFailedFunction · 0.70
TestDatabaseRecycleFunction · 0.70

Calls 14

PubKeyMethod · 0.95
AddParamMethod · 0.95
FormatMethod · 0.95
GetLocalPrivateKeyFunction · 0.92
PubKeyHashFunction · 0.92
NewDSNFunction · 0.92
GetLocalNodeIDFunction · 0.92
NewChainFunction · 0.92
NewRuntimeFunction · 0.92
evictSequencesMethod · 0.80
ShutdownMethod · 0.45
StopMethod · 0.45

Tested by 3

TestSingleDatabaseFunction · 0.56
TestInitFailedFunction · 0.56
TestDatabaseRecycleFunction · 0.56