Skip to content

Commit

Permalink
Optimization of initial schema (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Aug 24, 2022
1 parent c01dfb7 commit 661561b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
7 changes: 1 addition & 6 deletions cmd/api/handlers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,6 @@ func getInitialOperation(c *gin.Context, ctx *config.Context, address string) (o
return operation.Operation{}, err
}

filters := map[string]interface{}{
"destination_id": destination.ID,
"status": types.OperationStatusApplied,
"kind": types.OperationKindOrigination,
}
return ctx.Operations.Last(filters, 0)
return ctx.Operations.Origination(destination.ID)

}
33 changes: 21 additions & 12 deletions cmd/indexer/indexer/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/baking-bad/bcdhub/internal/logger"
"github.com/baking-bad/bcdhub/internal/models/account"
"github.com/baking-bad/bcdhub/internal/models/bigmapaction"
"github.com/baking-bad/bcdhub/internal/models/bigmapdiff"
"github.com/baking-bad/bcdhub/internal/models/block"
Expand All @@ -15,6 +16,11 @@ import (

func createStartIndices(db pg.DBI) error {
return db.RunInTransaction(context.Background(), func(tx *pg.Tx) error {
// Accounts
if _, err := db.Model((*account.Account)(nil)).Exec(`CREATE INDEX CONCURRENTLY IF NOT EXISTS accounts_address_idx ON ?TableName (address)`); err != nil {
return err
}

// Blocks
if _, err := db.Model((*block.Block)(nil)).Exec(`CREATE INDEX CONCURRENTLY IF NOT EXISTS blocks_level_idx ON ?TableName (level)`); err != nil {
return err
Expand Down Expand Up @@ -47,6 +53,15 @@ func createStartIndices(db pg.DBI) error {
return err
}

// Operations
if _, err := db.Model((*operation.Operation)(nil)).Exec(`CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_destination_idx ON ?TableName (destination_id)`); err != nil {
return err
}

if _, err := db.Model((*operation.Operation)(nil)).Exec(`CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_status_idx ON ?TableName (status)`); err != nil {
return err
}

return nil
})
}
Expand Down Expand Up @@ -111,6 +126,12 @@ func (bi *BlockchainIndexer) createIndices() {
logger.Error().Err(err).Msg("can't create index")
}

if _, err := bi.Context.StorageDB.DB.Model((*contract.Contract)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS contracts_jakarta_id_idx ON ?TableName (jakarta_id)
`); err != nil {
logger.Error().Err(err).Msg("can't create index")
}

// Global constants
if _, err := bi.Context.StorageDB.DB.Model((*contract.GlobalConstant)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS global_constants_address_idx ON ?TableName (address)
Expand Down Expand Up @@ -144,12 +165,6 @@ func (bi *BlockchainIndexer) createIndices() {
logger.Error().Err(err).Msg("can't create index")
}

if _, err := bi.Context.StorageDB.DB.Model((*operation.Operation)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_destination_idx ON ?TableName (destination_id)
`); err != nil {
logger.Error().Err(err).Msg("can't create index")
}

if _, err := bi.Context.StorageDB.DB.Model((*operation.Operation)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_opg_idx ON ?TableName (hash, counter, content_index)
`); err != nil {
Expand Down Expand Up @@ -180,12 +195,6 @@ func (bi *BlockchainIndexer) createIndices() {
logger.Error().Err(err).Msg("can't create index")
}

if _, err := bi.Context.StorageDB.DB.Model((*operation.Operation)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_status_idx ON ?TableName (status)
`); err != nil {
logger.Error().Err(err).Msg("can't create index")
}

if _, err := bi.Context.StorageDB.DB.Model((*operation.Operation)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_timestamp_idx ON ?TableName (timestamp)
`); err != nil {
Expand Down
30 changes: 15 additions & 15 deletions internal/models/mock/operation/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/models/operation/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Repository interface {
GetByHashAndCounter(hash string, counter int64) ([]Operation, error)
GetImplicitOperation(counter int64) (Operation, error)
OPG(address string, size, lastID int64) ([]OPG, error)
Origination(accountID int64) (Operation, error)

// GetOperations - get operation by `filter`. `Size` - if 0 - return all, else certain `size` operations.
// `Sort` - sort by time and content index by desc
Expand Down
11 changes: 11 additions & 0 deletions internal/postgres/operation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ func (storage *Storage) ContractStats(address string) (stats operation.ContractS
return
}

// Origination -
func (storage *Storage) Origination(accountID int64) (operation.Operation, error) {
var result operation.Operation
err := storage.DB.Model((*operation.Operation)(nil)).
Where("destination_id = ?", accountID).
Where("kind = ?", types.OperationKindOrigination).
Limit(1).
Select(&result)
return result, err
}

func addOperationSorting(query *orm.Query) {
query.OrderExpr("operation.level desc, operation.counter desc, operation.id asc")
}

0 comments on commit 661561b

Please sign in to comment.