Skip to content

Commit

Permalink
feat: COUNT API added
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Jun 20, 2022
1 parent 963e384 commit fe3fd47
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.cloud.firestore;

import java.util.Objects;

public class AggregateDemo {

public static void Demo1_CountOfDocumentsInACollection(Firestore db) throws Exception {
Query query = db.collection("games").document("halo").collection("players");
AggregateQuerySnapshot snapshot = query.count().get().get();
assertEqual(snapshot.getCount(), 5_000_000);
}

public static void Demo2_LimitNumberOfDocumentsScannedWithLimit(Firestore db) throws Exception {
// Limit the work / documents scanned by restricting underlying query.
Query query = db.collection("games").document("halo").collection("players").limit(1000);
AggregateQuerySnapshot snapshot = query.count().get().get();
assertEqual(snapshot.getCount(), 1000);
}

private static void assertEqual(Long num1, Long num2) {
if (!Objects.equals(num1, num2)) {
throw new AssertionError("num1!=num2");
}
}

private static void assertEqual(Long num1, int num2) {
assertEqual(num1, Long.valueOf(num2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.cloud.firestore;

import com.google.api.core.ApiFuture;
import javax.annotation.Nonnull;

public interface AggregateQuery {

@Nonnull
Query getQuery();

@Nonnull
ApiFuture<AggregateQuerySnapshot> get();

@Override
int hashCode();

@Override
boolean equals(Object obj);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.cloud.firestore;

import com.google.cloud.Timestamp;

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

public interface AggregateQuerySnapshot {

@Nonnull
AggregateQuery getQuery();

@Nonnull
Timestamp getReadTime();

@Nullable
Long getCount();

@Override
boolean equals(Object obj);

@Override
int hashCode();

}
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,11 @@ private boolean isRetryableError(Throwable throwable) {
return false;
}

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

/**
* Returns true if this Query is equal to the provided object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,9 @@ public ApiFuture<QuerySnapshot> get(@Nonnull Query query) {

return query.get(transactionId);
}

@Nonnull
public ApiFuture<AggregateQuerySnapshot> get(@Nonnull AggregateQuery query) {
throw new RuntimeException("not implemented");
}
}

0 comments on commit fe3fd47

Please sign in to comment.