use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class RemoteContinuousQueryTest method testContinuousQuery.
public void testContinuousQuery() {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<User> query = qf.<User>create("FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, User>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<KeyValuePair<String, User>> updated = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, User> listener = new ContinuousQueryListener<String, User>() {
@Override
public void resultJoining(String key, User value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultUpdated(String key, User value) {
updated.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue().getAge(), 32, 22);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
user3.setAge(30);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 1, (kv) -> kv.getValue().getAge(), 30);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
user1.setAge(23);
remoteCache.put("user" + user1.getId(), user1);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 1, (kv) -> kv.getValue().getAge(), 23);
expectElementsInQueue(left, 0);
user1.setAge(40);
user2.setAge(40);
user3.setAge(40);
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 3);
remoteCache.clear();
user1.setAge(21);
user2.setAge(22);
remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
expectElementsInQueue(joined, 2);
expectElementsInQueue(left, 0);
timeService.advance(6);
assertNull(remoteCache.get("expiredUser1"));
assertNull(remoteCache.get("expiredUser2"));
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 2);
continuousQuery.removeContinuousQueryListener(listener);
user2.setAge(22);
remoteCache.put("user" + user2.getId(), user2);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 0);
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class RemoteContinuousQueryTest method testContinuousQueryWithProjections.
public void testContinuousQueryWithProjections() {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<KeyValuePair<String, Object[]>> updated = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultUpdated(String key, Object[] value) {
updated.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
user3.setAge(30);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 1, (kv) -> kv.getValue()[0], 30);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
user1.setAge(23);
remoteCache.put("user" + user1.getId(), user1);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 1, (kv) -> kv.getValue()[0], 23);
expectElementsInQueue(left, 0);
user1.setAge(40);
user2.setAge(40);
user3.setAge(40);
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 3);
remoteCache.clear();
user1.setAge(21);
user2.setAge(22);
remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
expectElementsInQueue(joined, 2);
expectElementsInQueue(left, 0);
timeService.advance(6);
assertNull(remoteCache.get("expiredUser1"));
assertNull(remoteCache.get("expiredUser2"));
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 2);
continuousQuery.removeContinuousQueryListener(listener);
user2.setAge(22);
remoteCache.put("user" + user2.getId(), user2);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 0);
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class CacheKeyInvocationContextFactory method getMethodMetaData.
/**
* Returns the method meta data for the given method.
*
* @param method the method.
* @return an instance of {@link MethodMetaData}.
*/
private MethodMetaData<? extends Annotation> getMethodMetaData(Method method, Class<?> targetClass) {
KeyValuePair<Method, Class<?>> methodKey = new KeyValuePair<>(method, targetClass);
MethodMetaData<? extends Annotation> methodMetaData = methodMetaDataCache.get(methodKey);
if (methodMetaData == null) {
final String cacheName;
final Annotation cacheAnnotation;
final AggregatedParameterMetaData aggregatedParameterMetaData;
final CacheKeyGenerator cacheKeyGenerator;
final CacheResolverFactory cacheResolverFactory;
CacheDefaults cacheDefaultsAnnotation = getCacheDefaults(targetClass);
if (method.isAnnotationPresent(CacheResult.class)) {
final CacheResult cacheResultAnnotation = method.getAnnotation(CacheResult.class);
cacheKeyGenerator = getCacheKeyGenerator(beanManager, cacheResultAnnotation.cacheKeyGenerator(), cacheDefaultsAnnotation);
cacheResolverFactory = getCacheResolverFactory(beanManager, cacheResultAnnotation.cacheResolverFactory(), cacheDefaultsAnnotation);
cacheName = getCacheName(method, cacheResultAnnotation.cacheName(), cacheDefaultsAnnotation, true);
aggregatedParameterMetaData = getAggregatedParameterMetaData(method, false);
cacheAnnotation = cacheResultAnnotation;
} else if (method.isAnnotationPresent(CacheRemove.class)) {
final CacheRemove cacheRemoveEntryAnnotation = method.getAnnotation(CacheRemove.class);
cacheKeyGenerator = getCacheKeyGenerator(beanManager, cacheRemoveEntryAnnotation.cacheKeyGenerator(), cacheDefaultsAnnotation);
cacheResolverFactory = getCacheResolverFactory(beanManager, cacheRemoveEntryAnnotation.cacheResolverFactory(), cacheDefaultsAnnotation);
cacheName = getCacheName(method, cacheRemoveEntryAnnotation.cacheName(), cacheDefaultsAnnotation, false);
aggregatedParameterMetaData = getAggregatedParameterMetaData(method, false);
cacheAnnotation = cacheRemoveEntryAnnotation;
if (cacheName.isEmpty()) {
throw log.cacheRemoveEntryMethodWithoutCacheName(method.getName());
}
} else if (method.isAnnotationPresent(CacheRemoveAll.class)) {
final CacheRemoveAll cacheRemoveAllAnnotation = method.getAnnotation(CacheRemoveAll.class);
cacheKeyGenerator = null;
cacheResolverFactory = getCacheResolverFactory(beanManager, cacheRemoveAllAnnotation.cacheResolverFactory(), cacheDefaultsAnnotation);
cacheName = getCacheName(method, cacheRemoveAllAnnotation.cacheName(), cacheDefaultsAnnotation, false);
aggregatedParameterMetaData = getAggregatedParameterMetaData(method, false);
cacheAnnotation = cacheRemoveAllAnnotation;
if (cacheName.isEmpty()) {
throw log.cacheRemoveAllMethodWithoutCacheName(method.getName());
}
} else if (method.isAnnotationPresent(CachePut.class)) {
final CachePut cachePutAnnotation = method.getAnnotation(CachePut.class);
cacheKeyGenerator = getCacheKeyGenerator(beanManager, cachePutAnnotation.cacheKeyGenerator(), cacheDefaultsAnnotation);
cacheResolverFactory = getCacheResolverFactory(beanManager, cachePutAnnotation.cacheResolverFactory(), cacheDefaultsAnnotation);
cacheName = getCacheName(method, cachePutAnnotation.cacheName(), cacheDefaultsAnnotation, true);
aggregatedParameterMetaData = getAggregatedParameterMetaData(method, true);
cacheAnnotation = cachePutAnnotation;
} else {
throw log.methodWithoutCacheAnnotation(method.getName());
}
// Create a temporary MethodMetaData instance first to pass to CacheResolverFactory
MethodMetaData<? extends Annotation> tmpMethodMetaData = new MethodMetaData<>(method, aggregatedParameterMetaData, asSet(method.getAnnotations()), cacheKeyGenerator, null, null, cacheAnnotation, cacheName);
CacheResolver cacheResolver = null;
if (cacheResolverFactory != null) {
cacheResolver = cacheResolverFactory.getCacheResolver(tmpMethodMetaData);
}
CacheResolver exceptionCacheResolver = null;
if (cacheResolverFactory != null && cacheAnnotation instanceof CacheResult) {
String exceptionCacheName = ((CacheResult) cacheAnnotation).exceptionCacheName();
if (exceptionCacheName.trim().length() != 0) {
exceptionCacheResolver = cacheResolverFactory.getExceptionCacheResolver((CacheMethodDetails) tmpMethodMetaData);
}
}
MethodMetaData<Annotation> newMethodMetaData = new MethodMetaData<>(method, aggregatedParameterMetaData, asSet(method.getAnnotations()), cacheKeyGenerator, cacheResolver, exceptionCacheResolver, cacheAnnotation, cacheName);
methodMetaData = methodMetaDataCache.putIfAbsent(methodKey, newMethodMetaData);
if (methodMetaData == null) {
methodMetaData = newMethodMetaData;
}
}
return methodMetaData;
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class DefaultIterationManager method buildFilter.
private KeyValuePair<KeyValueFilterConverter, Boolean> buildFilter(KeyValueFilterConverterFactory factory, byte[][] params, Function<Object, Object> unmarshallParam) {
if (factory instanceof ParamKeyValueFilterConverterFactory) {
ParamKeyValueFilterConverterFactory paramFactory = (ParamKeyValueFilterConverterFactory) factory;
Object[] unmarshallParams;
if (paramFactory.binaryParam()) {
unmarshallParams = params;
} else {
unmarshallParams = Arrays.stream(params).map(unmarshallParam).toArray();
}
return new KeyValuePair<>(paramFactory.getFilterConverter(unmarshallParams), paramFactory.binaryParam());
} else {
return new KeyValuePair<>(factory.getFilterConverter(), false);
}
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class DefaultIterationManager method start.
@Override
public IterationState start(AdvancedCache cache, BitSet segments, String filterConverterFactory, List<byte[]> filterConverterParams, MediaType requestValueType, int batch, boolean metadata) {
String iterationId = Util.threadLocalRandomUUID().toString();
EmbeddedCacheManager cacheManager = SecurityActions.getEmbeddedCacheManager(cache);
EncoderRegistry encoderRegistry = SecurityActions.getGlobalComponentRegistry(cacheManager).getComponent(EncoderRegistry.class);
DataConversion valueDataConversion = cache.getValueDataConversion();
Function<Object, Object> unmarshaller = p -> encoderRegistry.convert(p, requestValueType, APPLICATION_OBJECT);
MediaType storageMediaType = cache.getValueDataConversion().getStorageMediaType();
IterationSegmentsListener segmentListener = new IterationSegmentsListener();
CacheStream<CacheEntry<Object, Object>> stream;
Stream<CacheEntry<Object, Object>> filteredStream;
Function<Object, Object> resultTransformer = Function.identity();
AdvancedCache iterationCache = cache;
if (filterConverterFactory == null) {
stream = cache.cacheEntrySet().stream();
if (segments != null) {
stream.filterKeySegments(IntSets.from(segments.stream().iterator()));
}
filteredStream = stream.segmentCompletionListener(segmentListener);
} else {
KeyValueFilterConverterFactory factory = getFactory(filterConverterFactory);
KeyValuePair<KeyValueFilterConverter, Boolean> filter = buildFilter(factory, filterConverterParams.toArray(Util.EMPTY_BYTE_ARRAY_ARRAY), unmarshaller);
KeyValueFilterConverter customFilter = filter.getKey();
MediaType filterMediaType = customFilter.format();
if (filterMediaType != null && filterMediaType.equals(storageMediaType)) {
iterationCache = cache.withMediaType(filterMediaType, filterMediaType);
}
stream = iterationCache.cacheEntrySet().stream();
if (segments != null) {
stream.filterKeySegments(IntSets.from(segments.stream().iterator()));
}
IterationFilter iterationFilter = new IterationFilter(storageMediaType, requestValueType, Optional.of(filter.getKey()));
filteredStream = filterAndConvert(stream.segmentCompletionListener(segmentListener), iterationFilter);
if (filterMediaType != null && !storageMediaType.equals(requestValueType)) {
resultTransformer = valueDataConversion::fromStorage;
}
}
Iterator<CacheEntry<Object, Object>> iterator = filteredStream.iterator();
DefaultIterationState iterationState = new DefaultIterationState(iterationId, segmentListener, iterator, stream, batch, metadata, resultTransformer, new IterationReaper(this, iterationId));
iterationStateMap.put(iterationId, iterationState);
if (log.isTraceEnabled())
log.tracef("Started iteration %s", iterationId);
return iterationState;
}
Aggregations