Skip to content

Commit

Permalink
Ingestion updates
Browse files Browse the repository at this point in the history
- Batch load account IDs and batch insert accounts that does not exist
  instead of using in-memory cache.
- Improve batch insert code by storing rows to insert in a slice,
  updating account IDs once per session and having a common insert code
  for each object batch.
  • Loading branch information
bartekn committed Feb 21, 2018
1 parent 09fc13c commit e07a7d8
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 184 deletions.
20 changes: 20 additions & 0 deletions services/horizon/internal/db2/history/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ func (q *AccountsQ) Select(dest interface{}) error {
return q.Err
}

// AccountsByAddresses loads a rows from `history_accounts`, by addresses
func (q *Q) AccountsByAddresses(dest interface{}, addresses []string) error {
sql := selectAccount.Where(map[string]interface{}{
"ha.address": addresses, // ha.address IN (...)
})
return q.Select(dest, sql)
}

// CreateAccounts creates rows for addresses in history_accounts table and
// put
func (q *Q) CreateAccounts(dest interface{}, addresses []string) error {
sql := sq.Insert("history_accounts").Columns("address")
for _, address := range addresses {
sql = sql.Values(address)
}
sql = sql.Suffix("RETURNING *")

return q.Select(dest, sql)
}

// Return id for account. If account doesn't exist, it will be created and the new id returned.
func (q *Q) GetCreateAccountID(
aid xdr.AccountId,
Expand Down
8 changes: 1 addition & 7 deletions services/horizon/internal/ingest/effect_ingestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ func (ei *EffectIngestion) Add(aid xdr.AccountId, typ history.EffectType, detail
}

ei.added++
var haid int64

haid, ei.err = ei.Dest.GetCreateAccountID(aid)
if ei.err != nil {
return false
}

ei.err = ei.Dest.Effect(haid, ei.OperationID, ei.added, typ, details)
ei.err = ei.Dest.Effect(aid.Address(), ei.OperationID, ei.added, typ, details)
if ei.err != nil {
return false
}
Expand Down
Loading

0 comments on commit e07a7d8

Please sign in to comment.