Search in sources :

Example 1 with KeyValueFilterConverterFactory

use of org.infinispan.filter.KeyValueFilterConverterFactory 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

Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 RemovalListener (com.github.benmanes.caffeine.cache.RemovalListener)1 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 BitSet (java.util.BitSet)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 AdvancedCache (org.infinispan.AdvancedCache)1 BaseCacheStream (org.infinispan.BaseCacheStream)1 CacheStream (org.infinispan.CacheStream)1