Skip to content

Commit

Permalink
immutable db for query
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshishuo committed Jun 17, 2020
1 parent 27d9788 commit 69667c2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
8 changes: 8 additions & 0 deletions modules/stake/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func getDb() *sql.DB {
return db
}

func getImmuDb() *sql.DB {
db, err := dbm.Sqliter.GetImmuDB()
if err != nil {
panic(err)
}
return db
}

type SqlTxWrapper struct {
tx *sql.Tx
withBlock bool
Expand Down
8 changes: 4 additions & 4 deletions modules/stake/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

func QueryCandidates() (candidates Candidates) {
db := getDb()
db := getImmuDb()
cond := make(map[string]interface{})
cond["active"] = "Y"
return queryCandidates(db, cond)
}

func QueryCandidateByAddress(address common.Address) *Candidate {
db := getDb()
db := getImmuDb()
cond := make(map[string]interface{})
cond["address"] = address.String()
candidates := queryCandidates(db, cond)
Expand All @@ -25,7 +25,7 @@ func QueryCandidateByAddress(address common.Address) *Candidate {
}

func QueryCandidateById(id int64) *Candidate {
db := getDb()
db := getImmuDb()
cond := make(map[string]interface{})
cond["id"] = id
candidates := queryCandidates(db, cond)
Expand All @@ -37,7 +37,7 @@ func QueryCandidateById(id int64) *Candidate {
}

func QueryDelegationsByAddress(delegatorAddress common.Address) (delegations []*Delegation) {
db := getDb()
db := getImmuDb()
cond := make(map[string]interface{})
cond["delegator_address"] = delegatorAddress.String()
return queryDelegations(db, cond)
Expand Down
32 changes: 30 additions & 2 deletions sdk/dbm/sqliter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type sqliter struct{
dbPath string
db *sql.DB
immuDb *sql.DB
}

var Sqliter = &sqliter{}
Expand Down Expand Up @@ -50,14 +51,41 @@ func (s *sqliter) GetDB() (*sql.DB, error) {
return db, nil
}

func (s *sqliter) GetImmuDB() (*sql.DB, error) {
if strings.Compare(s.dbPath, "") == 0 {
return nil, errors.ErrInternal("Sqlite database path is not set.")
}
if s.immuDb != nil {
if s.immuDb.Stats().OpenConnections > 0 {
return s.immuDb, nil
}
}
db, err := sql.Open("sqlite3", s.dbPath)
if err != nil {
return nil, errors.ErrInternal("Open database: " + err.Error())
}
if db.Ping(); err != nil {
return nil, errors.ErrInternal("Open database: " + err.Error())
}
db.Exec("PRAGMA journal_mode=WAL;")
s.immuDb = db
return db, nil
}

func (s *sqliter) CloseDB() {
if s.db != nil {
if err := s.db.Close(); err != nil {
log.Warn("Failed to close sqlite db: " + s.dbPath)
log.Warn("Failed to close sqlite db connection: " + s.dbPath)
}
log.Info("Sqlite db closed successfully!")
s.db = nil
}
if s.immuDb != nil {
if err := s.immuDb.Close(); err != nil {
log.Warn("Failed to close sqlite immutable connection: " + s.dbPath)
}
s.immuDb = nil
}
log.Info("Sqlite db closed successfully!")
}


Expand Down

0 comments on commit 69667c2

Please sign in to comment.