Skip to content

COUNT API proposal #3521

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -18,11 +18,8 @@ import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FieldPath
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings
import com.google.firebase.firestore.TestUtil
import com.google.firebase.firestore.*
import com.google.firebase.firestore.AggregateSource.SERVER_DIRECT
import com.google.firebase.firestore.model.ObjectValue
import com.google.firebase.firestore.testutil.TestUtil.wrap
import com.google.firebase.ktx.Firebase
Expand All @@ -41,6 +38,37 @@ const val API_KEY = "API_KEY"

const val EXISTING_APP = "existing"

class AggregateDemo {

fun Demo0_NormalQuery(db: FirebaseFirestore) {
val query = db.collection("games/halo/players")
val snapshot = query.get().result
assertThat(snapshot.size()).isEqualTo(5_000_000)
}

fun Demo1_CountOfDocumentsInACollection(db: FirebaseFirestore) {
val countQuery = db.collection("games/halo/players").count()
val snapshot = countQuery.get(SERVER_DIRECT).result
assertThat(snapshot.count).isEqualTo(5_000_000)
}

fun Demo2_CountOfDocumentsInACollectionWithFilter(db: FirebaseFirestore) {
val query = db.collection("games/halo/players").whereEqualTo("online", true)
val countQuery = query.count()
val snapshot = countQuery.get(SERVER_DIRECT).result
assertThat(snapshot.count).isEqualTo(2000)
}

fun Demo3_CountOfDocumentsInACollectionWithLimit(db: FirebaseFirestore) {
val query = db.collection("games/halo/players").limit(9000)
val countQuery = query.count()
val snapshot = countQuery.get(SERVER_DIRECT).result
assertThat(snapshot.count).isEqualTo(9000)
}

}


abstract class BaseTestCase {
@Before
fun setUp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022 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.firebase.firestore;

final class AggregateDemo {

private AggregateDemo() {}

public void Demo0_NormalQuery(FirebaseFirestore db) {
CollectionReference query = db.collection("games/halo/players");
QuerySnapshot snapshot = query.get().getResult();
assertEqual(snapshot.size(), 5_000_000);
}

public void Demo1_CountOfDocumentsInACollection(FirebaseFirestore db) {
AggregateQuery countQuery = db.collection("games/halo/players").count();
AggregateQuerySnapshot snapshot = countQuery.get(AggregateSource.SERVER_DIRECT).getResult();
assertEqual(snapshot.getCount(), 5_000_000);
}

public void Demo2_CountOfDocumentsInACollectionWithFilter(FirebaseFirestore db) {
Query query = db.collection("games/halo/players").whereEqualTo("online", true);
AggregateQuery countQuery = query.count();
AggregateQuerySnapshot snapshot = countQuery.get(AggregateSource.SERVER_DIRECT).getResult();
assertEqual(snapshot.getCount(), 2000);
}

public void Demo2_CountOfDocumentsInACollectionWithLimit(FirebaseFirestore db) {
Query query = db.collection("games/halo/players").limit(9000);
AggregateQuery countQuery = query.count();
AggregateQuerySnapshot snapshot = countQuery.get(AggregateSource.SERVER_DIRECT).getResult();
assertEqual(snapshot.getCount(), 9000);
}

private static void assertEqual(Object o1, Object o2) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 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.firebase.firestore;

import androidx.annotation.NonNull;
import com.google.android.gms.tasks.Task;

public class AggregateQuery {

AggregateQuery() {}

@NonNull
public Query getQuery() {
throw new RuntimeException("not implemented");
}

@NonNull
public Task<AggregateQuerySnapshot> get(@NonNull AggregateSource source) {
throw new RuntimeException("not implemented");
}

@Override
public int hashCode() {
throw new RuntimeException("not implemented");
}

@Override
public boolean equals(Object obj) {
throw new RuntimeException("not implemented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022 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.firebase.firestore;

import androidx.annotation.Nullable;
import javax.annotation.Nonnull;

public class AggregateQuerySnapshot {

AggregateQuerySnapshot() {}

@Nonnull
public AggregateQuery getQuery() {
throw new RuntimeException("not implemented");
}

@Nullable
public Long getCount() {
throw new RuntimeException("not implemented");
}

@Override
public boolean equals(@Nullable Object obj) {
throw new RuntimeException("not implemented");
}

@Override
public int hashCode() {
throw new RuntimeException("not implemented");
}

@Override
public String toString() {
throw new RuntimeException("not implemented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 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.firebase.firestore;

public enum AggregateSource {
SERVER_DIRECT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,11 @@ private void validateHasExplicitOrderByForLimitToLast() {
}
}

@NonNull
public AggregateQuery count() {
throw new RuntimeException("not implemented");
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down