use of com.google.cloud.datastore.Query in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method keyQueryByExample.
@Override
public <T> Iterable<Key> keyQueryByExample(Example<T> example, DatastoreQueryOptions queryOptions) {
Query query = exampleToQuery(example, queryOptions, true);
Iterable<Key> results = () -> getDatastoreReadWriter().run(query);
maybeEmitEvent(new AfterQueryEvent(results, query));
return results;
}
use of com.google.cloud.datastore.Query in project spring-cloud-gcp by spring-cloud.
the class GcpDatastoreEmulatorIntegrationTests method testDatastoreEmulatorConfiguration.
@Test
public void testDatastoreEmulatorConfiguration() {
DatastoreOptions.Builder builder = DatastoreOptions.newBuilder();
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpDatastoreAutoConfiguration.class, GcpContextAutoConfiguration.class, DatastoreTransactionManagerAutoConfiguration.class, DatastoreRepositoriesAutoConfiguration.class, GcpDatastoreEmulatorAutoConfiguration.class)).withUserConfiguration(TestConfiguration.class).withPropertyValues("spring.cloud.gcp.project-id=test-project", "spring.cloud.gcp.datastore.namespace=test-namespace", "spring.cloud.gcp.datastore.emulator.port=8181", "spring.cloud.gcp.datastore.emulator.enabled=true", "spring.cloud.gcp.datastore.emulator.consistency=0.9").run((context) -> {
DatastoreTemplate datastore = context.getBean(DatastoreTemplate.class);
Datastore datastoreClient = (Datastore) ((Supplier) context.getBean(context.getBeanNamesForType(ResolvableType.forClassWithGenerics(Supplier.class, Datastore.class))[0])).get();
GcpProjectIdProvider projectIdProvider = context.getBean(GcpProjectIdProvider.class);
builder.setServiceFactory(datastoreOptions -> datastoreClient).setProjectId(projectIdProvider.getProjectId());
EmulatorEntityTest entity = new EmulatorEntityTest();
entity.setProperty("property-test");
datastore.save(entity);
assertThat(entity.getId()).isNotNull();
assertThat(datastore.findById(entity.getId(), EmulatorEntityTest.class).getProperty()).isEqualTo("property-test");
});
Datastore datastore = builder.build().getService();
EntityQuery query = Query.newEntityQueryBuilder().setKind("RandomKind").setFilter(StructuredQuery.PropertyFilter.eq("key", "value")).build();
assertThatExceptionOfType(DatastoreException.class).isThrownBy(() -> datastore.run(query));
}
use of com.google.cloud.datastore.Query in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplateTests method setup.
@Before
public void setup() {
this.datastoreTemplate = new DatastoreTemplate(() -> this.datastore, this.datastoreEntityConverter, new DatastoreMappingContext(), this.objectToKeyFactory);
when(this.datastoreEntityConverter.getConversions()).thenReturn(this.readWriteConversions);
// The readWriteConversions are only mocked for purposes of collection-conversion
// for
// descendants. no other conversions take place in the template.
doAnswer((invocation) -> new LinkedList<>(invocation.getArgument(0))).when(this.readWriteConversions).convertOnRead(any(), any(), (Class) any());
this.ob1 = new TestEntity();
this.ob2 = new TestEntity();
this.ob1.id = "value1";
this.ob2.id = "value2";
Entity ce1 = Entity.newBuilder(this.keyChild1).build();
Query childTestEntityQuery = Query.newEntityQueryBuilder().setKind("child_entity").setFilter(PropertyFilter.hasAncestor(this.key1)).build();
this.childEntity1 = createChildEntity();
this.ob1.childEntities = new LinkedList<>();
this.childEntity2 = new ChildEntity();
this.ob1.childEntities.add(this.childEntity2);
this.childEntity3 = new ChildEntity();
this.ob1.childEntities.add(this.childEntity3);
this.childEntity4 = new ChildEntity();
this.ob1.singularReference = this.childEntity4;
this.ob1.multipleReference = new LinkedList<>();
this.childEntity5 = new ChildEntity();
this.ob1.multipleReference.add(this.childEntity5);
this.childEntity6 = new ChildEntity();
this.ob1.multipleReference.add(this.childEntity6);
this.ob1.lazyMultipleReference = new LinkedList<>();
this.childEntity7 = new ChildEntity();
this.ob1.lazyMultipleReference.add(this.childEntity7);
// mocked query results for entities and child entities.
QueryResults childTestEntityQueryResults = mock(QueryResults.class);
doAnswer((invocation) -> {
Arrays.asList(ce1).iterator().forEachRemaining(invocation.getArgument(0));
return null;
}).when(childTestEntityQueryResults).forEachRemaining(any());
QueryResults testEntityQueryResults = mock(QueryResults.class);
doAnswer((invocation) -> {
Arrays.asList(this.e1, this.e2).iterator().forEachRemaining(invocation.getArgument(0));
return null;
}).when(testEntityQueryResults).forEachRemaining(any());
setUpConverters(ce1, childTestEntityQuery, childTestEntityQueryResults, testEntityQueryResults);
}
use of com.google.cloud.datastore.Query in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQuery method applySelectWithFilter.
private void applySelectWithFilter(Object[] parameters, Builder builder) {
Iterator it = Arrays.asList(parameters).iterator();
Filter[] filters = this.filterParts.stream().map((part) -> {
// build properties chain for nested properties
// if the property is not nested, the list would contain only one property
List<DatastorePersistentProperty> propertiesChain = getPropertiesChain(part);
String fieldName = propertiesChain.stream().map(DatastorePersistentProperty::getFieldName).collect(Collectors.joining("."));
if (part.getType() == Part.Type.IS_NULL) {
return PropertyFilter.isNull(fieldName);
}
BiFunction<String, Value, PropertyFilter> filterFactory = FILTER_FACTORIES.get(part.getType());
if (filterFactory == null) {
throw new DatastoreDataException("Unsupported predicate keyword: " + part.getType());
}
if (!it.hasNext()) {
throw new DatastoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
}
Value convertedValue = convertParam(propertiesChain.get(propertiesChain.size() - 1), it.next());
return filterFactory.apply(fieldName, convertedValue);
}).toArray(Filter[]::new);
builder.setFilter((filters.length > 1) ? CompositeFilter.and(filters[0], Arrays.copyOfRange(filters, 1, filters.length)) : filters[0]);
}
use of com.google.cloud.datastore.Query in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method findAll.
@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);
}
Aggregations