/** * Search for a statistic on the descendant of the context that matches the tag and statistic name. * * @param context the context of the query * @param tag the tag we are looking for * @param statName statistic name * @param <T> type of the statistic that will be returned * @return the wanted statistic or null if no such statistic is found * @throws RuntimeException when more than one matching statistic is found */ public static <T> Optional<T> findStatisticOnDescendants(Object context, String tag, String statName) { @SuppressWarnings("unchecked") Set<TreeNode> statResult = queryBuilder() .descendants() .filter(context(attributes(Matchers.allOf( hasAttribute("name", statName), hasTag(tag))))) .build().execute(Collections.singleton(ContextManager.nodeFor(context))); if (statResult.size() > 1) { throw new RuntimeException("One stat expected for " + statName + " but found " + statResult.size()); } if (statResult.size() == 1) { @SuppressWarnings("unchecked") T result = (T) statResult.iterator().next().getContext().attributes().get("this"); return Optional.ofNullable(result); } // No such stat in this context return Optional.empty(); }
.descendants() .filter(context(attributes(Matchers.allOf(hasAttribute("name", "eviction"), hasAttribute("type", StoreOperationOutcomes.EvictionOutcome.class))))) .build();
.descendants() .filter(context(attributes(Matchers.allOf( hasAttribute("name", statName),
public static <T extends Enum<T>> boolean hasOperationStat(Object rootNode, Class<T> statisticType, String statName) { Query q = queryBuilder().descendants() .filter(context(identifier(subclassOf(OperationStatistic.class)))) .filter(context(attributes(Matchers.allOf( hasAttribute("name", statName), hasAttribute("this", new Matcher<OperationStatistic<T>>() { @Override protected boolean matchesSafely(OperationStatistic<T> object) { return object.type().equals(statisticType); } }) )))) .build(); Set<TreeNode> result = q.execute(Collections.singleton(ContextManager.nodeFor(rootNode))); if (result.size() > 1) { throw new RuntimeException("a zero or a single stat was expected; found " + result.size()); } return !result.isEmpty(); } }
private Set<TreeNode> queryProperty(String tag) { @SuppressWarnings("unchecked") Query statQuery = queryBuilder() .descendants() .filter(context(attributes(Matchers.<Map<String, Object>>allOf( hasAttribute("name", "get"), hasTag(tag) )))) .build(); return statQuery.execute(Collections.singleton(ContextManager.nodeFor(cache))); }
@SuppressWarnings("unchecked") private Set<TreeNode> queryProperty(String key, String value) { Query statQuery = queryBuilder() .descendants() .filter(context(attributes(Matchers.<Map<String, Object>>allOf( hasAttribute("name", "test"), hasProperty(key, value) )))) .build(); return statQuery.execute(Collections.singleton(ContextManager.nodeFor(cache))); }
@SuppressWarnings("unchecked") private static <T extends Enum<T>> OperationStatistic<T> findStat(Cache<?, ?> cache, final String statName, final String tag) { Query q = queryBuilder().chain(self()) .descendants().filter(context(identifier(subclassOf(OperationStatistic.class)))).build(); Set<TreeNode> operationStatisticNodes = q.execute(Collections.singleton(ContextManager.nodeFor(cache))); Set<TreeNode> result = queryBuilder() .filter( context(attributes(Matchers.<Map<String, Object>>allOf( hasAttribute("name", statName), hasAttribute("tags", new Matcher<Set<String>>() { @Override protected boolean matchesSafely(Set<String> object) { return object.contains(tag); } }))))).build().execute(operationStatisticNodes); switch (result.size()) { case 0: return null; case 1: { TreeNode node = result.iterator().next(); return (OperationStatistic<T>) node.getContext().attributes().get("this"); } default: throw new RuntimeException("query for unique stat '" + statName + "' with tag '" + tag + "' failed; found " + result.size() + " instance(s)"); } }
@SuppressWarnings("unchecked") private OperationStatistic<StoreOperationOutcomes.ExpirationOutcome> getExpirationStatistic(Store<?, ?> store) { StatisticsManager statisticsManager = new StatisticsManager(); statisticsManager.root(store); TreeNode treeNode = statisticsManager.queryForSingleton(QueryBuilder.queryBuilder() .descendants() .filter(org.terracotta.context.query.Matchers.context( org.terracotta.context.query.Matchers.allOf(org.terracotta.context.query.Matchers.identifier(org.terracotta.context.query.Matchers .subclassOf(OperationStatistic.class)), org.terracotta.context.query.Matchers.attributes(org.terracotta.context.query.Matchers.hasAttribute("name", "expiration"))))) .build()); return (OperationStatistic<StoreOperationOutcomes.ExpirationOutcome>) treeNode.getContext().attributes().get("this"); }
.descendants() .filter(context(attributes(hasAttribute("tags", new Matcher<Set<String>>() { @Override
@Override public <T extends Enum<T>> Set<Operation<T>> operations(Class<T> outcome, String name, String... tags) { Set<OperationStatistic<T>> sources = findOperationStatistic(manager, queryBuilder().descendants().build(), outcome, name, new HashSet<String>(Arrays.asList(tags))); if (sources.isEmpty()) { return Collections.emptySet(); } else { Set<Operation<T>> operations = new HashSet(); for (OperationStatistic<T> source : sources) { CompoundOperationImpl<T> operation = (CompoundOperationImpl<T>) customOperations.get(source); if (operation == null) { operation = new CompoundOperationImpl<T>(source, source.type(), 1, SECONDS, executor, 0, 1, SECONDS); CompoundOperationImpl<T> racer = (CompoundOperationImpl<T>) customOperations.putIfAbsent(source, operation); if (racer != null) { operation = racer; } } operations.add(operation); } return operations; } }
/** * Select the descendants of the current node set * * @return descendants */ EhcacheQuery descendants() { return new EhcacheQuery(queryBuilder().chain(query).descendants().build()); }
/** * Creates a query selecting all descendants. * * @return descendants query */ public static Query descendants() { return queryBuilder().descendants().build(); }
@Override public <T extends Enum<T>> Set<Operation<T>> operations(Class<T> outcome, String name, String... tags) { Set<OperationStatistic<T>> sources = findOperationStatistic(manager, queryBuilder().descendants().build(), outcome, name, new HashSet<String>(Arrays.asList(tags))); if (sources.isEmpty()) { return Collections.emptySet(); } else { Set<Operation<T>> operations = new HashSet(); for (OperationStatistic<T> source : sources) { CompoundOperationImpl<T> operation = (CompoundOperationImpl<T>) customOperations.get(source); if (operation == null) { operation = new CompoundOperationImpl<T>(source, source.type(), 1, SECONDS, executor, 0, 1, SECONDS); CompoundOperationImpl<T> racer = (CompoundOperationImpl<T>) customOperations.putIfAbsent(source, operation); if (racer != null) { operation = racer; } } operations.add(operation); } return operations; } }
@Override public <T extends Enum<T>> Set<Operation<T>> operations(Class<T> outcome, String name, String... tags) { Set<OperationStatistic<T>> sources = findOperationStatistic(manager, queryBuilder().descendants().build(), outcome, name, new HashSet<String>(Arrays.asList(tags))); if (sources.isEmpty()) { return Collections.emptySet(); } else { Set<Operation<T>> operations = new HashSet(); for (OperationStatistic<T> source : sources) { CompoundOperationImpl<T> operation = (CompoundOperationImpl<T>) customOperations.get(source); if (operation == null) { operation = new CompoundOperationImpl<T>(source, source.type(), 1, SECONDS, executor, 0, 1, SECONDS); CompoundOperationImpl<T> racer = (CompoundOperationImpl<T>) customOperations.putIfAbsent(source, operation); if (racer != null) { operation = racer; } } operations.add(operation); } return operations; } }
/** * Select the descendants of the current node set * * @return descendants */ EhcacheQuery descendants() { return new EhcacheQuery(queryBuilder().chain(query).descendants().build()); }
/** * Select the descendants of the current node set * * @return descendants */ EhcacheQuery descendants() { return new EhcacheQuery(queryBuilder().chain(query).descendants().build()); }
@SuppressWarnings("unchecked") private static <S extends Enum<S>> OperationStatistic<S> findOperationStat(Object rootNode, final Class<S> statisticType, final String statName) { Query q = queryBuilder().descendants() .filter(context(identifier(subclassOf(OperationStatistic.class)))) .filter(context(attributes(Matchers.allOf( hasAttribute("name", statName), hasAttribute("this", new Matcher<OperationStatistic<?>>() { @Override protected boolean matchesSafely(OperationStatistic<?> object) { return object.type().equals(statisticType); } }) )))).build(); Set<TreeNode> result = q.execute(Collections.singleton(ContextManager.nodeFor(rootNode))); if (result.size() != 1) { throw new RuntimeException("a single stat was expected; found " + result.size()); } TreeNode node = result.iterator().next(); return (OperationStatistic<S>) node.getContext().attributes().get("this"); }
public static <T extends Enum<T>> boolean hasOperationStat(Object rootNode, Class<T> statisticType, String statName) { Query q = queryBuilder().descendants() .filter(context(identifier(subclassOf(OperationStatistic.class)))) .filter(context(attributes(Matchers.allOf( hasAttribute("name", statName), hasAttribute("this", new Matcher<OperationStatistic<T>>() { @Override protected boolean matchesSafely(OperationStatistic<T> object) { return object.type().equals(statisticType); } }) )))) .build(); Set<TreeNode> result = q.execute(Collections.singleton(ContextManager.nodeFor(rootNode))); if (result.size() > 1) { throw new RuntimeException("a zero or a single stat was expected; found " + result.size()); } return !result.isEmpty(); } }
public static OperationStatistic findOperationStat(Cache<?, ?> cache1, final String statName, final String tag) { Query q = queryBuilder() .descendants().filter(context(identifier(subclassOf(OperationStatistic.class)))).build(); Set<TreeNode> operationStatisticNodes = q.execute(Collections.singleton(ContextManager.nodeFor(cache1))); Set<TreeNode> result = queryBuilder() .filter( context(attributes(Matchers.<Map<String, Object>>allOf( hasAttribute("name", statName), hasAttribute("tags", new Matcher<Set<String>>() { @Override protected boolean matchesSafely(Set<String> object) { return object.contains(tag); } }))))).build().execute(operationStatisticNodes); if (result.size() != 1) { throw new RuntimeException("single stat not found; found " + result.size()); } TreeNode node = result.iterator().next(); return (OperationStatistic) node.getContext().attributes().get("this"); }
public static ValueStatistic findValueStat(Cache<?, ?> cache1, final String statName, final String tag) { Query q = queryBuilder().chain(self()) .descendants().filter(context(identifier(subclassOf(ValueStatistic.class)))).build(); Set<TreeNode> nodes = q.execute(Collections.singleton(ContextManager.nodeFor(cache1))); Set<TreeNode> result = queryBuilder() .filter( context(attributes(Matchers.<Map<String, Object>>allOf( hasAttribute("name", statName), hasAttribute("tags", new Matcher<Set<String>>() { @Override protected boolean matchesSafely(Set<String> object) { return object.contains(tag); } }))))).build().execute(nodes); if (result.size() != 1) { throw new RuntimeException("single stat not found; found " + result.size()); } TreeNode node = result.iterator().next(); return (ValueStatistic) node.getContext().attributes().get("this"); }