Skip to content

Commit

Permalink
Merge pull request #12 from readwiseio/tristan/fix-persistent-limit-b…
Browse files Browse the repository at this point in the history
…uffer-case

re-exec a query if items were removed from the persistent results
  • Loading branch information
TristanH authored Dec 5, 2023
2 parents 249ec1a + 2df803b commit a958b6a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
26 changes: 20 additions & 6 deletions src/rx-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,28 @@ export class RxQueryBase<
);

for (const changedDoc of changedDocs) {
/*
* no need to fetch again, we already got the doc from the list of changed docs, and therefore we filter
* deleted docs as well
*/
persistedQueryCacheIds.delete(changedDoc[primaryPath] as string);
const docWasInOldPersistedResults = persistedQueryCacheIds.has(changedDoc[primaryPath] as string);
const docMatchesNow = this.doesDocumentDataMatch(changedDoc);

if (docWasInOldPersistedResults && !docMatchesNow && this.mangoQuery.limit) {
// Unfortunately if any doc was removed from the results since the last result,
// there is no way for us to be sure our calculated results are correct.
// So we should simply give up and re-exec the query.
this._persistentQueryCacheResult = value ?? undefined;
this._persistentQueryCacheResultLwt = lwt ?? undefined;
return;
}

if (docWasInOldPersistedResults) {
/*
* no need to fetch again, we already got the doc from the list of changed docs, and therefore we filter
* deleted docs as well
*/
persistedQueryCacheIds.delete(changedDoc[primaryPath] as string);
}

// ignore deleted docs or docs that do not match the query
if (!this.doesDocumentDataMatch(changedDoc)) {
if (!docMatchesNow) {
continue;
}

Expand Down
8 changes: 6 additions & 2 deletions test/unit/rx-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ describe('rx-query.test.ts', () => {
collection.database.destroy();
});

it.skip('gives correct limit results when items were removed', async () => {
it('gives correct limit results when items were removed', async () => {
const {collection} = await setUpPersistentQueryCacheCollection();
const human1 = schemaObjects.human('1', 30);
const human2 = schemaObjects.human('2', 40);
Expand Down Expand Up @@ -1865,10 +1865,14 @@ describe('rx-query.test.ts', () => {

// now when we create the query again, it has no way of knowing how to fill the missing item
const queryAgain = collection.find({ limit: 2, sort: [{age: 'asc'}], selector: { age: { $gt: 10 } } });
assert.strictEqual(queryAgain._execOverDatabaseCount, 0);

await queryAgain.enablePersistentQueryCache(cache);
const updatedResults = await queryAgain.exec();
assert.deepStrictEqual(updatedResults.map(h => h.passportId), ['2', '3']);

// We must re-exec the query to make it correct.
assert.strictEqual(queryAgain._execOverDatabaseCount, 1);
assert.deepStrictEqual(updatedResults.map(h => h.passportId), ['2', '3']);
collection.database.destroy();
});
});
Expand Down

0 comments on commit a958b6a

Please sign in to comment.