Search in sources :

Example 1 with TopicInfoListResult

use of org.eclipse.kapua.service.datastore.model.TopicInfoListResult 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 TopicInfoListResult

use of org.eclipse.kapua.service.datastore.model.TopicInfoListResult in project kapua by eclipse.

the class EsTopicDAO method query.

// 
// public BoolQueryBuilder getQueryBtTopic(String account,
// boolean isAnyAccount,
// String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic)
// {
// 
// QueryBuilder accountQuery = null;
// if (!isAnyAccount)
// accountQuery = QueryBuilders.termQuery(EsSchema.TOPIC_ACCOUNT, account);
// 
// // Asset clauses
// QueryBuilder assetQuery = null;
// if (!isAnyAsset) {
// assetQuery = QueryBuilders.termQuery(EsSchema.TOPIC_ASSET, asset);
// }
// 
// // Topic clauses
// QueryBuilder topicQuery = null;
// if (isAnySubtopic) {
// topicQuery = QueryBuilders.prefixQuery(EsSchema.TOPIC_SEM_NAME, semTopic);
// }
// else {
// topicQuery = QueryBuilders.termQuery(EsSchema.TOPIC_SEM_NAME, semTopic);
// }
// 
// // Composite clause
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 
// if (accountQuery != null)
// boolQuery.must(accountQuery);
// 
// if (assetQuery != null)
// boolQuery.must(assetQuery);
// 
// boolQuery.must(topicQuery);
// 
// return boolQuery;
// }
// 
// public BoolQueryBuilder getQueryBtTopicAndDate(String account,
// boolean isAnyAccount,
// String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// 
// BoolQueryBuilder boolQuery = this.getQueryBtTopic(account, isAnyAccount, asset, isAnyAsset, semTopic, isAnySubtopic);
// 
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.TOPIC_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
// //
// 
// return boolQuery;
// }
// 
// public void deleteByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic)
// {
// 
// boolean isAnyAccount = true;
// BoolQueryBuilder boolQuery = this.getQueryBtTopic(null, isAnyAccount, asset, isAnyAsset, semTopic, isAnySubtopic);
// 
// // delete by query API is deprecated, scroll with bulk delete must be used
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
// 
// public void deleteByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// 
// boolean isAnyAccount = true;
// BoolQueryBuilder boolQuery = this.getQueryBtTopicAndDate(null, isAnyAccount, asset, isAnyAsset, semTopic, isAnySubtopic, start, end);
// 
// // delete by query API is deprecated, scroll with bulk delete must be used
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
// 
// public void deleteByAccount(String accountName, long start, long end)
// throws KapuaInvalidTopicException
// {
// 
// KapuaTopic topic = new KapuaTopic(accountName, KapuaTopic.SINGLE_LEVEL_WCARD, KapuaTopic.MULTI_LEVEL_WCARD);
// String asset = topic.getAsset();
// boolean anyAsset = KapuaTopic.SINGLE_LEVEL_WCARD.equals(asset);
// String semTopic = topic.getSemanticTopic();
// boolean topicPrefix = KapuaTopic.MULTI_LEVEL_WCARD.equals(semTopic);
// if (topicPrefix) {
// semTopic = topic.getParentTopic();
// semTopic = semTopic == null ? "" : semTopic;
// }
// 
// this.deleteByTopic(asset, anyAsset, semTopic, topicPrefix, start, end);
// }
// 
// public SearchHits findByAccount(String semTopic,
// boolean isAnySubtopic,
// int offset,
// int size)
// {
// 
// long timeout = EsUtils.getQueryTimeout();
// 
// SearchResponse response = esTypeDAO.getClient().prepareSearch(esTypeDAO.getIndexName())
// .setTypes(esTypeDAO.getTypeName())
// .setFetchSource(false)
// .addFields("sem_topic", "timestamp", "asset", "account")
// .setFrom(offset)
// .setSize(size)
// .get(TimeValue.timeValueMillis(timeout));
// 
// SearchHits searchHits = response.getHits();
// return searchHits;
// }
public TopicInfoListResult query(TopicInfoQuery query) throws UnknownHostException, KapuaException, EsDatastoreException, KapuaInvalidTopicException, ParseException {
    // get one plus (if there is one) to later get the next key value
    TopicInfoQueryImpl localQuery = new TopicInfoQueryImpl();
    localQuery.setLimit(query.getLimit() + 1);
    TopicInfoQueryConverter tic = new TopicInfoQueryConverter();
    SearchRequestBuilder builder = tic.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 TopicInfoListResultImpl();
    int i = 0;
    int searchHitsSize = searchHits.getHits().length;
    List<TopicInfo> topicInfos = new ArrayList<TopicInfo>();
    TopicInfoBuilder topicInfoBuilder = new TopicInfoBuilder();
    for (SearchHit searchHit : searchHits.getHits()) {
        if (i < query.getLimit()) {
            TopicInfo topicInfo = topicInfoBuilder.buildFromTopic(searchHit).getTopicInfo();
            topicInfos.add(topicInfo);
        }
        i++;
    }
    // TODO check equivalence with CX with Pierantonio
    // TODO what is this nextKey
    Object nextKey = null;
    if (searchHits.getTotalHits() > query.getLimit()) {
        nextKey = query.getLimit();
    }
    TopicInfoListResult result = new TopicInfoListResultImpl(nextKey, searchHitsSize);
    result.addAll(topicInfos);
    return result;
}
Also used : TopicInfoQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoQueryConverter) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) TopicInfoListResult(org.eclipse.kapua.service.datastore.model.TopicInfoListResult) TopicInfo(org.eclipse.kapua.service.datastore.model.TopicInfo) SearchResponse(org.elasticsearch.action.search.SearchResponse) TopicInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl) SearchHits(org.elasticsearch.search.SearchHits) TopicInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.TopicInfoListResultImpl) TopicInfoBuilder(org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoBuilder)

Example 3 with TopicInfoListResult

use of org.eclipse.kapua.service.datastore.model.TopicInfoListResult in project kapua by eclipse.

the class TopicInfoStoreServiceImpl method delete.

@Override
public void delete(KapuaId scopeId, TopicInfoQuery query) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(query, "query");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.delete);
    // 
    // Do the find
    AccountInfo accountInfo = getAccountServicePlan(scopeId);
    String scopeName = accountInfo.getAccount().getName();
    LocalServicePlan accountServicePlan = accountInfo.getServicePlan();
    long ttl = accountServicePlan.getDataTimeToLive() * DAY_MILLIS;
    if (!accountServicePlan.getDataStorageEnabled() || ttl == LocalServicePlan.DISABLED) {
        logger.debug("Storage not enabled for account {}, skipping delete", scopeName);
        return;
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        TopicInfoListResult topics = this.query(scopeId, query);
        for (TopicInfo topicInfo : topics) {
            // TODO Improve performances
            MessageQueryImpl mqi = new MessageQueryImpl();
            TopicMatchPredicateImpl predicate = new TopicMatchPredicateImpl(topicInfo.getFullTopicName());
            mqi.setPredicate(predicate);
            EsMessageDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.MESSAGE_TYPE_NAME).setListener(null).deleteByQuery(mqi);
            MetricInfoQueryImpl miqi = new MetricInfoQueryImpl();
            mqi.setPredicate(predicate);
            EsMetricDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.METRIC_TYPE_NAME).setListener(null).deleteByQuery(miqi);
        }
        EsTopicDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.TOPIC_TYPE_NAME).deleteByQuery(query);
        return;
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : MessageQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl) LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) TopicInfoListResult(org.eclipse.kapua.service.datastore.model.TopicInfoListResult) MetricInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl) TopicMatchPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicMatchPredicateImpl) TopicInfo(org.eclipse.kapua.service.datastore.model.TopicInfo) KapuaException(org.eclipse.kapua.KapuaException)

Example 4 with TopicInfoListResult

use of org.eclipse.kapua.service.datastore.model.TopicInfoListResult in project kapua by eclipse.

the class TopicInfoStoreServiceImpl method query.

@Override
public TopicInfoListResult query(KapuaId scopeId, TopicInfoQuery query) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(query, "query");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.read);
    // 
    // Do the find
    AccountInfo accountInfo = getAccountServicePlan(scopeId);
    String scopeName = accountInfo.getAccount().getName();
    LocalServicePlan accountServicePlan = accountInfo.getServicePlan();
    long ttl = accountServicePlan.getDataTimeToLive() * DAY_MILLIS;
    if (!accountServicePlan.getDataStorageEnabled() || ttl == LocalServicePlan.DISABLED) {
        logger.debug("Storage not enabled for account {}, returning empty result", scopeName);
        return new TopicInfoListResultImpl();
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        TopicInfoListResult result = null;
        result = EsTopicDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.TOPIC_TYPE_NAME).query(query);
        return result;
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) TopicInfoListResult(org.eclipse.kapua.service.datastore.model.TopicInfoListResult) TopicInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.TopicInfoListResultImpl) KapuaException(org.eclipse.kapua.KapuaException)

Example 5 with TopicInfoListResult

use of org.eclipse.kapua.service.datastore.model.TopicInfoListResult in project kapua by eclipse.

the class TopicInfoStoreServiceImpl method find.

@Override
public TopicInfo find(KapuaId scopeId, StorableId id) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(id, "id");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.read);
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(id, "id");
    TopicInfoQueryImpl q = new TopicInfoQueryImpl();
    q.setLimit(1);
    ArrayList<StorableId> ids = new ArrayList<StorableId>();
    ids.add(id);
    AndPredicateImpl allPredicates = new AndPredicateImpl();
    allPredicates.addPredicate(new IdsPredicateImpl(EsMessageField.ID, ids));
    TopicInfoListResult result = this.query(scopeId, q);
    if (result == null || result.size() == 0)
        return null;
    TopicInfo topicInfo = result.get(0);
    return topicInfo;
}
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) TopicInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl) ArrayList(java.util.ArrayList) TopicInfoListResult(org.eclipse.kapua.service.datastore.model.TopicInfoListResult) TopicInfo(org.eclipse.kapua.service.datastore.model.TopicInfo)

Aggregations

TopicInfoListResult (org.eclipse.kapua.service.datastore.model.TopicInfoListResult)5 TopicInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl)3 TopicInfo (org.eclipse.kapua.service.datastore.model.TopicInfo)3 ArrayList (java.util.ArrayList)2 KapuaException (org.eclipse.kapua.KapuaException)2 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)2 TopicInfoListResultImpl (org.eclipse.kapua.service.datastore.internal.model.TopicInfoListResultImpl)2 MetricInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl)2 TopicMatchPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.TopicMatchPredicateImpl)2 KapuaTopic (org.eclipse.kapua.service.datastore.internal.elasticsearch.KapuaTopic)1 TopicInfoBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoBuilder)1 TopicInfoQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoQueryConverter)1 AndPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.AndPredicateImpl)1 AssetInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl)1 IdsPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.IdsPredicateImpl)1 MessageQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl)1 AssetInfoListResult (org.eclipse.kapua.service.datastore.model.AssetInfoListResult)1 MetricInfoListResult (org.eclipse.kapua.service.datastore.model.MetricInfoListResult)1 StorableId (org.eclipse.kapua.service.datastore.model.StorableId)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1