Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jchris/query-allDocs #601

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open

Jchris/query-allDocs #601

wants to merge 22 commits into from

Conversation

jchris
Copy link
Contributor

@jchris jchris commented Feb 21, 2025

this is before rebase, currently on main

Summary by CodeRabbit

  • New Features

    • Introduced a robust streaming API for real-time document updates and seamless subscriptions.
    • Added a unified query interface providing flexible snapshot and live query capabilities.
    • Upgraded dependency support for improved interoperability.
  • Refactor

    • Enhanced update processing and callback management for smoother real-time performance.
    • Refined indexing and type handling to deliver more reliable document retrieval.
  • Tests

    • Launched a comprehensive test suite to ensure the quality and accuracy of new streaming and querying features.

Copy link

coderabbitai bot commented Feb 21, 2025

Walkthrough

This update removes a redundant blank line from a build script and modifies dependency versions in the package manifest. It refactors several CRDT and helper modules to use asynchronous generator functions for update streaming and enhances callback management. The indexing system and ledger now use consolidated query methods with improved type definitions, while new type interfaces and a utility function support asynchronous iteration. Additionally, a new test suite validates the Streaming API functionality.

Changes

File(s) Summary of Changes
build-docs.sh Removed a trailing blank line.
package.json Downgraded idb from ^8.0.2 to ^8.0.1 and added iterator-helpers-polyfill with version ^3.0.1.
src/crdt-clock.ts
src/crdt-helpers.ts
In CRDTClock, updated update processing to use Array.fromAsync(...) and added return functions for unregistering callbacks. In crdt-helpers, refactored and split generator functions and added new utility docUpdateToDocWithId.
src/crdt.ts Refactored document retrieval: replaced promise-based results with a streaming model using async generators; introduced internal snapshot, subscribe, and stream methods; and removed outdated change methods.
src/indexer-helpers.ts
src/indexer.ts
Enhanced type safety and query functionality. Modified function signatures to support generic mapping, added overloads for query responses, and refactored internal snapshot and subscription logic.
src/ledger.ts Removed separate changes and allDocs methods; introduced consolidated select method with overloads and added explicit clock property.
src/types.ts Added new interfaces (Row, DocumentRow, InquiryResponse, QueryResponse) and types (QueryStreamMarker), and updated existing type definitions for callbacks and mapping functions.
src/utils.ts Added an asynchronous utility function arrayFromAsyncIterable for converting async iterables to arrays.
tests/fireproof/streaming-api.test.ts Introduced a comprehensive test suite for the Streaming API, covering snapshots, live streams, subscriptions, and query behaviors including exclusion of documents.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant CRDT
    participant Internal
    Client->>CRDT: allDocs({ waitFor })
    CRDT->>Internal: #snapshot() to get initial docs
    Internal-->>CRDT: Returns snapshot (async iterable)
    CRDT->>Internal: #subscribe() for real-time updates
    Internal-->>CRDT: Streams updates via async generator
    CRDT-->>Client: Returns QueryResponse (snapshot + live update stream)
Loading
sequenceDiagram
    participant App
    participant CRDTClock
    App->>CRDTClock: onTick(callback)
    CRDTClock-->>App: Returns unregister function
    App->>CRDTClock: onTock(callback)
    CRDTClock-->>App: Returns unregister function
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (9)
src/indexer-helpers.ts (3)

84-95: Consider using v ?? null instead of (v || null).
Using (v || null) will treat all falsy values (including 0 and "") as null. If you wish to preserve 0 or the empty string, consider v ?? null for clarity.

- value: (v || null) as R,
+ value: (v ?? null) as R,

170-185: Avoid spread syntax in array accumulator to improve performance.
Repeatedly using return [...acc, row] can cause an O(n^2) time complexity when handling large arrays. Consider pushing directly to acc:

- return [...acc, row];
+ acc.push(row);
+ return acc;

Additionally, the line let result = [...resp.result]; at line 172 is fine for making a single copy, but be mindful of potential overhead if resp.result can be very large.

🧰 Tools
🪛 Biome (1.9.4)

[error] 180-180: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)


187-202: Yielding a Promise, or fully awaiting the document retrieval?
You do yield crdt.get(res.id).then(...). This passes a Promise to the consumer. If you want to yield fully resolved docs, consider an await:

- yield crdt.get(res.id).then((val) => {
-   if (!val) return undefined;
-   const row: IndexRow<K, T, R> = { ...res, doc: val.doc };
-   return row;
- });
+ const val = await crdt.get(res.id);
+ if (val) yield { ...res, doc: val.doc };
src/crdt-helpers.ts (1)

242-255: Consider handling missing or invalid docValue.

If getValueFromLink fails or returns an unexpected result (e.g., missing doc), handling within this generator could provide clearer error feedback.

src/ledger.ts (3)

79-96: Multiple overload signatures might become unwieldy.

Consider consolidating overloads into a single function that takes a parameter object, reducing complexity.


178-198: Repeated overload structure within the shell.

Align with the approach in lines 79-96 or unify them to avoid duplicated code paths.


336-349: Duplicate interface for select within the implementation.

Refactoring or sharing a common helper could reduce code duplication and maintain a single source of truth for query logic.

src/indexer.ts (1)

117-117: Complex conditional handling for map function and metadata.

Consider breaking this logic into smaller helper methods for better clarity and maintainability.

tests/fireproof/streaming-api.test.ts (1)

87-87: Consider using fixed values for test data.

Using random values in tests (e.g., Math.floor(Math.random() * (10 - 1) + 1) and Math.floor(Math.random() * (10 - 4) + 4)) could make tests flaky. Consider using fixed values for better test reliability and reproducibility.

Apply this diff to use fixed values:

-    const amountOfNewDocs = Math.floor(Math.random() * (10 - 1) + 1);
+    const amountOfNewDocs = 5;

-    const amountOfSnapshotDocs = Math.floor(Math.random() * (10 - 4) + 4);
+    const amountOfSnapshotDocs = 7;

Also applies to: 114-114

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2afa46 and 67e91ca.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (11)
  • build-docs.sh (0 hunks)
  • package.json (1 hunks)
  • src/crdt-clock.ts (2 hunks)
  • src/crdt-helpers.ts (6 hunks)
  • src/crdt.ts (4 hunks)
  • src/indexer-helpers.ts (3 hunks)
  • src/indexer.ts (10 hunks)
  • src/ledger.ts (7 hunks)
  • src/types.ts (2 hunks)
  • src/utils.ts (1 hunks)
  • tests/fireproof/streaming-api.test.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • build-docs.sh
🧰 Additional context used
🪛 Biome (1.9.4)
src/indexer-helpers.ts

[error] 180-180: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

🔇 Additional comments (44)
src/indexer-helpers.ts (6)

27-29: Imports introduced for 'ClockHead', 'ChangesOptions', and 'IndexRow' look proper.
They seem directly aligned with the new functionality in this file, and there appear to be no issues.


34-34: Import 'clockChangesSince' is correctly placed.
No concerns here; it provides the functionality needed for filtering by clock head.


66-68: New IndexDoc interface usage.
Defining a strongly typed value: R is an excellent move towards better type safety and clarity.


76-79: indexEntriesForChanges function signature looks good.
It’s well-typed with generics, matching your use case for flexible document fragment handling.


164-168: applyQuery function signature.
Shifting to an AsyncGenerator aligns well with streaming and partial retrieval. The signature is consistent with your new query flow.


205-207: Outer loop for final yielding is straightforward.
This logic is concise and effectively passes along any filtered results from _apply().

src/crdt.ts (10)

18-20: New imports from crdt-helpers:
The addition of docUpdateToDocWithId, getAllEntries, and clockUpdatesSince aligns properly with your new doc and clock streaming approach.


33-36: Extended imports for streaming and subscription support
QueryResponse, ListenerFn, QueryStreamMarker, and ChangesOptions are well placed for the new reactive features.


41-41: arrayFromAsyncIterable and ensureLogger imports
Great additions for snapshot generation and consistent logging.


62-65: Subscription-related properties
Declaring _listening, _listeners, and _noupdate_listeners here is clear, enabling subscription logic.


163-182: allDocs method returning a QueryResponse
This provides a succinct entry point for snapshot and streaming. The design is consistent with your reactive architecture.


183-185: #currentDocs method
Delegating to either changes(...) or all(...) based on since is an elegant approach.


187-205: #snapshot private generator
Using waitFor before yielding docs ensures upstream readiness. Implementation is neat and covers the entire doc set.


206-215: #subscribe method
This robust subscription callback pattern will keep external code informed of doc updates in real time.


216-261: #stream method
This ReadableStream approach elegantly handles back-pressure and cancels. Enabling both preexisting and new docs is a good design choice.


272-279: New all and changes methods
They provide clear, asynchronous iteration over doc updates in line with your streaming model.

src/types.ts (6)

220-224: New Row interface
Defining id, key, and value together in a single typed structure will help unify row handling logic across the codebase.


226-228: New DocumentRow interface
Great extension of Row to include doc. This clarifies which rows also carry full document data.


232-232: Changed key type to IndexKey<K>
Making IndexRow consistent with newly introduced IndexKey<K> improves consistency across indexing definitions.


279-285: InquiryResponse interface
Encapsulating snapshot, streams, and array conversion ensures a uniform approach to reading rows in different modes.


291-301: QueryResponse interface
Mirroring InquiryResponse but with full DocumentRow ensures strong typing for doc retrieval in queries.


303-305: Updated EmitFn and MapFn signature
Accepting a generic R fosters more powerful mapping logic with typed fragments.

src/crdt-helpers.ts (6)

6-6: Import looks properly aligned with subsequent usage.


32-32: New import for ClockLink is consistent with usage.


90-92: Helper function simplifies doc creation logic.


256-269: Asynchronous generator structure is clear and concise.


271-285: Recursively yielding updates is well-structured.

It relies on didLinks to prevent revisiting events, which should avoid cycles. Verify that event.parents cannot create an unexpected loop in certain edge cases.


393-395: Compaction loop usage of clockChangesSince.

src/ledger.ts (3)

21-23: New imports for advanced types.


65-65: Exposing clock as a public property.


147-149: Getter delegates to underlying reference.

src/indexer.ts (9)

16-20: New type imports for extended query functionality.


39-39: Utility import for arrayFromAsyncIterable.


41-41: Importing docUpdateToDocWithId for direct usage.


52-57: Conditional index creation logic.

Potential concurrency concern: multiple calls to index with the same name at the same time. Confirm if there's a guard or if only a single call is expected.


70-70: Optional map function broadens indexing flexibility.


97-97: Constructor extends CRDT usage with typed mapFn.


158-158: Default map function usage.


395-395: Variable declaration in _updateIndex.


398-398: Reliance on Array.fromAsync.

Please confirm that Array.fromAsync is supported in the target environment or successfully polyfilled, as it can be unavailable in certain older runtimes.

src/crdt-clock.ts (1)

75-75: LGTM! Good memory management.

The addition of cleanup functions in onTick and onTock is a good practice for memory management, allowing proper removal of callbacks when they are no longer needed.

Also applies to: 80-80

tests/fireproof/streaming-api.test.ts (1)

175-175: Verify if skipped tests are intentional.

The "allDocs" test suite is skipped. Please verify if this is intentional or if these tests should be enabled.

src/utils.ts (1)

357-368: LGTM! Good polyfill implementation.

The polyfill implementation is correct and well-documented. It provides a good fallback for browsers that don't support Array.fromAsync.

package.json (1)

97-98:

❓ Verification inconclusive

Verify the idb version downgrade.

The idb package has been downgraded from ^8.0.2 to ^8.0.1. Please verify if this downgrade is intentional and if there are any compatibility concerns.


🌐 Web query:

What are the differences between idb 8.0.1 and 8.0.2?

💡 Result:

The differences between idb versions 8.0.1 and 8.0.2 primarily focus on ECMAScript Module (ESM) compatibility and dependency updates, though specific release notes for these versions aren't explicitly documented in public sources[1][2][10]. However, key insights can be inferred from related projects:

Key Changes

Feature/Aspect idb v8.0.1 idb v8.0.2
ESM Support Likely lacked full ESM compatibility Added explicit ESM support for modern JavaScript toolchains[7][10]
Dependencies May have used older versions of underlying libraries Updated dependencies to ensure compatibility with modern browser APIs[7][10]
Firebase Integration Caused compatibility issues requiring workarounds Became the recommended version for Firebase SDKs due to resolved issues[7]

Contextual Evidence

  • Firebase upgraded to [email protected] in SDK v9.x specifically to resolve ESM-related errors in browsers[7].
  • The update addressed issues where older idb versions caused failures in projects using strict ESM modules or modern bundlers like Webpack/ESBuild[7][10].
  • No breaking API changes were reported between these minor versions[1][2].

For most users, upgrading from v8.0.x to v8.x.y requires no code changes but ensures better compatibility with modern JavaScript ecosystems[7][10].

Citations:


Action Required: Confirm Intentional Downgrade of idb Version

We've observed that the idb package version has been downgraded from ^8.0.2 to ^8.0.1. Given that version 8.0.2 includes key improvements—such as enhanced ECMAScript Module (ESM) support and updated dependencies beneficial for modern bundlers and Firebase integration—it’s important to verify that this downgrade is intentional and won't introduce compatibility issues.

  • Confirm whether falling back to 8.0.1 aligns with our project’s ESM requirements and overall dependency strategy.
  • Assess if the removal of the ESM improvements and dependency updates present in 8.0.2 might affect the codebase or integration with tools that rely on them.

Comment on lines +61 to +62
const changes = await Array.fromAsync(clockChangesSince(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger));
internalUpdates = changes;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use the polyfill function for better compatibility.

Replace Array.fromAsync with the arrayFromAsyncIterable polyfill function from utils.ts for better browser compatibility.

Apply this diff to use the polyfill:

-      const changes = await Array.fromAsync(clockChangesSince(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger));
+      const changes = await arrayFromAsyncIterable(clockChangesSince(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const changes = await Array.fromAsync(clockChangesSince(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger));
internalUpdates = changes;
const changes = await arrayFromAsyncIterable(clockChangesSince(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger));
internalUpdates = changes;

@@ -58,8 +58,8 @@ export class CRDTClock<T extends DocTypes> {
async processUpdates(updatesAcc: DocUpdate<T>[], all: boolean, prevHead: ClockHead) {
let internalUpdates = updatesAcc;
if (this.watchers.size && !all) {
const changes = await clockChangesSince<T>(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger);
internalUpdates = changes.result;
const changes = await Array.fromAsync(clockChangesSince(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should not use a polyfill.

@@ -292,19 +303,17 @@ async function gatherUpdates<T extends DocTypes>(
}
for (let i = ops.length - 1; i >= 0; i--) {
const { key, value } = ops[i];
if (!keys.has(key)) {
if (!keys.has(key) && (allowedKeys === undefined || allowedKeys.has(key))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ! instead of ===

@@ -381,7 +390,9 @@ export async function doCompact(blockLog: CompactFetcher, head: ClockHead, logge
timeEnd("compact root blocks");

time("compact changes");
await clockChangesSince(blockLog, head, [], {}, logger);
for await (const x of clockChangesSince(blockLog, head, [], {}, logger)) {
void x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be more clear why we loop through this

@@ -55,6 +59,11 @@ export class CRDT<T extends DocTypes> {
readonly logger: Logger;
readonly sthis: SuperThis;

// Subscriptions
_listening = false;
readonly _listeners = new Set<ListenerFn<T>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not a fan on use Set of functions --- please use Map<string, Fn> and generate string this sthis.nextid()

* Retrieve the current set of documents.
*/
allDocs<T extends DocTypes>({ waitFor }: { waitFor?: Promise<unknown> } = {}): QueryResponse<T> {
const stream = this.#stream.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is exactly where we should make use of:

#stream = () => { }
declaration.

/**
* Retrieve the current set of documents.
*/
allDocs<T extends DocTypes>({ waitFor }: { waitFor?: Promise<unknown> } = {}): QueryResponse<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we decided that allDocs will stay with the current signature.

This should move to select.

let unsubscribe: undefined | (() => void);
let isClosed = false;

return new ReadableStream<{ doc: DocWithId<T>; marker: QueryStreamMarker }>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to test that with the worker runtime of Cloudflare. They protect against the direct use of ReadableStream.


#stream<T extends DocTypes>(
opts: { futureOnly: boolean; since?: ClockHead } & ChangesOptions,
{ waitFor }: { waitFor?: Promise<unknown> } = {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better use Future

changes.forEach(({ id: key, value, del }) => {
if (del || !value) return;
let mapCalled = false;
const mapReturn = mapFn({ ...(value as DocWithId<T>), _id: key }, (k: IndexKeyType, v?: DocFragment) => {
const mapReturn = mapFn({ ...value, _id: key }, (k: IndexKeyType, v?: R) => {
mapCalled = true;
if (typeof k === "undefined") return;
indexEntries.push({
key: [charwise.encode(k) as K, key],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need charwise there is sthis.txt

});
});
if (!mapCalled && mapReturn) {
indexEntries.push({
key: [charwise.encode(mapReturn) as K, key],
value: null,
value: null as R,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rethink null -> better falsy?


if (query.since) {
const gen = clockChangesSince(crdt.blockstore, crdt.clock.head, query.since, query.sinceOptions || {}, logger);
const ids = await Array.fromAsync(gen)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no polyfill

this.logger.Debug().Msg("post _updateIndex query");
await this._hydrateIndex();
this.logger.Debug().Msg("post _hydrateIndex query");
query(qryOpts: QueryOpts<K> & { excludeDocs: true }, intlOpts: { waitFor?: Promise<unknown> }): InquiryResponse<K, R>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to select --- leave query like it was

if (!this.indexHead || this.indexHead.length === 0) {
({ result, head } = await this.crdt.allDocs());
this.logger.Debug().Msg("enter crdt.allDocs");
result = await Array.fromAsync(this.crdt.all<T>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • polyfill

} else {
({ result, head } = await this.crdt.changes(this.indexHead));
result = await Array.fromAsync(this.crdt.changes<T>(this.indexHead));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • polyfill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants