Skip to content

Commit 65a53b2

Browse files
committed
wip
1 parent b8a061b commit 65a53b2

File tree

2 files changed

+79
-23
lines changed

2 files changed

+79
-23
lines changed

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Timestamp } from '../api/timestamp';
1818
import { User } from '../auth/user';
1919
import { Query } from '../core/query';
2020
import { BatchId, ProtoByteString } from '../core/types';
21+
import { DocumentKeySet } from '../model/collections';
2122
import { DocumentKey } from '../model/document_key';
2223
import { Mutation } from '../model/mutation';
2324
import { BATCHID_UNKNOWN, MutationBatch } from '../model/mutation_batch';
@@ -362,6 +363,39 @@ export class IndexedDbMutationQueue implements MutationQueue {
362363
.next(() => results);
363364
}
364365

366+
getAllMutationBatchesAffectingDocumentKeys(
367+
transaction: PersistenceTransaction,
368+
documentKeys: DocumentKeySet
369+
): PersistencePromise<MutationBatch[]> {
370+
const minKey = DbDocumentMutation.prefixForPath(
371+
this.userId,
372+
documentKeys.first().path
373+
);
374+
const maxKey = DbDocumentMutation.prefixForPath(
375+
this.userId,
376+
documentKeys.first().path
377+
);
378+
const keyRange = IDBKeyRange.bound(minKey, maxKey);
379+
let uniqueBatchIDs = new SortedSet<BatchId>(primitiveComparator);
380+
381+
const results: MutationBatch[] = [];
382+
return documentMutationsStore(transaction)
383+
.iterate({ range: keyRange }, (indexKey, _, control) => {
384+
const [userID, encodedPath, batchID] = indexKey;
385+
const path = EncodedResourcePath.decode(encodedPath);
386+
if (userID !== this.userId) {
387+
control.done();
388+
return;
389+
}
390+
391+
if (!documentKeys.has(new DocumentKey(path))) {
392+
return;
393+
}
394+
uniqueBatchIDs = uniqueBatchIDs.add(batchID);
395+
})
396+
.next(() => this.lookupMutationBatches(transaction, uniqueBatchIDs));
397+
}
398+
365399
getAllMutationBatchesAffectingQuery(
366400
transaction: PersistenceTransaction,
367401
query: Query
@@ -413,29 +447,33 @@ export class IndexedDbMutationQueue implements MutationQueue {
413447
}
414448
uniqueBatchIDs = uniqueBatchIDs.add(batchID);
415449
})
416-
.next(() => {
417-
const results: MutationBatch[] = [];
418-
const promises: Array<PersistencePromise<void>> = [];
419-
// TODO(rockwood): Implement this using iterate.
420-
uniqueBatchIDs.forEach(batchID => {
421-
const mutationKey = this.keyForBatchId(batchID);
422-
promises.push(
423-
mutationsStore(transaction)
424-
.get(mutationKey)
425-
.next(mutation => {
426-
if (mutation === null) {
427-
fail(
428-
'Dangling document-mutation reference found, ' +
429-
'which points to ' +
430-
mutationKey
431-
);
432-
}
433-
results.push(this.serializer.fromDbMutationBatch(mutation!));
434-
})
435-
);
436-
});
437-
return PersistencePromise.waitFor(promises).next(() => results);
438-
});
450+
.next(() => this.lookupMutationBatches(transaction, uniqueBatchIDs));
451+
}
452+
453+
private lookupMutationBatches(
454+
transaction: PersistenceTransaction,
455+
batchIDs: SortedSet<BatchId>) : PersistencePromise<MutationBatch[]> {
456+
const results: MutationBatch[] = [];
457+
const promises: Array<PersistencePromise<void>> = [];
458+
// TODO(rockwood): Implement this using iterate.
459+
batchIDs.forEach(batchID => {
460+
const mutationKey = this.keyForBatchId(batchID);
461+
promises.push(
462+
mutationsStore(transaction)
463+
.get(mutationKey)
464+
.next(mutation => {
465+
if (mutation === null) {
466+
fail(
467+
'Dangling document-mutation reference found, ' +
468+
'which points to ' +
469+
mutationKey
470+
);
471+
}
472+
results.push(this.serializer.fromDbMutationBatch(mutation!));
473+
})
474+
);
475+
});
476+
return PersistencePromise.waitFor(promises).next(() => results);
439477
}
440478

441479
removeMutationBatches(

packages/firestore/src/local/mutation_queue.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { Timestamp } from '../api/timestamp';
1818
import { Query } from '../core/query';
1919
import { BatchId, ProtoByteString } from '../core/types';
20+
import { DocumentKeySet } from '../model/collections';
2021
import { DocumentKey } from '../model/document_key';
2122
import { Mutation } from '../model/mutation';
2223
import { MutationBatch } from '../model/mutation_batch';
@@ -150,6 +151,23 @@ export interface MutationQueue extends GarbageSource {
150151
documentKey: DocumentKey
151152
): PersistencePromise<MutationBatch[]>;
152153

154+
/**
155+
* Finds all mutation batches that could possibly affect the given
156+
* set of document keys. Not all mutations in a batch will necessarily affect
157+
* each key, so when looping through the batch you'll need to
158+
* check that the mutation itself matches the key.
159+
*
160+
* Note that because of this requirement implementations are free to return
161+
* mutation batches that don't contain any of the document keys at all if it's
162+
* convenient.
163+
*/
164+
// TODO(mcg): This should really return an enumerator
165+
// also for b/32992024, all backing stores should really index by document key
166+
getAllMutationBatchesAffectingDocumentKeys(
167+
transaction: PersistenceTransaction,
168+
documentKeys: DocumentKeySet
169+
): PersistencePromise<MutationBatch[]>;
170+
153171
/**
154172
* Finds all mutation batches that could affect the results for the given
155173
* query. Not all mutations in a batch will necessarily affect the query, so

0 commit comments

Comments
 (0)