Skip to content

Commit 7f6dc84

Browse files
schmidt-sebastiankolea2
authored andcommitted
Firestore: Adding Collection Group queries (#4652)
* Firestore: Adding Collection Group queries * Using AutoValue * Adding comment * Add append() helper
1 parent 860fe58 commit 7f6dc84

9 files changed

Lines changed: 455 additions & 217 deletions

File tree

google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/CollectionReference.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class CollectionReference extends Query {
5959
*/
6060
@Nonnull
6161
public String getId() {
62-
return path.getId();
62+
return options.getCollectionId();
6363
}
6464

6565
/**
@@ -69,7 +69,7 @@ public String getId() {
6969
*/
7070
@Nullable
7171
public DocumentReference getParent() {
72-
ResourcePath parent = path.getParent();
72+
ResourcePath parent = options.getParentPath();
7373
return parent.isDocument() ? new DocumentReference(firestore, parent) : null;
7474
}
7575

@@ -81,7 +81,7 @@ public DocumentReference getParent() {
8181
*/
8282
@Nonnull
8383
public String getPath() {
84-
return path.getPath();
84+
return getResourcePath().getPath();
8585
}
8686

8787
/**
@@ -104,10 +104,10 @@ public DocumentReference document() {
104104
*/
105105
@Nonnull
106106
public DocumentReference document(@Nonnull String childPath) {
107-
ResourcePath documentPath = path.append(childPath);
107+
ResourcePath documentPath = getResourcePath().append(childPath);
108108
Preconditions.checkArgument(
109109
documentPath.isDocument(),
110-
String.format("Path should point to a Document Reference: %s", path));
110+
String.format("Path should point to a Document Reference: %s", getPath()));
111111
return new DocumentReference(firestore, documentPath);
112112
}
113113

@@ -124,8 +124,8 @@ public DocumentReference document(@Nonnull String childPath) {
124124
@Nonnull
125125
public Iterable<DocumentReference> listDocuments() {
126126
ListDocumentsRequest.Builder request = ListDocumentsRequest.newBuilder();
127-
request.setParent(path.getParent().toString());
128-
request.setCollectionId(this.getId());
127+
request.setParent(options.getParentPath().toString());
128+
request.setCollectionId(options.getCollectionId());
129129
request.setMask(DocumentMask.getDefaultInstance());
130130
request.setShowMissing(true);
131131

@@ -206,4 +206,9 @@ public ApiFuture<DocumentReference> add(Object pojo) {
206206
}
207207
return add((Map<String, Object>) converted);
208208
}
209+
210+
/** Returns a resource path pointing to this collection. */
211+
ResourcePath getResourcePath() {
212+
return options.getParentPath().append(options.getCollectionId());
213+
}
209214
}

google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/Firestore.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public interface Firestore extends Service<FirestoreOptions>, AutoCloseable {
6262
@Nonnull
6363
Iterable<CollectionReference> getCollections();
6464

65+
/**
66+
* Creates and returns a new @link{Query} that includes all documents in the database that are
67+
* contained in a collection or subcollection with the given @code{collectionId}.
68+
*
69+
* @param collectionId Identifies the collections to query over. Every collection or subcollection
70+
* with this ID as the last segment of its path will be included. Cannot contain a slash.
71+
* @return The created Query.
72+
*/
73+
Query collectionGroup(@Nonnull String collectionId);
74+
6575
/**
6676
* Executes the given updateFunction and then attempts to commit the changes applied within the
6777
* transaction. If any document read within the transaction has changed, the updateFunction will

google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ public WriteBatch batch() {
106106
@Nonnull
107107
@Override
108108
public CollectionReference collection(@Nonnull String collectionPath) {
109-
return new CollectionReference(this, databasePath.append(collectionPath));
109+
ResourcePath path = databasePath.append(collectionPath);
110+
Preconditions.checkArgument(
111+
path.isCollection(), "Invalid path specified. Path should point to a collection");
112+
return new CollectionReference(this, path);
110113
}
111114

112115
@Nonnull
@@ -244,6 +247,16 @@ public void onCompleted() {
244247
return futureList;
245248
}
246249

250+
@Nonnull
251+
@Override
252+
public Query collectionGroup(@Nonnull final String collectionId) {
253+
Preconditions.checkArgument(
254+
!collectionId.contains("/"),
255+
String.format(
256+
"Invalid collectionId '%s'. Collection IDs must not contain '/'.", collectionId));
257+
return new Query(this, collectionId);
258+
}
259+
247260
@Nonnull
248261
@Override
249262
public <T> ApiFuture<T> runTransaction(@Nonnull final Transaction.Function<T> updateFunction) {
@@ -401,6 +414,11 @@ String getDatabaseName() {
401414
return databasePath.getDatabaseName().toString();
402415
}
403416

417+
/** Returns a path to the Firestore project associated with this client. */
418+
ResourcePath getResourcePath() {
419+
return databasePath;
420+
}
421+
404422
/** Returns the underlying RPC client. */
405423
FirestoreRpc getClient() {
406424
return firestoreClient;

0 commit comments

Comments
 (0)