|
19 | 19 | import com.google.api.core.ApiFunction; |
20 | 20 | import com.google.api.core.ApiFuture; |
21 | 21 | import com.google.api.core.ApiFutures; |
| 22 | +import com.google.api.gax.rpc.ApiException; |
| 23 | +import com.google.api.gax.rpc.ApiExceptions; |
| 24 | +import com.google.cloud.firestore.v1beta1.FirestoreClient.ListDocumentsPagedResponse; |
22 | 25 | import com.google.common.base.Preconditions; |
| 26 | +import com.google.firestore.v1beta1.Document; |
| 27 | +import com.google.firestore.v1beta1.DocumentMask; |
| 28 | +import com.google.firestore.v1beta1.ListDocumentsRequest; |
| 29 | +import java.util.Iterator; |
23 | 30 | import java.util.Map; |
24 | 31 | import javax.annotation.Nonnull; |
25 | 32 | import javax.annotation.Nullable; |
@@ -104,6 +111,62 @@ public DocumentReference document(@Nonnull String childPath) { |
104 | 111 | return new DocumentReference(firestore, documentPath); |
105 | 112 | } |
106 | 113 |
|
| 114 | + /** |
| 115 | + * Retrieves the list of documents in this collection. |
| 116 | + * |
| 117 | + * <p>The document references returned may include references to "missing documents", i.e. |
| 118 | + * document locations that have no document present but which contain subcollections with |
| 119 | + * documents. Attempting to read such a document reference (e.g. via `get()` or `onSnapshot()`) |
| 120 | + * will return a `DocumentSnapshot` whose `exists()` method returns false. |
| 121 | + * |
| 122 | + * @return {Promise<DocumentReference[]>} @return {Promise<DocumentReference[]>} The list of |
| 123 | + * documents in this * collection. |
| 124 | + */ |
| 125 | + @Nonnull |
| 126 | + public Iterable<DocumentReference> listDocuments() { |
| 127 | + ListDocumentsRequest.Builder request = ListDocumentsRequest.newBuilder(); |
| 128 | + request.setParent(path.getParent().toString()); |
| 129 | + request.setCollectionId(this.getId()); |
| 130 | + request.setMask(DocumentMask.getDefaultInstance()); |
| 131 | + request.setShowMissing(true); |
| 132 | + |
| 133 | + final ListDocumentsPagedResponse response; |
| 134 | + |
| 135 | + try { |
| 136 | + response = |
| 137 | + ApiExceptions.callAndTranslateApiException( |
| 138 | + firestore.sendRequest( |
| 139 | + request.build(), firestore.getClient().listDocumentsPagedCallable())); |
| 140 | + } catch (ApiException exception) { |
| 141 | + throw FirestoreException.apiException(exception); |
| 142 | + } |
| 143 | + |
| 144 | + return new Iterable<DocumentReference>() { |
| 145 | + @Override |
| 146 | + @Nonnull |
| 147 | + public Iterator<DocumentReference> iterator() { |
| 148 | + final Iterator<Document> iterator = response.iterateAll().iterator(); |
| 149 | + return new Iterator<DocumentReference>() { |
| 150 | + @Override |
| 151 | + public boolean hasNext() { |
| 152 | + return iterator.hasNext(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public DocumentReference next() { |
| 157 | + ResourcePath path = ResourcePath.create(iterator.next().getName()); |
| 158 | + return document(path.getId()); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void remove() { |
| 163 | + throw new UnsupportedOperationException("remove"); |
| 164 | + } |
| 165 | + }; |
| 166 | + } |
| 167 | + }; |
| 168 | + } |
| 169 | + |
107 | 170 | /** |
108 | 171 | * Adds a new document to this collection with the specified data, assigning it a document ID |
109 | 172 | * automatically. |
|
0 commit comments