Search in sources :

Example 1 with Query

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();
}
Also used : QueryResult(com.google.apphosting.datastore.DatastoreV3Pb.QueryResult) Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) NextRequest(com.google.apphosting.datastore.DatastoreV3Pb.NextRequest)

Example 2 with Query

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;
}
Also used : Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) CompiledQuery(com.google.apphosting.datastore.DatastoreV3Pb.CompiledQuery) Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) ByteString(com.google.protobuf.ByteString) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 3 with Query

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();
}
Also used : Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) GetResponse(com.google.apphosting.datastore.DatastoreV3Pb.GetResponse) GetRequest(com.google.apphosting.datastore.DatastoreV3Pb.GetRequest) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity)

Example 4 with Query

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);
}
Also used : Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) CompiledCursor(com.google.apphosting.datastore.DatastoreV3Pb.CompiledCursor) Test(org.junit.Test)

Example 5 with Query

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);
}
Also used : Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) ByteArrayInputStream(java.io.ByteArrayInputStream) CompiledCursor(com.google.apphosting.datastore.DatastoreV3Pb.CompiledCursor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompiledCursor(com.google.apphosting.datastore.DatastoreV3Pb.CompiledCursor) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

Query (com.google.apphosting.datastore.DatastoreV3Pb.Query)5 CompiledCursor (com.google.apphosting.datastore.DatastoreV3Pb.CompiledCursor)2 Test (org.junit.Test)2 CompiledQuery (com.google.apphosting.datastore.DatastoreV3Pb.CompiledQuery)1 GetRequest (com.google.apphosting.datastore.DatastoreV3Pb.GetRequest)1 GetResponse (com.google.apphosting.datastore.DatastoreV3Pb.GetResponse)1 NextRequest (com.google.apphosting.datastore.DatastoreV3Pb.NextRequest)1 Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)1 QueryResult (com.google.apphosting.datastore.DatastoreV3Pb.QueryResult)1 ByteString (com.google.protobuf.ByteString)1 OnestoreEntity (com.google.storage.onestore.v3.OnestoreEntity)1 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)1 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1