Search in sources :

Example 16 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project thingsboard by thingsboard.

the class BaseAssetService method findAssetsByQuery.

@Override
public ListenableFuture<List<Asset>> findAssetsByQuery(AssetSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Asset>> assets = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Asset>>) relations1 -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Asset>> futures = new ArrayList<>();
        for (EntityRelation relation : relations1) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.ASSET) {
                futures.add(findAssetByIdAsync(new AssetId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });
    assets = Futures.transform(assets, (Function<List<Asset>, List<Asset>>) assetList -> assetList == null ? Collections.emptyList() : assetList.stream().filter(asset -> query.getAssetTypes().contains(asset.getType())).collect(Collectors.toList()));
    return assets;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Customer(org.thingsboard.server.common.data.Customer) AssetId(org.thingsboard.server.common.data.id.AssetId) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) Function(com.google.common.base.Function) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) AssetSearchQuery(org.thingsboard.server.common.data.asset.AssetSearchQuery) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId)

Example 17 with AsyncFunction

use of com.google.common.util.concurrent.AsyncFunction in project druid by druid-io.

the class BatchAppenderatorDriver method pushAndClear.

private SegmentsAndCommitMetadata pushAndClear(Collection<String> sequenceNames, long pushAndClearTimeoutMs) throws InterruptedException, ExecutionException, TimeoutException {
    final Set<SegmentIdWithShardSpec> requestedSegmentIdsForSequences = getAppendingSegments(sequenceNames);
    final ListenableFuture<SegmentsAndCommitMetadata> future = Futures.transform(pushInBackground(null, requestedSegmentIdsForSequences, false), (AsyncFunction<SegmentsAndCommitMetadata, SegmentsAndCommitMetadata>) this::dropInBackground);
    final SegmentsAndCommitMetadata segmentsAndCommitMetadata = pushAndClearTimeoutMs == 0L ? future.get() : future.get(pushAndClearTimeoutMs, TimeUnit.MILLISECONDS);
    // Sanity check
    final Map<SegmentIdWithShardSpec, DataSegment> pushedSegmentIdToSegmentMap = segmentsAndCommitMetadata.getSegments().stream().collect(Collectors.toMap(SegmentIdWithShardSpec::fromDataSegment, Function.identity()));
    if (!pushedSegmentIdToSegmentMap.keySet().equals(requestedSegmentIdsForSequences)) {
        throw new ISE("Pushed segments[%s] are different from the requested ones[%s]", pushedSegmentIdToSegmentMap.keySet(), requestedSegmentIdsForSequences);
    }
    synchronized (segments) {
        for (String sequenceName : sequenceNames) {
            final SegmentsForSequence segmentsForSequence = segments.get(sequenceName);
            if (segmentsForSequence == null) {
                throw new ISE("Can't find segmentsForSequence for sequence[%s]", sequenceName);
            }
            segmentsForSequence.getAllSegmentsOfInterval().forEach(segmentsOfInterval -> {
                final SegmentWithState appendingSegment = segmentsOfInterval.getAppendingSegment();
                if (appendingSegment != null) {
                    final DataSegment pushedSegment = pushedSegmentIdToSegmentMap.get(appendingSegment.getSegmentIdentifier());
                    if (pushedSegment == null) {
                        throw new ISE("Can't find pushedSegments for segment[%s]", appendingSegment.getSegmentIdentifier());
                    }
                    segmentsOfInterval.finishAppendingToCurrentActiveSegment(segmentWithState -> segmentWithState.pushAndDrop(pushedSegment));
                }
            });
        }
    }
    return segmentsAndCommitMetadata;
}
Also used : ISE(org.apache.druid.java.util.common.ISE) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) DataSegment(org.apache.druid.timeline.DataSegment)

Aggregations

AsyncFunction (com.google.common.util.concurrent.AsyncFunction)17 Futures (com.google.common.util.concurrent.Futures)14 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)14 List (java.util.List)12 ArrayList (java.util.ArrayList)11 Function (com.google.common.base.Function)7 FutureCallback (com.google.common.util.concurrent.FutureCallback)7 Slf4j (lombok.extern.slf4j.Slf4j)7 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)6 Collections (java.util.Collections)6 Nonnull (javax.annotation.Nonnull)6 Future (java.util.concurrent.Future)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)5 Optional (com.google.common.base.Optional)4 JdkFutureAdapters (com.google.common.util.concurrent.JdkFutureAdapters)4 BigInteger (java.math.BigInteger)4 UUID (java.util.UUID)4 Nullable (javax.annotation.Nullable)4 Inject (javax.inject.Inject)4