query.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //! Filter and pagination primitives shared by every [`Store`](crate::store::Store)
  2. //! backend, stated once above the trait seam.
  3. //!
  4. //! A backend's `query_transfers`/`query_postings` implementation does only what
  5. //! is genuinely backend-specific: load the candidate records (via an account
  6. //! index, a store-wide scan, or a SQL `LIMIT` push-down). Everything after that,
  7. //! the time-window/book predicate and the `total` + `skip`/`take` cut, is the
  8. //! same contract regardless of backend and lives here.
  9. use crate::store::{Page, TransferQuery, TransferRecord};
  10. /// Keep only the transfers matching a query's time-window and book predicates.
  11. ///
  12. /// The account/subaccount filter is *not* applied here: a backend narrows to
  13. /// participating accounts when it loads candidates (an in-memory participation
  14. /// index or the SQL `transfer_accounts` join), because that filter is what the
  15. /// backend can push down. This covers every remaining predicate so both
  16. /// backends agree on the contract.
  17. pub fn filter_transfers(
  18. records: Vec<TransferRecord>,
  19. query: &TransferQuery,
  20. ) -> Vec<TransferRecord> {
  21. records
  22. .into_iter()
  23. .filter(|r| {
  24. if let Some(from) = query.from_ts
  25. && r.created_at < from
  26. {
  27. return false;
  28. }
  29. if let Some(to) = query.to_ts
  30. && r.created_at >= to
  31. {
  32. return false;
  33. }
  34. if let Some(from) = query.from_value_date
  35. && r.envelope.value_date().is_none_or(|d| d < from)
  36. {
  37. return false;
  38. }
  39. if let Some(to) = query.to_value_date
  40. && r.envelope.value_date().is_none_or(|d| d >= to)
  41. {
  42. return false;
  43. }
  44. if let Some(book) = query.book
  45. && r.envelope.book() != book
  46. {
  47. return false;
  48. }
  49. true
  50. })
  51. .collect()
  52. }
  53. /// The effective `(offset, limit)` window from a query's optional bounds: offset
  54. /// defaults to 0, limit to unbounded. One definition shared by the in-memory
  55. /// [`paginate`] cut and the SQL `LIMIT`/`OFFSET` push-down, so the two agree on
  56. /// what an absent bound means instead of each restating the defaults.
  57. pub fn window(offset: Option<u32>, limit: Option<u32>) -> (u32, u32) {
  58. (offset.unwrap_or(0), limit.unwrap_or(u32::MAX))
  59. }
  60. /// Cut a fully-filtered, ordered record set into one page: `total` is the
  61. /// pre-pagination count (see [`Page`]), then apply the [`window`] (skip `offset`,
  62. /// take `limit`).
  63. ///
  64. /// Callers that push `LIMIT`/`OFFSET` into the store (e.g. SQL `query_postings`)
  65. /// build their own [`Page`] and skip this; it exists for the backends that hold
  66. /// the full candidate set in memory.
  67. pub fn paginate<T>(records: Vec<T>, offset: Option<u32>, limit: Option<u32>) -> Page<T> {
  68. let total = records.len() as u64;
  69. let (offset, limit) = window(offset, limit);
  70. let items = records
  71. .into_iter()
  72. .skip(offset as usize)
  73. .take(limit as usize)
  74. .collect();
  75. Page { items, total }
  76. }