Search in sources :

Example 1 with AssetInfoListResultImpl

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

the class AssetInfoStoreServiceImpl method query.

@Override
public AssetInfoListResult query(KapuaId scopeId, AssetInfoQuery 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 AssetInfoListResultImpl();
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        AssetInfoListResult result = null;
        result = EsAssetDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.ASSET_TYPE_NAME).query(query);
        return result;
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : AssetInfoListResult(org.eclipse.kapua.service.datastore.model.AssetInfoListResult) LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) AssetInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.AssetInfoListResultImpl) KapuaException(org.eclipse.kapua.KapuaException)

Example 2 with AssetInfoListResultImpl

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

AssetInfoListResultImpl (org.eclipse.kapua.service.datastore.internal.model.AssetInfoListResultImpl)2 ArrayList (java.util.ArrayList)1 KapuaException (org.eclipse.kapua.KapuaException)1 AssetInfoBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.AssetInfoBuilder)1 AssetInfoQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.AssetInfoQueryConverter)1 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)1 AssetInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.AssetInfoQueryImpl)1 AssetInfo (org.eclipse.kapua.service.datastore.model.AssetInfo)1 AssetInfoListResult (org.eclipse.kapua.service.datastore.model.AssetInfoListResult)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