Search in sources :

Example 1 with AssetInfoQueryImpl

use of org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl in project kapua by eclipse.

the class MessageStoreServiceImpl method resetCache.

private void resetCache(String accountName, String topic) throws Exception {
    boolean isAnyAsset;
    boolean isAssetToDelete = false;
    String semTopic;
    if (topic != null) {
        // determine if we should delete an asset if topic = account/asset/#
        KapuaTopic kapuaTopic = new KapuaTopic(topic);
        isAnyAsset = kapuaTopic.isAnyAsset();
        semTopic = kapuaTopic.getSemanticTopic();
        if (semTopic.isEmpty() && !isAnyAsset)
            isAssetToDelete = true;
    } else {
        isAnyAsset = true;
        semTopic = "";
        isAssetToDelete = true;
    }
    // Find all topics
    String everyIndex = EsUtils.getAnyIndexName(accountName);
    int pageSize = 1000;
    int offset = 0;
    long totalHits = 1;
    MetricInfoQueryImpl metricQuery = new MetricInfoQueryImpl();
    metricQuery.setLimit(pageSize + 1);
    metricQuery.setOffset(offset);
    TopicMatchPredicateImpl topicPredicate = new TopicMatchPredicateImpl();
    topicPredicate.setExpression(topic);
    metricQuery.setPredicate(topicPredicate);
    // Remove metrics
    while (totalHits > 0) {
        MetricInfoListResult metrics = EsMetricDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.METRIC_TYPE_NAME).query(metricQuery);
        totalHits = metrics.size();
        LocalCache<String, Boolean> metricsCache = DatastoreCacheManager.getInstance().getMetricsCache();
        long toBeProcessed = totalHits > pageSize ? pageSize : totalHits;
        for (int i = 0; i < toBeProcessed; i++) {
            String id = metrics.get(i).getId().toString();
            if (metricsCache.get(id))
                metricsCache.remove(id);
        }
        if (totalHits > pageSize)
            offset += (pageSize + 1);
    }
    logger.debug(String.format("Removed cached topic metrics for [%s]", topic));
    EsMetricDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.METRIC_TYPE_NAME).deleteByQuery(metricQuery);
    logger.debug(String.format("Removed topic metrics for [%s]", topic));
    // 
    TopicInfoQueryImpl topicQuery = new TopicInfoQueryImpl();
    topicQuery.setLimit(pageSize + 1);
    topicQuery.setOffset(offset);
    topicPredicate = new TopicMatchPredicateImpl();
    topicPredicate.setExpression(topic);
    topicQuery.setPredicate(topicPredicate);
    // Remove topic
    offset = 0;
    totalHits = 1;
    while (totalHits > 0) {
        TopicInfoListResult topics = EsTopicDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.TOPIC_TYPE_NAME).query(topicQuery);
        totalHits = topics.size();
        LocalCache<String, Boolean> topicsCache = DatastoreCacheManager.getInstance().getTopicsCache();
        long toBeProcessed = totalHits > pageSize ? pageSize : totalHits;
        for (int i = 0; i < toBeProcessed; i++) {
            String id = topics.get(0).getId().toString();
            if (topicsCache.get(id))
                topicsCache.remove(id);
        }
        if (totalHits > pageSize)
            offset += (pageSize + 1);
    }
    logger.debug(String.format("Removed cached topics for [%s]", topic));
    EsTopicDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.TOPIC_TYPE_NAME).deleteByQuery(topicQuery);
    logger.debug(String.format("Removed topics for [%s]", topic));
    // Remove asset
    if (isAssetToDelete) {
        AssetInfoQueryImpl assetQuery = new AssetInfoQueryImpl();
        assetQuery.setLimit(pageSize + 1);
        assetQuery.setOffset(offset);
        topicPredicate = new TopicMatchPredicateImpl();
        topicPredicate.setExpression(topic);
        assetQuery.setPredicate(topicPredicate);
        offset = 0;
        totalHits = 1;
        while (totalHits > 0) {
            AssetInfoListResult assets = EsAssetDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.ASSET_TYPE_NAME).query(assetQuery);
            totalHits = assets.size();
            LocalCache<String, Boolean> assetsCache = DatastoreCacheManager.getInstance().getAssetsCache();
            long toBeProcessed = totalHits > pageSize ? pageSize : totalHits;
            for (int i = 0; i < toBeProcessed; i++) {
                String id = assets.get(i).getId().toString();
                if (assetsCache.get(id))
                    assetsCache.remove(id);
            }
            if (totalHits > pageSize)
                offset += (pageSize + 1);
        }
        logger.debug(String.format("Removed cached assets for [%s]", topic));
        EsAssetDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.ASSET_TYPE_NAME).deleteByQuery(assetQuery);
        logger.debug(String.format("Removed assets for [%s]", topic));
    }
}
Also used : KapuaTopic(org.eclipse.kapua.service.datastore.internal.elasticsearch.KapuaTopic) MetricInfoListResult(org.eclipse.kapua.service.datastore.model.MetricInfoListResult) AssetInfoListResult(org.eclipse.kapua.service.datastore.model.AssetInfoListResult) MetricInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl) TopicInfoListResult(org.eclipse.kapua.service.datastore.model.TopicInfoListResult) TopicInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl) AssetInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl) TopicMatchPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicMatchPredicateImpl)

Example 2 with AssetInfoQueryImpl

use of org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl in project kapua by eclipse.

the class AssetInfoStoreServiceImpl method find.

@Override
public AssetInfo find(KapuaId scopeId, StorableId id) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(id, "id");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.read);
    AssetInfoQueryImpl q = new AssetInfoQueryImpl();
    q.setLimit(1);
    ArrayList<StorableId> ids = new ArrayList<StorableId>();
    ids.add(id);
    AndPredicateImpl allPredicates = new AndPredicateImpl();
    allPredicates.addPredicate(new IdsPredicateImpl(EsMessageField.ID, ids));
    AssetInfoListResult result = this.query(scopeId, q);
    if (result == null || result.size() == 0)
        return null;
    AssetInfo assetInfo = result.get(0);
    return assetInfo;
}
Also used : StorableId(org.eclipse.kapua.service.datastore.model.StorableId) IdsPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.IdsPredicateImpl) AndPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.AndPredicateImpl) AssetInfoListResult(org.eclipse.kapua.service.datastore.model.AssetInfoListResult) ArrayList(java.util.ArrayList) AssetInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl) AssetInfo(org.eclipse.kapua.service.datastore.model.AssetInfo)

Example 3 with AssetInfoQueryImpl

use of org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl in project kapua by eclipse.

the class EsAssetDAO method query.

// 
// public void deleteByAccount(long start, long end)
// {
// this.deleteByQuery(this.getQueryByAssetAndDate(KapuaTopic.SINGLE_LEVEL_WCARD, true, start, end));
// }
// 
// public void deleteByAsset(String asset, boolean isAnyAsset)
// {
// this.deleteByQuery(this.getQueryByAsset(asset, isAnyAsset));
// }
public AssetInfoListResult query(AssetInfoQuery query) throws UnknownHostException, KapuaException, EsDatastoreException, ParseException {
    AssetInfoQueryImpl localQuery = new AssetInfoQueryImpl();
    localQuery.copy(query);
    // get one plus (if there is one) to later get the next key value
    localQuery.setLimit(query.getLimit() + 1);
    AssetInfoQueryConverter aic = new AssetInfoQueryConverter();
    SearchRequestBuilder builder = aic.toSearchRequestBuilder(esTypeDAO.getIndexName(), esTypeDAO.getTypeName(), query);
    SearchResponse response = builder.get(TimeValue.timeValueMillis(EsUtils.getQueryTimeout()));
    SearchHits searchHits = response.getHits();
    if (searchHits == null || searchHits.getTotalHits() == 0)
        return new AssetInfoListResultImpl();
    int i = 0;
    int searchHitsSize = searchHits.getHits().length;
    List<AssetInfo> assetInfos = new ArrayList<AssetInfo>();
    AssetInfoBuilder assetInfoBuilder = new AssetInfoBuilder();
    for (SearchHit searchHit : searchHits.getHits()) {
        if (i < query.getLimit()) {
            AssetInfo assetInfo = assetInfoBuilder.build(searchHit).getAssetInfo();
            assetInfos.add(assetInfo);
        }
        i++;
    }
    // TODO check equivalence with CX with Pierantonio
    // TODO what is this nextKey
    Object nextKey = null;
    if (searchHits.getTotalHits() > query.getLimit()) {
        nextKey = query.getLimit();
    }
    AssetInfoListResultImpl result = new AssetInfoListResultImpl(nextKey, searchHitsSize);
    result.addAll(assetInfos);
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) AssetInfoQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.AssetInfoQueryConverter) ArrayList(java.util.ArrayList) AssetInfoBuilder(org.eclipse.kapua.service.datastore.internal.elasticsearch.AssetInfoBuilder) AssetInfo(org.eclipse.kapua.service.datastore.model.AssetInfo) SearchResponse(org.elasticsearch.action.search.SearchResponse) AssetInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl) SearchHits(org.elasticsearch.search.SearchHits) AssetInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.AssetInfoListResultImpl)

Aggregations

AssetInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl)3 ArrayList (java.util.ArrayList)2 AssetInfo (org.eclipse.kapua.service.datastore.model.AssetInfo)2 AssetInfoListResult (org.eclipse.kapua.service.datastore.model.AssetInfoListResult)2 AssetInfoBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.AssetInfoBuilder)1 AssetInfoQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.AssetInfoQueryConverter)1 KapuaTopic (org.eclipse.kapua.service.datastore.internal.elasticsearch.KapuaTopic)1 AssetInfoListResultImpl (org.eclipse.kapua.service.datastore.internal.model.AssetInfoListResultImpl)1 AndPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.AndPredicateImpl)1 IdsPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.IdsPredicateImpl)1 MetricInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl)1 TopicInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl)1 TopicMatchPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.TopicMatchPredicateImpl)1 MetricInfoListResult (org.eclipse.kapua.service.datastore.model.MetricInfoListResult)1 StorableId (org.eclipse.kapua.service.datastore.model.StorableId)1 TopicInfoListResult (org.eclipse.kapua.service.datastore.model.TopicInfoListResult)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1