Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

[Draft] Implement vector field type#1899

Closed
wu-hui wants to merge 9 commits intomainfrom
wuandy/VectorType
Closed

[Draft] Implement vector field type#1899
wu-hui wants to merge 9 commits intomainfrom
wuandy/VectorType

Conversation

@wu-hui
Copy link
Copy Markdown
Contributor

@wu-hui wu-hui commented Sep 15, 2023

No description provided.

@wu-hui wu-hui requested review from a team September 15, 2023 14:22
@conventional-commit-lint-gcf
Copy link
Copy Markdown

conventional-commit-lint-gcf Bot commented Sep 15, 2023

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@product-auto-label product-auto-label Bot added size: m Pull request size is medium. api: firestore Issues related to the googleapis/nodejs-firestore API. labels Sep 15, 2023
gcf-owl-bot Bot added a commit that referenced this pull request Nov 28, 2023
Revert "chore: remove compile protos from post processor (#1899)"

This reverts commit 73248044166b6ba2dd102862f8cd2c4829e868db.

Source-Link: googleapis/synthtool@2f0fea6
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:37f2f91dea317c75ebf4e19880aa8f10b2228b8d07859c0f384dbcf660735ba2
@product-auto-label product-auto-label Bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Dec 5, 2023
gcf-merge-on-green Bot pushed a commit that referenced this pull request Jan 16, 2024
Revert "chore: remove compile protos from post processor (#1899)"

This reverts commit 73248044166b6ba2dd102862f8cd2c4829e868db.

Source-Link: https://togithub.com/googleapis/synthtool/commit/2f0fea69b6ae56dba965ea8817750793862b3b2d
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:37f2f91dea317c75ebf4e19880aa8f10b2228b8d07859c0f384dbcf660735ba2
Copy link
Copy Markdown
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

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

LGTM with suggestions

Comment thread dev/src/field-value.ts Outdated
}

public toArray(): number[] {
return this.values;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider returning a copy of the internal data so the values array cannot be modified.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

This is usually a good suggestion, I am wondering if it is as good with vectors, given the size and the likelihood of people modifying vectors.

Nevertheless, I am OK with making copies for now, and later optimize.

Comment thread dev/src/field-value.ts Outdated
export class VectorValue implements firestore.VectorValue {
private readonly values: number[];
constructor(values: number[] | undefined) {
this.values = values || [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider making a copy of the argument so the internal values array cannot be modified.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/src/index.ts Outdated
'client',
'init',
`${JSON.stringify((client as any)._opts.servicePath)}`
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it intentional to leave this logger statement here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like something you may have used for debugging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah. Deleted.

Comment thread dev/src/serializer.ts Outdated

const RESERVED_MAP_KEY = '__type__';
const RESERVED_MAP_KEY_VECTOR_VALUE = '__vector__';
const RESERVED_VECTOR_MAP_VECTORS_KEY = 'value';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: "RESERVED_VECTOR_MAP_VECTORS_KEY" may not need the "RESERVED_" prefix since the value is not surrounded with "__"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/src/field-value.ts Outdated
Comment on lines +47 to +54
return serializer.encodeVector({
arrayValue: {
values: this.values.map(value => {
return {
doubleValue: value,
};
}),
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: it's a little challenging to read this proto serialization since it is spread across this fie and the Serializer. Would it be cleaner to move the array serialization into encodeVector so it's all in one place?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or move the whole implementation here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/system-test/firestore.ts Outdated

const version = require('../../package.json').version;

setLogFunction(console.log);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not sure if this statement is in the PR intentionally

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another debug line. Deleted.

Comment thread dev/system-test/firestore.ts Outdated
internalSettings.databaseId = process.env.FIRESTORE_NAMED_DATABASE;
}

settings.host = 'test-firestore.sandbox.googleapis.com';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FWIW. You can also set environment variable FIRESTORE_TARGET_BACKEND=nightly to run against nightly. I set up multiple IntelliJ run configs, so easily switch back and forth.

I think this statement needs to be removed before committing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deleted.

Comment thread dev/src/field-value.ts Outdated
import api = proto.google.firestore.v1;

export class VectorValue implements firestore.VectorValue {
private readonly values: number[];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider prefixing the variable with underscore to discourage use by JS developers

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.


const snap1 = await ref.get();
expect(snap1.get('vectorEmpty')).to.deep.equal(FieldValue.vector());
expect(snap1.get('vector1')).to.deep.equal(FieldValue.vector([1, 2, 3.99]));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider using VectorValue.isEqual instead of to.deep.equal because to.deep.equal relies on equality testing using private members.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I want to change this in the other PR to save myself from conflict resolving, hopefully that is OK with you.

Copy link
Copy Markdown
Contributor Author

@wu-hui wu-hui left a comment

Choose a reason for hiding this comment

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

Thank for the review!

Comment thread dev/src/field-value.ts Outdated
import api = proto.google.firestore.v1;

export class VectorValue implements firestore.VectorValue {
private readonly values: number[];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/src/field-value.ts Outdated
export class VectorValue implements firestore.VectorValue {
private readonly values: number[];
constructor(values: number[] | undefined) {
this.values = values || [];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/src/field-value.ts Outdated
}

public toArray(): number[] {
return this.values;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

This is usually a good suggestion, I am wondering if it is as good with vectors, given the size and the likelihood of people modifying vectors.

Nevertheless, I am OK with making copies for now, and later optimize.

Comment thread dev/src/field-value.ts Outdated
Comment on lines +47 to +54
return serializer.encodeVector({
arrayValue: {
values: this.values.map(value => {
return {
doubleValue: value,
};
}),
},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/src/index.ts Outdated
'client',
'init',
`${JSON.stringify((client as any)._opts.servicePath)}`
);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah. Deleted.

Comment thread dev/src/serializer.ts Outdated

const RESERVED_MAP_KEY = '__type__';
const RESERVED_MAP_KEY_VECTOR_VALUE = '__vector__';
const RESERVED_VECTOR_MAP_VECTORS_KEY = 'value';
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread dev/system-test/firestore.ts Outdated

const version = require('../../package.json').version;

setLogFunction(console.log);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another debug line. Deleted.

Comment thread dev/system-test/firestore.ts Outdated
internalSettings.databaseId = process.env.FIRESTORE_NAMED_DATABASE;
}

settings.host = 'test-firestore.sandbox.googleapis.com';
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deleted.


const snap1 = await ref.get();
expect(snap1.get('vectorEmpty')).to.deep.equal(FieldValue.vector());
expect(snap1.get('vector1')).to.deep.equal(FieldValue.vector([1, 2, 3.99]));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I want to change this in the other PR to save myself from conflict resolving, hopefully that is OK with you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: firestore Issues related to the googleapis/nodejs-firestore API. size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants