/
Query.java
301 lines (263 loc) · 9.65 KB
/
Query.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2015 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.datastore;
import com.google.api.core.InternalApi;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.Map;
/**
* A Google Cloud Datastore query. For usage examples see {@link GqlQuery}, {@link StructuredQuery}
* and {@link AggregationQuery}.
*
* <p>Note that queries require proper indexing. See <a
* href="https://cloud.google.com/datastore/docs/tools/indexconfig">Cloud Datastore Index
* Configuration</a> for help configuring indexes.
*
* @param <V> the type of the values returned by this query.
* @see <a href="https://cloud.google.com/datastore/docs/concepts/queries">Datastore Queries</a>
*/
public abstract class Query<V> implements Serializable {
private static final long serialVersionUID = 7967659059395653941L;
private final String namespace;
/**
* This class represents the expected type of the result. ENTITY: A full entity represented by
* {@link Entity}. PROJECTION_ENTITY: A projection entity, represented by {@link
* ProjectionEntity}. KEY: An entity's {@link Key}.
*/
public abstract static class ResultType<V> implements Serializable {
private static final long serialVersionUID = 2104157695425806623L;
private static final Map<com.google.datastore.v1.EntityResult.ResultType, ResultType<?>>
PB_TO_INSTANCE = Maps.newEnumMap(com.google.datastore.v1.EntityResult.ResultType.class);
static final ResultType<?> UNKNOWN =
new ResultType<Object>(null, Object.class) {
private static final long serialVersionUID = 1602329532153860907L;
@Override
Object convert(com.google.datastore.v1.Entity entityPb) {
if (entityPb.getPropertiesMap().isEmpty()) {
if (!entityPb.hasKey()) {
return null;
}
return Key.fromPb(entityPb.getKey());
}
return ProjectionEntity.fromPb(entityPb);
}
};
public static final ResultType<Entity> ENTITY =
new ResultType<Entity>(com.google.datastore.v1.EntityResult.ResultType.FULL, Entity.class) {
private static final long serialVersionUID = 7712959777507168274L;
@Override
Entity convert(com.google.datastore.v1.Entity entityPb) {
return Entity.fromPb(entityPb);
}
};
public static final ResultType<Key> KEY =
new ResultType<Key>(com.google.datastore.v1.EntityResult.ResultType.KEY_ONLY, Key.class) {
private static final long serialVersionUID = -8514289244104446252L;
@Override
Key convert(com.google.datastore.v1.Entity entityPb) {
return Key.fromPb(entityPb.getKey());
}
};
public static final ResultType<ProjectionEntity> PROJECTION_ENTITY =
new ResultType<ProjectionEntity>(
com.google.datastore.v1.EntityResult.ResultType.PROJECTION, ProjectionEntity.class) {
private static final long serialVersionUID = -7591409419690650246L;
@Override
ProjectionEntity convert(com.google.datastore.v1.Entity entityPb) {
return ProjectionEntity.fromPb(entityPb);
}
};
private final Class<V> resultClass;
private final com.google.datastore.v1.EntityResult.ResultType queryType;
private ResultType(
com.google.datastore.v1.EntityResult.ResultType queryType, Class<V> resultClass) {
this.queryType = queryType;
this.resultClass = resultClass;
if (queryType != null) {
PB_TO_INSTANCE.put(queryType, this);
}
}
public Class<V> resultClass() {
return resultClass;
}
@InternalApi
public com.google.datastore.v1.EntityResult.ResultType getQueryType() {
return this.queryType;
}
@Override
public int hashCode() {
return resultClass.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ResultType)) {
return false;
}
ResultType<?> other = (ResultType<?>) obj;
return resultClass.equals(other.resultClass);
}
@Override
public String toString() {
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
toStringHelper.add("queryType", queryType);
toStringHelper.add("resultClass", resultClass);
return toStringHelper.toString();
}
boolean isAssignableFrom(ResultType<?> otherResultType) {
return resultClass.isAssignableFrom(otherResultType.resultClass);
}
abstract V convert(com.google.datastore.v1.Entity entityPb);
static ResultType<?> fromPb(com.google.datastore.v1.EntityResult.ResultType typePb) {
return MoreObjects.firstNonNull(PB_TO_INSTANCE.get(typePb), UNKNOWN);
}
}
Query(String namespace) {
this.namespace = namespace;
}
public String getNamespace() {
return namespace;
}
ToStringHelper toStringHelper() {
return MoreObjects.toStringHelper(this).add("namespace", namespace);
}
/**
* Returns a new {@link GqlQuery} builder.
*
* <p>Example of creating and running a GQL query.
*
* <pre>{@code
* String kind = "my_kind";
* String gqlQuery = "select * from " + kind;
* Query<?> query = Query.newGqlQueryBuilder(gqlQuery).build();
* QueryResults<?> results = datastore.run(query);
* // Use results
* }</pre>
*
* @see <a href="https://cloud.google.com/datastore/docs/apis/gql/gql_reference">GQL Reference</a>
*/
public static GqlQuery.Builder<?> newGqlQueryBuilder(String gql) {
return newGqlQueryBuilder(ResultType.UNKNOWN, gql);
}
/**
* Returns a new {@link GqlQuery} builder.
*
* <p>Example of creating and running a typed GQL query.
*
* <pre>{@code
* String kind = "my_kind";
* String gqlQuery = "select * from " + kind;
* Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, gqlQuery).build();
* QueryResults<Entity> results = datastore.run(query);
* // Use results
* }</pre>
*
* @see <a href="https://cloud.google.com/datastore/docs/apis/gql/gql_reference">GQL Reference</a>
*/
public static <V> GqlQuery.Builder<V> newGqlQueryBuilder(ResultType<V> resultType, String gql) {
return new GqlQuery.Builder<>(resultType, gql);
}
/**
* Returns a new {@link StructuredQuery} builder for full (complete entities) queries.
*
* <p>Example of creating and running an entity query.
*
* <pre>{@code
* String kind = "my_kind";
* Query<Entity> query = Query.newEntityQueryBuilder().setKind(kind).build();
* QueryResults<Entity> results = datastore.run(query);
* // Use results
* }</pre>
*/
public static EntityQuery.Builder newEntityQueryBuilder() {
return new EntityQuery.Builder();
}
/**
* Returns a new {@link StructuredQuery} builder for key only queries.
*
* <p>Example of creating and running a key query.
*
* <pre>{@code
* String kind = "my_kind";
* Query<Key> query = Query.newKeyQueryBuilder().setKind(kind).build();
* QueryResults<Key> results = datastore.run(query);
* // Use results
* }</pre>
*/
public static KeyQuery.Builder newKeyQueryBuilder() {
return new KeyQuery.Builder();
}
/**
* Returns a new {@link StructuredQuery} builder for projection queries.
*
* <p>Example of creating and running a projection entity query.
*
* <pre>{@code
* String kind = "my_kind";
* String property = "my_property";
* Query<ProjectionEntity> query = Query.newProjectionEntityQueryBuilder()
* .setKind(kind)
* .addProjection(property)
* .build();
* QueryResults<ProjectionEntity> results = datastore.run(query);
* // Use results
* }</pre>
*/
public static ProjectionEntityQuery.Builder newProjectionEntityQueryBuilder() {
return new ProjectionEntityQuery.Builder();
}
/**
* Returns a new {@link AggregationQuery} builder.
*
* <p>Example of creating and running an {@link AggregationQuery}.
*
* <p>{@link StructuredQuery} example:
*
* <pre>{@code
* EntityQuery selectAllQuery = Query.newEntityQueryBuilder()
* .setKind("Task")
* .build();
* AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
* .addAggregation(count().as("total_count"))
* .over(selectAllQuery)
* .build();
* AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
* // Use aggregationResults
* }</pre>
*
* <p>{@link GqlQuery} example:
*
* <pre>{@code
* GqlQuery<?> selectAllGqlQuery = Query.newGqlQueryBuilder(
* "AGGREGATE COUNT(*) AS total_count OVER(SELECT * FROM Task)"
* )
* .setAllowLiteral(true)
* .build();
* AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
* .over(selectAllGqlQuery)
* .build();
* AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
* // Use aggregationResults
* }</pre>
*/
public static AggregationQuery.Builder newAggregationQueryBuilder() {
return new AggregationQuery.Builder();
}
}