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;
}
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);
}
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);
}
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;
}
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;
}
Aggregations