org.infinispan.functional.impl.ReadOnlyMapImpl Maven / Gradle / Ivy
package org.infinispan.functional.impl;
import static org.infinispan.functional.impl.Params.withFuture;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.infinispan.commands.functional.ReadOnlyKeyCommand;
import org.infinispan.commands.functional.ReadOnlyManyCommand;
import org.infinispan.commons.api.functional.EntryView.ReadEntryView;
import org.infinispan.commons.api.functional.FunctionalMap.ReadOnlyMap;
import org.infinispan.commons.api.functional.Param;
import org.infinispan.commons.api.functional.Param.FutureMode;
import org.infinispan.commons.api.functional.Traversable;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.Experimental;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
/**
* Read-only map implementation.
*
* @since 8.0
*/
@Experimental
public final class ReadOnlyMapImpl extends AbstractFunctionalMap implements ReadOnlyMap {
private static final Log log = LogFactory.getLog(ReadOnlyMapImpl.class);
private final Params params;
private ReadOnlyMapImpl(Params params, FunctionalMapImpl functionalMap) {
super(functionalMap);
this.params = params;
}
public static ReadOnlyMap create(FunctionalMapImpl functionalMap) {
return new ReadOnlyMapImpl<>(Params.from(functionalMap.params.params), functionalMap);
}
private static ReadOnlyMap create(Params params, FunctionalMapImpl functionalMap) {
return new ReadOnlyMapImpl<>(params, functionalMap);
}
@Override
public CompletableFuture eval(K key, Function, R> f) {
log.tracef("Invoked eval(k=%s, %s)", key, params);
Param futureMode = params.get(FutureMode.ID);
ReadOnlyKeyCommand cmd = fmap.cmdFactory().buildReadOnlyKeyCommand(key, f);
InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(false, 1);
return withFuture(futureMode, fmap.asyncExec(), () -> (R) fmap.chain().invoke(ctx, cmd));
}
@Override
public Traversable evalMany(Set extends K> keys, Function, R> f) {
log.tracef("Invoked evalMany(m=%s, %s)", keys, params);
ReadOnlyManyCommand cmd = fmap.cmdFactory().buildReadOnlyManyCommand(keys, f);
InvocationContext ctx = fmap.invCtxFactory().createInvocationContext(false, keys.size());
return Traversables.of((Stream) fmap.chain().invoke(ctx, cmd));
}
@Override
public Traversable keys() {
log.tracef("Invoked keys(%s)", params);
return Traversables.of(fmap.cache.keySet().stream());
}
@Override
public Traversable> entries() {
log.tracef("Invoked entries(%s)", params);
CloseableIterator> it = fmap.cache.cacheEntrySet().iterator();
// TODO: Don't really need a Stream here...
Stream> stream = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(it, Spliterator.IMMUTABLE), false);
return Traversables.of(stream.map(EntryViews::readOnly));
}
@Override
public ReadOnlyMap withParams(Param>... ps) {
if (ps == null || ps.length == 0)
return this;
if (params.containsAll(ps))
return this; // We already have all specified params
return create(params.addAll(ps), fmap);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy