Java Code Examples for com.google.appengine.api.datastore.Query#setDistinct()

The following examples show how to use com.google.appengine.api.datastore.Query#setDistinct() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectionTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void projectionQuery_grouping_filtersDuplicates() {
  putTestData("some duplicate", 0L);
  putTestData("some duplicate", 0L);
  putTestData("too big", 1L);

  // [START grouping]
  Query q = new Query("TestKind");
  q.addProjection(new PropertyProjection("A", String.class));
  q.addProjection(new PropertyProjection("B", Long.class));
  q.setDistinct(true);
  q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L));
  q.addSort("B", Query.SortDirection.DESCENDING);
  q.addSort("A");
  // [END grouping]

  List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5));
  assertThat(entities).hasSize(1);
  Entity entity = entities.get(0);
  assertWithMessage("entity.A")
      .that((String) entity.getProperty("A"))
      .isEqualTo("some duplicate");
  assertWithMessage("entity.B").that((long) entity.getProperty("B")).isEqualTo(0L);
}
 
Example 2
Source File: AppEngineBackEnd.java    From appengine-pipelines with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> getRootPipelinesDisplayName() {
  Query query = new Query(JobRecord.DATA_STORE_KIND);
  query.addProjection(
      new PropertyProjection(JobRecord.ROOT_JOB_DISPLAY_NAME, String.class));
  query.setDistinct(true);
  final PreparedQuery preparedQuery = dataStore.prepare(query);
  return tryFiveTimes(new Operation<Set<String>>("getRootPipelinesDisplayName") {
    @Override
    public Set<String> call() {
      Set<String> pipelines = new LinkedHashSet<>();
      for (Entity entity : preparedQuery.asIterable()) {
        pipelines.add((String) entity.getProperty(JobRecord.ROOT_JOB_DISPLAY_NAME));
      }
      return pipelines;
    }
  });
}
 
Example 3
Source File: DistinctTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctSort() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("floatData", Float.class));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    query.setDistinct(true);
    assertEquals(7, service.prepare(query).countEntities(fo));
    assertTrue(query.getDistinct());
    query.addSort("floatData", Query.SortDirection.DESCENDING);
    assertEquals(7, service.prepare(query).countEntities(fo));
}
 
Example 4
Source File: DistinctTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctFilter() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("floatData", Float.class));
    query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.NOT_EQUAL, "string1"));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    query.setDistinct(true);
    assertEquals(5, service.prepare(query).countEntities(fo));
    assertTrue(query.getDistinct());
}
 
Example 5
Source File: DistinctTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctFilter2() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("floatData", Float.class));
    query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.GREATER_THAN,
        "string0"));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    query.addSort("floatData", Query.SortDirection.DESCENDING);
    query.setDistinct(true);
    assertEquals(2, service.prepare(query).countEntities(fo));
    assertTrue(query.getDistinct());
}