org.infinispan.commands.read.KeySetCommand Maven / Gradle / Ivy
package org.infinispan.commands.read;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.stream.StreamSupport;
import org.infinispan.Cache;
import org.infinispan.CacheSet;
import org.infinispan.CacheStream;
import org.infinispan.commands.VisitableCommand;
import org.infinispan.commands.Visitor;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.CloseableSpliterator;
import org.infinispan.commons.util.Closeables;
import org.infinispan.commons.util.EnumUtil;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.Flag;
import org.infinispan.context.InvocationContext;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.stream.impl.local.KeyStreamSupplier;
import org.infinispan.stream.impl.local.LocalCacheStream;
import org.infinispan.util.DataContainerRemoveIterator;
/**
* Command implementation for {@link java.util.Map#keySet()} functionality.
*
* @author Galder Zamarreño
* @author [email protected]
* @author Trustin Lee
* @author William Burns
* @since 4.0
*/
public class KeySetCommand extends AbstractLocalCommand implements VisitableCommand {
private final Cache cache;
public KeySetCommand(Cache cache, long flagsBitSet) {
setFlagsBitSet(flagsBitSet);
if (flagsBitSet != EnumUtil.EMPTY_BIT_SET) {
this.cache = cache.getAdvancedCache().withFlags(EnumUtil.enumArrayOf(flagsBitSet, Flag.class));
} else {
this.cache = cache;
}
}
@Override
public Object acceptVisitor(InvocationContext ctx, Visitor visitor) throws Throwable {
return visitor.visitKeySetCommand(ctx, this);
}
@Override
public LoadType loadType() {
throw new UnsupportedOperationException();
}
@Override
public Set perform(InvocationContext ctx) throws Throwable {
return new BackingKeySet<>(cache);
}
@Override
public String toString() {
return "KeySetCommand{" +
"cache=" + cache.getName() +
'}';
}
private static class BackingKeySet extends AbstractCloseableIteratorCollection implements CacheSet {
public BackingKeySet(Cache cache) {
super(cache);
}
@Override
public CloseableIterator iterator() {
return new EntryToKeyIterator(new DataContainerRemoveIterator<>(cache));
}
@Override
public CloseableSpliterator spliterator() {
return Closeables.spliterator(iterator(), cache.getAdvancedCache().getDataContainer().size(),
Spliterator.CONCURRENT | Spliterator.DISTINCT | Spliterator.NONNULL);
}
@Override
public int size() {
return cache.getAdvancedCache().getDataContainer().size();
}
@Override
public boolean contains(Object o) {
return cache.containsKey(o);
}
@Override
public boolean remove(Object o) {
return cache.remove(o) != null;
}
@Override
public CacheStream stream() {
DistributionManager dm = cache.getAdvancedCache().getDistributionManager();
return new LocalCacheStream<>(new KeyStreamSupplier<>(cache, dm != null ? dm.getConsistentHash() : null,
() -> StreamSupport.stream(spliterator(), false)), false,
cache.getAdvancedCache().getComponentRegistry());
}
@Override
public CacheStream parallelStream() {
DistributionManager dm = cache.getAdvancedCache().getDistributionManager();
return new LocalCacheStream<>(new KeyStreamSupplier<>(cache, dm != null ? dm.getConsistentHash() : null,
() -> StreamSupport.stream(spliterator(), false)), true,
cache.getAdvancedCache().getComponentRegistry());
}
}
private static class EntryToKeyIterator implements CloseableIterator {
private final Iterator> iterator;
public EntryToKeyIterator(Iterator> iterator) {
this.iterator = iterator;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public K next() {
return iterator.next().getKey();
}
@Override
public void remove() {
iterator.remove();
}
@Override
public void close() {
// Do nothing as we can't close regular iterator
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy