Search in sources :

Example 11 with DatastoreResultsIterable

use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.

the class SimpleDatastoreRepositoryTests method findAllByExamplePageCursor.

@Test
public void findAllByExamplePageCursor() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2), cursor)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort).build()));
    doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(3, 4), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor).build()));
    doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)).when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
    Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(0, 2, sort));
    assertThat(result).containsExactly(1, 2);
    assertThat(result.getTotalElements()).isEqualTo(5);
    Page<Object> resultNext = this.simpleDatastoreRepository.findAll(example, result.getPageable().next());
    assertThat(resultNext).containsExactly(3, 4);
    assertThat(resultNext.getTotalElements()).isEqualTo(5);
    verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort).build()));
    verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor).build()));
    verify(this.datastoreTemplate, times(1)).keyQueryByExample(same(example), isNull());
}
Also used : DatastoreQueryOptions(org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions) Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor) Test(org.junit.Test)

Example 12 with DatastoreResultsIterable

use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.

the class SimpleDatastoreRepositoryTests method findAllByExamplePage.

@Test
public void findAllByExamplePage() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).build()));
    doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)).when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
    Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(1, 2, sort));
    assertThat(result).containsExactly(1, 2);
    assertThat(result.getTotalElements()).isEqualTo(5);
    verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).build()));
    verify(this.datastoreTemplate, times(1)).keyQueryByExample(same(example), isNull());
}
Also used : Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Test(org.junit.Test)

Example 13 with DatastoreResultsIterable

use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQueryTests method prepareDeleteResults.

private void prepareDeleteResults(boolean isCollection) {
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
        StructuredQuery<?> statement = invocation.getArgument(0);
        StructuredQuery.Builder builder = isCollection ? StructuredQuery.newEntityQueryBuilder() : StructuredQuery.newKeyQueryBuilder();
        StructuredQuery<?> expected = builder.setFilter(PropertyFilter.eq("action", "BUY")).setKind("trades").build();
        assertThat(statement).isEqualTo(expected);
        return new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor);
    });
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor)

Example 14 with DatastoreResultsIterable

use of org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method runQuery.

private Object runQuery(Object[] parameters, Class returnedElementType, Class<?> collectionType, boolean requiresCount) {
    ExecutionOptions options = new ExecutionOptions(returnedElementType, collectionType, requiresCount);
    DatastoreResultsIterable rawResults = getDatastoreOperations().queryKeysOrEntities(applyQueryBody(parameters, options.getQueryBuilder(), requiresCount, options.isSingularResult(), null), this.entityType);
    Object result = StreamSupport.stream(rawResults.spliterator(), false).map(options.isReturnedTypeIsNumber() ? Function.identity() : this::processRawObjectForProjection).collect(options.getResultsCollector());
    if (this.tree.isDelete()) {
        deleteFoundEntities(options.isReturnedTypeIsNumber(), (Iterable) result);
    }
    if (!this.tree.isExistsProjection() && !options.isCountingQuery()) {
        return new ExecutionResult(convertResultCollection(result, collectionType), rawResults.getCursor());
    } else if (options.isCountingQuery() && this.tree.isDelete()) {
        return ((List) result).size();
    } else {
        return result;
    }
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)

Aggregations

DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)14 Test (org.junit.Test)8 Cursor (com.google.cloud.datastore.Cursor)6 EntityQuery (com.google.cloud.datastore.EntityQuery)4 GqlQuery (com.google.cloud.datastore.GqlQuery)4 DoubleValue (com.google.cloud.datastore.DoubleValue)3 KeyValue (com.google.cloud.datastore.KeyValue)3 LongValue (com.google.cloud.datastore.LongValue)3 Value (com.google.cloud.datastore.Value)3 Slice (org.springframework.data.domain.Slice)3 Parameters (org.springframework.data.repository.query.Parameters)3 Page (org.springframework.data.domain.Page)2 Sort (org.springframework.data.domain.Sort)2 KeyQuery (com.google.cloud.datastore.KeyQuery)1 StructuredQuery (com.google.cloud.datastore.StructuredQuery)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 DatastoreQueryOptions (org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions)1 DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)1 DiscriminatorField (org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorField)1