Skip to content

Commit

Permalink
Reduce the scope of change to asc order
Browse files Browse the repository at this point in the history
  • Loading branch information
urvisavla committed Aug 29, 2024
1 parent 3b04b66 commit 37fe7f6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
10 changes: 3 additions & 7 deletions services/horizon/internal/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,14 @@ func GetPageQuery(ledgerState *ledger.State, r *http.Request, opts ...Opt) (db2.

return db2.PageQuery{}, err
}
elderCursor := toid.AfterLedger(
ordered.Max(0, ledgerState.CurrentStatus().HistoryElder-1),
).String()

if defaultTOID && pageQuery.Order == db2.OrderAscending {
if cursor == "" || errors.Is(validateCursorWithinHistory(ledgerState, pageQuery), &hProblem.BeforeHistory) {
pageQuery.Cursor = elderCursor
pageQuery.Cursor = toid.AfterLedger(
ordered.Max(0, ledgerState.CurrentStatus().HistoryElder-1),
).String()
}
} else if defaultTOID && pageQuery.Order == db2.OrderDescending {
pageQuery.ElderCursor = elderCursor
}

return pageQuery, nil
}

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/account_signers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (q *Q) SignersForAccounts(ctx context.Context, accounts []string) ([]Accoun
// AccountsForSigner returns a list of `AccountSigner` rows for a given signer
func (q *Q) AccountsForSigner(ctx context.Context, signer string, page db2.PageQuery) ([]AccountSigner, error) {
sql := selectAccountSigners.Where("accounts_signers.signer = ?", signer)
sql, err := page.ApplyRawTo(sql, "accounts_signers.account_id")
sql, err := page.ApplyToUsingCursor(sql, "accounts_signers.account_id", page.Cursor)
if err != nil {
return nil, errors.Wrap(err, "could not apply query to page")
}
Expand Down
8 changes: 4 additions & 4 deletions services/horizon/internal/db2/history/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (q *Q) AccountsForAsset(ctx context.Context, asset xdr.Asset, page db2.Page
"trust_lines.asset_code": code,
})

sql, err := page.ApplyRawTo(sql, "trust_lines.account_id")
sql, err := page.ApplyToUsingCursor(sql, "trust_lines.account_id", page.Cursor)
if err != nil {
return nil, errors.Wrap(err, "could not apply query to page")
}
Expand All @@ -168,7 +168,7 @@ func (q *Q) AccountsForLiquidityPool(ctx context.Context, poolID string, page db
"trust_lines.liquidity_pool_id": poolID,
})

sql, err := page.ApplyRawTo(sql, "trust_lines.account_id")
sql, err := page.ApplyToUsingCursor(sql, "trust_lines.account_id", page.Cursor)
if err != nil {
return nil, errors.Wrap(err, "could not apply query to page")
}
Expand All @@ -189,7 +189,7 @@ func selectBySponsor(table, sponsor string, page db2.PageQuery) (sq.SelectBuilde
"sponsor": sponsor,
})

sql, err := page.ApplyRawTo(sql, "account_id")
sql, err := page.ApplyToUsingCursor(sql, "account_id", page.Cursor)
if err != nil {
return sql, errors.Wrap(err, "could not apply query to page")
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func (q *Q) AccountEntriesForSigner(ctx context.Context, signer string, page db2
"accounts_signers.signer": signer,
})

sql, err := page.ApplyRawTo(sql, "accounts_signers.account_id")
sql, err := page.ApplyToUsingCursor(sql, "accounts_signers.account_id", page.Cursor)
if err != nil {
return nil, errors.Wrap(err, "could not apply query to page")
}
Expand Down
7 changes: 3 additions & 4 deletions services/horizon/internal/db2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package db2
// PageQuery represents a portion of a Query struct concerned with paging
// through a large dataset.
type PageQuery struct {
Cursor string
Order string
Limit uint64
ElderCursor string
Cursor string
Order string
Limit uint64
}
28 changes: 14 additions & 14 deletions services/horizon/internal/db2/page_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ func (p PageQuery) ApplyTo(
return sql, err
}

return p.applyToUsingCursor(sql, col, cursor)
return p.ApplyToUsingCursor(sql, col, cursor)
}

// ApplyRawTo applies the raw PageQuery.Cursor cursor to the builder
func (p PageQuery) ApplyRawTo(
sql sq.SelectBuilder,
col string,
) (sq.SelectBuilder, error) {
return p.applyToUsingCursor(sql, col, p.Cursor)
return p.ApplyToUsingCursor(sql, col, p.Cursor)
}

// ApplyToUsingCursor returns a new SelectBuilder after applying the paging effects of
// `p` to `sql`. This method allows any type of cursor by a single column
func (p PageQuery) applyToUsingCursor(
func (p PageQuery) ApplyToUsingCursor(
sql sq.SelectBuilder,
col string,
cursor interface{},
Expand All @@ -81,23 +81,23 @@ func (p PageQuery) applyToUsingCursor(

switch p.Order {
case "asc":
if cursor != "" {
if cursor == "" {
sql = sql.
Where(fmt.Sprintf("%s > ?", col), cursor)
OrderBy(fmt.Sprintf("%s asc", col))
} else {
sql = sql.
Where(fmt.Sprintf("%s > ?", col), cursor).
OrderBy(fmt.Sprintf("%s asc", col))
}
sql = sql.
OrderBy(fmt.Sprintf("%s asc", col))
case "desc":
if cursor != "" {
if cursor == "" {
sql = sql.
Where(fmt.Sprintf("%s < ?", col), cursor)
}
if p.ElderCursor != "" {
OrderBy(fmt.Sprintf("%s desc", col))
} else {
sql = sql.
Where(fmt.Sprintf("%s > ?", col), p.ElderCursor)
Where(fmt.Sprintf("%s < ?", col), cursor).
OrderBy(fmt.Sprintf("%s desc", col))
}
sql = sql.
OrderBy(fmt.Sprintf("%s desc", col))
default:
return sql, errors.Errorf("invalid order: %s", p.Order)
}
Expand Down

0 comments on commit 37fe7f6

Please sign in to comment.