Search in sources :

Example 1 with DatastoreResultsIterable

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

the class GqlDatastoreQuery method execute.

@Override
public Object execute(Object[] parameters) {
    if (getAnnotation(this.entityType.getSuperclass(), DiscriminatorField.class) != null) {
        throw new DatastoreDataException("Can't append discrimination condition");
    }
    ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues = new ParsedQueryWithTagsAndValues(this.originalParamTags, parameters);
    GqlQuery query = parsedQueryWithTagsAndValues.bindArgsToGqlQuery();
    Class returnedItemType = this.queryMethod.getReturnedObjectType();
    boolean isNonEntityReturnType = isNonEntityReturnedType(returnedItemType);
    DatastoreResultsIterable found = isNonEntityReturnType ? this.datastoreOperations.queryIterable(query, GqlDatastoreQuery::getNonEntityObjectFromRow) : this.datastoreOperations.queryKeysOrEntities(query, this.entityType);
    Object result;
    if (isPageQuery() || isSliceQuery()) {
        result = buildPageOrSlice(parameters, parsedQueryWithTagsAndValues, found);
    } else if (this.queryMethod.isCollectionQuery()) {
        result = convertCollectionResult(returnedItemType, found);
    } else {
        result = convertSingularResult(returnedItemType, isNonEntityReturnType, found);
    }
    return result;
}
Also used : DiscriminatorField(org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorField) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) GqlQuery(com.google.cloud.datastore.GqlQuery)

Example 2 with DatastoreResultsIterable

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

the class SimpleDatastoreRepository method findAll.

@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
    Assert.notNull(pageable, "A non-null pageable is required.");
    Iterable<S> entities = this.datastoreTemplate.queryByExample(example, new DatastoreQueryOptions.Builder().setLimit(pageable.getPageSize()).setOffset((int) pageable.getOffset()).setSort(pageable.getSort()).setCursor(getCursor(pageable)).build());
    List<S> result = StreamSupport.stream(entities.spliterator(), false).collect(Collectors.toList());
    Long totalCount = getOrComputeTotalCount(pageable, () -> count(example));
    Pageable cursorPageable = DatastorePageable.from(pageable, entities instanceof DatastoreResultsIterable ? ((DatastoreResultsIterable) entities).getCursor() : null, totalCount);
    return new PageImpl<>(result, cursorPageable, totalCount);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Pageable(org.springframework.data.domain.Pageable) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)

Example 3 with DatastoreResultsIterable

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

the class SimpleDatastoreRepositoryTests method findOneByExample.

@Test
public void findOneByExample() {
    Example<Object> example = Example.of(new Object());
    doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(1).build()));
    assertThat(this.simpleDatastoreRepository.findOne(example).get()).isEqualTo(1);
    verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(1).build()));
}
Also used : DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Test(org.junit.Test)

Example 4 with DatastoreResultsIterable

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

the class PartTreeDatastoreQueryTests method usingIdField.

@Test
public 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(eq("trades"), eq("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(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 5 with DatastoreResultsIterable

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

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(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) KeyQuery(com.google.cloud.datastore.KeyQuery)

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