readOptions =
- this.readOptionProtoPreparer.prepare(
- ImmutableList.of(ReadOption.transactionId(transactionId)));
+ this.readOptionProtoPreparer.prepare(ImmutableList.of(transactionId(transactionId)));
return datastore.run(readOptions, query);
}
+ @Override
+ public AggregationResults runAggregation(AggregationQuery query) {
+ return datastore.runAggregation(query, transactionId(transactionId));
+ }
+
@Override
public Transaction.Response commit() {
validateActive();
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/Aggregation.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/Aggregation.java
new file mode 100644
index 000000000..8a8e8cc18
--- /dev/null
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/Aggregation.java
@@ -0,0 +1,47 @@
+/*
+ * 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
+ *
+ * https://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.datastore.aggregation;
+
+import com.google.api.core.BetaApi;
+import com.google.api.core.InternalApi;
+import com.google.datastore.v1.AggregationQuery;
+
+/**
+ * Represents a Google Cloud Datastore Aggregation which is used with an {@link AggregationQuery}.
+ */
+@BetaApi
+public abstract class Aggregation {
+
+ private final String alias;
+
+ public Aggregation(String alias) {
+ this.alias = alias;
+ }
+
+ /** Returns the alias for this aggregation. */
+ public String getAlias() {
+ return alias;
+ }
+
+ @InternalApi
+ public abstract AggregationQuery.Aggregation toPb();
+
+ /** Returns a {@link CountAggregation} builder. */
+ public static CountAggregation.Builder count() {
+ return new CountAggregation.Builder();
+ }
+}
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/AggregationBuilder.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/AggregationBuilder.java
new file mode 100644
index 000000000..5e90b86aa
--- /dev/null
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/AggregationBuilder.java
@@ -0,0 +1,31 @@
+/*
+ * 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
+ *
+ * https://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.datastore.aggregation;
+
+import com.google.api.core.BetaApi;
+
+/**
+ * An interface to represent the builders which build and customize {@link Aggregation} for {@link
+ * com.google.cloud.datastore.AggregationQuery}.
+ *
+ * Used by {@link
+ * com.google.cloud.datastore.AggregationQuery.Builder#addAggregation(AggregationBuilder)}.
+ */
+@BetaApi
+public interface AggregationBuilder {
+ A build();
+}
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/CountAggregation.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/CountAggregation.java
new file mode 100644
index 000000000..a5295addf
--- /dev/null
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/CountAggregation.java
@@ -0,0 +1,83 @@
+/*
+ * 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
+ *
+ * https://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.datastore.aggregation;
+
+import com.google.api.core.BetaApi;
+import com.google.datastore.v1.AggregationQuery;
+import com.google.datastore.v1.AggregationQuery.Aggregation.Count;
+import java.util.Objects;
+
+/** Represents an {@link Aggregation} which returns count. */
+@BetaApi
+public class CountAggregation extends Aggregation {
+
+ /** @param alias Alias to used when running this aggregation. */
+ public CountAggregation(String alias) {
+ super(alias);
+ }
+
+ @Override
+ public AggregationQuery.Aggregation toPb() {
+ Count.Builder countBuilder = Count.newBuilder();
+
+ AggregationQuery.Aggregation.Builder aggregationBuilder =
+ AggregationQuery.Aggregation.newBuilder().setCount(countBuilder);
+ if (this.getAlias() != null) {
+ aggregationBuilder.setAlias(this.getAlias());
+ }
+ return aggregationBuilder.build();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ CountAggregation that = (CountAggregation) o;
+ boolean bothAliasAreNull = getAlias() == null && that.getAlias() == null;
+ if (bothAliasAreNull) {
+ return true;
+ } else {
+ boolean bothArePresent = getAlias() != null && that.getAlias() != null;
+ return bothArePresent && getAlias().equals(that.getAlias());
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(getAlias());
+ }
+
+ /** A builder class to create and customize a {@link CountAggregation}. */
+ public static class Builder implements AggregationBuilder {
+
+ private String alias;
+
+ public Builder as(String alias) {
+ this.alias = alias;
+ return this;
+ }
+
+ @Override
+ public CountAggregation build() {
+ return new CountAggregation(alias);
+ }
+ }
+}
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/AggregationQueryExecutor.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/AggregationQueryExecutor.java
new file mode 100644
index 000000000..14e425845
--- /dev/null
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/AggregationQueryExecutor.java
@@ -0,0 +1,66 @@
+/*
+ * 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
+ *
+ * https://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.datastore.execution;
+
+import com.google.api.core.InternalApi;
+import com.google.cloud.datastore.AggregationQuery;
+import com.google.cloud.datastore.AggregationResults;
+import com.google.cloud.datastore.DatastoreOptions;
+import com.google.cloud.datastore.ReadOption;
+import com.google.cloud.datastore.ReadOption.QueryAndReadOptions;
+import com.google.cloud.datastore.execution.request.AggregationQueryRequestProtoPreparer;
+import com.google.cloud.datastore.execution.response.AggregationQueryResponseTransformer;
+import com.google.cloud.datastore.spi.v1.DatastoreRpc;
+import com.google.datastore.v1.RunAggregationQueryRequest;
+import com.google.datastore.v1.RunAggregationQueryResponse;
+import java.util.Arrays;
+
+/**
+ * An implementation of {@link QueryExecutor} which executes {@link AggregationQuery} and returns
+ * {@link AggregationResults}.
+ */
+@InternalApi
+public class AggregationQueryExecutor
+ implements QueryExecutor {
+
+ private final DatastoreRpc datastoreRpc;
+ private final AggregationQueryRequestProtoPreparer protoPreparer;
+ private final AggregationQueryResponseTransformer responseTransformer;
+
+ public AggregationQueryExecutor(DatastoreRpc datastoreRpc, DatastoreOptions datastoreOptions) {
+ this.datastoreRpc = datastoreRpc;
+ this.protoPreparer = new AggregationQueryRequestProtoPreparer(datastoreOptions);
+ this.responseTransformer = new AggregationQueryResponseTransformer();
+ }
+
+ @Override
+ public AggregationResults execute(AggregationQuery query, ReadOption... readOptions) {
+ RunAggregationQueryRequest runAggregationQueryRequest =
+ getRunAggregationQueryRequest(query, readOptions);
+ RunAggregationQueryResponse runAggregationQueryResponse =
+ this.datastoreRpc.runAggregationQuery(runAggregationQueryRequest);
+ return this.responseTransformer.transform(runAggregationQueryResponse);
+ }
+
+ private RunAggregationQueryRequest getRunAggregationQueryRequest(
+ AggregationQuery query, ReadOption... readOptions) {
+ QueryAndReadOptions queryAndReadOptions =
+ readOptions == null
+ ? QueryAndReadOptions.create(query)
+ : QueryAndReadOptions.create(query, Arrays.asList(readOptions));
+ return this.protoPreparer.prepare(queryAndReadOptions);
+ }
+}
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/QueryExecutor.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/QueryExecutor.java
new file mode 100644
index 000000000..856c64a02
--- /dev/null
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/QueryExecutor.java
@@ -0,0 +1,40 @@
+/*
+ * 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
+ *
+ * https://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.datastore.execution;
+
+import com.google.api.core.InternalApi;
+import com.google.cloud.datastore.Query;
+import com.google.cloud.datastore.ReadOption;
+
+/**
+ * An internal functional interface whose implementation has the responsibility to execute a {@link
+ * Query} and returns the result. This class will have the responsibility to orchestrate between
+ * {@link com.google.cloud.datastore.execution.request.ProtoPreparer}, {@link
+ * com.google.cloud.datastore.spi.v1.DatastoreRpc} and {@link
+ * com.google.cloud.datastore.execution.response.ResponseTransformer} layers.
+ *
+ * @param A {@link Query} to execute.
+ * @param