-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for the VectorValue type (#1716)
Implement VectorValue type support.
- Loading branch information
1 parent
4384970
commit 81bfa0d
Showing
11 changed files
with
509 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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
23 changes: 23 additions & 0 deletions
23
google-cloud-firestore/src/main/java/com/google/cloud/firestore/MapType.java
This file contains 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,23 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file 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. | ||
*/ | ||
|
||
package com.google.cloud.firestore; | ||
|
||
abstract class MapType { | ||
static final String RESERVED_MAP_KEY = "__type__"; | ||
static final String RESERVED_MAP_KEY_VECTOR_VALUE = "__vector__"; | ||
static final String VECTOR_MAP_VECTORS_KEY = "value"; | ||
} |
This file contains 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 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
72 changes: 72 additions & 0 deletions
72
google-cloud-firestore/src/main/java/com/google/cloud/firestore/VectorValue.java
This file contains 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,72 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file 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. | ||
*/ | ||
|
||
package com.google.cloud.firestore; | ||
|
||
import com.google.firestore.v1.MapValue; | ||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Represents a vector in Firestore documents. Create an instance with {@link FieldValue#vector}. | ||
*/ | ||
public final class VectorValue implements Serializable { | ||
private final double[] values; | ||
|
||
VectorValue(@Nullable double[] values) { | ||
if (values == null) this.values = new double[] {}; | ||
else this.values = values.clone(); | ||
} | ||
|
||
/** | ||
* Returns a representation of the vector as an array of doubles. | ||
* | ||
* @return A representation of the vector as an array of doubles | ||
*/ | ||
@Nonnull | ||
public double[] toArray() { | ||
return this.values.clone(); | ||
} | ||
|
||
/** | ||
* Returns true if this VectorValue is equal to the provided object. | ||
* | ||
* @param obj The object to compare against. | ||
* @return Whether this VectorValue is equal to the provided object. | ||
*/ | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
VectorValue otherArray = (VectorValue) obj; | ||
return Arrays.equals(this.values, otherArray.values); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(values); | ||
} | ||
|
||
MapValue toProto() { | ||
return UserDataConverter.encodeVector(this.values); | ||
} | ||
} |
This file contains 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.