Skip to content

Commit

Permalink
Merge pull request #7 from celenium-io/optimization/tx-by-height-query
Browse files Browse the repository at this point in the history
Optimization: tx by height query
  • Loading branch information
aopoltorzhicky authored Mar 22, 2024
2 parents a6ae80d + ec3b2d7 commit 363f614
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions internal/storage/postgres/scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func addressListFilter(query *bun.SelectQuery, fltrs storage.AddressListFilter)
func txFilter(query *bun.SelectQuery, fltrs storage.TxFilter) *bun.SelectQuery {
query = limitScope(query, fltrs.Limit)
query = sortScope(query, "tx.id", fltrs.Sort)
query = offsetScope(query, fltrs.Offset)

if !fltrs.ActionTypes.Empty() {
query = query.Where("action_types & ? > 0", fltrs.ActionTypes.Bits)
Expand Down
17 changes: 10 additions & 7 deletions internal/storage/postgres/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ func (tx *Tx) ByHash(ctx context.Context, hash []byte) (transaction storage.Tx,
}

func (tx *Tx) ByHeight(ctx context.Context, height types.Level, limit, offset int) (txs []storage.Tx, err error) {
query := tx.DB().NewSelect().Model(&txs).
Where("tx.height = ?", height).
Relation("Signer")
query := tx.DB().NewSelect().Model((*storage.Tx)(nil)).
Where("tx.height = ?", height)

query = limitScope(query, limit)
if offset > 0 {
query = query.Offset(offset)
}

err = query.Scan(ctx)
err = tx.DB().NewSelect().
TableExpr("(?) as tx", query).
ColumnExpr("tx.*").
ColumnExpr("address.hash as signer__hash").
Join("left join address on address.id = tx.signer_id").
Scan(ctx, &txs)
return
}

func (tx *Tx) Filter(ctx context.Context, fltrs storage.TxFilter) (txs []storage.Tx, err error) {
query := tx.DB().NewSelect().Model(&txs).Offset(fltrs.Offset).Relation("Signer")
query := tx.DB().NewSelect().Model(&txs).Relation("Signer")
query = txFilter(query, fltrs)

err = query.Scan(ctx)
Expand All @@ -64,8 +68,7 @@ func (tx *Tx) ByAddress(ctx context.Context, addressId uint64, fltrs storage.TxF
query := tx.DB().NewSelect().
Model(&txs).
Where("signer_id = ?", addressId).
Relation("Signer").
Offset(fltrs.Offset)
Relation("Signer")

query = txFilter(query, fltrs)

Expand Down
3 changes: 3 additions & 0 deletions internal/storage/postgres/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *StorageTestSuite) TestTxByHeight() {
s.Require().EqualValues(hash, tx.Hash)

s.Require().NotNil(tx.Signer)
s.Require().Equal("3fff1c39b9d163bfb9bcbf9dfea78675f1b4bc2c", hex.EncodeToString(tx.Signer.Hash))
}

func (s *StorageTestSuite) TestTxFilter() {
Expand Down Expand Up @@ -87,6 +88,8 @@ func (s *StorageTestSuite) TestTxFilter() {
s.Require().EqualValues(hash, tx.Hash)

s.Require().NotNil(tx.Signer)
s.Require().Equal("3fff1c39b9d163bfb9bcbf9dfea78675f1b4bc2c", hex.EncodeToString(tx.Signer.Hash))

s.Require().Len(tx.Actions, 1)
}

Expand Down

0 comments on commit 363f614

Please sign in to comment.