Skip to content

Replace usage of transform with updateTransforms #4153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-birds-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': minor
---

Field tranforms performed within a write no longer count as additional operations. However, a field transform on its own still counts as an operation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FieldTransform is not a concept we expose to users. How about:

"A write to a document that contains FieldValue sentinels is no longer split up into two separate operations. This reduces the number of writes the backend performs"

We could maybe also mention how this affects WriteBatch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference docs refer to transforms as: "Within a write operation, field transforms like ... each count as an additional operation" (link). I also wanted to clarify that multiple field transforms also count as a single write. Changed to reflect your wording.

I did consider mentioning how this might affect WriteBatch's 500-write limit, but then I realized that considering we're doing all this work with BulkWriter to mitigate backend pressure from big batches, why encourage bigger batches to begin with? WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most users access Firestore through our SDK. They are not aware of the terminology that is used in the Proto, and we should use the SDK terms in our release notes (at least in addition to the Proto terms).

BulkWriter will not come to the Mobile SDKs. WriteBatches on Mobile are meant for atomic commits and I don't expect that people will use them for large bulk insertions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining. I changed it to use your wording and added a bit about WriteBatch limits.

15 changes: 6 additions & 9 deletions packages/firestore/exp/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ export function setDoc<T>(
options
);

const mutations = parsed.toMutations(reference._key, Precondition.none());
return executeWrite(firestore, mutations);
const mutation = parsed.toMutation(reference._key, Precondition.none());
return executeWrite(firestore, [mutation]);
}

/**
Expand Down Expand Up @@ -394,11 +394,8 @@ export function updateDoc(
);
}

const mutations = parsed.toMutations(
reference._key,
Precondition.exists(true)
);
return executeWrite(firestore, mutations);
const mutation = parsed.toMutation(reference._key, Precondition.exists(true));
return executeWrite(firestore, [mutation]);
}

/**
Expand Down Expand Up @@ -448,8 +445,8 @@ export function addDoc<T>(
{}
);

const mutations = parsed.toMutations(docRef._key, Precondition.exists(false));
return executeWrite(firestore, mutations).then(() => docRef);
const mutation = parsed.toMutation(docRef._key, Precondition.exists(false));
return executeWrite(firestore, [mutation]).then(() => docRef);
}

// TODO(firestorexp): Make sure these overloads are tested via the Firestore
Expand Down
12 changes: 6 additions & 6 deletions packages/firestore/exp/src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ export interface DocumentChange<T = DocumentData> {
* access will return 'undefined'. You can use the `exists()` method to
* explicitly verify a document's existence.
*/
export class DocumentSnapshot<
T = DocumentData
> extends LiteDocumentSnapshot<T> {
export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<
T
> {
private readonly _firestoreImpl: FirebaseFirestore;

/**
Expand Down Expand Up @@ -291,9 +291,9 @@ export class DocumentSnapshot<
* `exists` property will always be true and `data()` will never return
* 'undefined'.
*/
export class QueryDocumentSnapshot<
T = DocumentData
> extends DocumentSnapshot<T> {
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
/**
* Retrieves all fields in the document as an `Object`.
*
Expand Down
21 changes: 9 additions & 12 deletions packages/firestore/lite/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,10 +1478,9 @@ export function setDoc<T>(
);

const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(
datastore,
parsed.toMutations(reference._key, Precondition.none())
);
return invokeCommitRpc(datastore, [
parsed.toMutation(reference._key, Precondition.none())
]);
}

/**
Expand Down Expand Up @@ -1569,10 +1568,9 @@ export function updateDoc(
}

const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(
datastore,
parsed.toMutations(reference._key, Precondition.exists(true))
);
return invokeCommitRpc(datastore, [
parsed.toMutation(reference._key, Precondition.exists(true))
]);
}

/**
Expand Down Expand Up @@ -1634,10 +1632,9 @@ export function addDoc<T>(
);

const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(
datastore,
parsed.toMutations(docRef._key, Precondition.exists(false))
).then(() => docRef);
return invokeCommitRpc(datastore, [
parsed.toMutation(docRef._key, Precondition.exists(false))
]).then(() => docRef);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/lite/src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ export class DocumentSnapshot<T = DocumentData> {
* `exists` property will always be true and `data()` will never return
* 'undefined'.
*/
export class QueryDocumentSnapshot<
T = DocumentData
> extends DocumentSnapshot<T> {
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
/**
* Retrieves all fields in the document as an `Object`.
*
Expand Down
8 changes: 3 additions & 5 deletions packages/firestore/lite/src/api/write_batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ export class WriteBatch {
ref._converter !== null,
options
);
this._mutations = this._mutations.concat(
parsed.toMutations(ref._key, Precondition.none())
);
this._mutations.push(parsed.toMutation(ref._key, Precondition.none()));
return this;
}

Expand Down Expand Up @@ -186,8 +184,8 @@ export class WriteBatch {
);
}

this._mutations = this._mutations.concat(
parsed.toMutations(ref._key, Precondition.exists(true))
this._mutations.push(
parsed.toMutation(ref._key, Precondition.exists(true))
);
return this;
}
Expand Down
49 changes: 26 additions & 23 deletions packages/firestore/src/api/user_data_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import {
DocumentData,
SetOptions,
FieldPath as PublicFieldPath
FieldPath as PublicFieldPath,
SetOptions
} from '@firebase/firestore-types';

import {
Value as ProtoValue,
MapValue as ProtoMapValue
MapValue as ProtoMapValue,
Value as ProtoValue
} from '../protos/firestore_proto_api';
import { Timestamp } from './timestamp';
import { DatabaseId } from '../core/database_info';
Expand All @@ -34,8 +34,7 @@ import {
Mutation,
PatchMutation,
Precondition,
SetMutation,
TransformMutation
SetMutation
} from '../model/mutation';
import { FieldPath as InternalFieldPath } from '../model/path';
import { debugAssert, fail } from '../util/assert';
Expand Down Expand Up @@ -79,19 +78,23 @@ export class ParsedSetData {
readonly fieldTransforms: FieldTransform[]
) {}

toMutations(key: DocumentKey, precondition: Precondition): Mutation[] {
const mutations = [] as Mutation[];
toMutation(key: DocumentKey, precondition: Precondition): Mutation {
if (this.fieldMask !== null) {
mutations.push(
new PatchMutation(key, this.data, this.fieldMask, precondition)
return new PatchMutation(
key,
this.data,
this.fieldMask,
precondition,
this.fieldTransforms
);
} else {
mutations.push(new SetMutation(key, this.data, precondition));
}
if (this.fieldTransforms.length > 0) {
mutations.push(new TransformMutation(key, this.fieldTransforms));
return new SetMutation(
key,
this.data,
precondition,
this.fieldTransforms
);
}
return mutations;
}
}

Expand All @@ -103,14 +106,14 @@ export class ParsedUpdateData {
readonly fieldTransforms: FieldTransform[]
) {}

toMutations(key: DocumentKey, precondition: Precondition): Mutation[] {
const mutations = [
new PatchMutation(key, this.data, this.fieldMask, precondition)
] as Mutation[];
if (this.fieldTransforms.length > 0) {
mutations.push(new TransformMutation(key, this.fieldTransforms));
}
return mutations;
toMutation(key: DocumentKey, precondition: Precondition): Mutation {
return new PatchMutation(
key,
this.data,
this.fieldMask,
precondition,
this.fieldTransforms
);
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/firestore/src/core/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ export class Transaction {
}

set(key: DocumentKey, data: ParsedSetData): void {
this.write(data.toMutations(key, this.precondition(key)));
this.write(data.toMutation(key, this.precondition(key)));
this.writtenDocs.add(key.toString());
}

update(key: DocumentKey, data: ParsedUpdateData): void {
try {
this.write(data.toMutations(key, this.preconditionForUpdate(key)));
this.write(data.toMutation(key, this.preconditionForUpdate(key)));
} catch (e) {
this.lastWriteError = e;
}
this.writtenDocs.add(key.toString());
}

delete(key: DocumentKey): void {
this.write([new DeleteMutation(key, this.precondition(key))]);
this.write(new DeleteMutation(key, this.precondition(key)));
this.writtenDocs.add(key.toString());
}

Expand Down Expand Up @@ -193,9 +193,9 @@ export class Transaction {
}
}

private write(mutations: Mutation[]): void {
private write(mutation: Mutation): void {
this.ensureCommitNotCalled();
this.mutations = this.mutations.concat(mutations);
this.mutations.push(mutation);
}

private ensureCommitNotCalled(): void {
Expand Down
24 changes: 24 additions & 0 deletions packages/firestore/src/local/local_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ export function fromDbMutationBatch(
const baseMutations = (dbBatch.baseMutations || []).map(m =>
fromMutation(localSerializer.remoteSerializer, m)
);

// Squash old transform mutations into existing patch or set mutations.
// The replacement of representing `transforms` with `update_transforms`
// on the SDK means that old `transform` mutations stored in IndexedDB need
// to be updated to `update_transforms`.
// TODO(b/174608374): Remove this code once we perform a schema migration.
let i = dbBatch.mutations.length - 1;
while (i >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be easier to parse as a for loop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

const mutationProto = dbBatch.mutations[i];
if (mutationProto?.transform !== undefined) {
debugAssert(
i >= 1 &&
dbBatch.mutations[i - 1].transform === undefined &&
dbBatch.mutations[i - 1].update !== undefined,
'TransformMutation should be preceded by a patch or set mutation'
);
const mutationToJoin = dbBatch.mutations[i - 1];
mutationToJoin.updateTransforms = mutationProto.transform.fieldTransforms;
dbBatch.mutations.splice(i, 1);
--i;
}
--i;
}

const mutations = dbBatch.mutations.map(m =>
fromMutation(localSerializer.remoteSerializer, m)
);
Expand Down
Loading