@syncular/server 0.85.0 → 0.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -159,16 +159,18 @@ export interface LiveKey {
159
159
 
160
160
  /**
161
161
  * One row of a live-window candidate page: a candidate commit joined to its
162
- * metadata (absent when the commit vanished) and, per key, the stored bytes
163
- * (payload, row id, scopes) of its changes that key reads: a covered table
164
- * and the key's value in the stored scopes. A commit with no such change
165
- * comes back once without `key`. Rows are in (commitSeq, key) order.
162
+ * metadata (absent when the commit vanished) and, per key, the count and
163
+ * stored bytes (payload, row id, scopes) of its changes that key reads: a
164
+ * covered table and the key's value in the stored scopes. A commit with no
165
+ * such change comes back once without `key`. Rows are in (commitSeq, key)
166
+ * order.
166
167
  */
167
168
  export interface LiveCandidateRow {
168
169
  readonly commitSeq: number;
169
170
  readonly meta?: { readonly actorId: string; readonly createdAtMs: number };
170
171
  readonly key?: number;
171
172
  readonly bytes?: number;
173
+ readonly changes?: number;
172
174
  }
173
175
 
174
176
  /**
@@ -184,6 +186,11 @@ export interface LiveCandidateRow {
184
186
  * bound. Every candidate advances the scan, so a page of non-matching
185
187
  * candidates never repeats.
186
188
  *
189
+ * The counts bound the delivered changes from above, because every change
190
+ * a unit keeps is one its key reads: the scan loads pending candidates once
191
+ * their counts could reach the rest of `limitChanges`, so a page loads
192
+ * about the changes it delivers and never a candidate past the cut.
193
+ *
187
194
  * The sizes bound the loaded bytes by `limitBytes` before any payload is
188
195
  * read: the section stops before the commit that would pass it, and a
189
196
  * commit that passes it alone ends the read as `oversized`, naming the
@@ -236,6 +243,8 @@ export async function readLiveWindowPages(
236
243
  const limit = Math.max(64, query.limitChanges);
237
244
  let delivered = 0;
238
245
  let loaded = 0;
246
+ // Upper bound of the changes the pending candidates deliver.
247
+ let bound = 0;
239
248
  let scanned = -1;
240
249
  // Candidates sized but not loaded yet: (scanned, pending.at(-1)].
241
250
  let pending: { commitSeq: number; meta?: LiveCandidateRow['meta'] }[] = [];
@@ -251,6 +260,7 @@ export async function readLiveWindowPages(
251
260
  pending.map((c) => c.commitSeq),
252
261
  );
253
262
  pending = [];
263
+ bound = 0;
254
264
  scanned = last.commitSeq;
255
265
  for (let i = 0; i < rows.length;) {
256
266
  const seq = rows[i]!.commitSeq;
@@ -287,11 +297,13 @@ export async function readLiveWindowPages(
287
297
  const head = rows[i]!;
288
298
  candidates += 1;
289
299
  let bytes = 0;
300
+ let changes = 0;
290
301
  const sizes: [key: number, bytes: number][] = [];
291
302
  for (; rows[i]?.commitSeq === head.commitSeq; i++) {
292
303
  const row = rows[i]!;
293
304
  if (row.key === undefined) continue;
294
305
  bytes += row.bytes ?? 0;
306
+ changes += row.changes ?? 0;
295
307
  sizes.push([row.key, row.bytes ?? 0]);
296
308
  }
297
309
  if (bytes > query.limitBytes) {
@@ -324,7 +336,12 @@ export async function readLiveWindowPages(
324
336
  return { commits, throughSeq: cut ?? scanned };
325
337
  }
326
338
  loaded += bytes;
339
+ bound += changes;
327
340
  pending.push({ commitSeq: head.commitSeq, meta: head.meta });
341
+ if (delivered + bound >= query.limitChanges) {
342
+ const cut = await load();
343
+ if (cut !== undefined) return { commits, throughSeq: cut };
344
+ }
328
345
  }
329
346
  const cut = await load();
330
347
  if (cut !== undefined) return { commits, throughSeq: cut };
package/src/storage.ts CHANGED
@@ -1494,6 +1494,17 @@ export interface ServerStorage {
1494
1494
  /** Scope-filtered snapshot scan, ordered by rowId (bootstrap paging). */
1495
1495
  scanRows(partition: string, query: RowScanQuery): Promise<StoredRow[]>;
1496
1496
 
1497
+ /**
1498
+ * The rows `scanRows` returns for the filter from the start of the table,
1499
+ * counted (§4.7 bootstrap extent). Reads only the row-scope index: one
1500
+ * index range of the variable `scanRows` seeks plus one index seek per
1501
+ * other variable and candidate, never the row table.
1502
+ */
1503
+ countScopedRows(
1504
+ partition: string,
1505
+ query: Pick<RowScanQuery, 'table' | 'scopeFilter'>,
1506
+ ): Promise<number>;
1507
+
1497
1508
  /**
1498
1509
  * Optional registered-query capability. The implementation MUST replace
1499
1510
  * every generated app-table relation with a partition-filtered relation and