Skip to content

Commit 322e27d

Browse files
committed
Use serializer constructed from firestore client.
1 parent e2f17ca commit 322e27d

File tree

8 files changed

+79
-55
lines changed

8 files changed

+79
-55
lines changed

packages/firestore/exp/src/api/database.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
indexedDbStoragePrefix
4747
} from '../../../src/local/indexeddb_persistence';
4848
import { LoadBundleTask } from '../../../src/api/bundle';
49-
import { Query } from '../../../lite';
5049
import {
5150
getLocalStore,
5251
getPersistence,
@@ -332,9 +331,15 @@ export function loadBundle(
332331
const firestoreImpl = cast(firestore, Firestore);
333332
const resultTask = new LoadBundleTask();
334333
// eslint-disable-next-line @typescript-eslint/no-floating-promises
335-
getSyncEngine(firestoreImpl).then(syncEngine =>
336-
enqueueLoadBundle(firestoreImpl._queue, syncEngine, bundleData, resultTask)
337-
);
334+
getSyncEngine(firestoreImpl).then(async syncEngine => {
335+
enqueueLoadBundle(
336+
(await firestoreImpl._getConfiguration()).databaseInfo.databaseId,
337+
firestoreImpl._queue,
338+
syncEngine,
339+
bundleData,
340+
resultTask
341+
);
342+
});
338343

339344
return resultTask;
340345
}

packages/firestore/src/core/bundle.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export type BundledDocuments = BundledDocument[];
8383
* Helper to convert objects from bundles to model objects in the SDK.
8484
*/
8585
export class BundleConverter {
86-
constructor(private serializer: JsonProtoSerializer) {}
86+
constructor(private readonly serializer: JsonProtoSerializer) {}
8787

8888
toDocumentKey(name: string): DocumentKey {
8989
return fromName(this.serializer, name);
@@ -165,7 +165,8 @@ export class BundleLoader {
165165

166166
constructor(
167167
private metadata: bundleProto.BundleMetadata,
168-
private localStore: LocalStore
168+
private localStore: LocalStore,
169+
private serializer: JsonProtoSerializer
169170
) {
170171
this.progress = bundleInitialProgress(metadata);
171172
}
@@ -216,9 +217,7 @@ export class BundleLoader {
216217
documents: BundledDocuments
217218
): Map<string, DocumentKeySet> {
218219
const queryDocumentMap = new Map<string, DocumentKeySet>();
219-
const bundleConverter = new BundleConverter(
220-
this.localStore.getSerializer()
221-
);
220+
const bundleConverter = new BundleConverter(this.serializer);
222221
for (const bundleDoc of documents) {
223222
if (bundleDoc.metadata.queries) {
224223
const documentKey = bundleConverter.toDocumentKey(

packages/firestore/src/core/firestore_client.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ import { TransactionRunner } from './transaction_runner';
5555
import { Datastore } from '../remote/datastore';
5656
import { BundleReader } from '../util/bundle_reader';
5757
import { LoadBundleTask } from '../api/bundle';
58-
import { newTextEncoder } from '../platform/serializer';
58+
import { newSerializer, newTextEncoder } from '../platform/serializer';
5959
import { toByteStreamReader } from '../platform/byte_stream_reader';
6060
import { NamedQuery } from './bundle';
61+
import { JsonProtoSerializer } from '../remote/serializer';
6162

6263
const LOG_TAG = 'FirestoreClient';
6364
export const MAX_CONCURRENT_LIMBO_RESOLUTIONS = 100;
@@ -537,7 +538,10 @@ export class FirestoreClient {
537538
): void {
538539
this.verifyNotTerminated();
539540

540-
const reader = createBundleReader(data);
541+
const reader = createBundleReader(
542+
data,
543+
newSerializer(this.databaseInfo.databaseId)
544+
);
541545
this.asyncQueue.enqueueAndForget(async () => {
542546
loadBundle(this.syncEngine, reader, resultTask);
543547
return resultTask.catch(e => {
@@ -798,24 +802,26 @@ export function enqueueExecuteQueryViaSnapshotListener(
798802
}
799803

800804
function createBundleReader(
801-
data: ReadableStream<Uint8Array> | ArrayBuffer | string
805+
data: ReadableStream<Uint8Array> | ArrayBuffer | string,
806+
serializer: JsonProtoSerializer
802807
): BundleReader {
803808
let content: ReadableStream<Uint8Array> | ArrayBuffer;
804809
if (typeof data === 'string') {
805810
content = newTextEncoder().encode(data);
806811
} else {
807812
content = data;
808813
}
809-
return new BundleReader(toByteStreamReader(content));
814+
return new BundleReader(toByteStreamReader(content), serializer);
810815
}
811816

812817
export function enqueueLoadBundle(
818+
databaseId: DatabaseId,
813819
asyncQueue: AsyncQueue,
814820
syncEngine: SyncEngine,
815821
data: ReadableStream<Uint8Array> | ArrayBuffer | string,
816822
resultTask: LoadBundleTask
817823
): void {
818-
const reader = createBundleReader(data);
824+
const reader = createBundleReader(data, newSerializer(databaseId));
819825
asyncQueue.enqueueAndForget(async () => {
820826
loadBundle(syncEngine, reader, resultTask);
821827
return resultTask.catch(e => {

packages/firestore/src/core/sync_engine.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,11 @@ async function loadBundleImpl(
13581358

13591359
task._updateProgress(bundleInitialProgress(metadata));
13601360

1361-
const loader = new BundleLoader(metadata, syncEngine.localStore);
1361+
const loader = new BundleLoader(
1362+
metadata,
1363+
syncEngine.localStore,
1364+
reader.serializer
1365+
);
13621366
let element = await reader.nextElement();
13631367
while (element) {
13641368
debugAssert(

packages/firestore/src/local/local_store.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ 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;
283280
}
284281

285282
/**
@@ -1086,10 +1083,6 @@ class LocalStoreImpl implements LocalStore {
10861083
txn => garbageCollector.collect(txn, this.targetDataByTarget)
10871084
);
10881085
}
1089-
1090-
getSerializer(): JsonProtoSerializer {
1091-
return this.serializer;
1092-
}
10931086
}
10941087

10951088
export function newLocalStore(
@@ -1391,42 +1384,46 @@ export async function saveNamedQuery(
13911384
const allocated = await localStore.allocateTarget(
13921385
queryToTarget(fromBundledQuery(query.bundledQuery!))
13931386
);
1387+
13941388
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
13951389
return localStoreImpl.persistence.runTransaction(
13961390
'Save named query',
13971391
'readwrite',
13981392
transaction => {
1399-
// Update allocated target's read time, if the bundle's read time is newer.
1400-
let readTimeUpdated = PersistencePromise.resolve();
14011393
const readTime = fromVersion(query.readTime!);
1402-
if (allocated.snapshotVersion.compareTo(readTime) < 0) {
1403-
const newTargetData = allocated.withResumeToken(
1404-
ByteString.EMPTY_BYTE_STRING,
1405-
readTime
1406-
);
1407-
readTimeUpdated = localStoreImpl.targetCache
1408-
.updateTargetData(transaction, newTargetData)
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-
)
1421-
);
1422-
localStoreImpl.targetDataByTarget = localStoreImpl.targetDataByTarget.insert(
1423-
newTargetData.targetId,
1424-
newTargetData
1425-
);
1394+
// Simply save the query itself if it is older than what the SDK already
1395+
// has.
1396+
if (allocated.snapshotVersion.compareTo(readTime) >= 0) {
1397+
return localStoreImpl.bundleCache.saveNamedQuery(transaction, query);
14261398
}
1427-
return readTimeUpdated.next(() =>
1428-
localStoreImpl.bundleCache.saveNamedQuery(transaction, query)
1399+
1400+
// Update existing target data because the query from the bundle is newer.
1401+
const newTargetData = allocated.withResumeToken(
1402+
ByteString.EMPTY_BYTE_STRING,
1403+
readTime
14291404
);
1405+
localStoreImpl.targetDataByTarget = localStoreImpl.targetDataByTarget.insert(
1406+
newTargetData.targetId,
1407+
newTargetData
1408+
);
1409+
return localStoreImpl.targetCache
1410+
.updateTargetData(transaction, newTargetData)
1411+
.next(() =>
1412+
localStoreImpl.targetCache.removeMatchingKeysForTargetId(
1413+
transaction,
1414+
allocated.targetId
1415+
)
1416+
)
1417+
.next(() =>
1418+
localStoreImpl.targetCache.addMatchingKeys(
1419+
transaction,
1420+
documents,
1421+
allocated.targetId
1422+
)
1423+
)
1424+
.next(() =>
1425+
localStoreImpl.bundleCache.saveNamedQuery(transaction, query)
1426+
);
14301427
}
14311428
);
14321429
}

packages/firestore/src/util/bundle_reader.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Deferred } from './promise';
2323
import { debugAssert } from './assert';
2424
import { toByteStreamReader } from '../platform/byte_stream_reader';
2525
import { newTextDecoder } from '../platform/serializer';
26+
import { JsonProtoSerializer } from '../remote/serializer';
2627

2728
/**
2829
* A complete element in the bundle stream, together with the byte length it
@@ -70,13 +71,20 @@ export class BundleReader {
7071
/** The decoder used to parse binary data into strings. */
7172
private textDecoder: TextDecoder;
7273

73-
static fromBundleSource(source: BundleSource): BundleReader {
74-
return new BundleReader(toByteStreamReader(source, BYTES_PER_READ));
74+
static fromBundleSource(
75+
source: BundleSource,
76+
serializer: JsonProtoSerializer
77+
): BundleReader {
78+
return new BundleReader(
79+
toByteStreamReader(source, BYTES_PER_READ),
80+
serializer
81+
);
7582
}
7683

7784
constructor(
7885
/** The reader to read from underlying binary bundle data source. */
79-
private reader: ReadableStreamReader<Uint8Array>
86+
private reader: ReadableStreamReader<Uint8Array>,
87+
readonly serializer: JsonProtoSerializer
8088
) {
8189
this.textDecoder = newTextDecoder();
8290
// Read the metadata (which is the first element).

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ abstract class TestRunner {
477477

478478
private async doLoadBundle(bundle: string): Promise<void> {
479479
const reader = new BundleReader(
480-
toByteStreamReader(newTextEncoder().encode(bundle))
480+
toByteStreamReader(newTextEncoder().encode(bundle)),
481+
this.serializer
481482
);
482483
const task = new LoadBundleTask();
483484
return this.queue.enqueue(async () => {

packages/firestore/test/unit/util/bundle.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
doc2
4141
} from './bundle_data';
4242
import { newTextEncoder } from '../../../src/platform/serializer';
43+
import { JSON_SERIALIZER } from '../local/persistence_test_helpers';
4344

4445
use(chaiAsPromised);
4546

@@ -92,7 +93,10 @@ describe('Bundle ', () => {
9293

9394
function genericBundleReadingTests(bytesPerRead: number): void {
9495
function bundleFromString(s: string): BundleReader {
95-
return new BundleReader(byteStreamReaderFromString(s, bytesPerRead));
96+
return new BundleReader(
97+
byteStreamReaderFromString(s, bytesPerRead),
98+
JSON_SERIALIZER
99+
);
96100
}
97101

98102
async function getAllElements(

0 commit comments

Comments
 (0)