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);
}
}
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;
}
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);
}
}
Aggregations