Search in sources :

Example 1 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQuery method runQuery.

private Object runQuery(Object[] parameters, Class returnedElementType, Class<?> collectionType, boolean requiresCount) {
    ExecutionOptions options = new ExecutionOptions(returnedElementType, collectionType, requiresCount, getQueryMethod().isStreamQuery());
    DatastoreResultsIterable rawResults = getDatastoreOperations().queryKeysOrEntities(applyQueryBody(parameters, options.getQueryBuilder(), requiresCount, options.isSingularResult(), null), this.entityType);
    if (getQueryMethod().isStreamQuery()) {
        return StreamSupport.stream(rawResults.spliterator(), false);
    }
    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(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable)

Example 2 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQueryTests method pageableTestPageCursor.

@Test
void pageableTestPageCursor() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";
    Cursor cursorInPageable = Cursor.copyFrom("cde".getBytes());
    long countInPageable = 123L;
    Object[] paramVals = new Object[] { 1, new DatastorePageable(PageRequest.of(0, 2), cursorInPageable, countInPageable) };
    String[] paramNames = new String[] { "price", null };
    Parameters parameters = buildParameters(paramVals, paramNames);
    Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
    when(parameters.hasPageableParameter()).thenReturn(true);
    when(parameters.getPageableIndex()).thenReturn(1);
    GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, true, true);
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    doAnswer(invocation -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString()).isEqualTo(expected);
        Map<String, Object> paramMap = statement.getNamedBindings();
        assertThat(paramMap).hasSize(3);
        assertThat(((Value) paramMap.get("price")).get()).isEqualTo(1L);
        assertThat(((Value) paramMap.get("limit")).get()).isEqualTo(2L);
        assertThat(paramMap).containsEntry("offset", cursorInPageable);
        return new DatastoreResultsIterable(Collections.emptyList(), cursor);
    }).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
    doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
    doAnswer(invocation -> invocation.getArgument(0)).when(gqlDatastoreQuery).processRawObjectForProjection(any());
    Slice result = (Page) gqlDatastoreQuery.execute(paramVals);
    assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
    assertThat(((DatastorePageable) result.getPageable()).getTotalCount()).isEqualTo(countInPageable);
    assertThat(((Page) result).getTotalElements()).isEqualTo(countInPageable);
    verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), eq(Trade.class));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Page(org.springframework.data.domain.Page) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Slice(org.springframework.data.domain.Slice) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Example 3 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQueryTests method pageableTestSlice.

@Test
void pageableTestSlice() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    Object[] paramVals = new Object[] { 1, PageRequest.of(0, 2) };
    String[] paramNames = new String[] { "price", null };
    Parameters parameters = buildParameters(paramVals, paramNames);
    Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
    when(parameters.hasPageableParameter()).thenReturn(true);
    when(parameters.getPageableIndex()).thenReturn(1);
    GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, true);
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    List<Map> params = new ArrayList<>();
    doAnswer(invocation -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString()).isEqualTo("SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset");
        Map paramMap = statement.getNamedBindings();
        params.add(paramMap);
        return new DatastoreResultsIterable(Collections.emptyList(), cursor);
    }).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
    doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
    doAnswer(invocation -> invocation.getArgument(0)).when(gqlDatastoreQuery).processRawObjectForProjection(any());
    Slice result = (Slice) gqlDatastoreQuery.execute(paramVals);
    assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
    verify(this.datastoreTemplate, times(2)).queryKeysOrEntities(any(), eq(Trade.class));
    assertThat(((Value) params.get(0).get("price")).get()).isEqualTo(1L);
    assertThat(((Value) params.get(0).get("limit")).get()).isEqualTo(2L);
    assertThat(((Value) params.get(0).get("offset")).get()).isEqualTo(0L);
    assertThat(((Value) params.get(1).get("price")).get()).isEqualTo(1L);
    assertThat(((Value) params.get(1).get("limit")).get()).isEqualTo(1L);
    assertThat(params.get(1)).containsEntry("offset", cursor);
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) ArrayList(java.util.ArrayList) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Slice(org.springframework.data.domain.Slice) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 4 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQueryTests method usingIdField.

@Test
void usingIdField() throws NoSuchMethodException {
    Trade trade = new Trade();
    queryWithMockResult("findByActionAndId", null, getClass().getMethod("findByActionAndId", String.class, String.class), true, null);
    Object[] params = new Object[] { "BUY", "id1" };
    when(this.datastoreTemplate.createKey("trades", "id1")).thenAnswer(invocation -> Key.newBuilder("project", invocation.getArgument(0), invocation.getArgument(1)).build());
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer(invocation -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(CompositeFilter.and(PropertyFilter.eq("action", "BUY"), PropertyFilter.eq("__key__", KeyValue.of(Key.newBuilder("project", "trades", "id1").build())))).setKind("trades").setLimit(1).build();
        assertThat(statement).isEqualTo(expected);
        List<Trade> results = Collections.singletonList(trade);
        return new DatastoreResultsIterable(results.iterator(), null);
    });
    assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade);
}
Also used : DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.jupiter.api.Test)

Example 5 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQueryTests method preparePageResults.

private void preparePageResults(int offset, Integer limit, Cursor cursor, List<Integer> pageResults, List<Integer> fullResults) {
    when(this.datastoreTemplate.queryKeysOrEntities(isA(EntityQuery.class), any())).thenAnswer(invocation -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setStartCursor(cursor).setOffset(cursor != null ? 0 : offset).setOrderBy(OrderBy.desc("__key__")).setLimit(limit).build();
        assertThat(statement).isEqualTo(expected);
        return new DatastoreResultsIterable(pageResults.iterator(), Cursor.copyFrom("abc".getBytes()));
    });
    when(this.datastoreTemplate.queryKeysOrEntities(isA(KeyQuery.class), any())).thenAnswer(invocation -> {
        KeyQuery statement = invocation.getArgument(0);
        KeyQuery expected = StructuredQuery.newKeyQueryBuilder().setFilter(FILTER).setKind("trades").setOrderBy(OrderBy.desc("__key__")).build();
        assertThat(statement).isEqualTo(expected);
        return new DatastoreResultsIterable(fullResults.iterator(), Cursor.copyFrom("def".getBytes()));
    });
}
Also used : DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) KeyQuery(com.google.cloud.datastore.KeyQuery)

Aggregations

DatastoreResultsIterable (com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable)19 Test (org.junit.jupiter.api.Test)13 Cursor (com.google.cloud.datastore.Cursor)7 GqlQuery (com.google.cloud.datastore.GqlQuery)5 Sort (org.springframework.data.domain.Sort)5 EntityQuery (com.google.cloud.datastore.EntityQuery)4 Parameters (org.springframework.data.repository.query.Parameters)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 DatastoreQueryOptions (com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions)2 Page (org.springframework.data.domain.Page)2 FetchableFluentQuery (org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery)2 KeyQuery (com.google.cloud.datastore.KeyQuery)1 StructuredQuery (com.google.cloud.datastore.StructuredQuery)1 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)1 DiscriminatorField (com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorField)1 DatastorePageable (com.google.cloud.spring.data.datastore.repository.query.DatastorePageable)1