Skip to content

Commit e6b8525

Browse files
Vector Type (#8215)
Implement VectorValue type support.
1 parent 1601572 commit e6b8525

25 files changed

+792
-572
lines changed

.changeset/nice-eyes-tan.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@firebase/firestore": minor
3+
"firebase": minor
4+
---
5+
6+
Add support for reading and writing Firestore vectors.

common/api-review/firestore-lite.api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,16 @@ export function updateDoc<AppModelType, DbModelType extends DocumentData>(refere
460460
// @public
461461
export function updateDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;
462462

463+
// @public
464+
export function vector(values?: number[]): VectorValue;
465+
466+
// @public
467+
export class VectorValue {
468+
/* Excluded from this release type: __constructor */
469+
isEqual(other: VectorValue): boolean;
470+
toArray(): number[];
471+
}
472+
463473
// @public
464474
export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: unknown): QueryFieldFilterConstraint;
465475

common/api-review/firestore.api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,16 @@ export function updateDoc<AppModelType, DbModelType extends DocumentData>(refere
745745
// @public
746746
export function updateDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;
747747

748+
// @public
749+
export function vector(values?: number[]): VectorValue;
750+
751+
// @public
752+
export class VectorValue {
753+
/* Excluded from this release type: __constructor */
754+
isEqual(other: VectorValue): boolean;
755+
toArray(): number[];
756+
}
757+
748758
// @public
749759
export function waitForPendingWrites(firestore: Firestore): Promise<void>;
750760

docs-devsite/_toc.yaml

Lines changed: 0 additions & 555 deletions
This file was deleted.

docs-devsite/firestore_.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ https://github.com/firebase/firebase-js-sdk
124124
| [endBefore(snapshot)](./firestore_.md#endbefore_9a4477f) | Creates a [QueryEndAtConstraint](./firestore_.queryendatconstraint.md#queryendatconstraint_class) that modifies the result set to end before the provided document (exclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of the query. |
125125
| [startAfter(snapshot)](./firestore_.md#startafter_9a4477f) | Creates a [QueryStartAtConstraint](./firestore_.querystartatconstraint.md#querystartatconstraint_class) that modifies the result set to start after the provided document (exclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of the query. |
126126
| [startAt(snapshot)](./firestore_.md#startat_9a4477f) | Creates a [QueryStartAtConstraint](./firestore_.querystartatconstraint.md#querystartatconstraint_class) that modifies the result set to start at the provided document (inclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the <code>orderBy</code> of this query. |
127+
| <b>function(values, ...)</b> |
128+
| [vector(values)](./firestore_.md#vector_0dbdaf2) | Creates a new <code>VectorValue</code> constructed with a copy of the given array of numbers. |
127129

128130
## Classes
129131

@@ -155,6 +157,7 @@ https://github.com/firebase/firebase-js-sdk
155157
| [SnapshotMetadata](./firestore_.snapshotmetadata.md#snapshotmetadata_class) | Metadata about a snapshot, describing the state of the snapshot. |
156158
| [Timestamp](./firestore_.timestamp.md#timestamp_class) | A <code>Timestamp</code> represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time.<!-- -->It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.<!-- -->For examples and further specifications, refer to the [Timestamp definition](https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto)<!-- -->. |
157159
| [Transaction](./firestore_.transaction.md#transaction_class) | A reference to a transaction.<!-- -->The <code>Transaction</code> object passed to a transaction's <code>updateFunction</code> provides the methods to read and write data within the transaction context. See [runTransaction()](./firestore_.md#runtransaction_6f03ec4)<!-- -->. |
160+
| [VectorValue](./firestore_.vectorvalue.md#vectorvalue_class) | Represents a vector type in Firestore documents. Create an instance with . VectorValue |
158161
| [WriteBatch](./firestore_.writebatch.md#writebatch_class) | A write batch, used to perform multiple writes as a single atomic unit.<!-- -->A <code>WriteBatch</code> object can be acquired by calling [writeBatch()](./firestore_.md#writebatch_231a8e0)<!-- -->. It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) until [WriteBatch.commit()](./firestore_.writebatch.md#writebatchcommit) is called. |
159162

160163
## Interfaces
@@ -2452,6 +2455,30 @@ export declare function startAt<AppModelType, DbModelType extends DocumentData>(
24522455

24532456
A [QueryStartAtConstraint](./firestore_.querystartatconstraint.md#querystartatconstraint_class) to pass to `query()`<!-- -->.
24542457

2458+
## function(values, ...)
2459+
2460+
### vector(values) {:#vector_0dbdaf2}
2461+
2462+
Creates a new `VectorValue` constructed with a copy of the given array of numbers.
2463+
2464+
<b>Signature:</b>
2465+
2466+
```typescript
2467+
export declare function vector(values?: number[]): VectorValue;
2468+
```
2469+
2470+
#### Parameters
2471+
2472+
| Parameter | Type | Description |
2473+
| --- | --- | --- |
2474+
| values | number\[\] | Create a <code>VectorValue</code> instance with a copy of this array of numbers. |
2475+
2476+
<b>Returns:</b>
2477+
2478+
[VectorValue](./firestore_.vectorvalue.md#vectorvalue_class)
2479+
2480+
A new `VectorValue` constructed with a copy of the given array of numbers.
2481+
24552482
## CACHE\_SIZE\_UNLIMITED
24562483

24572484
Constant used to indicate the LRU garbage collection should be disabled. Set this value as the `cacheSizeBytes` on the settings passed to the [Firestore](./firestore_.firestore.md#firestore_class) instance.

docs-devsite/firestore_lite.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ https://github.com/firebase/firebase-js-sdk
8989
| [endBefore(snapshot)](./firestore_lite.md#endbefore_9a4477f) | Creates a [QueryEndAtConstraint](./firestore_.queryendatconstraint.md#queryendatconstraint_class) that modifies the result set to end before the provided document (exclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of the query. |
9090
| [startAfter(snapshot)](./firestore_lite.md#startafter_9a4477f) | Creates a [QueryStartAtConstraint](./firestore_.querystartatconstraint.md#querystartatconstraint_class) that modifies the result set to start after the provided document (exclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of the query. |
9191
| [startAt(snapshot)](./firestore_lite.md#startat_9a4477f) | Creates a [QueryStartAtConstraint](./firestore_.querystartatconstraint.md#querystartatconstraint_class) that modifies the result set to start at the provided document (inclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the <code>orderBy</code> of this query. |
92+
| <b>function(values, ...)</b> |
93+
| [vector(values)](./firestore_lite.md#vector_0dbdaf2) | Creates a new <code>VectorValue</code> constructed with a copy of the given array of numbers. |
9294

9395
## Classes
9496

@@ -117,6 +119,7 @@ https://github.com/firebase/firebase-js-sdk
117119
| [QueryStartAtConstraint](./firestore_lite.querystartatconstraint.md#querystartatconstraint_class) | A <code>QueryStartAtConstraint</code> is used to exclude documents from the start of a result set returned by a Firestore query. <code>QueryStartAtConstraint</code>s are created by invoking [startAt()](./firestore_.md#startat_9a4477f) or [startAfter()](./firestore_.md#startafter_9a4477f) and can then be passed to [query()](./firestore_.md#query_9f7b0f4) to create a new query instance that also contains this <code>QueryStartAtConstraint</code>. |
118120
| [Timestamp](./firestore_lite.timestamp.md#timestamp_class) | A <code>Timestamp</code> represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time.<!-- -->It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.<!-- -->For examples and further specifications, refer to the [Timestamp definition](https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto)<!-- -->. |
119121
| [Transaction](./firestore_lite.transaction.md#transaction_class) | A reference to a transaction.<!-- -->The <code>Transaction</code> object passed to a transaction's <code>updateFunction</code> provides the methods to read and write data within the transaction context. See [runTransaction()](./firestore_.md#runtransaction_6f03ec4)<!-- -->. |
122+
| [VectorValue](./firestore_lite.vectorvalue.md#vectorvalue_class) | Represents a vector type in Firestore documents. Create an instance with . VectorValue |
120123
| [WriteBatch](./firestore_lite.writebatch.md#writebatch_class) | A write batch, used to perform multiple writes as a single atomic unit.<!-- -->A <code>WriteBatch</code> object can be acquired by calling [writeBatch()](./firestore_.md#writebatch_231a8e0)<!-- -->. It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) until [WriteBatch.commit()](./firestore_.writebatch.md#writebatchcommit) is called. |
121124

122125
## Interfaces
@@ -1568,6 +1571,30 @@ export declare function startAt<AppModelType, DbModelType extends DocumentData>(
15681571

15691572
A [QueryStartAtConstraint](./firestore_.querystartatconstraint.md#querystartatconstraint_class) to pass to `query()`<!-- -->.
15701573

1574+
## function(values, ...)
1575+
1576+
### vector(values) {:#vector_0dbdaf2}
1577+
1578+
Creates a new `VectorValue` constructed with a copy of the given array of numbers.
1579+
1580+
<b>Signature:</b>
1581+
1582+
```typescript
1583+
export declare function vector(values?: number[]): VectorValue;
1584+
```
1585+
1586+
#### Parameters
1587+
1588+
| Parameter | Type | Description |
1589+
| --- | --- | --- |
1590+
| values | number\[\] | Create a <code>VectorValue</code> instance with a copy of this array of numbers. |
1591+
1592+
<b>Returns:</b>
1593+
1594+
[VectorValue](./firestore_lite.vectorvalue.md#vectorvalue_class)
1595+
1596+
A new `VectorValue` constructed with a copy of the given array of numbers.
1597+
15711598
## AddPrefixToKeys
15721599

15731600
Returns a new map where every key is prefixed with the outer key appended to a dot.

packages/firestore/lite/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export {
127127
arrayRemove,
128128
arrayUnion,
129129
serverTimestamp,
130-
deleteField
130+
deleteField,
131+
vector
131132
} from '../src/lite-api/field_value_impl';
132133

133134
export {
@@ -138,6 +139,8 @@ export {
138139
snapshotEqual
139140
} from '../src/lite-api/snapshot';
140141

142+
export { VectorValue } from '../src/lite-api/vector_value';
143+
141144
export { WriteBatch, writeBatch } from '../src/lite-api/write_batch';
142145

143146
export { TransactionOptions } from '../src/lite-api/transaction_options';

packages/firestore/src/api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,12 @@ export {
172172
arrayUnion,
173173
deleteField,
174174
increment,
175-
serverTimestamp
175+
serverTimestamp,
176+
vector
176177
} from './api/field_value_impl';
177178

179+
export { VectorValue } from './lite-api/vector_value';
180+
178181
export { LogLevelString as LogLevel, setLogLevel } from './util/log';
179182

180183
export { Bytes } from './api/bytes';

packages/firestore/src/api/field_value_impl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export {
2020
arrayRemove,
2121
arrayUnion,
2222
serverTimestamp,
23-
deleteField
23+
deleteField,
24+
vector
2425
} from '../lite-api/field_value_impl';

packages/firestore/src/index/firestore_index_value_writer.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import {
2121
normalizeNumber,
2222
normalizeTimestamp
2323
} from '../model/normalize';
24-
import { isMaxValue } from '../model/values';
24+
import {
25+
isVectorValue,
26+
VECTOR_MAP_VECTORS_KEY,
27+
isMaxValue
28+
} from '../model/values';
2529
import { ArrayValue, MapValue, Value } from '../protos/firestore_proto_api';
2630
import { fail } from '../util/assert';
2731
import { isNegativeZero } from '../util/types';
@@ -41,6 +45,7 @@ const INDEX_TYPE_BLOB = 30;
4145
const INDEX_TYPE_REFERENCE = 37;
4246
const INDEX_TYPE_GEOPOINT = 45;
4347
const INDEX_TYPE_ARRAY = 50;
48+
const INDEX_TYPE_VECTOR = 53;
4449
const INDEX_TYPE_MAP = 55;
4550
const INDEX_TYPE_REFERENCE_SEGMENT = 60;
4651

@@ -121,6 +126,8 @@ export class FirestoreIndexValueWriter {
121126
} else if ('mapValue' in indexValue) {
122127
if (isMaxValue(indexValue)) {
123128
this.writeValueTypeLabel(encoder, Number.MAX_SAFE_INTEGER);
129+
} else if (isVectorValue(indexValue)) {
130+
this.writeIndexVector(indexValue.mapValue!, encoder);
124131
} else {
125132
this.writeIndexMap(indexValue.mapValue!, encoder);
126133
this.writeTruncationMarker(encoder);
@@ -160,6 +167,24 @@ export class FirestoreIndexValueWriter {
160167
}
161168
}
162169

170+
private writeIndexVector(
171+
mapIndexValue: MapValue,
172+
encoder: DirectionalIndexByteEncoder
173+
): void {
174+
const map = mapIndexValue.fields || {};
175+
this.writeValueTypeLabel(encoder, INDEX_TYPE_VECTOR);
176+
177+
// Vectors sort first by length
178+
const key = VECTOR_MAP_VECTORS_KEY;
179+
const length = map[key].arrayValue?.values?.length || 0;
180+
this.writeValueTypeLabel(encoder, INDEX_TYPE_NUMBER);
181+
encoder.writeNumber(normalizeNumber(length));
182+
183+
// Vectors then sort by position value
184+
this.writeIndexString(key, encoder);
185+
this.writeIndexValueAux(map[key], encoder);
186+
}
187+
163188
private writeIndexArray(
164189
arrayIndexValue: ArrayValue,
165190
encoder: DirectionalIndexByteEncoder

0 commit comments

Comments
 (0)