Search in sources :

Example 1 with SliceImpl

use of org.springframework.data.domain.SliceImpl in project spring-data-mongodb by spring-projects.

the class StringBasedAggregation method doExecute.

/*
	 * (non-Javascript)
	 * @see org.springframework.data.mongodb.repository.query.AbstractReactiveMongoQuery#doExecute(org.springframework.data.mongodb.repository.query.MongoQueryMethod, org.springframework.data.repository.query.ResultProcessor, org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor, java.lang.Class)
	 */
@Override
protected Object doExecute(MongoQueryMethod method, ResultProcessor resultProcessor, ConvertingParameterAccessor accessor, Class<?> typeToRead) {
    Class<?> sourceType = method.getDomainClass();
    Class<?> targetType = typeToRead;
    List<AggregationOperation> pipeline = computePipeline(method, accessor);
    AggregationUtils.appendSortIfPresent(pipeline, accessor, typeToRead);
    if (method.isSliceQuery()) {
        AggregationUtils.appendLimitAndOffsetIfPresent(pipeline, accessor, LongUnaryOperator.identity(), limit -> limit + 1);
    } else {
        AggregationUtils.appendLimitAndOffsetIfPresent(pipeline, accessor);
    }
    boolean isSimpleReturnType = isSimpleReturnType(typeToRead);
    boolean isRawAggregationResult = ClassUtils.isAssignable(AggregationResults.class, typeToRead);
    if (isSimpleReturnType) {
        targetType = Document.class;
    } else if (isRawAggregationResult) {
        // 🙈
        targetType = method.getReturnType().getRequiredActualType().getRequiredComponentType().getType();
    }
    AggregationOptions options = computeOptions(method, accessor);
    TypedAggregation<?> aggregation = new TypedAggregation<>(sourceType, pipeline, options);
    if (method.isStreamQuery()) {
        Stream<?> stream = mongoOperations.aggregateStream(aggregation, targetType).stream();
        if (isSimpleReturnType) {
            return stream.map(it -> AggregationUtils.extractSimpleTypeResult((Document) it, typeToRead, mongoConverter));
        }
        return stream;
    }
    AggregationResults<Object> result = (AggregationResults<Object>) mongoOperations.aggregate(aggregation, targetType);
    if (isRawAggregationResult) {
        return result;
    }
    List<Object> results = result.getMappedResults();
    if (method.isCollectionQuery()) {
        return isSimpleReturnType ? convertResults(typeToRead, results) : results;
    }
    if (method.isSliceQuery()) {
        Pageable pageable = accessor.getPageable();
        int pageSize = pageable.getPageSize();
        List<Object> resultsToUse = isSimpleReturnType ? convertResults(typeToRead, results) : results;
        boolean hasNext = resultsToUse.size() > pageSize;
        return new SliceImpl<>(hasNext ? resultsToUse.subList(0, pageSize) : resultsToUse, pageable, hasNext);
    }
    Object uniqueResult = result.getUniqueMappedResult();
    return isSimpleReturnType ? AggregationUtils.extractSimpleTypeResult((Document) uniqueResult, typeToRead, mongoConverter) : uniqueResult;
}
Also used : AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) Document(org.bson.Document) SliceImpl(org.springframework.data.domain.SliceImpl) Pageable(org.springframework.data.domain.Pageable) AggregationResults(org.springframework.data.mongodb.core.aggregation.AggregationResults) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) AggregationOperation(org.springframework.data.mongodb.core.aggregation.AggregationOperation)

Example 2 with SliceImpl

use of org.springframework.data.domain.SliceImpl in project credhub by cloudfoundry-incubator.

the class EncryptionKeyRotatorTest method beforeEach.

@Before
public void beforeEach() {
    oldUuid = UUID.randomUUID();
    UUID activeUuid = UUID.randomUUID();
    encryptedValueDataService = mock(EncryptedValueDataService.class);
    keySet = new EncryptionKeySet();
    keySet.add(new EncryptionKey(mock(EncryptionService.class), oldUuid, mock(Key.class)));
    keySet.add(new EncryptionKey(mock(EncryptionService.class), activeUuid, mock(Key.class)));
    keySet.setActive(activeUuid);
    encryptedValue1 = mock(EncryptedValue.class);
    encryptedValue2 = mock(EncryptedValue.class);
    encryptedValue3 = mock(EncryptedValue.class);
    encryptionKeyCanaryMapper = mock(EncryptionKeyCanaryMapper.class);
    inactiveCanaries = newArrayList(oldUuid);
    when(encryptedValueDataService.findByCanaryUuids(inactiveCanaries)).thenReturn(new SliceImpl<>(asList(encryptedValue1, encryptedValue2))).thenReturn(new SliceImpl<>(asList(encryptedValue3))).thenReturn(new SliceImpl<>(new ArrayList<>()));
    final EncryptionKeyRotator encryptionKeyRotator = new EncryptionKeyRotator(encryptedValueDataService, encryptionKeyCanaryMapper, keySet);
    encryptionKeyRotator.rotate();
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) UUID(java.util.UUID) EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) EncryptedValueDataService(org.cloudfoundry.credhub.data.EncryptedValueDataService) SliceImpl(org.springframework.data.domain.SliceImpl) Before(org.junit.Before)

Aggregations

SliceImpl (org.springframework.data.domain.SliceImpl)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 Document (org.bson.Document)1 EncryptedValueDataService (org.cloudfoundry.credhub.data.EncryptedValueDataService)1 EncryptedValue (org.cloudfoundry.credhub.entity.EncryptedValue)1 Before (org.junit.Before)1 Pageable (org.springframework.data.domain.Pageable)1 AggregationOperation (org.springframework.data.mongodb.core.aggregation.AggregationOperation)1 AggregationOptions (org.springframework.data.mongodb.core.aggregation.AggregationOptions)1 AggregationResults (org.springframework.data.mongodb.core.aggregation.AggregationResults)1 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)1