Search in sources :

Example 1 with MessageQueryImpl

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

the class TopicInfoStoreServiceImpl method delete.

// 
// @Override
// public StorableId store(KapuaId scopeId, TopicInfoCreator creator)
// throws KapuaException
// {
// // TODO DAOs are ready, need to evaluate if this functionality
// // have to be available or not. Currently entries are added by
// // the message service directy
// throw KapuaException.internalError("Not implemented");
// }
// 
// @Override
// public StorableId update(KapuaId scopeId, TopicInfo creator)
// throws KapuaException
// {
// // TODO DAOs are ready, need to evaluate if this functionality
// // have to be available or not. Currently entries are added by
// // the message service directy
// throw KapuaException.internalError("Not implemented");
// }
@Override
public void delete(KapuaId scopeId, StorableId id) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(id, "id");
    // 
    // 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 {}, return", scopeName);
        return;
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        TopicInfo topicInfo = this.find(scopeId, id);
        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).deleteById(id.toString());
    } 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) 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 2 with MessageQueryImpl

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

the class EsMessageDAO method query.

// 
// public BoolQueryBuilder getQueryByAccountAndDate(String account, boolean isAnyAccount, long start, long end)
// {
// 
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
// 
// return boolQuery;
// }
// 
// public BoolQueryBuilder getQueryByAssetAndDate(String asset, boolean isAnyAsset, long start, long end)
// {
// 
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 
// // Asset clauses
// QueryBuilder assetQuery = null;
// if (!isAnyAsset) {
// assetQuery = QueryBuilders.termQuery(EsSchema.MESSAGE_AS_NAME, asset);
// boolQuery.must(assetQuery);
// }
// 
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
// 
// return boolQuery;
// }
// 
// public BoolQueryBuilder getQueryByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic)
// {
// 
// // Asset clauses
// QueryBuilder assetQuery = null;
// if (!isAnyAsset) {
// assetQuery = QueryBuilders.termQuery(EsSchema.MESSAGE_AS_NAME, asset);
// }
// 
// // Topic clauses
// QueryBuilder topicQuery = null;
// if (isAnySubtopic) {
// topicQuery = QueryBuilders.prefixQuery(EsSchema.MESSAGE_SEM_TOPIC, semTopic);
// }
// else {
// topicQuery = QueryBuilders.termQuery(EsSchema.MESSAGE_SEM_TOPIC, semTopic);
// }
// 
// // Composite clause
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// if (assetQuery != null)
// boolQuery.must(assetQuery);
// boolQuery.must(topicQuery);
// //
// return boolQuery;
// }
// 
// public BoolQueryBuilder getQueryByTopicAndDate(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// 
// BoolQueryBuilder boolQuery = this.getQueryByTopic(asset, isAnyAsset, semTopic, isAnySubtopic);
// 
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
// 
// return boolQuery;
// }
// 
// public BoolQueryBuilder getQueryByMetricValueAndDate(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// String fullMetricName,
// long start,
// long end,
// Object min,
// Object max)
// {
// 
// BoolQueryBuilder boolQuery = this.getQueryByTopic(asset, isAnyAsset, semTopic, isAnySubtopic);
// 
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
// 
// // Metric value clause
// QueryBuilder valueQuery = QueryBuilders.rangeQuery(fullMetricName).gte(min).lte(max);
// boolQuery.must(valueQuery);
// //
// 
// return boolQuery;
// }
// 
// public void deleteByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// BoolQueryBuilder boolQuery = this.getQueryByTopicAndDate(asset, isAnyAsset, semTopic, isAnySubtopic, start, end);
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
// 
// public void deleteByAccount(long start,
// long end)
// {
// // Query clauses
// boolean isAnyAccount = true;
// BoolQueryBuilder boolQuery = this.getQueryByAccountAndDate(null, isAnyAccount, start, end);
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
public MessageListResult query(MessageQuery query) throws Exception {
    MessageQueryImpl localQuery = new MessageQueryImpl();
    localQuery.copy(query);
    localQuery.setLimit(query.getLimit() + 1);
    MessageQueryConverter converter = new MessageQueryConverter();
    SearchRequestBuilder builder = converter.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 MessageListResultImpl();
    int i = 0;
    int searchHitsSize = searchHits.getHits().length;
    List<Message> messages = new ArrayList<Message>();
    MessageBuilder msgBuilder = new MessageBuilder();
    for (SearchHit searchHit : searchHits.getHits()) {
        if (i < query.getLimit()) {
            Message message = msgBuilder.build(searchHit, query.getFetchStyle()).getMessage();
            messages.add(message);
        }
        i++;
    }
    // TODO check equivalence with CX with Pierantonio
    // TODO what is this nextKey
    Object nextKey = null;
    if (searchHitsSize > query.getLimit()) {
        Message lastMessage = msgBuilder.build(searchHits.getHits()[searchHitsSize - 1], query.getFetchStyle()).getMessage();
        nextKey = lastMessage.getTimestamp().getTime();
    }
    // TODO verifiy total count
    long totalCount = 0;
    if (query.isAskTotalCount()) {
        // totalCount = MessageDAO.findByTopicCount(keyspace, topic, start, end);
        totalCount = searchHits.getTotalHits();
    }
    if (totalCount > Integer.MAX_VALUE)
        throw new Exception("Total hits exceeds integer max value");
    MessageListResultImpl result = new MessageListResultImpl(nextKey, (int) totalCount);
    result.addAll(messages);
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Message(org.eclipse.kapua.service.datastore.model.Message) SearchHit(org.elasticsearch.search.SearchHit) MessageListResultImpl(org.eclipse.kapua.service.datastore.internal.model.MessageListResultImpl) MessageQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter) ArrayList(java.util.ArrayList) EsDatastoreException(org.eclipse.kapua.service.datastore.internal.elasticsearch.EsDatastoreException) UnknownHostException(java.net.UnknownHostException) KapuaException(org.eclipse.kapua.KapuaException) SearchResponse(org.elasticsearch.action.search.SearchResponse) MessageQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl) MessageBuilder(org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageBuilder) SearchHits(org.elasticsearch.search.SearchHits)

Example 3 with MessageQueryImpl

use of org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl 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)

Aggregations

KapuaException (org.eclipse.kapua.KapuaException)3 MessageQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl)3 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)2 MetricInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl)2 TopicMatchPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.TopicMatchPredicateImpl)2 TopicInfo (org.eclipse.kapua.service.datastore.model.TopicInfo)2 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 EsDatastoreException (org.eclipse.kapua.service.datastore.internal.elasticsearch.EsDatastoreException)1 MessageBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageBuilder)1 MessageQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter)1 MessageListResultImpl (org.eclipse.kapua.service.datastore.internal.model.MessageListResultImpl)1 Message (org.eclipse.kapua.service.datastore.model.Message)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