use of com.google.apphosting.datastore.DatastoreV3Pb.Query in project appengine-java-standard by GoogleCloudPlatform.
the class RemoteApiServlet method executeRunQuery.
private byte[] executeRunQuery(Request request) {
Query queryRequest = new Query();
parseFromBytes(queryRequest, request.getRequestAsBytes());
int batchSize = Math.max(1000, queryRequest.getLimit());
queryRequest.setCount(batchSize);
QueryResult runQueryResponse = new QueryResult();
byte[] res = ApiProxy.makeSyncCall("datastore_v3", "RunQuery", request.getRequestAsBytes());
parseFromBytes(runQueryResponse, res);
if (queryRequest.hasLimit()) {
// Try to pull all results
while (runQueryResponse.isMoreResults()) {
NextRequest nextRequest = new NextRequest();
nextRequest.getMutableCursor().mergeFrom(runQueryResponse.getCursor());
nextRequest.setCount(batchSize);
byte[] nextRes = ApiProxy.makeSyncCall("datastore_v3", "Next", nextRequest.toByteArray());
parseFromBytes(runQueryResponse, nextRes);
}
}
return runQueryResponse.toByteArray();
}
use of com.google.apphosting.datastore.DatastoreV3Pb.Query in project appengine-java-standard by GoogleCloudPlatform.
the class LocalDatastoreService method createIndexOnlyQueryResults.
/**
* Converts a normal result set into the results seen in an index-only query (a projection).
*
* @param queryEntities the results to convert
* @param entityComparator the comparator derived from the query
* @return the converted results
*/
private List<EntityProto> createIndexOnlyQueryResults(List<EntityProto> queryEntities, EntityProtoComparator entityComparator) {
Set<String> postfixProps = Sets.newHashSetWithExpectedSize(entityComparator.getAdjustedOrders().size());
for (Query.Order order : entityComparator.getAdjustedOrders()) {
postfixProps.add(order.getProperty());
}
List<EntityProto> results = Lists.newArrayListWithExpectedSize(queryEntities.size());
for (EntityProto entity : queryEntities) {
List<EntityProto> indexEntities = createIndexEntities(entity, postfixProps, entityComparator);
results.addAll(indexEntities);
}
return results;
}
use of com.google.apphosting.datastore.DatastoreV3Pb.Query in project appengine-java-standard by GoogleCloudPlatform.
the class RemoteApiServlet method executeTxQuery.
private byte[] executeTxQuery(Request request) {
RemoteApiPb.TransactionQueryResult result = new RemoteApiPb.TransactionQueryResult();
Query query = new Query();
parseFromBytes(query, request.getRequestAsBytes());
if (!query.hasAncestor()) {
throw new ApiProxy.ApplicationException(BAD_REQUEST.getValue(), "No ancestor in transactional query.");
}
// Make __entity_group__ key
OnestoreEntity.Reference egKey = result.getMutableEntityGroupKey().mergeFrom(query.getAncestor());
OnestoreEntity.Path.Element root = egKey.getPath().getElement(0);
egKey.getMutablePath().clearElement().addElement(root);
OnestoreEntity.Path.Element egElement = new OnestoreEntity.Path.Element();
egElement.setType("__entity_group__").setId(1);
egKey.getMutablePath().addElement(egElement);
// And then perform the transaction with the ancestor query and __entity_group__ fetch.
byte[] tx = beginTransaction(false);
parseFromBytes(query.getMutableTransaction(), tx);
byte[] queryBytes = ApiProxy.makeSyncCall("datastore_v3", "RunQuery", query.toByteArray());
parseFromBytes(result.getMutableResult(), queryBytes);
GetRequest egRequest = new GetRequest();
egRequest.addKey(egKey);
GetResponse egResponse = txGet(tx, egRequest);
if (egResponse.getEntity(0).hasEntity()) {
result.setEntityGroup(egResponse.getEntity(0).getEntity());
}
rollback(tx);
return result.toByteArray();
}
use of com.google.apphosting.datastore.DatastoreV3Pb.Query in project appengine-java-standard by GoogleCloudPlatform.
the class CursorTest method testCursorLifeCycle.
@Test
public void testCursorLifeCycle() {
Cursor reconstituted = Cursor.fromWebSafeString(cursor.toWebSafeString());
assertThat(reconstituted).isEqualTo(cursor);
Query query = new Query();
query.setOffset(3);
query.setCompiledCursor(toPb(reconstituted));
assertThat(query.getCompiledCursor()).isEqualTo(compiledCursor);
assertThat(query.getOffset()).isEqualTo(3);
}
use of com.google.apphosting.datastore.DatastoreV3Pb.Query in project appengine-java-standard by GoogleCloudPlatform.
the class CursorTest method testSerialization.
@Test
public void testSerialization() throws Exception {
CompiledCursor compiledCursor = new CompiledCursor();
CompiledCursor.Position position = compiledCursor.getMutablePosition();
position.setStartKey("Hello World");
position.setStartInclusive(true);
Cursor original = toCursor(compiledCursor);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(original);
byte[] bytes = baos.toByteArray();
ObjectInputStream iis = new ObjectInputStream(new ByteArrayInputStream(bytes));
Cursor readCursor = (Cursor) iis.readObject();
assertThat(readCursor).isNotSameInstanceAs(original);
assertThat(readCursor).isEqualTo(original);
Query query = new Query();
query.setOffset(3);
query.setCompiledCursor(toPb(readCursor));
assertThat(query.getCompiledCursor()).isEqualTo(compiledCursor);
assertThat(query.getOffset()).isEqualTo(3);
}
Aggregations