Create add new database to the miner dbms.
(instance *types.ServiceInstance, cleanup bool)
| 412 | |
| 413 | // Create add new database to the miner dbms. |
| 414 | func (dbms *DBMS) Create(instance *types.ServiceInstance, cleanup bool) (err error) { |
| 415 | if _, alreadyExists := dbms.getMeta(instance.DatabaseID); alreadyExists { |
| 416 | return ErrAlreadyExists |
| 417 | } |
| 418 | |
| 419 | // set database root dir |
| 420 | rootDir := filepath.Join(dbms.cfg.RootDir, string(instance.DatabaseID)) |
| 421 | |
| 422 | // clear current data |
| 423 | if cleanup { |
| 424 | if err = os.RemoveAll(rootDir); err != nil { |
| 425 | return |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | var db *Database |
| 430 | |
| 431 | defer func() { |
| 432 | // close on failed |
| 433 | if err != nil { |
| 434 | if db != nil { |
| 435 | db.Shutdown() |
| 436 | } |
| 437 | |
| 438 | dbms.removeMeta(instance.DatabaseID) |
| 439 | } |
| 440 | }() |
| 441 | |
| 442 | // new db |
| 443 | dbCfg := &DBConfig{ |
| 444 | DatabaseID: instance.DatabaseID, |
| 445 | RootDir: dbms.cfg.RootDir, |
| 446 | DataDir: rootDir, |
| 447 | KayakMux: dbms.kayakMux, |
| 448 | ChainMux: dbms.chainMux, |
| 449 | MaxWriteTimeGap: dbms.cfg.MaxReqTimeGap, |
| 450 | EncryptionKey: instance.ResourceMeta.EncryptionKey, |
| 451 | SpaceLimit: instance.ResourceMeta.Space, |
| 452 | UpdateBlockCount: conf.GConf.BillingBlockCount, |
| 453 | UseEventualConsistency: instance.ResourceMeta.UseEventualConsistency, |
| 454 | ConsistencyLevel: instance.ResourceMeta.ConsistencyLevel, |
| 455 | IsolationLevel: instance.ResourceMeta.IsolationLevel, |
| 456 | SlowQueryTime: DefaultSlowQueryTime, |
| 457 | } |
| 458 | |
| 459 | // set last billing height |
| 460 | if profile, ok := dbms.busService.RequestSQLProfile(dbCfg.DatabaseID); ok { |
| 461 | dbCfg.LastBillingHeight = int32(profile.LastUpdatedHeight) |
| 462 | } |
| 463 | |
| 464 | if db, err = NewDatabase(dbCfg, instance.Peers, instance.GenesisBlock); err != nil { |
| 465 | return |
| 466 | } |
| 467 | |
| 468 | // add to meta |
| 469 | err = dbms.addMeta(instance.DatabaseID, db) |
| 470 | |
| 471 | // update metrics |
no test coverage detected