Skip to content

Commit 00ac9d2

Browse files
Replace usage of Set<DocumentKey> with Set<path> (#3652)
1 parent 8072624 commit 00ac9d2

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

packages/firestore/src/core/transaction.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
import { fail, debugAssert } from '../util/assert';
3434
import { Code, FirestoreError } from '../util/error';
3535
import { SnapshotVersion } from './snapshot_version';
36-
import { ResourcePath } from '../model/path';
3736

3837
/**
3938
* Internal transaction object responsible for accumulating the mutations to
@@ -57,7 +56,7 @@ export class Transaction {
5756
* When there's more than one write to the same key in a transaction, any
5857
* writes after the first are handled differently.
5958
*/
60-
private writtenDocs: Set<DocumentKey> = new Set();
59+
private writtenDocs: Set</* path= */ string> = new Set();
6160

6261
constructor(private datastore: Datastore) {}
6362

@@ -83,7 +82,7 @@ export class Transaction {
8382

8483
set(key: DocumentKey, data: ParsedSetData): void {
8584
this.write(data.toMutations(key, this.precondition(key)));
86-
this.writtenDocs.add(key);
85+
this.writtenDocs.add(key.toString());
8786
}
8887

8988
update(key: DocumentKey, data: ParsedUpdateData): void {
@@ -92,12 +91,12 @@ export class Transaction {
9291
} catch (e) {
9392
this.lastWriteError = e;
9493
}
95-
this.writtenDocs.add(key);
94+
this.writtenDocs.add(key.toString());
9695
}
9796

9897
delete(key: DocumentKey): void {
9998
this.write([new DeleteMutation(key, this.precondition(key))]);
100-
this.writtenDocs.add(key);
99+
this.writtenDocs.add(key.toString());
101100
}
102101

103102
async commit(): Promise<void> {
@@ -114,7 +113,7 @@ export class Transaction {
114113
// For each document that was read but not written to, we want to perform
115114
// a `verify` operation.
116115
unwritten.forEach((_, path) => {
117-
const key = new DocumentKey(ResourcePath.fromString(path));
116+
const key = DocumentKey.fromPath(path);
118117
this.mutations.push(new VerifyMutation(key, this.precondition(key)));
119118
});
120119
await invokeCommitRpc(this.datastore, this.mutations);
@@ -153,7 +152,7 @@ export class Transaction {
153152
*/
154153
private precondition(key: DocumentKey): Precondition {
155154
const version = this.readVersions.get(key.toString());
156-
if (!this.writtenDocs.has(key) && version) {
155+
if (!this.writtenDocs.has(key.toString()) && version) {
157156
return Precondition.updateTime(version);
158157
} else {
159158
return Precondition.none();
@@ -167,7 +166,7 @@ export class Transaction {
167166
const version = this.readVersions.get(key.toString());
168167
// The first time a document is written, we want to take into account the
169168
// read time and existence
170-
if (!this.writtenDocs.has(key) && version) {
169+
if (!this.writtenDocs.has(key.toString()) && version) {
171170
if (version.isEqual(SnapshotVersion.min())) {
172171
// The document doesn't exist, so fail the transaction.
173172

packages/firestore/src/local/memory_persistence.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ export class MemoryEagerDelegate implements MemoryReferenceDelegate {
191191
/** Tracks all documents that are active in Query views. */
192192
private localViewReferences: ReferenceSet = new ReferenceSet();
193193
/** The list of documents that are potentially GCed after each transaction. */
194-
private _orphanedDocuments: Set<DocumentKey> | null = null;
194+
private _orphanedDocuments: Set</* path= */ string> | null = null;
195195

196196
private constructor(private readonly persistence: MemoryPersistence) {}
197197

198198
static factory(persistence: MemoryPersistence): MemoryEagerDelegate {
199199
return new MemoryEagerDelegate(persistence);
200200
}
201201

202-
private get orphanedDocuments(): Set<DocumentKey> {
202+
private get orphanedDocuments(): Set<string> {
203203
if (!this._orphanedDocuments) {
204204
throw fail('orphanedDocuments is only valid during a transaction.');
205205
} else {
@@ -213,7 +213,7 @@ export class MemoryEagerDelegate implements MemoryReferenceDelegate {
213213
key: DocumentKey
214214
): PersistencePromise<void> {
215215
this.localViewReferences.addReference(key, targetId);
216-
this.orphanedDocuments.delete(key);
216+
this.orphanedDocuments.delete(key.toString());
217217
return PersistencePromise.resolve();
218218
}
219219

@@ -223,15 +223,15 @@ export class MemoryEagerDelegate implements MemoryReferenceDelegate {
223223
key: DocumentKey
224224
): PersistencePromise<void> {
225225
this.localViewReferences.removeReference(key, targetId);
226-
this.orphanedDocuments.add(key);
226+
this.orphanedDocuments.add(key.toString());
227227
return PersistencePromise.resolve();
228228
}
229229

230230
markPotentiallyOrphaned(
231231
txn: PersistenceTransaction,
232232
key: DocumentKey
233233
): PersistencePromise<void> {
234-
this.orphanedDocuments.add(key);
234+
this.orphanedDocuments.add(key.toString());
235235
return PersistencePromise.resolve();
236236
}
237237

@@ -242,18 +242,18 @@ export class MemoryEagerDelegate implements MemoryReferenceDelegate {
242242
const orphaned = this.localViewReferences.removeReferencesForId(
243243
targetData.targetId
244244
);
245-
orphaned.forEach(key => this.orphanedDocuments.add(key));
245+
orphaned.forEach(key => this.orphanedDocuments.add(key.toString()));
246246
const cache = this.persistence.getTargetCache();
247247
return cache
248248
.getMatchingKeysForTargetId(txn, targetData.targetId)
249249
.next(keys => {
250-
keys.forEach(key => this.orphanedDocuments.add(key));
250+
keys.forEach(key => this.orphanedDocuments.add(key.toString()));
251251
})
252252
.next(() => cache.removeTargetData(txn, targetData));
253253
}
254254

255255
onTransactionStarted(): void {
256-
this._orphanedDocuments = new Set<DocumentKey>();
256+
this._orphanedDocuments = new Set<string>();
257257
}
258258

259259
onTransactionCommitted(
@@ -264,7 +264,8 @@ export class MemoryEagerDelegate implements MemoryReferenceDelegate {
264264
const changeBuffer = cache.newChangeBuffer();
265265
return PersistencePromise.forEach(
266266
this.orphanedDocuments,
267-
(key: DocumentKey) => {
267+
(path: string) => {
268+
const key = DocumentKey.fromPath(path);
268269
return this.isReferenced(txn, key).next(isReferenced => {
269270
if (!isReferenced) {
270271
changeBuffer.removeEntry(key);
@@ -283,9 +284,9 @@ export class MemoryEagerDelegate implements MemoryReferenceDelegate {
283284
): PersistencePromise<void> {
284285
return this.isReferenced(txn, key).next(isReferenced => {
285286
if (isReferenced) {
286-
this.orphanedDocuments.delete(key);
287+
this.orphanedDocuments.delete(key.toString());
287288
} else {
288-
this.orphanedDocuments.add(key);
289+
this.orphanedDocuments.add(key.toString());
289290
}
290291
});
291292
}

packages/firestore/src/model/document_key.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class DocumentKey {
2828
);
2929
}
3030

31+
static fromPath(path: string): DocumentKey {
32+
return new DocumentKey(ResourcePath.fromString(path));
33+
}
34+
3135
static fromName(name: string): DocumentKey {
3236
return new DocumentKey(ResourcePath.fromString(name).popFirst(5));
3337
}

0 commit comments

Comments
 (0)