Skip to content

Don't deserialize full Document proto for Query execution #2115

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 7 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 28 additions & 27 deletions packages/firestore/src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export class Document extends MaybeDocument {
private readonly converter?: (value: api.Value) => FieldValue
) {
super(key, version);
assert(
this.objectValue !== undefined ||
(this.proto !== undefined && this.converter !== undefined),
'If objectValue is not defined, proto and converter need to be set.'
);

this.hasLocalMutations = !!options.hasLocalMutations;
this.hasCommittedMutations = !!options.hasCommittedMutations;
}
Expand All @@ -83,11 +89,6 @@ export class Document extends MaybeDocument {
if (this.objectValue) {
return this.objectValue.field(path);
} else {
assert(
this.proto !== undefined && this.converter !== undefined,
'Expected proto and converter to be defined.'
);

if (!this.fieldValueCache) {
// TODO(b/136090445): Remove the cache when `getField` is no longer
// called during Query ordering.
Expand All @@ -103,42 +104,21 @@ export class Document extends MaybeDocument {
// deserialize the value at the requested field path. This speeds up
// Query execution as query filters can discard documents based on a
// single field.
let protoValue: api.Value | undefined = this.proto!.fields[
path.firstSegment()
];
for (let i = 1; protoValue !== undefined && i < path.length; ++i) {
if (protoValue.mapValue) {
protoValue = protoValue.mapValue.fields[path.get(i)];
} else {
protoValue = undefined;
}
}

const protoValue = this.getProtoField(path);
if (protoValue === undefined) {
fieldValue = null;
} else {
fieldValue = this.converter!(protoValue);
}

this.fieldValueCache.set(canonicalPath, fieldValue);
}

return fieldValue!;
}
}

fieldValue(path: FieldPath): unknown {
const field = this.field(path);
return field ? field.value() : undefined;
}

data(): ObjectValue {
if (!this.objectValue) {
assert(
this.proto !== undefined && this.converter !== undefined,
'Expected proto and converter to be defined.'
);

let result = ObjectValue.EMPTY;
obj.forEach(this.proto!.fields, (key: string, value: api.Value) => {
result = result.set(new FieldPath([key]), this.converter!(value));
Expand Down Expand Up @@ -180,6 +160,27 @@ export class Document extends MaybeDocument {
return this.hasLocalMutations || this.hasCommittedMutations;
}

/**
* Returns a the nested Protobuf value for 'path`. Can only be called if
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: s/a the/the/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

* `proto` was provided at construction time.
*/
private getProtoField(path: FieldPath): api.Value | undefined {
assert(
this.proto !== undefined,
"Can only call getProtoField() when proto is defined"
);
let protoValue: api.Value | undefined = this.proto!.fields[
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: add an empty line after assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

path.firstSegment()
];
for (let i = 1; i < path.length; ++i) {
if (!protoValue || !protoValue.mapValue) {
return undefined;
}
protoValue = protoValue.mapValue.fields[path.get(i)];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: add an empty line right after the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return protoValue;
}

static compareByField(field: FieldPath, d1: Document, d2: Document): number {
const v1 = d1.field(field);
const v2 = d2.field(field);
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/test/unit/model/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ describe('Document', () => {
};
const document = doc('rooms/Eros', 1, data, { hasLocalMutations: true });

expect(document.fieldValue(field('desc'))).to.deep.equal(
expect(document.field(field('desc'))!.value()).to.deep.equal(
'Discuss all the project related stuff'
);
expect(document.fieldValue(field('owner.title'))).to.deep.equal(
expect(document.field(field('owner.title'))!.value()).to.deep.equal(
'scallywag'
);
expect(document.hasLocalMutations).to.equal(true);
Expand Down