Skip to content

Commit

Permalink
chore: simplify storage
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent cbb99e4 commit d0935fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
16 changes: 2 additions & 14 deletions internal/storage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,16 @@ func (s *Store) selectAccounts(date *time.Time, expandVolumes, expandEffectiveVo
}

if s.ledger.HasFeature(ledger.FeatureMovesHistory, "ON") && needPCV {
selectAccountWithAssetAndVolumes := s.selectAccountWithAssetAndVolumes(date, true, nil)
selectAccountWithVolumes := s.db.NewSelect().
TableExpr("(?) values", selectAccountWithAssetAndVolumes).
Group("accounts_address").
Column("accounts_address").
ColumnExpr("aggregate_objects(json_build_object(asset, volumes)::jsonb) as pcv")
ret = ret.Join(
`left join (?) pcv on pcv.accounts_address = accounts.address`,
selectAccountWithVolumes,
s.selectAccountWithAggregatedVolumes(date, true, "pcv"),
).Column("pcv.*")
}

if s.ledger.HasFeature(ledger.FeatureMovesHistoryPostCommitEffectiveVolumes, "SYNC") && expandEffectiveVolumes {
selectAccountWithAssetAndVolumes := s.selectAccountWithAssetAndVolumes(date, false, nil)
selectAccountWithVolumes := s.db.NewSelect().
TableExpr("(?) values", selectAccountWithAssetAndVolumes).
Group("accounts_address").
Column("accounts_address").
ColumnExpr("aggregate_objects(json_build_object(asset, volumes)::jsonb) as pcev")
ret = ret.Join(
`left join (?) pcev on pcev.accounts_address = accounts.address`,
selectAccountWithVolumes,
s.selectAccountWithAggregatedVolumes(date, false, "pcev"),
).Column("pcev.*")
}

Expand Down
9 changes: 9 additions & 0 deletions internal/storage/ledger/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa
return selectAccountsWithVolumes
}

func (s *Store) selectAccountWithAggregatedVolumes(date *time.Time, useInsertionDate bool, alias string) *bun.SelectQuery {
selectAccountWithAssetAndVolumes := s.selectAccountWithAssetAndVolumes(date, useInsertionDate, nil)
return s.db.NewSelect().
TableExpr("(?) values", selectAccountWithAssetAndVolumes).
Group("accounts_address").
Column("accounts_address").
ColumnExpr("aggregate_objects(json_build_object(asset, volumes)::jsonb) as " + alias)
}

func (s *Store) SelectAggregatedBalances(date *time.Time, useInsertionDate bool, builder query.Builder) *bun.SelectQuery {

selectAccountsWithVolumes := s.selectAccountWithAssetAndVolumes(date, useInsertionDate, builder)
Expand Down

0 comments on commit d0935fe

Please sign in to comment.