Search in sources :

Example 6 with Order

use of com.google.apphosting.datastore.DatastoreV3Pb.Query.Order 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 7 with Order

use of com.google.apphosting.datastore.DatastoreV3Pb.Query.Order in project appengine-java-standard by GoogleCloudPlatform.

the class EntityComparatorTests method testMultiValuePropertySort.

@Test
public void testMultiValuePropertySort() {
    Entity e1 = new Entity("foo");
    e1.setProperty("a", Arrays.asList(null, 4L));
    OnestoreEntity.EntityProto p1 = EntityTranslator.convertToPb(e1);
    Entity e2 = new Entity("foo");
    e2.setProperty("a", Arrays.asList(2L, 3L));
    OnestoreEntity.EntityProto p2 = EntityTranslator.convertToPb(e2);
    Order desc = new Order().setProperty("a").setDirection(Direction.DESCENDING);
    EntityComparator comp = new EntityComparator(Collections.singletonList(desc));
    assertThat(comp.compare(e1, e2)).isEqualTo(-1);
    EntityProtoComparator protoComp = new EntityProtoComparator(Collections.singletonList(desc));
    assertThat(protoComp.compare(p1, p2)).isEqualTo(-1);
    Order asc = new Order().setProperty("a").setDirection(Direction.ASCENDING);
    comp = new EntityComparator(Collections.singletonList(asc));
    assertThat(comp.compare(e1, e2)).isEqualTo(-1);
    protoComp = new EntityProtoComparator(Collections.singletonList(asc));
    assertThat(protoComp.compare(p1, p2)).isEqualTo(-1);
}
Also used : Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) EntityProtoComparator(com.google.appengine.api.datastore.EntityProtoComparators.EntityProtoComparator) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) Test(org.junit.Test)

Example 8 with Order

use of com.google.apphosting.datastore.DatastoreV3Pb.Query.Order in project appengine-java-standard by GoogleCloudPlatform.

the class QueryTranslatorTest method testSort.

@Test
public void testSort() {
    Query query = new Query("foo");
    query.addSort("stringProp");
    query.addSort("doubleProp", Query.SortDirection.DESCENDING);
    DatastoreV3Pb.Query proto = QueryTranslator.convertToPb(query, withDefaults());
    assertThat(proto.orderSize()).isEqualTo(2);
    Order order1 = proto.getOrder(0);
    assertThat(order1.getProperty()).isEqualTo("stringProp");
    assertThat(order1.getDirectionEnum()).isEqualTo(Direction.ASCENDING);
    Order order2 = proto.getOrder(1);
    assertThat(order2.getProperty()).isEqualTo("doubleProp");
    assertThat(order2.getDirectionEnum()).isEqualTo(Direction.DESCENDING);
}
Also used : Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) Test(org.junit.Test)

Example 9 with Order

use of com.google.apphosting.datastore.DatastoreV3Pb.Query.Order in project appengine-java-standard by GoogleCloudPlatform.

the class QueryTranslator method convertSortPredicateToPb.

static Order convertSortPredicateToPb(Query.SortPredicate predicate) {
    Order order = new Order();
    order.setProperty(predicate.getPropertyName());
    order.setDirection(getSortOp(predicate.getDirection()));
    return order;
}
Also used : Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)

Example 10 with Order

use of com.google.apphosting.datastore.DatastoreV3Pb.Query.Order in project appengine-java-standard by GoogleCloudPlatform.

the class QueryTranslator method convertToPb.

@SuppressWarnings("deprecation")
public static DatastoreV3Pb.Query convertToPb(Query query, FetchOptions fetchOptions) {
    Key ancestor = query.getAncestor();
    List<Query.SortPredicate> sortPredicates = query.getSortPredicates();
    DatastoreV3Pb.Query proto = new DatastoreV3Pb.Query();
    if (query.getKind() != null) {
        proto.setKind(query.getKind());
    }
    proto.setApp(query.getAppIdNamespace().getAppId());
    String nameSpace = query.getAppIdNamespace().getNamespace();
    if (nameSpace.length() != 0) {
        proto.setNameSpace(nameSpace);
    }
    if (fetchOptions.getOffset() != null) {
        proto.setOffset(fetchOptions.getOffset());
    }
    if (fetchOptions.getLimit() != null) {
        proto.setLimit(fetchOptions.getLimit());
    }
    if (fetchOptions.getPrefetchSize() != null) {
        proto.setCount(fetchOptions.getPrefetchSize());
    } else if (fetchOptions.getChunkSize() != null) {
        proto.setCount(fetchOptions.getChunkSize());
    }
    if (fetchOptions.getStartCursor() != null) {
        if (!proto.getMutableCompiledCursor().parseFrom(fetchOptions.getStartCursor().toByteString())) {
            throw new IllegalArgumentException("Invalid cursor");
        }
    }
    if (fetchOptions.getEndCursor() != null) {
        if (!proto.getMutableEndCompiledCursor().parseFrom(fetchOptions.getEndCursor().toByteString())) {
            throw new IllegalArgumentException("Invalid cursor");
        }
    }
    if (fetchOptions.getCompile() != null) {
        proto.setCompile(fetchOptions.getCompile());
    }
    if (ancestor != null) {
        Reference ref = KeyTranslator.convertToPb(ancestor);
        if (!ref.getApp().equals(proto.getApp())) {
            throw new IllegalArgumentException("Query and ancestor appid/namespace mismatch");
        }
        proto.setAncestor(ref);
    }
    if (query.getDistinct()) {
        if (query.getProjections().isEmpty()) {
            throw new IllegalArgumentException("Projected properties must be set to " + "allow for distinct projections");
        }
        for (Projection projection : query.getProjections()) {
            proto.addGroupByPropertyName(projection.getPropertyName());
        }
    }
    proto.setKeysOnly(query.isKeysOnly());
    Query.Filter filter = query.getFilter();
    if (filter != null) {
        // At this point, all non-geo queries have had their filters
        // converted to sets of FilterPredicate objects; so this must be
        // a geo query.
        copyGeoFilterToPb(filter, proto);
    } else {
        for (Query.FilterPredicate filterPredicate : query.getFilterPredicates()) {
            Filter filterPb = proto.addFilter();
            filterPb.copyFrom(convertFilterPredicateToPb(filterPredicate));
        }
    }
    for (Query.SortPredicate sortPredicate : sortPredicates) {
        Order order = proto.addOrder();
        order.copyFrom(convertSortPredicateToPb(sortPredicate));
    }
    for (Projection projection : query.getProjections()) {
        proto.addPropertyName(projection.getPropertyName());
    }
    return proto;
}
Also used : Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter) Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter)

Aggregations

Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)17 Filter (com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter)10 EntityProtoComparator (com.google.appengine.api.datastore.EntityProtoComparators.EntityProtoComparator)5 Test (org.junit.Test)5 DatastoreV3Pb (com.google.apphosting.datastore.DatastoreV3Pb)3 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)3 ByteString (com.google.protobuf.ByteString)2 OnestoreEntity (com.google.storage.onestore.v3.OnestoreEntity)2 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)2 Index (com.google.storage.onestore.v3.OnestoreEntity.Index)2 Property (com.google.storage.onestore.v3.OnestoreEntity.Index.Property)2 Property (com.google.storage.onestore.v3.OnestoreEntity.Property)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Key (com.google.appengine.api.datastore.Key)1 Utils.getLastElement (com.google.appengine.api.datastore.dev.Utils.getLastElement)1 CompiledQuery (com.google.apphosting.datastore.DatastoreV3Pb.CompiledQuery)1 GeoRegion (com.google.apphosting.datastore.DatastoreV3Pb.GeoRegion)1 Query (com.google.apphosting.datastore.DatastoreV3Pb.Query)1 Operator (com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter.Operator)1