Skip to content

Rework FieldMask to use a SortedSet of FieldPaths #1405

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 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions packages/firestore/src/api/user_data_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class UserDataConverter {
let fieldTransforms: FieldTransform[];

if (!fieldPaths) {
fieldMask = new FieldMask(context.fieldMask);
fieldMask = FieldMask.fromArray(context.fieldMask);
fieldTransforms = context.fieldTransforms;
} else {
const validatedFieldPaths: FieldPath[] = [];
Expand Down Expand Up @@ -367,7 +367,7 @@ export class UserDataConverter {
validatedFieldPaths.push(fieldPath);
}

fieldMask = new FieldMask(validatedFieldPaths);
fieldMask = FieldMask.fromArray(validatedFieldPaths);
fieldTransforms = context.fieldTransforms.filter(transform =>
fieldMask.covers(transform.field)
);
Expand Down Expand Up @@ -407,7 +407,7 @@ export class UserDataConverter {
}
});

const mask = new FieldMask(fieldMaskPaths);
const mask = FieldMask.fromArray(fieldMaskPaths);
return new ParsedUpdateData(updateData, mask, context.fieldTransforms);
}

Expand Down Expand Up @@ -462,7 +462,7 @@ export class UserDataConverter {
}
}

const mask = new FieldMask(fieldMaskPaths);
const mask = FieldMask.fromArray(fieldMaskPaths);
return new ParsedUpdateData(updateData, mask, context.fieldTransforms);
}

Expand Down
25 changes: 16 additions & 9 deletions packages/firestore/src/model/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Timestamp } from '../api/timestamp';
import { SnapshotVersion } from '../core/snapshot_version';
import { assert } from '../util/assert';
import * as misc from '../util/misc';
import { SortedSet } from '../util/sorted_set';

import {
Document,
Expand All @@ -41,28 +42,34 @@ import { TransformOperation } from './transform_operation';
* containing foo
*/
export class FieldMask {
constructor(readonly fields: FieldPath[]) {
private constructor(readonly fields: SortedSet<FieldPath>) {
// TODO(dimond): validation of FieldMask
}

static fromArray(fields: FieldPath[]): FieldMask {
let fieldsAsSet = new SortedSet<FieldPath>(FieldPath.comparator);
fields.forEach(fieldPath => (fieldsAsSet = fieldsAsSet.add(fieldPath)));
return new FieldMask(fieldsAsSet);
}

/**
* Verifies that `fieldPath` is included by at least one field in this field
* mask.
*
* This is an O(n) operation, where `n` is the size of the field mask.
*/
covers(fieldPath: FieldPath): boolean {
for (const fieldMaskPath of this.fields) {
let found = false;
this.fields.forEach(fieldMaskPath => {
if (fieldMaskPath.isPrefixOf(fieldPath)) {
return true;
found = true;
}
}

return false;
});
return found;
}

isEqual(other: FieldMask): boolean {
return misc.arrayEquals(this.fields, other.fields);
return this.fields.isEqual(other.fields);
}
}

Expand Down Expand Up @@ -448,7 +455,7 @@ export class PatchMutation extends Mutation {
}

private patchObject(data: ObjectValue): ObjectValue {
for (const fieldPath of this.fieldMask.fields) {
this.fieldMask.fields.forEach(fieldPath => {
if (!fieldPath.isEmpty()) {
const newValue = this.data.field(fieldPath);
if (newValue !== undefined) {
Expand All @@ -457,7 +464,7 @@ export class PatchMutation extends Mutation {
data = data.delete(fieldPath);
}
}
}
});
return data;
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/firestore/src/remote/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1304,15 +1304,19 @@ export class JsonProtoSerializer {
}

toDocumentMask(fieldMask: FieldMask): api.DocumentMask {
const canonicalFields: string[] = [];
fieldMask.fields.forEach(field =>
canonicalFields.push(field.canonicalString())
);
return {
fieldPaths: fieldMask.fields.map(field => field.canonicalString())
fieldPaths: canonicalFields
};
}

fromDocumentMask(proto: api.DocumentMask): FieldMask {
const paths = proto.fieldPaths || [];
const fields = paths.map(path => FieldPath.fromServerFormat(path));
return new FieldMask(fields);
return FieldMask.fromArray(fields);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/test/unit/remote/node/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ describe('Serializer', () => {
// tslint:disable-next-line:ban TODO(b/34988481): Implement correct escaping
it.skip('converts a weird path', () => {
const expected: api.DocumentMask = { fieldPaths: ['foo.`bar.baz\\qux`'] };
const mask = new FieldMask([
const mask = FieldMask.fromArray([
FieldPath.fromServerFormat('foo.bar\\.baz\\\\qux')
]);
const actual = s.toDocumentMask(mask);
Expand All @@ -618,7 +618,7 @@ describe('Serializer', () => {

// tslint:disable-next-line:ban TODO(b/34988481): Implement correct escaping
it.skip('converts a weird path', () => {
const expected = new FieldMask([
const expected = FieldMask.fromArray([
FieldPath.fromServerFormat('foo.bar\\.baz\\\\qux')
]);
const proto: api.DocumentMask = { fieldPaths: ['foo.`bar.baz\\qux`'] };
Expand Down