-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(firestore): add VectorValue type and vector() API #8739
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
+308
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright (c) 2016-present Invertase Limited & Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this library except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| const COLLECTION = 'firestore'; | ||
|
|
||
| describe('firestore.VectorValue', function () { | ||
| describe('modular', function () { | ||
| function ref(id) { | ||
| const { doc, getFirestore } = firestoreModular; | ||
| return doc(getFirestore(), `${COLLECTION}/vector_${id}`); | ||
| } | ||
|
|
||
| it('writes and reads a vector', async function () { | ||
| const { setDoc, getDoc, vector } = firestoreModular; | ||
|
|
||
| const r = ref('basic'); | ||
| await setDoc(r, { embedding: vector([0.12, 0.34, 0.56]) }); | ||
|
|
||
| const snap = await getDoc(r); | ||
| const v = snap.get('embedding'); | ||
| should.exist(v); | ||
| v.toArray().should.eql([0.12, 0.34, 0.56]); | ||
| }); | ||
|
|
||
| it('supports vectors in nested structures', async function () { | ||
| const { setDoc, getDoc, vector } = firestoreModular; | ||
|
|
||
| const r = ref('nested'); | ||
| await setDoc(r, { | ||
| a: { b: vector([1, 2, 3]) }, | ||
| }); | ||
|
|
||
| const snap = await getDoc(r); | ||
| snap.get('a').b.toArray().should.eql([1, 2, 3]); | ||
| }); | ||
|
|
||
| it('updates a vector field', async function () { | ||
| const { setDoc, getDoc, updateDoc, vector } = firestoreModular; | ||
|
|
||
| const r = ref('update'); | ||
| await setDoc(r, { x: 1 }); | ||
| await updateDoc(r, { embedding: vector([9, 8, 7]) }); | ||
|
|
||
| const snap = await getDoc(r); | ||
| snap.get('embedding').toArray().should.eql([9, 8, 7]); | ||
| }); | ||
|
|
||
| it('batch writes a vector', async function () { | ||
| const { getFirestore, writeBatch, getDoc, vector } = firestoreModular; | ||
| const r = ref('batch'); | ||
| const b = writeBatch(getFirestore()); | ||
| b.set(r, { embedding: vector([0.1, 0.2]) }); | ||
| await b.commit(); | ||
|
|
||
| const snap = await getDoc(r); | ||
| snap.get('embedding').toArray().should.eql([0.1, 0.2]); | ||
| }); | ||
|
|
||
| it('transaction writes a vector', async function () { | ||
| const { getFirestore, runTransaction, getDoc, vector } = firestoreModular; | ||
| const r = ref('transaction'); | ||
| await runTransaction(getFirestore(), async tx => { | ||
| tx.set(r, { embedding: vector([3.14, 2.72]) }); | ||
| }); | ||
|
|
||
| const snap = await getDoc(r); | ||
| snap.get('embedding').toArray().should.eql([3.14, 2.72]); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright (c) 2016-present Invertase Limited & Contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this library except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| import { isArray, isNumber } from '@react-native-firebase/app/lib/common'; | ||
|
|
||
| export default class FirestoreVectorValue { | ||
| constructor(values) { | ||
| if (values === undefined) { | ||
| this._values = []; | ||
| return; | ||
| } | ||
|
|
||
| if (!isArray(values)) { | ||
| throw new Error( | ||
| "firebase.firestore.VectorValue(values?) 'values' expected an array of numbers or undefined.", | ||
| ); | ||
| } | ||
|
|
||
| for (let i = 0; i < values.length; i++) { | ||
| const v = values[i]; | ||
| if (!isNumber(v)) { | ||
| throw new Error( | ||
| `firebase.firestore.VectorValue(values?) 'values[${i}]' expected a number value.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Store a shallow copy to ensure immutability semantics for the input array | ||
| this._values = values.slice(); | ||
| } | ||
|
|
||
| static fromJSON(json) { | ||
| parsedVector = JSON.parse(json); | ||
| return new FirestoreVectorValue(parsedVector.vectorValues); | ||
| } | ||
|
|
||
| isEqual(other) { | ||
| if (!(other instanceof FirestoreVectorValue)) { | ||
| throw new Error( | ||
| "firebase.firestore.VectorValue.isEqual(*) 'other' expected a VectorValue instance.", | ||
| ); | ||
| } | ||
|
|
||
| const a = this._values; | ||
| const b = other._values; | ||
| if (a.length !== b.length) return false; | ||
| for (let i = 0; i < a.length; i++) { | ||
| // Use strict equality; Firestore numbers allow NaN/Infinity – equality semantics match JS | ||
| if (a[i] !== b[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| toArray() { | ||
| return this._values.slice(); | ||
| } | ||
|
|
||
| toJSON() { | ||
| return { vectorValues: this._values.slice() }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * Represents a vector type in Firestore documents. Create an instance with vector(). | ||
| */ | ||
| export declare class VectorValue { | ||
| // Note the values array and constructor are not public APIs. | ||
|
|
||
| /** | ||
| * Builds a VectorValue instance from a JSON object created by VectorValue.toJSON(). | ||
| * | ||
| * @param json a JSON object represention of a VectorValue instance. | ||
| */ | ||
| static fromJSON(json: object): VectorValue; | ||
|
|
||
| /** | ||
| * Returns true if the two VectorValue values have the same raw number arrays, returns false otherwise. | ||
| */ | ||
| isEqual(other: VectorValue): boolean; | ||
|
|
||
| /** | ||
| * Returns a copy of the raw number array form of the vector. | ||
| */ | ||
| toArray(): number[]; | ||
|
|
||
| /** | ||
| * Returns a JSON-serializable representation of this VectorValue instance. | ||
| */ | ||
| toJSON(): { values: number[] }; | ||
| } | ||
|
|
||
| export declare function vector(values?: number[]): VectorValue; | ||
mikehardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import FirestoreVectorValue from '../FirestoreVectorValue'; | ||
|
|
||
| export const VectorValue = FirestoreVectorValue; | ||
|
|
||
| /** | ||
| * @param {number[]=} values | ||
| * @returns {VectorValue} | ||
| */ | ||
| export function vector(values) { | ||
mikehardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new VectorValue(values); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.