Search in sources :

Example 1 with Query

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;
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) Query(com.google.cloud.datastore.Query) KeyQuery(com.google.cloud.datastore.KeyQuery) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) StructuredQuery(com.google.cloud.datastore.StructuredQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) BaseKey(com.google.cloud.datastore.BaseKey)

Example 2 with Query

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));
}
Also used : DatastoreTransactionManagerAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.DatastoreTransactionManagerAutoConfiguration) Query(com.google.cloud.datastore.Query) AutoConfigurationPackage(org.springframework.boot.autoconfigure.AutoConfigurationPackage) GcpDatastoreAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.GcpDatastoreAutoConfiguration) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpDatastoreEmulatorAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.GcpDatastoreEmulatorAutoConfiguration) Datastore(com.google.cloud.datastore.Datastore) Supplier(java.util.function.Supplier) GcpProjectIdProvider(org.springframework.cloud.gcp.core.GcpProjectIdProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ResolvableType(org.springframework.core.ResolvableType) StructuredQuery(com.google.cloud.datastore.StructuredQuery) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) Credentials(com.google.auth.Credentials) Test(org.junit.Test) DatastoreException(com.google.cloud.datastore.DatastoreException) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) EntityQuery(com.google.cloud.datastore.EntityQuery) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) DatastoreRepositoriesAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.DatastoreRepositoriesAutoConfiguration) Id(org.springframework.data.annotation.Id) GcpContextAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration) Mockito.mock(org.mockito.Mockito.mock) Datastore(com.google.cloud.datastore.Datastore) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Supplier(java.util.function.Supplier) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) DatastoreException(com.google.cloud.datastore.DatastoreException) EntityQuery(com.google.cloud.datastore.EntityQuery) GcpProjectIdProvider(org.springframework.cloud.gcp.core.GcpProjectIdProvider) Test(org.junit.Test)

Example 3 with 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);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) Query(com.google.cloud.datastore.Query) KeyQuery(com.google.cloud.datastore.KeyQuery) GqlQuery(com.google.cloud.datastore.GqlQuery) StructuredQuery(com.google.cloud.datastore.StructuredQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) QueryResults(com.google.cloud.datastore.QueryResults) Before(org.junit.Before)

Example 4 with Query

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]);
}
Also used : Query(com.google.cloud.datastore.Query) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) LESS_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN_EQUAL) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) Map(java.util.Map) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Collector(java.util.stream.Collector) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) LESS_THAN(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Builder(com.google.cloud.datastore.StructuredQuery.Builder) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) KeyValue(com.google.cloud.datastore.KeyValue) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) List(java.util.List) PropertyDescriptor(java.beans.PropertyDescriptor) OrPart(org.springframework.data.repository.query.parser.PartTree.OrPart) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) PageImpl(org.springframework.data.domain.PageImpl) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Part(org.springframework.data.repository.query.parser.Part) Cursor(com.google.cloud.datastore.Cursor) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) StreamSupport(java.util.stream.StreamSupport) StructuredQuery(com.google.cloud.datastore.StructuredQuery) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) DatastoreOperations(org.springframework.cloud.gcp.data.datastore.core.DatastoreOperations) Value(com.google.cloud.datastore.Value) DatastoreQueryOptions(org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) Collections(java.util.Collections) Assert(org.springframework.util.Assert) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) BiFunction(java.util.function.BiFunction) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Iterator(java.util.Iterator) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) List(java.util.List) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 5 with Query

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);
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) Query(com.google.cloud.datastore.Query) KeyQuery(com.google.cloud.datastore.KeyQuery) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) StructuredQuery(com.google.cloud.datastore.StructuredQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) QueryResults(com.google.cloud.datastore.QueryResults)

Aggregations

EntityQuery (com.google.cloud.datastore.EntityQuery)5 Query (com.google.cloud.datastore.Query)5 StructuredQuery (com.google.cloud.datastore.StructuredQuery)5 KeyQuery (com.google.cloud.datastore.KeyQuery)4 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)3 QueryResults (com.google.cloud.datastore.QueryResults)2 DatastoreTemplate (org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate)2 DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)2 AfterQueryEvent (org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 Credentials (com.google.auth.Credentials)1 BaseKey (com.google.cloud.datastore.BaseKey)1 Cursor (com.google.cloud.datastore.Cursor)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Entity (com.google.cloud.datastore.Entity)1 FullEntity (com.google.cloud.datastore.FullEntity)1 GqlQuery (com.google.cloud.datastore.GqlQuery)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1