/** * 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(); }
Query statQuery = queryBuilder() .descendants() .filter(context(attributes(Matchers.allOf(hasAttribute("name", "eviction"), hasAttribute("type", StoreOperationOutcomes.EvictionOutcome.class)))))
/** * Find an operation statistic attached (as a children) to this context that matches the statistic name and type * * @param context the context of the query * @param type type of the operation statistic * @param statName statistic name * @param <T> type of the operation statistic content * @return the operation statistic searched for * @throws RuntimeException if 0 or more than 1 result is found */ public static <T extends Enum<T>> OperationStatistic<T> findOperationStatisticOnChildren(Object context, Class<T> type, String statName) { @SuppressWarnings("unchecked") Query query = queryBuilder() .children() .filter(context(attributes(Matchers.allOf(hasAttribute("name", statName), hasAttribute("type", type))))) .build(); Set<TreeNode> result = query.execute(Collections.singleton(ContextManager.nodeFor(context))); if (result.size() > 1) { throw new RuntimeException("result must be unique"); } if (result.isEmpty()) { throw new RuntimeException("result must not be null"); } @SuppressWarnings("unchecked") OperationStatistic<T> statistic = (OperationStatistic<T>) result.iterator().next().getContext().attributes().get("this"); return statistic; }
Set<TreeNode> statResult = queryBuilder() .descendants() .filter(context(attributes(Matchers.allOf(
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(); } }
@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 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))); }
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))); }
@Test public void testStatisticsAssociations() throws Exception { OffHeapDiskStore.Provider provider = new OffHeapDiskStore.Provider(); ServiceLocator serviceLocator = dependencySet().with(mock(SerializationProvider.class)) .with(new DefaultTimeSourceService(null)).with(mock(DiskResourceService.class)).build(); provider.start(serviceLocator); OffHeapDiskStore<Long, String> store = provider.createStore(getStoreConfig(), mock(PersistableResourceService.PersistenceSpaceIdentifier.class)); @SuppressWarnings("unchecked") Query storeQuery = queryBuilder() .children() .filter(context(attributes(Matchers.allOf( hasAttribute("tags", new Matcher<Set<String>>() { @Override protected boolean matchesSafely(Set<String> object) { return object.contains("Disk"); } }))))) .build(); Set<TreeNode> nodes = singleton(ContextManager.nodeFor(store)); Set<TreeNode> storeResult = storeQuery.execute(nodes); assertThat(storeResult, not(empty())); provider.releaseStore(store); storeResult = storeQuery.execute(nodes); assertThat(storeResult, empty()); }
@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"); }
Query invalidateAllQuery = QueryBuilder.queryBuilder() .descendants() .filter(context(attributes(hasAttribute("tags", new Matcher<Set<String>>() {
/** * Creates a query selecting caches. * * @return cache query */ static EhcacheQuery cache() { return new EhcacheQuery(queryBuilder().build()).children(Cache.class); }
/** * Creates a query selecting all descendants. * * @return descendants query */ static EhcacheQuery descendants() { return new EhcacheQuery(queryBuilder().build()).descendants(); }
/** * Creates a query selecting all children. * * @return children query */ static EhcacheQuery children() { return new EhcacheQuery(queryBuilder().build()).children(); }
/** * Return the unique node selected by running this query against this * {@code ContextManager}'s root context. * <p> * If this query does not return a single unique result then an * {@code IllegalStateException} will be thrown. More details on the query * execution context can be found in {@link #query(Query)}. * * @see #query(Query) * @see QueryBuilder#ensureUnique() * @param query the query to execute * @return the node selected by the query * @throws IllegalStateException if the query does not select a unique node */ public TreeNode queryForSingleton(Query query) throws IllegalStateException { return query(queryBuilder().chain(query).ensureUnique().build()).iterator().next(); }
/** * Add the given nodes to the current node set * * @return merged node set */ EhcacheQuery add(final EhcacheQuery chain) { return new EhcacheQuery(queryBuilder().chain(query).chain(new Query() { @Override public Set<TreeNode> execute(Set<TreeNode> input) { Set<TreeNode> result = new HashSet<TreeNode>(); result.addAll(input); result.addAll(chain.execute(input)); return result; } }).build()); }
/** * Select the children of the current node set * * @return children */ EhcacheQuery children() { return new EhcacheQuery(queryBuilder().chain(query).children().build()); }
/** * Select the descendants of the current node set * * @return descendants */ EhcacheQuery descendants() { return new EhcacheQuery(queryBuilder().chain(query).descendants().build()); }
/** * Select the children of the current node set that are subtypes of the specified class * * @param klazz class to be selected * @return children of the specified type */ EhcacheQuery children(Class<?> klazz) { return new EhcacheQuery(queryBuilder().chain(query).children().filter(context(identifier(subclassOf(klazz)))).build()); }
/** * Remove instances of the specified type from the node set * * @param klazz class to be excluded * @return current set minus the given type */ EhcacheQuery exclude(Class<?> klazz) { return new EhcacheQuery(queryBuilder().chain(query).filter(context(identifier(not(subclassOf(klazz))))).build()); }