Search in sources :

Example 6 with KeyValuePair

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);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 7 with KeyValuePair

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);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 8 with KeyValuePair

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;
}
Also used : CacheRemove(javax.cache.annotation.CacheRemove) KeyValuePair(org.infinispan.util.KeyValuePair) Method(java.lang.reflect.Method) CacheResolver(javax.cache.annotation.CacheResolver) Annotation(java.lang.annotation.Annotation) CachePut(javax.cache.annotation.CachePut) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory) CacheLookupHelper.getCacheResolverFactory(org.infinispan.jcache.annotation.CacheLookupHelper.getCacheResolverFactory) CacheDefaults(javax.cache.annotation.CacheDefaults) CacheResult(javax.cache.annotation.CacheResult) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CacheLookupHelper.getCacheKeyGenerator(org.infinispan.jcache.annotation.CacheLookupHelper.getCacheKeyGenerator) CacheMethodDetails(javax.cache.annotation.CacheMethodDetails)

Example 9 with KeyValuePair

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);
    }
}
Also used : KeyValuePair(org.infinispan.util.KeyValuePair) ParamKeyValueFilterConverterFactory(org.infinispan.filter.ParamKeyValueFilterConverterFactory)

Example 10 with KeyValuePair

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;
}
Also used : WithinThreadExecutor(org.infinispan.util.concurrent.WithinThreadExecutor) Arrays(java.util.Arrays) CacheStream(org.infinispan.CacheStream) TimeServiceTicker(org.infinispan.commons.time.TimeServiceTicker) IntSets(org.infinispan.commons.util.IntSets) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) APPLICATION_OBJECT(org.infinispan.commons.dataconversion.MediaType.APPLICATION_OBJECT) Map(java.util.Map) KeyValueFilterConverterFactory(org.infinispan.filter.KeyValueFilterConverterFactory) ParamKeyValueFilterConverterFactory(org.infinispan.filter.ParamKeyValueFilterConverterFactory) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) RemovalListener(com.github.benmanes.caffeine.cache.RemovalListener) KeyValuePair(org.infinispan.util.KeyValuePair) KeyValueFilterConverter(org.infinispan.filter.KeyValueFilterConverter) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) CacheFilters.filterAndConvert(org.infinispan.filter.CacheFilters.filterAndConvert) Iterator(java.util.Iterator) CacheEntry(org.infinispan.container.entries.CacheEntry) BaseCacheStream(org.infinispan.BaseCacheStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Util(org.infinispan.commons.util.Util) Log(org.infinispan.server.hotrod.logging.Log) Set(java.util.Set) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) OperationStatus(org.infinispan.server.hotrod.OperationStatus) Closeable(java.io.Closeable) Optional(java.util.Optional) LogFactory(org.infinispan.commons.logging.LogFactory) DataConversion(org.infinispan.encoding.DataConversion) BitSet(java.util.BitSet) Collections(java.util.Collections) TimeService(org.infinispan.commons.time.TimeService) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) CacheEntry(org.infinispan.container.entries.CacheEntry) DataConversion(org.infinispan.encoding.DataConversion) KeyValueFilterConverterFactory(org.infinispan.filter.KeyValueFilterConverterFactory) ParamKeyValueFilterConverterFactory(org.infinispan.filter.ParamKeyValueFilterConverterFactory) KeyValueFilterConverter(org.infinispan.filter.KeyValueFilterConverter) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache)

Aggregations

KeyValuePair (org.infinispan.util.KeyValuePair)26 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 UserPB (org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)6 ContinuousQueryListener (org.infinispan.query.api.continuous.ContinuousQueryListener)6 QueryFactory (org.infinispan.query.dsl.QueryFactory)6 User (org.infinispan.query.dsl.embedded.testdomain.User)6 HotRodClient (org.infinispan.server.hotrod.test.HotRodClient)4 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Set (java.util.Set)3 InvocationStage (org.infinispan.interceptors.InvocationStage)3 SyncInvocationStage (org.infinispan.interceptors.SyncInvocationStage)3 Metadata (org.infinispan.metadata.Metadata)3 Flowable (io.reactivex.rxjava3.core.Flowable)2 ByteBuffer (java.nio.ByteBuffer)2 TreeSet (java.util.TreeSet)2 CompletionStage (java.util.concurrent.CompletionStage)2 Predicate (java.util.function.Predicate)2