public static Matcher<Map<String, Object>> hasTag(final String tag) { return hasAttribute("tags", new Matcher<Set<String>>() { @Override protected boolean matchesSafely(Set<String> object) { return object.contains(tag); } }); }
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(); } }
/** * Filters the current node set using the supplied {@code Matcher}. * <p> * Nodes in the current node set that are not selected by the supplied matcher * are removed. * * @param filter matcher to apply * @return this query builder */ public QueryBuilder filter(Matcher<? super TreeNode> filter) { return chain(new Filter(filter)); }
/** * 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()); }
/** * Selects the union of the current node sets child nodes. * * @return this query builder */ public QueryBuilder children() { return chain(Children.INSTANCE); }
@Override protected boolean matchesSafely(T object) { return !matcher.matches(object); } };
/** * Creates a new query builder instance. * <p> * A newly constructed query builder represents the identity query. It simply * returns the input node set as the output node set. * * @return a new query builder */ public static QueryBuilder queryBuilder() { return new QueryBuilder(); }
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))); }
public static Matcher<Map<String, Object>> hasProperty(final String key, final String value) { return hasAttribute("properties", new Matcher<Map<String, Object>>() { @Override protected boolean matchesSafely(Map<String, Object> properties) { Object val = properties.get(key); return val != null && value.equals(val); } }); }
@Override protected boolean matchesSafely(T object) { for (Matcher<? super T> matcher : matchers) { if (matcher.matches(object)) { return true; } } return false; } };
@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))); }
/** * 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; }
Query statQuery = queryBuilder() .descendants() .filter(context(attributes(Matchers.allOf(hasAttribute("name", "eviction"), hasAttribute("type", StoreOperationOutcomes.EvictionOutcome.class))))) .build(); Set<TreeNode> statResult = statQuery.execute(Collections.singleton(ContextManager.nodeFor(cache)));
/** * 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(); }
Set<TreeNode> statResult = queryBuilder() .descendants() .filter(context(attributes(Matchers.allOf( hasAttribute("name", statName), hasProperty("discriminator", discriminator), hasTag(tag))))) .build().execute(Collections.singleton(ContextManager.nodeFor(context)));
@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"); }
@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)"); } }
@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()); }
Query invalidateAllQuery = QueryBuilder.queryBuilder() .descendants() .filter(context(attributes(hasAttribute("tags", new Matcher<Set<String>>() { @Override protected boolean matchesSafely(Set<String> object) { .filter(context(attributes(hasAttribute("name", "invalidateAll")))) .ensureUnique() .build(); .execute(singleton(nodeFor(cache))) .iterator() .next()