Skip to content

Commit

Permalink
use sql intead of orm (#258)
Browse files Browse the repository at this point in the history
* use sql intead of orm

* fix

* fix
  • Loading branch information
Lawliet-Chan authored Dec 25, 2024
1 parent d77ca08 commit 0246c0a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
48 changes: 29 additions & 19 deletions core/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ func (bc *BlockChain) appendCompactBlock(b *CompactBlock) error {

func (bc *BlockChain) ExistsBlock(blockHash Hash) (bool, error) {
var bss []BlocksScheme
err := bc.chain.Db().Where(&BlocksScheme{
Hash: blockHash.String(),
}).Find(&bss).Error
//err := bc.chain.Db().Where(&BlocksScheme{
// Hash: blockHash.String(),
//}).Find(&bss).Error

err := bc.chain.Db().Raw("select * from blockchain where hash = ?", blockHash.String()).Find(&bss).Error
if err != nil {
return false, err
}
Expand All @@ -168,9 +170,11 @@ func (bc *BlockChain) ExistsBlock(blockHash Hash) (bool, error) {

func (bc *BlockChain) GetCompactBlock(blockHash Hash) (*CompactBlock, error) {
var bs BlocksScheme
result := bc.chain.Db().Where(&BlocksScheme{
Hash: blockHash.String(),
}).Find(&bs)
//result := bc.chain.Db().Where(&BlocksScheme{
// Hash: blockHash.String(),
//}).Find(&bs)

result := bc.chain.Db().Raw("select * from blockchain where hash = ?", blockHash.String()).Find(&bs)
err := result.Error

if err != nil {
Expand All @@ -195,9 +199,11 @@ func (bc *BlockChain) GetCompactBlockByHeight(height BlockNum) (*CompactBlock, e
return block.Compact(), nil
}
var bs BlocksScheme
result := bc.chain.Db().Where(&BlocksScheme{
Height: height,
}).Find(&bs)
//result := bc.chain.Db().Where(&BlocksScheme{
// Height: height,
//}).Find(&bs)

result := bc.chain.Db().Raw("select * from blockchain where height = ?", height).Find(&bs)
err := result.Error

if err != nil {
Expand All @@ -215,10 +221,12 @@ func (bc *BlockChain) GetFinalizedCompactBlockByHeight(height BlockNum) (*Compac
return block.Compact(), nil
}
var bs BlocksScheme
result := bc.chain.Db().Where(&BlocksScheme{
Height: height,
Finalize: true,
}).Find(&bs)
//result := bc.chain.Db().Where(&BlocksScheme{
// Height: height,
// Finalize: true,
//}).Find(&bs)

result := bc.chain.Db().Raw("SELECT * FROM blockchain WHERE height = ? AND finalize = ?", height, true).Find(&bs)
err := result.Error

if err != nil {
Expand Down Expand Up @@ -271,7 +279,8 @@ func (bc *BlockChain) GetAllBlocksByHeight(height BlockNum) ([]*Block, error) {

func (bc *BlockChain) GetAllCompactBlocksByHeight(height BlockNum) ([]*CompactBlock, error) {
var bss []BlocksScheme
err := bc.chain.Db().Where(&BlocksScheme{Height: height}).Find(&bss).Error
//err := bc.chain.Db().Where(&BlocksScheme{Height: height}).Find(&bss).Error
err := bc.chain.Db().Raw("select * from blockchain where height = ?", height).Find(&bss).Error
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -369,11 +378,12 @@ func (bc *BlockChain) LastFinalizedCompact() (*CompactBlock, error) {
return block.Compact(), nil
}
var bs BlocksScheme
err := bc.chain.Db().Model(&BlocksScheme{}).
Where("finalize = ?", true).
Order("height DESC").
Limit(1).
Find(&bs).Error
//err := bc.chain.Db().Model(&BlocksScheme{}).
// Where("finalize = ?", true).
// Order("height DESC").
// Limit(1).
// Find(&bs).Error
err := bc.chain.Db().Raw("select * from blockchain where finalize = ? ORDER BY height DESC LIMIT 1", true).Find(&bs).Error
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion infra/storage/sql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func NewMysql(cfg *config.SqlDbConf) (*Mysql, error) {
newLogger := logger.New(
log.New(os.Stdout, "/r/n", log.LstdFlags), logger.Config{SlowThreshold: time.Second})
db, err := gorm.Open(mysql.Open(cfg.Dsn), &gorm.Config{
Logger: newLogger,
Logger: newLogger,
PrepareStmt: true,
})
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion infra/storage/sql/postgre.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func NewPostgreSql(cfg *config.SqlDbConf) (*PostgreSql, error) {
newLogger := logger.New(
log.New(os.Stdout, "/r/n", log.LstdFlags), logger.Config{SlowThreshold: time.Second})
db, err := gorm.Open(postgres.Open(cfg.Dsn), &gorm.Config{
Logger: newLogger,
Logger: newLogger,
PrepareStmt: true,
})
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions infra/storage/sql/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewSqlite(cfg *config.SqlDbConf) (*Sqlite, error) {
log.New(os.Stdout, "/r/n", log.LstdFlags), logger.Config{SlowThreshold: time.Second})
db, err := gorm.Open(sqlite.Open(cfg.Dsn), &gorm.Config{
Logger: newLogger,
PrepareStmt: true,
CreateBatchSize: 50000,
})
if err != nil {
Expand Down

0 comments on commit 0246c0a

Please sign in to comment.