Skip to content

Firestore: Fix COUNT APIs to match the API proposal #4127

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ public void testCanRunCount() {
"b", map("k", "b"),
"c", map("k", "c")));

AggregateQuerySnapshot snapshot =
waitFor(collection.count().get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(3), snapshot.getCount());
AggregateQuerySnapshot snapshot = waitFor(collection.count().get(AggregateSource.SERVER));
assertEquals(3, snapshot.getCount());
}

@Test
Expand All @@ -103,8 +102,8 @@ public void testCanRunCountWithFilters() {
"c", map("k", "c")));

AggregateQuerySnapshot snapshot =
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(1), snapshot.getCount());
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER));
assertEquals(1, snapshot.getCount());
}

@Test
Expand All @@ -118,9 +117,9 @@ public void testCanRunCountWithOrderBy() {
"d", map("absent", "d")));

AggregateQuerySnapshot snapshot =
waitFor(collection.orderBy("k").count().get(AggregateSource.SERVER_DIRECT));
waitFor(collection.orderBy("k").count().get(AggregateSource.SERVER));
// "d" is filtered out because it is ordered by "k".
assertEquals(Long.valueOf(3), snapshot.getCount());
assertEquals(3, snapshot.getCount());
}

@Test
Expand All @@ -132,7 +131,7 @@ public void testTerminateDoesNotCrashWithFlyingCountQuery() {
"b", map("k", "b"),
"c", map("k", "c")));

collection.orderBy("k").count().get(AggregateSource.SERVER_DIRECT);
collection.orderBy("k").count().get(AggregateSource.SERVER);
waitFor(collection.firestore.terminate());
}

Expand All @@ -146,15 +145,15 @@ public void testSnapshotEquals() {
"c", map("k", "c")));

AggregateQuerySnapshot snapshot1 =
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER_DIRECT));
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER));
AggregateQuerySnapshot snapshot1_same =
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER_DIRECT));
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER));

AggregateQuerySnapshot snapshot2 =
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER_DIRECT));
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER));
waitFor(collection.document("d").set(map("k", "a")));
AggregateQuerySnapshot snapshot2_different =
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER_DIRECT));
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER));

assertTrue(snapshot1.equals(snapshot1_same));
assertEquals(snapshot1.hashCode(), snapshot1_same.hashCode());
Expand Down Expand Up @@ -196,9 +195,9 @@ public void testCanRunCollectionGroupQuery() {
waitFor(batch.commit());

AggregateQuerySnapshot snapshot =
waitFor(db.collectionGroup(collectionGroup).count().get(AggregateSource.SERVER_DIRECT));
waitFor(db.collectionGroup(collectionGroup).count().get(AggregateSource.SERVER));
assertEquals(
Long.valueOf(5), // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
5, // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
snapshot.getCount());
}

Expand All @@ -213,40 +212,33 @@ public void testCanRunCountWithFiltersAndLimits() {
"d", map("k", "d")));

AggregateQuerySnapshot snapshot =
waitFor(
collection.whereEqualTo("k", "a").limit(2).count().get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(2), snapshot.getCount());
waitFor(collection.whereEqualTo("k", "a").limit(2).count().get(AggregateSource.SERVER));
assertEquals(2, snapshot.getCount());

snapshot =
waitFor(
collection
.whereEqualTo("k", "a")
.limitToLast(2)
.count()
.get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(2), snapshot.getCount());
collection.whereEqualTo("k", "a").limitToLast(2).count().get(AggregateSource.SERVER));
assertEquals(2, snapshot.getCount());

snapshot =
waitFor(
collection
.whereEqualTo("k", "d")
.limitToLast(1000)
.count()
.get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(1), snapshot.getCount());
.get(AggregateSource.SERVER));
assertEquals(1, snapshot.getCount());
}

@Test
public void testCanRunCountOnNonExistentCollection() {
CollectionReference collection = testFirestore().collection("random-coll");

AggregateQuerySnapshot snapshot =
waitFor(collection.count().get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(0), snapshot.getCount());
AggregateQuerySnapshot snapshot = waitFor(collection.count().get(AggregateSource.SERVER));
assertEquals(0, snapshot.getCount());

snapshot =
waitFor(collection.whereEqualTo("k", 100).count().get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(0), snapshot.getCount());
snapshot = waitFor(collection.whereEqualTo("k", 100).count().get(AggregateSource.SERVER));
assertEquals(0, snapshot.getCount());
}

@Test
Expand All @@ -259,14 +251,13 @@ public void testFailWithoutNetwork() {
"c", map("k", "c")));
waitFor(collection.getFirestore().disableNetwork());

Exception e = waitForException(collection.count().get(AggregateSource.SERVER_DIRECT));
Exception e = waitForException(collection.count().get(AggregateSource.SERVER));
assertThat(e, instanceOf(FirebaseFirestoreException.class));
assertEquals(
FirebaseFirestoreException.Code.UNAVAILABLE, ((FirebaseFirestoreException) e).getCode());

waitFor(collection.getFirestore().enableNetwork());
AggregateQuerySnapshot snapshot =
waitFor(collection.count().get(AggregateSource.SERVER_DIRECT));
assertEquals(Long.valueOf(3), snapshot.getCount());
AggregateQuerySnapshot snapshot = waitFor(collection.count().get(AggregateSource.SERVER));
assertEquals(3, snapshot.getCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.google.firebase.firestore.util.Preconditions.checkNotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Objects;

/**
Expand All @@ -44,12 +43,8 @@ public AggregateQuery getQuery() {
return query;
}

/**
* @return The result of a document count aggregation. Returns null if no count aggregation is
* available in the result.
*/
@Nullable
public Long getCount() {
/** @return The result of a document count aggregation. */
public long getCount() {
return count;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ enum AggregateSource {
*
* <p>Requires client to be online.
*/
SERVER_DIRECT,
SERVER,
}