Search in sources :

Example 1 with MessageListResultImpl

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

the class MessageStoreServiceImpl method query.

@Override
public MessageListResult query(KapuaId scopeId, MessageQuery 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 MessageListResultImpl();
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        MessageListResult result = null;
        result = EsMessageDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.MESSAGE_TYPE_NAME).query(query);
        return result;
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : MessageListResultImpl(org.eclipse.kapua.service.datastore.internal.model.MessageListResultImpl) LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) MessageListResult(org.eclipse.kapua.service.datastore.model.MessageListResult) KapuaInvalidTopicException(org.eclipse.kapua.service.datastore.internal.elasticsearch.KapuaInvalidTopicException) ParseException(java.text.ParseException) EsDatastoreException(org.eclipse.kapua.service.datastore.internal.elasticsearch.EsDatastoreException) DocumentAlreadyExistsException(org.elasticsearch.index.engine.DocumentAlreadyExistsException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) KapuaException(org.eclipse.kapua.KapuaException)

Example 2 with MessageListResultImpl

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

Aggregations

UnknownHostException (java.net.UnknownHostException)2 KapuaException (org.eclipse.kapua.KapuaException)2 EsDatastoreException (org.eclipse.kapua.service.datastore.internal.elasticsearch.EsDatastoreException)2 MessageListResultImpl (org.eclipse.kapua.service.datastore.internal.model.MessageListResultImpl)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 KapuaInvalidTopicException (org.eclipse.kapua.service.datastore.internal.elasticsearch.KapuaInvalidTopicException)1 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)1 MessageBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageBuilder)1 MessageQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter)1 MessageQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl)1 Message (org.eclipse.kapua.service.datastore.model.Message)1 MessageListResult (org.eclipse.kapua.service.datastore.model.MessageListResult)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 DocumentAlreadyExistsException (org.elasticsearch.index.engine.DocumentAlreadyExistsException)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1