Skip to content

Commit e2f17ca

Browse files
committed
Better code structure.
1 parent b00494e commit e2f17ca

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

packages/firestore/src/core/bundle.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ import { MaybeDocument, NoDocument } from '../model/document';
3131
import { debugAssert } from '../util/assert';
3232
import {
3333
applyBundleDocuments,
34-
getQueryDocumentMapping,
3534
LocalStore,
3635
saveNamedQuery
3736
} from '../local/local_store';
3837
import { SizedBundleElement } from '../util/bundle_reader';
39-
import { MaybeDocumentMap } from '../model/collections';
38+
import {
39+
documentKeySet,
40+
DocumentKeySet,
41+
MaybeDocumentMap
42+
} from '../model/collections';
4043
import { BundleMetadata } from '../protos/firestore_bundle_proto';
4144

4245
/**
@@ -209,6 +212,30 @@ export class BundleLoader {
209212
return null;
210213
}
211214

215+
private getQueryDocumentMapping(
216+
documents: BundledDocuments
217+
): Map<string, DocumentKeySet> {
218+
const queryDocumentMap = new Map<string, DocumentKeySet>();
219+
const bundleConverter = new BundleConverter(
220+
this.localStore.getSerializer()
221+
);
222+
for (const bundleDoc of documents) {
223+
if (bundleDoc.metadata.queries) {
224+
const documentKey = bundleConverter.toDocumentKey(
225+
bundleDoc.metadata.name!
226+
);
227+
for (const queryName of bundleDoc.metadata.queries) {
228+
const documentKeys = (
229+
queryDocumentMap.get(queryName) || documentKeySet()
230+
).add(documentKey);
231+
queryDocumentMap.set(queryName, documentKeys);
232+
}
233+
}
234+
}
235+
236+
return queryDocumentMap;
237+
}
238+
212239
/**
213240
* Update the progress to 'Success' and return the updated progress.
214241
*/
@@ -224,10 +251,8 @@ export class BundleLoader {
224251
this.documents
225252
);
226253

227-
const queryDocumentMap = getQueryDocumentMapping(
228-
this.localStore,
229-
this.documents
230-
);
254+
const queryDocumentMap = this.getQueryDocumentMapping(this.documents);
255+
231256
for (const q of this.queries) {
232257
await saveNamedQuery(this.localStore, q, queryDocumentMap.get(q.name!));
233258
}

packages/firestore/src/local/local_store.ts

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ export interface LocalStore {
277277
executeQuery(query: Query, usePreviousResults: boolean): Promise<QueryResult>;
278278

279279
collectGarbage(garbageCollector: LruGarbageCollector): Promise<LruResults>;
280+
281+
/** Returns the serializer associated with the local store. */
282+
getSerializer(): JsonProtoSerializer;
280283
}
281284

282285
/**
@@ -1083,6 +1086,10 @@ class LocalStoreImpl implements LocalStore {
10831086
txn => garbageCollector.collect(txn, this.targetDataByTarget)
10841087
);
10851088
}
1089+
1090+
getSerializer(): JsonProtoSerializer {
1091+
return this.serializer;
1092+
}
10861093
}
10871094

10881095
export function newLocalStore(
@@ -1249,30 +1256,6 @@ export async function ignoreIfPrimaryLeaseLoss(
12491256
}
12501257
}
12511258

1252-
export function getQueryDocumentMapping(
1253-
localStore: LocalStore,
1254-
documents: BundledDocuments
1255-
): Map<string, DocumentKeySet> {
1256-
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
1257-
const queryDocumentMap = new Map<string, DocumentKeySet>();
1258-
const bundleConverter = new BundleConverter(localStoreImpl.serializer);
1259-
for (const bundleDoc of documents) {
1260-
if (bundleDoc.metadata.queries) {
1261-
const documentKey = bundleConverter.toDocumentKey(
1262-
bundleDoc.metadata.name!
1263-
);
1264-
for (const queryName of bundleDoc.metadata.queries) {
1265-
const documentKeys = (
1266-
queryDocumentMap.get(queryName) || documentKeySet()
1267-
).add(documentKey);
1268-
queryDocumentMap.set(queryName, documentKeys);
1269-
}
1270-
}
1271-
}
1272-
1273-
return queryDocumentMap;
1274-
}
1275-
12761259
/**
12771260
* Applies the documents from a bundle to the "ground-state" (remote)
12781261
* documents.
@@ -1324,9 +1307,6 @@ export function applyBundleDocuments(
13241307
txn,
13251308
changedDocs
13261309
);
1327-
})
1328-
.next(changedDocuments => {
1329-
return PersistencePromise.resolve(changedDocuments);
13301310
});
13311311
}
13321312
);
@@ -1417,41 +1397,36 @@ export async function saveNamedQuery(
14171397
'readwrite',
14181398
transaction => {
14191399
// Update allocated target's read time, if the bundle's read time is newer.
1420-
let updateReadTime = PersistencePromise.resolve(false);
1400+
let readTimeUpdated = PersistencePromise.resolve();
14211401
const readTime = fromVersion(query.readTime!);
14221402
if (allocated.snapshotVersion.compareTo(readTime) < 0) {
14231403
const newTargetData = allocated.withResumeToken(
14241404
ByteString.EMPTY_BYTE_STRING,
14251405
readTime
14261406
);
1427-
updateReadTime = localStoreImpl.targetCache
1407+
readTimeUpdated = localStoreImpl.targetCache
14281408
.updateTargetData(transaction, newTargetData)
1429-
.next(
1430-
() => true,
1431-
() => false
1409+
.next(() =>
1410+
localStoreImpl.targetCache.removeMatchingKeysForTargetId(
1411+
transaction,
1412+
allocated.targetId
1413+
)
1414+
)
1415+
.next(() =>
1416+
localStoreImpl.targetCache.addMatchingKeys(
1417+
transaction,
1418+
documents,
1419+
allocated.targetId
1420+
)
14321421
);
14331422
localStoreImpl.targetDataByTarget = localStoreImpl.targetDataByTarget.insert(
14341423
newTargetData.targetId,
14351424
newTargetData
14361425
);
14371426
}
1438-
return updateReadTime
1439-
.next(updated => {
1440-
if (updated) {
1441-
return localStoreImpl.targetCache
1442-
.removeMatchingKeysForTargetId(transaction, allocated.targetId)
1443-
.next(() =>
1444-
localStoreImpl.targetCache.addMatchingKeys(
1445-
transaction,
1446-
documents,
1447-
allocated.targetId
1448-
)
1449-
);
1450-
}
1451-
})
1452-
.next(() =>
1453-
localStoreImpl.bundleCache.saveNamedQuery(transaction, query)
1454-
);
1427+
return readTimeUpdated.next(() =>
1428+
localStoreImpl.bundleCache.saveNamedQuery(transaction, query)
1429+
);
14551430
}
14561431
);
14571432
}

0 commit comments

Comments
 (0)