/** * 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(); }
.build(); Set<TreeNode> statResult = statQuery.execute(Collections.singleton(ContextManager.nodeFor(cache)));
/** * 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; }
hasProperty("discriminator", discriminator), hasTag(tag))))) .build().execute(Collections.singleton(ContextManager.nodeFor(context)));
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(); } }
/** * {@inheritDoc} */ @Override public Set<TreeNode> execute(Set<TreeNode> input) { return query.execute(input); } }
@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)"); } }
@Override public Set<TreeNode> execute(Set<TreeNode> input) { Set<TreeNode> descendants = new HashSet<TreeNode>(); for (Set<TreeNode> children = Children.INSTANCE.execute(input); !children.isEmpty(); children = Children.INSTANCE.execute(children)) { if (!descendants.addAll(children)) { break; } } return descendants; }
@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 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))); }
/** * Run the supplied {@code Query} against this {@code ContextManager}'s * root context. * <p> * The initial node in the queries traversal will be the node whose children * form the root set of this {@code ContextManager}. That is, the following * code will select the root set of this instance.<br> * <pre> * public static Set<TreeNode> roots(ContextManager manager) { * return manager.query(QueryBuilder.queryBuilder().children().build()); * } * </pre> * * @param query the query to execute * @return the set of nodes selected by the query */ public Set<TreeNode> query(Query query) { return query.execute(Collections.<TreeNode>singleton(root)); }
.execute(singleton(nodeFor(cache))) .iterator() .next()
return object.containsAll(tags); }))))).build().execute(passThroughStatisticNodes);
/** * {@inheritDoc} */ @Override public Set<TreeNode> execute(Set<TreeNode> input) { return query.execute(input); } }
/** * {@inheritDoc} */ @Override public Set<TreeNode> execute(Set<TreeNode> input) { return query.execute(input); } }
return object.containsAll(tags); }))))).build().execute(operationStatisticNodes);