-
Notifications
You must be signed in to change notification settings - Fork 67
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
feat: Add support for vector search with Query#findNearest #1827
Merged
Merged
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a971555
Refactor to support find nearest
MarkDuckworth 44d364d
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth 8fe28ce
FindNearest with tests
MarkDuckworth 151f245
Cleanup and comments
MarkDuckworth c2da4a8
Cleanup and addressing API breaking changes
MarkDuckworth dff55dc
PR feedback and adding tests
MarkDuckworth a29c6b5
PR comment changes
MarkDuckworth 96a3940
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth b391efb
chore: generate libraries at Fri Sep 27 17:15:13 UTC 2024
cloud-java-bot 7bd43aa
Fix dependency check
MarkDuckworth 58756e0
Merge branch 'markduckworth/find-nearest' of github.com:googleapis/ja…
MarkDuckworth 264e103
chore: generate libraries at Fri Sep 27 17:33:40 UTC 2024
cloud-java-bot bb83ee7
PR remove unnecessary override
MarkDuckworth 31e9be2
Merge branch 'markduckworth/find-nearest' of github.com:googleapis/ja…
MarkDuckworth 1f61461
PR feedback
MarkDuckworth f29120c
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth 87fcff4
chore: generate libraries at Tue Oct 1 19:36:20 UTC 2024
cloud-java-bot 5fa33f5
Example code
MarkDuckworth 1fe96df
javadoc fix
MarkDuckworth c606e57
Merge branch 'main' of github.com:googleapis/java-firestore into mark…
MarkDuckworth 7cfab37
Refactor GenericQuerySnapshot so the constructor does not accept a do…
MarkDuckworth 84a8f8b
chore: generate libraries at Tue Oct 1 21:53:10 UTC 2024
cloud-java-bot 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 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
176 changes: 176 additions & 0 deletions
176
google-cloud-firestore/src/main/java/com/google/cloud/firestore/GenericQuerySnapshot.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,176 @@ | ||
/* | ||
* 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.cloud.Timestamp; | ||
import com.google.cloud.firestore.encoding.CustomClassMapper; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Abstract. A GenericQuerySnapshot represents the results of a query that returns documents. It can | ||
* contain zero or more DocumentSnapshot objects. | ||
*/ | ||
public abstract class GenericQuerySnapshot<QueryT> implements Iterable<QueryDocumentSnapshot> { | ||
protected final QueryT query; | ||
protected final Timestamp readTime; | ||
|
||
private List<DocumentChange> documentChanges; | ||
private List<QueryDocumentSnapshot> documents; | ||
|
||
private final DocumentSet documentSet; | ||
|
||
// Elevated access level for mocking. | ||
protected GenericQuerySnapshot( | ||
QueryT query, | ||
Timestamp readTime, | ||
final List<QueryDocumentSnapshot> documents, | ||
final DocumentSet documentSet, | ||
final List<DocumentChange> documentChanges) { | ||
this.query = query; | ||
this.readTime = readTime; | ||
this.documentChanges = | ||
documentChanges != null ? Collections.unmodifiableList(documentChanges) : documentChanges; | ||
this.documentSet = documentSet; | ||
this.documents = documents; | ||
} | ||
|
||
/** | ||
* Returns the query for the snapshot. | ||
* | ||
* @return The backing query that produced this snapshot. | ||
*/ | ||
@Nonnull | ||
public QueryT getQuery() { | ||
return query; | ||
} | ||
|
||
/** | ||
* Returns the time at which this snapshot was read. | ||
* | ||
* @return The read time of this snapshot. | ||
*/ | ||
@Nonnull | ||
public Timestamp getReadTime() { | ||
return readTime; | ||
} | ||
|
||
/** | ||
* Returns the documents in this QuerySnapshot as a List in order of the query. | ||
* | ||
* @return The list of documents. | ||
*/ | ||
@Nonnull | ||
public List<QueryDocumentSnapshot> getDocuments() { | ||
if (documents == null) { | ||
synchronized (documentSet) { | ||
if (documents == null) { | ||
documents = documentSet.toList(); | ||
} | ||
} | ||
} | ||
return Collections.unmodifiableList(documents); | ||
} | ||
|
||
/** Returns true if there are no documents in the QuerySnapshot. */ | ||
public boolean isEmpty() { | ||
return this.size() == 0; | ||
} | ||
|
||
@Nonnull | ||
public Iterator<QueryDocumentSnapshot> iterator() { | ||
return getDocuments().iterator(); | ||
} | ||
|
||
/** | ||
* Returns the contents of the documents in the QuerySnapshot, converted to the provided class, as | ||
* a list. | ||
* | ||
* @param clazz The POJO type used to convert the documents in the list. | ||
*/ | ||
@Nonnull | ||
public <T> List<T> toObjects(@Nonnull Class<T> clazz) { | ||
List<QueryDocumentSnapshot> documents = getDocuments(); | ||
List<T> results = new ArrayList<>(documents.size()); | ||
for (DocumentSnapshot documentSnapshot : documents) { | ||
results.add( | ||
CustomClassMapper.convertToCustomClass( | ||
documentSnapshot.getData(), clazz, documentSnapshot.getReference())); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
/** | ||
* Returns the list of documents that changed since the last snapshot. If it's the first snapshot | ||
* all documents will be in the list as added changes. | ||
* | ||
* @return The list of documents that changed since the last snapshot. | ||
*/ | ||
@Nonnull | ||
public List<DocumentChange> getDocumentChanges() { | ||
if (documentChanges == null) { | ||
synchronized (documents) { | ||
if (documentChanges == null) { | ||
int size = documents.size(); | ||
ImmutableList.Builder<DocumentChange> builder = | ||
ImmutableList.builderWithExpectedSize(size); | ||
for (int i = 0; i < size; ++i) { | ||
builder.add(new DocumentChange(documents.get(i), DocumentChange.Type.ADDED, -1, i)); | ||
} | ||
documentChanges = builder.build(); | ||
} | ||
} | ||
} | ||
|
||
return documentChanges; | ||
} | ||
|
||
/** Returns the number of DocumentSnapshots in this snapshot. */ | ||
public int size() { | ||
return getDocuments().size(); | ||
} | ||
|
||
/** | ||
* Tests for equality with this object. | ||
* | ||
* @param o is tested for equality with this object. | ||
* @return `true` if equal, otherwise `false` | ||
*/ | ||
public boolean equals(Object o) { | ||
tom-andersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
GenericQuerySnapshot that = (GenericQuerySnapshot) o; | ||
return Objects.equals(query, that.query) | ||
&& Objects.equals(this.getDocumentChanges(), that.getDocumentChanges()) | ||
&& Objects.equals(this.getDocuments(), that.getDocuments()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(query, this.getDocumentChanges(), this.getDocuments()); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to me that
documentSet
is just converted todocuments
.I suggest you remove parameter, and just have subclasses use
documents
parameter instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also pre-existing logic, so I'm not certain of the reasoning, but there are several instances of lazy-loading and memoization in this code. My suspicion is that it's related to performance. I don't have strong feelings on this either way. What are your thoughts with that context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is of slightly greater concern because
documentSet
is also omitted fromequals
method.I prefer this get fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this.
I don't think there was an issue before. The value of
documentSet
was tested for equality because it's value was converted by thegetDocuments()
method.However, I think the new approach is equally fine. The benefits of lazily converting
documentSet
to a list of document snapshots are rarely if ever seen.