use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class AsyncAPICancelRunnable method cancelAsyncAPI.
/**
* This method cancels queries based on threshold.
* @param type AsyncAPI Type Implementation.
*/
protected <T extends AsyncAPI> void cancelAsyncAPI(Class<T> type) {
try {
TransactionRegistry transactionRegistry = elide.getTransactionRegistry();
Map<UUID, DataStoreTransaction> runningTransactionMap = transactionRegistry.getRunningTransactions();
// Running transaction UUIDs
Set<UUID> runningTransactionUUIDs = runningTransactionMap.keySet();
// Construct filter expression
PathElement statusPathElement = new PathElement(type, QueryStatus.class, "status");
FilterExpression fltStatusExpression = new InPredicate(statusPathElement, QueryStatus.CANCELLED, QueryStatus.PROCESSING, QueryStatus.QUEUED);
Iterable<T> asyncAPIIterable = asyncAPIDao.loadAsyncAPIByFilter(fltStatusExpression, type);
// Active AsyncAPI UUIDs
Set<UUID> asyncTransactionUUIDs = StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getStatus() == QueryStatus.CANCELLED || TimeUnit.SECONDS.convert(Math.abs(new Date(System.currentTimeMillis()).getTime() - query.getCreatedOn().getTime()), TimeUnit.MILLISECONDS) > maxRunTimeSeconds).map(query -> UUID.fromString(query.getRequestId())).collect(Collectors.toSet());
// AsyncAPI UUIDs that have active transactions
Set<UUID> queryUUIDsToCancel = Sets.intersection(runningTransactionUUIDs, asyncTransactionUUIDs);
// AsyncAPI IDs that need to be cancelled
Set<String> queryIDsToCancel = queryUUIDsToCancel.stream().map(uuid -> StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getRequestId().equals(uuid.toString())).map(T::getId).findFirst().orElseThrow(IllegalStateException::new)).collect(Collectors.toSet());
// Cancel Transactions
queryUUIDsToCancel.stream().forEach((uuid) -> {
DataStoreTransaction runningTransaction = transactionRegistry.getRunningTransaction(uuid);
if (runningTransaction != null) {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
RequestScope scope = new RequestScope("", "query", NO_VERSION, jsonApiDoc, runningTransaction, null, queryParams, Collections.emptyMap(), uuid, elide.getElideSettings());
runningTransaction.cancel(scope);
}
});
// Change queryStatus for cancelled queries
if (!queryIDsToCancel.isEmpty()) {
PathElement idPathElement = new PathElement(type, String.class, "id");
FilterExpression fltIdExpression = new InPredicate(idPathElement, queryIDsToCancel);
asyncAPIDao.updateStatusAsyncAPIByFilter(fltIdExpression, QueryStatus.CANCEL_COMPLETE, type);
}
} catch (Exception e) {
log.error("Exception in scheduled cancellation: {}", e.toString());
}
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class InMemoryStoreTransaction method fetchData.
private DataStoreIterable<Object> fetchData(DataFetcher fetcher, EntityProjection projection, boolean filterInMemory, RequestScope scope) {
Optional<FilterExpression> filterExpression = Optional.ofNullable(projection.getFilterExpression());
Pair<Optional<FilterExpression>, Optional<FilterExpression>> expressionSplit = splitFilterExpression(scope, projection, filterInMemory);
Optional<FilterExpression> dataStoreFilter = expressionSplit.getLeft();
Optional<FilterExpression> inMemoryFilter = expressionSplit.getRight();
Optional<Sorting> dataStoreSorting = getDataStoreSorting(scope, projection, filterInMemory);
boolean sortingInMemory = dataStoreSorting.isEmpty() && projection.getSorting() != null;
Optional<Pagination> dataStorePagination = inMemoryFilter.isPresent() || sortingInMemory ? Optional.empty() : Optional.ofNullable(projection.getPagination());
DataStoreIterable<Object> loadedRecords = fetcher.fetch(dataStoreFilter, dataStoreSorting, dataStorePagination, scope);
if (loadedRecords == null) {
return new DataStoreIterableBuilder().build();
}
if (inMemoryFilter.isPresent() || (loadedRecords.needsInMemoryFilter() && projection.getFilterExpression() != null)) {
loadedRecords = filterLoadedData(loadedRecords, filterExpression, scope);
}
return sortAndPaginateLoadedData(loadedRecords, sortingInMemory, projection.getSorting(), projection.getPagination(), scope);
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class EntityProjectionTranslatorTest method testWherePromotion.
@Test
public void testWherePromotion() throws ParseException {
FilterExpression originalFilter = filterParser.parseFilterExpression("overallRating==Good,lowScore[foo:bar]<45", playerStatsType, false);
EntityProjection projection = basicProjection.copyOf().filterExpression(originalFilter).build();
EntityProjectionTranslator translator = new EntityProjectionTranslator(engine, playerStatsTable, projection, scope, true);
Query query = translator.getQuery();
SplitFilterExpressionVisitor visitor = new SplitFilterExpressionVisitor(playerStatsTable);
FilterConstraints constraints = originalFilter.accept(visitor);
FilterExpression whereFilter = constraints.getWhereExpression();
FilterExpression havingFilter = constraints.getHavingExpression();
assertEquals(whereFilter, query.getWhereFilter());
assertEquals(havingFilter, query.getHavingFilter());
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class EntityProjectionTranslatorTest method testHavingClauseMetricsMissingFromProjection.
@Test
public void testHavingClauseMetricsMissingFromProjection() throws ParseException {
FilterExpression filter = filterParser.parseFilterExpression("lowScore>45", playerStatsType, false);
EntityProjection projection = EntityProjection.builder().type(PlayerStats.class).filterExpression(filter).attribute(Attribute.builder().type(long.class).name("highScore").build()).attribute(Attribute.builder().type(String.class).name("overallRating").build()).build();
EntityProjectionTranslator translator = new EntityProjectionTranslator(engine, playerStatsTable, projection, scope, true);
Query query = translator.getQuery();
List<String> metricNames = query.getMetricProjections().stream().map(MetricProjection::getName).collect(Collectors.toList());
assertEquals(metricNames, Arrays.asList("highScore", "lowScore"));
}
use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.
the class SubqueryFilterSplitter method splitFilter.
public static SplitFilter splitFilter(MetaDataStore metaDataStore, FilterExpression expression) {
if (expression == null) {
return SplitFilter.builder().build();
}
FilterExpressionNormalizationVisitor normalizer = new FilterExpressionNormalizationVisitor();
FilterExpression normalizedExpression = expression.accept(normalizer);
return normalizedExpression.accept(new SubqueryFilterSplitter(metaDataStore));
}
Aggregations