Skip to content

Commit 4ad5733

Browse files
Remove "cyclic" references (#2969)
1 parent ce07010 commit 4ad5733

36 files changed

+182
-221
lines changed

packages/firestore/src/api/database.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import {
6969
validateStringEnum,
7070
valueDescription
7171
} from '../util/input_validation';
72-
import { logError, setLogLevel, LogLevel, getLogLevel } from '../util/log';
72+
import { getLogLevel, logError, LogLevel, setLogLevel } from '../util/log';
7373
import { AutoId } from '../util/misc';
7474
import { Deferred, Rejecter, Resolver } from '../util/promise';
7575
import { FieldPath as ExternalFieldPath } from './field_path';
@@ -836,7 +836,7 @@ export class WriteBatch implements firestore.WriteBatch {
836836
convertedValue
837837
);
838838
this._mutations = this._mutations.concat(
839-
parsed.toMutations(ref._key, Precondition.NONE)
839+
parsed.toMutations(ref._key, Precondition.none())
840840
);
841841
return this;
842842
}
@@ -906,7 +906,7 @@ export class WriteBatch implements firestore.WriteBatch {
906906
this._firestore
907907
);
908908
this._mutations = this._mutations.concat(
909-
new DeleteMutation(ref._key, Precondition.NONE)
909+
new DeleteMutation(ref._key, Precondition.none())
910910
);
911911
return this;
912912
}
@@ -1031,7 +1031,7 @@ export class DocumentReference<T = firestore.DocumentData>
10311031
)
10321032
: this.firestore._dataReader.parseSetData(functionName, convertedValue);
10331033
return this._firestoreClient.write(
1034-
parsed.toMutations(this._key, Precondition.NONE)
1034+
parsed.toMutations(this._key, Precondition.none())
10351035
);
10361036
}
10371037

@@ -1075,7 +1075,7 @@ export class DocumentReference<T = firestore.DocumentData>
10751075
delete(): Promise<void> {
10761076
validateExactNumberOfArgs('DocumentReference.delete', arguments, 0);
10771077
return this._firestoreClient.write([
1078-
new DeleteMutation(this._key, Precondition.NONE)
1078+
new DeleteMutation(this._key, Precondition.none())
10791079
]);
10801080
}
10811081

@@ -1447,32 +1447,31 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14471447

14481448
// Enumerated from the WhereFilterOp type in index.d.ts.
14491449
const whereFilterOpEnums = [
1450-
'<',
1451-
'<=',
1452-
'==',
1453-
'>=',
1454-
'>',
1455-
'array-contains',
1456-
'in',
1457-
'array-contains-any'
1450+
Operator.LESS_THAN,
1451+
Operator.LESS_THAN_OR_EQUAL,
1452+
Operator.EQUAL,
1453+
Operator.GREATER_THAN_OR_EQUAL,
1454+
Operator.GREATER_THAN,
1455+
Operator.ARRAY_CONTAINS,
1456+
Operator.IN,
1457+
Operator.ARRAY_CONTAINS_ANY
14581458
];
1459-
validateStringEnum('Query.where', whereFilterOpEnums, 2, opStr);
1459+
const op = validateStringEnum('Query.where', whereFilterOpEnums, 2, opStr);
14601460

14611461
let fieldValue: api.Value;
14621462
const fieldPath = fieldPathFromArgument('Query.where', field);
1463-
const operator = Operator.fromString(opStr);
14641463
if (fieldPath.isKeyField()) {
14651464
if (
1466-
operator === Operator.ARRAY_CONTAINS ||
1467-
operator === Operator.ARRAY_CONTAINS_ANY
1465+
op === Operator.ARRAY_CONTAINS ||
1466+
op === Operator.ARRAY_CONTAINS_ANY
14681467
) {
14691468
throw new FirestoreError(
14701469
Code.INVALID_ARGUMENT,
1471-
`Invalid Query. You can't perform '${operator.toString()}' ` +
1470+
`Invalid Query. You can't perform '${op}' ` +
14721471
'queries on FieldPath.documentId().'
14731472
);
1474-
} else if (operator === Operator.IN) {
1475-
this.validateDisjunctiveFilterElements(value, operator);
1473+
} else if (op === Operator.IN) {
1474+
this.validateDisjunctiveFilterElements(value, op);
14761475
const referenceList: api.Value[] = [];
14771476
for (const arrayValue of value as api.Value[]) {
14781477
referenceList.push(this.parseDocumentIdValue(arrayValue));
@@ -1482,20 +1481,17 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14821481
fieldValue = this.parseDocumentIdValue(value);
14831482
}
14841483
} else {
1485-
if (
1486-
operator === Operator.IN ||
1487-
operator === Operator.ARRAY_CONTAINS_ANY
1488-
) {
1489-
this.validateDisjunctiveFilterElements(value, operator);
1484+
if (op === Operator.IN || op === Operator.ARRAY_CONTAINS_ANY) {
1485+
this.validateDisjunctiveFilterElements(value, op);
14901486
}
14911487
fieldValue = this.firestore._dataReader.parseQueryValue(
14921488
'Query.where',
14931489
value,
14941490
// We only allow nested arrays for IN queries.
1495-
/** allowArrays = */ operator === Operator.IN ? true : false
1491+
/** allowArrays = */ op === Operator.IN
14961492
);
14971493
}
1498-
const filter = FieldFilter.create(fieldPath, operator, fieldValue);
1494+
const filter = FieldFilter.create(fieldPath, op, fieldValue);
14991495
this.validateNewFilter(filter);
15001496
return new Query(
15011497
this._query.addFilter(filter),

packages/firestore/src/core/query.ts

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,27 @@ export class Query {
8080

8181
get orderBy(): OrderBy[] {
8282
if (this.memoizedOrderBy === null) {
83+
this.memoizedOrderBy = [];
84+
8385
const inequalityField = this.getInequalityFilterField();
8486
const firstOrderByField = this.getFirstOrderByField();
8587
if (inequalityField !== null && firstOrderByField === null) {
8688
// In order to implicitly add key ordering, we must also add the
8789
// inequality filter field for it to be a valid query.
8890
// Note that the default inequality field and key ordering is ascending.
89-
if (inequalityField.isKeyField()) {
90-
this.memoizedOrderBy = [KEY_ORDERING_ASC];
91-
} else {
92-
this.memoizedOrderBy = [
93-
new OrderBy(inequalityField),
94-
KEY_ORDERING_ASC
95-
];
91+
if (!inequalityField.isKeyField()) {
92+
this.memoizedOrderBy.push(new OrderBy(inequalityField));
9693
}
94+
this.memoizedOrderBy.push(
95+
new OrderBy(FieldPath.keyField(), Direction.ASCENDING)
96+
);
9797
} else {
9898
debugAssert(
9999
inequalityField === null ||
100100
(firstOrderByField !== null &&
101101
inequalityField.isEqual(firstOrderByField)),
102102
'First orderBy should match inequality field.'
103103
);
104-
this.memoizedOrderBy = [];
105104
let foundKeyOrdering = false;
106105
for (const orderBy of this.explicitOrderBy) {
107106
this.memoizedOrderBy.push(orderBy);
@@ -117,9 +116,7 @@ export class Query {
117116
? this.explicitOrderBy[this.explicitOrderBy.length - 1].dir
118117
: Direction.ASCENDING;
119118
this.memoizedOrderBy.push(
120-
lastDirection === Direction.ASCENDING
121-
? KEY_ORDERING_ASC
122-
: KEY_ORDERING_DESC
119+
new OrderBy(FieldPath.keyField(), lastDirection)
123120
);
124121
}
125122
}
@@ -468,48 +465,15 @@ export abstract class Filter {
468465
abstract isEqual(filter: Filter): boolean;
469466
}
470467

471-
export class Operator {
472-
static LESS_THAN = new Operator('<');
473-
static LESS_THAN_OR_EQUAL = new Operator('<=');
474-
static EQUAL = new Operator('==');
475-
static GREATER_THAN = new Operator('>');
476-
static GREATER_THAN_OR_EQUAL = new Operator('>=');
477-
static ARRAY_CONTAINS = new Operator('array-contains');
478-
static IN = new Operator('in');
479-
static ARRAY_CONTAINS_ANY = new Operator('array-contains-any');
480-
481-
static fromString(op: string): Operator {
482-
switch (op) {
483-
case '<':
484-
return Operator.LESS_THAN;
485-
case '<=':
486-
return Operator.LESS_THAN_OR_EQUAL;
487-
case '==':
488-
return Operator.EQUAL;
489-
case '>=':
490-
return Operator.GREATER_THAN_OR_EQUAL;
491-
case '>':
492-
return Operator.GREATER_THAN;
493-
case 'array-contains':
494-
return Operator.ARRAY_CONTAINS;
495-
case 'in':
496-
return Operator.IN;
497-
case 'array-contains-any':
498-
return Operator.ARRAY_CONTAINS_ANY;
499-
default:
500-
return fail('Unknown FieldFilter operator: ' + op);
501-
}
502-
}
503-
504-
constructor(public name: string) {}
505-
506-
toString(): string {
507-
return this.name;
508-
}
509-
510-
isEqual(other: Operator): boolean {
511-
return this.name === other.name;
512-
}
468+
export const enum Operator {
469+
LESS_THAN = '<',
470+
LESS_THAN_OR_EQUAL = '<=',
471+
EQUAL = '==',
472+
GREATER_THAN = '>',
473+
GREATER_THAN_OR_EQUAL = '>=',
474+
ARRAY_CONTAINS = 'array-contains',
475+
IN = 'in',
476+
ARRAY_CONTAINS_ANY = 'array-contains-any'
513477
}
514478

515479
export class FieldFilter extends Filter {
@@ -635,7 +599,7 @@ export class FieldFilter extends Filter {
635599
isEqual(other: Filter): boolean {
636600
if (other instanceof FieldFilter) {
637601
return (
638-
this.op.isEqual(other.op) &&
602+
this.op === other.op &&
639603
this.field.isEqual(other.field) &&
640604
valueEquals(this.value, other.value)
641605
);
@@ -737,15 +701,9 @@ export class ArrayContainsAnyFilter extends FieldFilter {
737701
/**
738702
* The direction of sorting in an order by.
739703
*/
740-
export class Direction {
741-
static ASCENDING = new Direction('asc');
742-
static DESCENDING = new Direction('desc');
743-
744-
private constructor(public name: string) {}
745-
746-
toString(): string {
747-
return this.name;
748-
}
704+
export const enum Direction {
705+
ASCENDING = 'asc',
706+
DESCENDING = 'desc'
749707
}
750708

751709
/**
@@ -875,9 +833,3 @@ export class OrderBy {
875833
return this.dir === other.dir && this.field.isEqual(other.field);
876834
}
877835
}
878-
879-
const KEY_ORDERING_ASC = new OrderBy(FieldPath.keyField(), Direction.ASCENDING);
880-
const KEY_ORDERING_DESC = new OrderBy(
881-
FieldPath.keyField(),
882-
Direction.DESCENDING
883-
);

packages/firestore/src/core/snapshot_version.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import { Timestamp } from '../api/timestamp';
2222
* timestamp, such as update_time or read_time.
2323
*/
2424
export class SnapshotVersion {
25-
static readonly MIN = new SnapshotVersion(new Timestamp(0, 0));
26-
2725
static fromTimestamp(value: Timestamp): SnapshotVersion {
2826
return new SnapshotVersion(value);
2927
}
3028

31-
static forDeletedDoc(): SnapshotVersion {
32-
return SnapshotVersion.MIN;
29+
static min(): SnapshotVersion {
30+
return new SnapshotVersion(new Timestamp(0, 0));
3331
}
3432

3533
private constructor(private timestamp: Timestamp) {}

packages/firestore/src/core/sync_engine.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
import { MaybeDocument, NoDocument } from '../model/document';
3434
import { DocumentKey } from '../model/document_key';
3535
import { Mutation } from '../model/mutation';
36-
import { MutationBatchResult, BATCHID_UNKNOWN } from '../model/mutation_batch';
36+
import { BATCHID_UNKNOWN, MutationBatchResult } from '../model/mutation_batch';
3737
import { RemoteEvent, TargetChange } from '../remote/remote_event';
3838
import { RemoteStore } from '../remote/remote_store';
3939
import { RemoteSyncer } from '../remote/remote_syncer';
@@ -52,7 +52,7 @@ import {
5252
} from '../local/shared_client_state_syncer';
5353
import { SortedSet } from '../util/sorted_set';
5454
import { ListenSequence } from './listen_sequence';
55-
import { Query, LimitType } from './query';
55+
import { LimitType, Query } from './query';
5656
import { SnapshotVersion } from './snapshot_version';
5757
import { Target } from './target';
5858
import { TargetIdGenerator } from './target_id_generator';
@@ -504,11 +504,11 @@ export class SyncEngine implements RemoteSyncer {
504504
);
505505
documentUpdates = documentUpdates.insert(
506506
limboKey,
507-
new NoDocument(limboKey, SnapshotVersion.forDeletedDoc())
507+
new NoDocument(limboKey, SnapshotVersion.min())
508508
);
509509
const resolvedLimboDocuments = documentKeySet().add(limboKey);
510510
const event = new RemoteEvent(
511-
SnapshotVersion.MIN,
511+
SnapshotVersion.min(),
512512
/* targetChanges= */ new Map<TargetId, TargetChange>(),
513513
/* targetMismatches= */ new SortedSet<TargetId>(primitiveComparator),
514514
documentUpdates,

packages/firestore/src/core/transaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { ParsedSetData, ParsedUpdateData } from '../api/user_data_reader';
1919
import { documentVersionMap } from '../model/collections';
20-
import { Document, NoDocument, MaybeDocument } from '../model/document';
20+
import { Document, MaybeDocument, NoDocument } from '../model/document';
2121

2222
import { DocumentKey } from '../model/document_key';
2323
import {
@@ -27,7 +27,7 @@ import {
2727
VerifyMutation
2828
} from '../model/mutation';
2929
import { Datastore } from '../remote/datastore';
30-
import { fail, debugAssert } from '../util/assert';
30+
import { debugAssert, fail } from '../util/assert';
3131
import { Code, FirestoreError } from '../util/error';
3232
import { SnapshotVersion } from './snapshot_version';
3333

@@ -123,7 +123,7 @@ export class Transaction {
123123
docVersion = doc.version;
124124
} else if (doc instanceof NoDocument) {
125125
// For deleted docs, we must use baseVersion 0 when we overwrite them.
126-
docVersion = SnapshotVersion.forDeletedDoc();
126+
docVersion = SnapshotVersion.min();
127127
} else {
128128
throw fail('Document in a transaction was a ' + doc.constructor.name);
129129
}
@@ -151,7 +151,7 @@ export class Transaction {
151151
if (!this.writtenDocs.has(key) && version) {
152152
return Precondition.updateTime(version);
153153
} else {
154-
return Precondition.NONE;
154+
return Precondition.none();
155155
}
156156
}
157157

@@ -163,7 +163,7 @@ export class Transaction {
163163
// The first time a document is written, we want to take into account the
164164
// read time and existence
165165
if (!this.writtenDocs.has(key) && version) {
166-
if (version.isEqual(SnapshotVersion.forDeletedDoc())) {
166+
if (version.isEqual(SnapshotVersion.min())) {
167167
// The document doesn't exist, so fail the transaction.
168168

169169
// This has to be validated locally because you can't send a

packages/firestore/src/local/index_free_query_engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class IndexFreeQueryEngine implements QueryEngine {
7878

7979
// Queries that have never seen a snapshot without limbo free documents
8080
// should also be run as a full collection scan.
81-
if (lastLimboFreeSnapshotVersion.isEqual(SnapshotVersion.MIN)) {
81+
if (lastLimboFreeSnapshotVersion.isEqual(SnapshotVersion.min())) {
8282
return this.executeFullCollectionScan(transaction, query);
8383
}
8484

@@ -204,7 +204,7 @@ export class IndexFreeQueryEngine implements QueryEngine {
204204
return this.localDocumentsView!.getDocumentsMatchingQuery(
205205
transaction,
206206
query,
207-
SnapshotVersion.MIN
207+
SnapshotVersion.min()
208208
);
209209
}
210210
}

0 commit comments

Comments
 (0)