Răsfoiți Sursa

Give the pagination window one owner across backends

query_postings and query_transfers each state the offset/limit contract
twice: the in-memory paginate helper defaults an absent offset to 0 and
an absent limit to unbounded, and every SQL push-down restates the same
two unwrap_or rules inline before building its LIMIT/OFFSET clause.

Extract the defaulting into query::window, called by paginate and by
both SQL query builders, so an absent bound means one thing in one place.
State the Page.total contract (count of all matching rows, before the
window) on the Page type itself, where both the Rust cut and the SQL
COUNT(*) can point at it. The SQL push-down stays; only the shared
contract is unified, not the backend-specific mechanics.
Cesar Rodas 3 luni în urmă
părinte
comite
db33d3ddb9

+ 1 - 2
crates/kuatia-storage-sql/src/posting.rs

@@ -253,8 +253,7 @@ impl PostingStore for SqlStore {
             }
             w.push_str(state);
             let c = format!("SELECT COUNT(*) as cnt FROM {source} {w}");
-            let limit = query.limit.unwrap_or(u32::MAX);
-            let offset = query.offset.unwrap_or(0);
+            let (offset, limit) = kuatia_storage::query::window(query.offset, query.limit);
             // Order by the posting primary key so pagination is deterministic:
             // without it LIMIT/OFFSET could skip or repeat rows across pages.
             w.push_str(&format!(

+ 1 - 2
crates/kuatia-storage-sql/src/transfer.rs

@@ -192,8 +192,7 @@ impl TransferStore for SqlStore {
             .try_get("cnt")
             .map_err(|e| StoreError::Internal(e.to_string()))?;
 
-        let limit = query.limit.unwrap_or(u32::MAX);
-        let offset = query.offset.unwrap_or(0);
+        let (offset, limit) = kuatia_storage::query::window(query.offset, query.limit);
         let data_sql = format!(
             "SELECT t.transfer, t.receipt, t.created_at {from_clause}{where_sql} \
              ORDER BY t.created_at LIMIT {limit} OFFSET {offset}"

+ 16 - 5
crates/kuatia-storage/src/query.rs

@@ -43,17 +43,28 @@ pub fn filter_transfers(
         .collect()
 }
 
+/// The effective `(offset, limit)` window from a query's optional bounds: offset
+/// defaults to 0, limit to unbounded. One definition shared by the in-memory
+/// [`paginate`] cut and the SQL `LIMIT`/`OFFSET` push-down, so the two agree on
+/// what an absent bound means instead of each restating the defaults.
+pub fn window(offset: Option<u32>, limit: Option<u32>) -> (u32, u32) {
+    (offset.unwrap_or(0), limit.unwrap_or(u32::MAX))
+}
+
 /// Cut a fully-filtered, ordered record set into one page: `total` is the
-/// pre-pagination count, then skip `offset` (default 0) and take `limit`
-/// (default unbounded).
+/// pre-pagination count (see [`Page`]), then apply the [`window`] (skip `offset`,
+/// take `limit`).
 ///
 /// Callers that push `LIMIT`/`OFFSET` into the store (e.g. SQL `query_postings`)
 /// build their own [`Page`] and skip this; it exists for the backends that hold
 /// the full candidate set in memory.
 pub fn paginate<T>(records: Vec<T>, offset: Option<u32>, limit: Option<u32>) -> Page<T> {
     let total = records.len() as u64;
-    let offset = offset.unwrap_or(0) as usize;
-    let limit = limit.unwrap_or(u32::MAX) as usize;
-    let items = records.into_iter().skip(offset).take(limit).collect();
+    let (offset, limit) = window(offset, limit);
+    let items = records
+        .into_iter()
+        .skip(offset as usize)
+        .take(limit as usize)
+        .collect();
     Page { items, total }
 }

+ 8 - 2
crates/kuatia-storage/src/store.rs

@@ -69,11 +69,17 @@ pub struct TransferQuery {
 }
 
 /// A page of results with total count for pagination.
+///
+/// The pagination contract every backend implements: `items` is the page cut by
+/// the query's `(offset, limit)` window, and `total` is the count of all matching
+/// rows *ignoring* that window. In-memory backends satisfy it via
+/// [`paginate`](crate::query::paginate); a SQL backend via `LIMIT`/`OFFSET` plus a
+/// separate `COUNT(*)`.
 #[derive(Debug, Clone)]
 pub struct Page<T> {
-    /// The items in this page.
+    /// The items in this page (the `(offset, limit)` window).
     pub items: Vec<T>,
-    /// Total number of matching items (before pagination).
+    /// Count of all matching rows, before the page window is applied.
     pub total: u64,
 }