Java Code Examples for com.google.cloud.datastore.EntityQuery#Builder

The following examples show how to use com.google.cloud.datastore.EntityQuery#Builder . 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: PartTreeDatastoreQuery.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private Slice executeSliceQuery(Object[] parameters) {
	StructuredQuery.Builder builder = getEntityOrProjectionQueryBuilder()
			.setKind(this.datastorePersistentEntity.kindName());
	StructuredQuery query = applyQueryBody(parameters, builder, false, false, null);
	DatastoreResultsIterable<?> resultList = this.datastoreOperations.queryKeysOrEntities(query, this.entityType);

	ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);

	Pageable pageable = DatastorePageable.from(paramAccessor.getPageable(), resultList.getCursor(), null);

	EntityQuery.Builder builderNext = newEntityQueryBuilder().setKind(this.datastorePersistentEntity.kindName());
	StructuredQuery queryNext = applyQueryBody(parameters, builderNext, false, true, resultList.getCursor());
	Iterable nextResult = this.datastoreOperations.query(queryNext, x -> x);

	List<Object> result =
					StreamSupport.stream(resultList.spliterator(), false).collect(Collectors.toList());

	return (Slice) this.processRawObjectForProjection(
			new SliceImpl(result, pageable, nextResult.iterator().hasNext()));
}
 
Example 2
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void findAllTestSort() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("custom_test_kind");

	this.datastoreTemplate.findAll(TestEntity.class,
			new DatastoreQueryOptions.Builder().setSort(Sort.by("sortProperty"))
					.build());
	verify(this.datastore, times(1)).run(
			builder.setOrderBy(
					new StructuredQuery.OrderBy("prop", StructuredQuery.OrderBy.Direction.ASCENDING)).build());

	this.datastoreTemplate.findAll(TestEntity.class,
			new DatastoreQueryOptions.Builder()
					.setSort(Sort.by(Sort.Direction.DESC, "sortProperty")).build());
	verify(this.datastore, times(1)).run(
			builder.setOrderBy(
					new StructuredQuery.OrderBy("prop", StructuredQuery.OrderBy.Direction.DESCENDING)).build());
}
 
Example 3
Source File: DatastoreStorage.java    From styx with Apache License 2.0 6 votes vote down vote up
private EntityQuery.Builder backfillQueryBuilder(boolean showAll, Filter... filters) {
  final EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind(KIND_BACKFILL);

  final List<Filter> andedFilters = Lists.newArrayList(filters);

  if (!showAll) {
    andedFilters.add(PropertyFilter.eq(PROPERTY_ALL_TRIGGERED, false));
    andedFilters.add(PropertyFilter.eq(PROPERTY_HALTED, false));
  }

  if (!andedFilters.isEmpty()) {
    final Filter head = andedFilters.get(0);
    final Filter[] tail = andedFilters.stream().skip(1).toArray(Filter[]::new);
    queryBuilder.setFilter(CompositeFilter.and(head, tail));
  }

  return queryBuilder;
}
 
Example 4
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private Cursor cursorPaging(int pageSize, Cursor pageCursor) {
  // [START datastore_cursor_paging]
  EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind("Task")
      .setLimit(pageSize);
  if (pageCursor != null) {
    queryBuilder.setStartCursor(pageCursor);
  }
  QueryResults<Entity> tasks = datastore.run(queryBuilder.build());
  while (tasks.hasNext()) {
    Entity task = tasks.next();
    // do something with the task
  }
  Cursor nextPageCursor = tasks.getCursorAfter();
  // [END datastore_cursor_paging]
  return nextPageCursor;
}
 
Example 5
Source File: DatastoreTemplate.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public <T> DatastoreResultsCollection<T> findAll(Class<T> entityClass, DatastoreQueryOptions queryOptions) {
	DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(entityClass);
	EntityQuery.Builder builder = Query.newEntityQueryBuilder()
			.setKind(persistentEntity.kindName());
	applyQueryOptions(builder, queryOptions, persistentEntity);
	Query query = builder.build();
	QueryResults queryResults = getDatastoreReadWriter().run(query);
	Collection<T> convertedResults = convertEntitiesForRead(queryResults, entityClass);
	maybeEmitEvent(new AfterQueryEvent(convertedResults, query));
	return new DatastoreResultsCollection<>(convertedResults,
			queryResults != null ? queryResults.getCursorAfter() : null);
}
 
Example 6
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void findAllTestLimitOffset() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("custom_test_kind");

	this.datastoreTemplate.findAll(TestEntity.class,
			new DatastoreQueryOptions.Builder().setLimit(1).setOffset(5).build());
	verify(this.datastore, times(1)).run(builder.setLimit(1).setOffset(5).build());

	this.datastoreTemplate.findAll(TestEntity.class,
			new DatastoreQueryOptions.Builder().build());
	verify(this.datastore, times(1)).run(builder.build());
}
 
Example 7
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void findAllDiscrimination() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");

	this.datastoreTemplate.findAll(SimpleDiscriminationTestEntity.class);
	verify(this.datastore, times(1)).run(builder.setFilter(PropertyFilter.eq("discrimination_field", "A")).build());
}
 
Example 8
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void combineFiltersDiscrimination() {
	PropertyFilter propertyFilter = PropertyFilter.eq("field", "some value");
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind")
			.setFilter(propertyFilter);
	DatastoreTemplate.applyQueryOptions(builder,
			new DatastoreQueryOptions.Builder().setLimit(1).setOffset(2).build(),
			new DatastoreMappingContext().getPersistentEntity(SimpleDiscriminationTestEntity.class));

	assertThat(builder.build().getFilter()).isEqualTo(
			StructuredQuery.CompositeFilter.and(propertyFilter, PropertyFilter.eq("discrimination_field", "A")));

	assertThat(builder.build().getLimit()).isEqualTo(1);
	assertThat(builder.build().getOffset()).isEqualTo(2);
}
 
Example 9
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void findAllTestSortLimitOffset() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("custom_test_kind");

	this.datastoreTemplate.findAll(TestEntity.class,
			new DatastoreQueryOptions.Builder().setLimit(2).setOffset(3)
					.setSort(Sort.by("sortProperty")).build());
	verify(this.datastore, times(1)).run(
			builder.setLimit(2).setOffset(3)
					.setOrderBy(
							new StructuredQuery.OrderBy("prop", StructuredQuery.OrderBy.Direction.ASCENDING))
					.build());
}
 
Example 10
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleSimpleEntityTest() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
	StructuredQuery.CompositeFilter filter = StructuredQuery.CompositeFilter
			.and(PropertyFilter.eq("color", "simple_test_color"),
					PropertyFilter.eq("int_field", 1));
	EntityQuery query = builder.setFilter(filter).build();
	verifyBeforeAndAfterEvents(null, new AfterQueryEvent(Collections.emptyList(), query),
			() -> this.datastoreTemplate.queryByExample(
					Example.of(this.simpleTestEntity, ExampleMatcher.matching().withIgnorePaths("id")), null),
			x -> x.verify(this.datastore, times(1)).run(query));
}
 
Example 11
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleIgnoreFieldTest() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
	this.datastoreTemplate.queryByExample(
			Example.of(this.simpleTestEntity, ExampleMatcher.matching().withIgnorePaths("id", "intField")), null);

	StructuredQuery.CompositeFilter filter = StructuredQuery.CompositeFilter
			.and(PropertyFilter.eq("color", "simple_test_color"));
	verify(this.datastore, times(1)).run(builder.setFilter(filter).build());
}
 
Example 12
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleIncludeNullValuesTest() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
	this.datastoreTemplate.queryByExample(
			Example.of(this.simpleTestEntityNullVallues,
					ExampleMatcher.matching().withIgnorePaths("id").withIncludeNullValues()),
			null);

	StructuredQuery.CompositeFilter filter = StructuredQuery.CompositeFilter
			.and(PropertyFilter.eq("color", NullValue.of()),
					PropertyFilter.eq("int_field", NullValue.of()));
	verify(this.datastore, times(1)).run(builder.setFilter(filter).build());
}
 
Example 13
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleNoNullValuesTest() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
	this.datastoreTemplate.queryByExample(
			Example.of(this.simpleTestEntityNullVallues, ExampleMatcher.matching().withIgnorePaths("id")), null);

	verify(this.datastore, times(1)).run(builder.build());
}
 
Example 14
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void queryByExampleOptions() {
	EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind");
	this.datastoreTemplate.queryByExample(
			Example.of(this.simpleTestEntity, ExampleMatcher.matching().withIgnorePaths("id")),
			new DatastoreQueryOptions.Builder().setLimit(10).setOffset(1).setSort(Sort.by("intField"))
					.build());

	StructuredQuery.CompositeFilter filter = StructuredQuery.CompositeFilter
			.and(PropertyFilter.eq("color", "simple_test_color"),
					PropertyFilter.eq("int_field", 1));
	verify(this.datastore, times(1)).run(builder.setFilter(filter)
			.addOrderBy(StructuredQuery.OrderBy.asc("int_field")).setLimit(10).setOffset(1).build());
}