org.infinispan.commands.read.GetCacheEntryCommand Maven / Gradle / Ivy
package org.infinispan.commands.read;
import static org.infinispan.commons.util.Util.toStr;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Set;
import org.infinispan.commands.Visitor;
import org.infinispan.container.InternalEntryFactory;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.Flag;
import org.infinispan.context.InvocationContext;
/**
* Used to fetch a full CacheEntry rather than just the value.
* This functionality was originally incorporated into GetKeyValueCommand.
*
* @author Sanne Grinovero (C) 2014 Red Hat Inc.
* @since 7.1
*/
public final class GetCacheEntryCommand extends AbstractDataCommand implements RemoteFetchingCommand {
public static final byte COMMAND_ID = 45;
private InternalEntryFactory entryFactory;
private InternalCacheEntry remotelyFetchedValue;
public GetCacheEntryCommand(Object key, Set flags, InternalEntryFactory entryFactory) {
this.key = key;
this.flags = flags;
this.entryFactory = entryFactory;
}
public GetCacheEntryCommand() {
}
@Override
public Object acceptVisitor(InvocationContext ctx, Visitor visitor) throws Throwable {
return visitor.visitGetCacheEntryCommand(ctx, this);
}
@Override
public boolean readsExistingValues() {
return true;
}
@Override
public Object perform(InvocationContext ctx) throws Throwable {
CacheEntry entry = ctx.lookupEntry(key);
if (entry == null || entry.isNull()) {
return null;
}
if (entry.isRemoved()) {
return null;
}
return entryFactory.copy(entry);
}
@Override
public byte getCommandId() {
return COMMAND_ID;
}
/**
* @see #getRemotelyFetchedValue()
*/
public void setRemotelyFetchedValue(InternalCacheEntry remotelyFetchedValue) {
this.remotelyFetchedValue = remotelyFetchedValue;
}
/**
* If the cache needs to go remotely in order to obtain the value associated to this key, then the remote value
* is stored in this field.
* TODO: this method should be able to removed with the refactoring from ISPN-2177
*/
public InternalCacheEntry getRemotelyFetchedValue() {
return remotelyFetchedValue;
}
@Override
public void writeTo(ObjectOutput output) throws IOException {
output.writeObject(key);
output.writeObject(Flag.copyWithoutRemotableFlags(flags));
}
@Override
public void readFrom(ObjectInput input) throws IOException, ClassNotFoundException {
key = input.readObject();
flags = (Set) input.readObject();
}
public String toString() {
return new StringBuilder()
.append("GetCacheEntryCommand {key=")
.append(toStr(key))
.append(", flags=").append(flags)
.append("}")
.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy