Skip to content

Commit

Permalink
Tree-Shakeable LocalSerializer (#3241)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian authored Jun 23, 2020
1 parent 58e1b6f commit 2c8c17c
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 214 deletions.
18 changes: 11 additions & 7 deletions packages/firestore/src/local/indexeddb_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ import {
DbMutationQueue,
DbMutationQueueKey
} from './indexeddb_schema';
import { LocalSerializer } from './local_serializer';
import {
fromDbMutationBatch,
LocalSerializer,
toDbMutationBatch
} from './local_serializer';
import { MutationQueue } from './mutation_queue';
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
import { PersistencePromise } from './persistence_promise';
Expand Down Expand Up @@ -148,7 +152,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
baseMutations,
mutations
);
const dbBatch = this.serializer.toDbMutationBatch(this.userId, batch);
const dbBatch = toDbMutationBatch(this.serializer, this.userId, batch);

const promises: Array<PersistencePromise<void>> = [];
let collectionParents = new SortedSet<ResourcePath>((l, r) =>
Expand Down Expand Up @@ -193,7 +197,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
dbBatch.userId === this.userId,
`Unexpected user '${dbBatch.userId}' for mutation batch ${batchId}`
);
return this.serializer.fromDbMutationBatch(dbBatch);
return fromDbMutationBatch(this.serializer, dbBatch);
}
return null;
});
Expand Down Expand Up @@ -244,7 +248,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
dbBatch.batchId >= nextBatchId,
'Should have found mutation after ' + nextBatchId
);
foundBatch = this.serializer.fromDbMutationBatch(dbBatch);
foundBatch = fromDbMutationBatch(this.serializer, dbBatch);
}
control.done();
}
Expand Down Expand Up @@ -282,7 +286,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
return mutationsStore(transaction)
.loadAll(DbMutationBatch.userMutationsIndex, range)
.next(dbBatches =>
dbBatches.map(dbBatch => this.serializer.fromDbMutationBatch(dbBatch))
dbBatches.map(dbBatch => fromDbMutationBatch(this.serializer, dbBatch))
);
}

Expand Down Expand Up @@ -331,7 +335,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
mutation.userId === this.userId,
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
);
results.push(this.serializer.fromDbMutationBatch(mutation));
results.push(fromDbMutationBatch(this.serializer, mutation));
});
})
.next(() => results);
Expand Down Expand Up @@ -462,7 +466,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
mutation.userId === this.userId,
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
);
results.push(this.serializer.fromDbMutationBatch(mutation));
results.push(fromDbMutationBatch(this.serializer, mutation));
})
);
});
Expand Down
28 changes: 18 additions & 10 deletions packages/firestore/src/local/indexeddb_remote_document_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ import {
DbRemoteDocumentGlobalKey,
DbRemoteDocumentKey
} from './indexeddb_schema';
import { LocalSerializer } from './local_serializer';
import {
fromDbRemoteDocument,
fromDbTimestampKey,
LocalSerializer,
toDbRemoteDocument,
toDbTimestampKey
} from './local_serializer';
import { PersistenceTransaction } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { RemoteDocumentCache } from './remote_document_cache';
Expand Down Expand Up @@ -262,7 +268,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
// since all document changes to queries that have a
// lastLimboFreeSnapshotVersion (`sinceReadTime`) have a read time set.
const collectionKey = query.path.toArray();
const readTimeKey = this.serializer.toDbTimestampKey(sinceReadTime);
const readTimeKey = toDbTimestampKey(sinceReadTime);
iterationOptions.range = IDBKeyRange.lowerBound(
[collectionKey, readTimeKey],
/* open= */ true
Expand All @@ -281,7 +287,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
return;
}

const maybeDoc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
const maybeDoc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
if (!query.path.isPrefixOf(maybeDoc.key.path)) {
control.done();
} else if (maybeDoc instanceof Document && query.matches(maybeDoc)) {
Expand All @@ -305,7 +311,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
}> {
let changedDocs = maybeDocumentMap();

let lastReadTime = this.serializer.toDbTimestampKey(sinceReadTime);
let lastReadTime = toDbTimestampKey(sinceReadTime);

const documentsStore = remoteDocumentsStore(transaction);
const range = IDBKeyRange.lowerBound(lastReadTime, true);
Expand All @@ -315,15 +321,15 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
(_, dbRemoteDoc) => {
// Unlike `getEntry()` and others, `getNewDocumentChanges()` parses
// the documents directly since we want to keep sentinel deletes.
const doc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
const doc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
changedDocs = changedDocs.insert(doc.key, doc);
lastReadTime = dbRemoteDoc.readTime!;
}
)
.next(() => {
return {
changedDocs,
readTime: this.serializer.fromDbTimestampKey(lastReadTime)
readTime: fromDbTimestampKey(lastReadTime)
};
});
}
Expand All @@ -346,7 +352,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
{ index: DbRemoteDocument.readTimeIndex, reverse: true },
(key, dbRemoteDoc, control) => {
if (dbRemoteDoc.readTime) {
readTime = this.serializer.fromDbTimestampKey(dbRemoteDoc.readTime);
readTime = fromDbTimestampKey(dbRemoteDoc.readTime);
}
control.done();
}
Expand Down Expand Up @@ -393,7 +399,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
dbRemoteDoc: DbRemoteDocument | null
): MaybeDocument | null {
if (dbRemoteDoc) {
const doc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
const doc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
if (
doc instanceof NoDocument &&
doc.version.isEqual(SnapshotVersion.min())
Expand Down Expand Up @@ -456,7 +462,8 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
!this.readTime.isEqual(SnapshotVersion.min()),
'Cannot add a document with a read time of zero'
);
const doc = this.documentCache.serializer.toDbRemoteDocument(
const doc = toDbRemoteDocument(
this.documentCache.serializer,
maybeDocument,
this.readTime
);
Expand All @@ -472,7 +479,8 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
// RemoteDocumentCache. This entry is represented by a NoDocument
// with a version of 0 and ignored by `maybeDecodeDocument()` but
// preserved in `getNewDocumentChanges()`.
const deletedDoc = this.documentCache.serializer.toDbRemoteDocument(
const deletedDoc = toDbRemoteDocument(
this.documentCache.serializer,
new NoDocument(key, SnapshotVersion.min()),
this.readTime
);
Expand Down
19 changes: 12 additions & 7 deletions packages/firestore/src/local/indexeddb_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@
import { BatchId, ListenSequenceNumber, TargetId } from '../core/types';
import { ResourcePath } from '../model/path';
import * as api from '../protos/firestore_proto_api';
import { hardAssert, debugAssert } from '../util/assert';
import { debugAssert, hardAssert } from '../util/assert';

import { SnapshotVersion } from '../core/snapshot_version';
import { BATCHID_UNKNOWN } from '../model/mutation_batch';
import {
decodeResourcePath,
encodeResourcePath,
EncodedResourcePath
EncodedResourcePath,
encodeResourcePath
} from './encoded_resource_path';
import { removeMutationBatch } from './indexeddb_mutation_queue';
import { dbDocumentSize } from './indexeddb_remote_document_cache';
import { LocalSerializer } from './local_serializer';
import {
fromDbMutationBatch,
fromDbTarget,
LocalSerializer,
toDbTarget
} from './local_serializer';
import { MemoryCollectionParentIndex } from './memory_index_manager';
import { PersistencePromise } from './persistence_promise';
import { SimpleDbSchemaConverter, SimpleDbTransaction } from './simple_db';
Expand Down Expand Up @@ -202,7 +207,7 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
dbBatch.userId === queue.userId,
`Cannot process batch ${dbBatch.batchId} from unexpected user`
);
const batch = this.serializer.fromDbMutationBatch(dbBatch);
const batch = fromDbMutationBatch(this.serializer, dbBatch);

return removeMutationBatch(
txn,
Expand Down Expand Up @@ -324,8 +329,8 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
): PersistencePromise<void> {
const targetStore = txn.store<DbTargetKey, DbTarget>(DbTarget.store);
return targetStore.iterate((key, originalDbTarget) => {
const originalTargetData = this.serializer.fromDbTarget(originalDbTarget);
const updatedDbTarget = this.serializer.toDbTarget(originalTargetData);
const originalTargetData = fromDbTarget(originalDbTarget);
const updatedDbTarget = toDbTarget(this.serializer, originalTargetData);
return targetStore.put(updatedDbTarget);
});
}
Expand Down
12 changes: 6 additions & 6 deletions packages/firestore/src/local/indexeddb_target_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
DbTargetGlobalKey,
DbTargetKey
} from './indexeddb_schema';
import { LocalSerializer } from './local_serializer';
import { fromDbTarget, LocalSerializer, toDbTarget } from './local_serializer';
import { ActiveTargets } from './lru_garbage_collector';
import { PersistenceTransaction } from './persistence';
import { PersistencePromise } from './persistence_promise';
Expand Down Expand Up @@ -162,7 +162,7 @@ export class IndexedDbTargetCache implements TargetCache {
const promises: Array<PersistencePromise<void>> = [];
return targetsStore(txn)
.iterate((key, value) => {
const targetData = this.serializer.fromDbTarget(value);
const targetData = fromDbTarget(value);
if (
targetData.sequenceNumber <= upperBound &&
activeTargetIds.get(targetData.targetId) === null
Expand All @@ -183,7 +183,7 @@ export class IndexedDbTargetCache implements TargetCache {
f: (q: TargetData) => void
): PersistencePromise<void> {
return targetsStore(txn).iterate((key, value) => {
const targetData = this.serializer.fromDbTarget(value);
const targetData = fromDbTarget(value);
f(targetData);
});
}
Expand Down Expand Up @@ -211,7 +211,7 @@ export class IndexedDbTargetCache implements TargetCache {
targetData: TargetData
): PersistencePromise<void> {
return targetsStore(transaction).put(
this.serializer.toDbTarget(targetData)
toDbTarget(this.serializer, targetData)
);
}

Expand Down Expand Up @@ -262,7 +262,7 @@ export class IndexedDbTargetCache implements TargetCache {
.iterate(
{ range, index: DbTarget.queryTargetsIndexName },
(key, value, control) => {
const found = this.serializer.fromDbTarget(value);
const found = fromDbTarget(value);
// After finding a potential match, check that the target is
// actually equal to the requested target.
if (targetEquals(target, found.target)) {
Expand Down Expand Up @@ -392,7 +392,7 @@ export class IndexedDbTargetCache implements TargetCache {
.get(targetId)
.next(found => {
if (found) {
return this.serializer.fromDbTarget(found);
return fromDbTarget(found);
} else {
return null;
}
Expand Down
Loading

0 comments on commit 2c8c17c

Please sign in to comment.