Skip to content

Commit

Permalink
IGNITE-5311: Added ability to get CacheObject value without CacheObje…
Browse files Browse the repository at this point in the history
…ctContext. This closes #2019.
  • Loading branch information
devozerov committed May 27, 2017
1 parent 858e5b7 commit aad3b0c
Show file tree
Hide file tree
Showing 37 changed files with 655 additions and 409 deletions.
Expand Up @@ -31,6 +31,7 @@
import org.apache.ignite.internal.processors.cache.CacheObject; import org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.CacheObjectAdapter; import org.apache.ignite.internal.processors.cache.CacheObjectAdapter;
import org.apache.ignite.internal.processors.cache.CacheObjectContext; import org.apache.ignite.internal.processors.cache.CacheObjectContext;
import org.apache.ignite.internal.processors.cache.CacheObjectValueContext;
import org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl; import org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl;
import org.apache.ignite.internal.util.typedef.internal.S; import org.apache.ignite.internal.util.typedef.internal.S;
import org.apache.ignite.internal.util.typedef.internal.SB; import org.apache.ignite.internal.util.typedef.internal.SB;
Expand Down Expand Up @@ -280,7 +281,7 @@ public BinaryEnumObjectImpl(BinaryContext ctx, byte[] arr) {
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) {
return deserialize(); return deserialize();
} }


Expand Down Expand Up @@ -335,7 +336,7 @@ public BinaryEnumObjectImpl(BinaryContext ctx, byte[] arr) {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void finishUnmarshal(CacheObjectContext ctx, ClassLoader ldr) throws IgniteCheckedException { @Override public void finishUnmarshal(CacheObjectContext ctx, ClassLoader ldr) throws IgniteCheckedException {
this.ctx = ((CacheObjectBinaryProcessorImpl)ctx.processor()).binaryContext(); this.ctx = ((CacheObjectBinaryProcessorImpl)ctx.kernalContext().cacheObjects()).binaryContext();
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.apache.ignite.internal.processors.cache.CacheObject; import org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.CacheObjectAdapter; import org.apache.ignite.internal.processors.cache.CacheObjectAdapter;
import org.apache.ignite.internal.processors.cache.CacheObjectContext; import org.apache.ignite.internal.processors.cache.CacheObjectContext;
import org.apache.ignite.internal.processors.cache.CacheObjectValueContext;
import org.apache.ignite.internal.processors.cache.KeyCacheObject; import org.apache.ignite.internal.processors.cache.KeyCacheObject;
import org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl; import org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl;
import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.internal.util.typedef.internal.U;
Expand Down Expand Up @@ -135,7 +136,7 @@ public BinaryObjectImpl(BinaryContext ctx, byte[] arr, int start) {


/** {@inheritDoc} */ /** {@inheritDoc} */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) {
Object obj0 = obj; Object obj0 = obj;


if (obj0 == null || (cpy && needCopy(ctx))) if (obj0 == null || (cpy && needCopy(ctx)))
Expand Down Expand Up @@ -188,7 +189,7 @@ public BinaryObjectImpl(BinaryContext ctx, byte[] arr, int start) {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void finishUnmarshal(CacheObjectContext ctx, ClassLoader ldr) throws IgniteCheckedException { @Override public void finishUnmarshal(CacheObjectContext ctx, ClassLoader ldr) throws IgniteCheckedException {
this.ctx = ((CacheObjectBinaryProcessorImpl)ctx.processor()).binaryContext(); this.ctx = ((CacheObjectBinaryProcessorImpl)ctx.kernalContext().cacheObjects()).binaryContext();
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down Expand Up @@ -787,9 +788,9 @@ else if (fieldOffsetLen == BinaryUtils.OFFSET_2)
* @param coCtx CacheObjectContext. * @param coCtx CacheObjectContext.
* @return Object. * @return Object.
*/ */
private Object deserializeValue(@Nullable CacheObjectContext coCtx) { private Object deserializeValue(@Nullable CacheObjectValueContext coCtx) {
BinaryReaderExImpl reader = reader(null, BinaryReaderExImpl reader = reader(null, coCtx != null ?
coCtx != null ? coCtx.kernalContext().config().getClassLoader() : ctx.configuration().getClassLoader(), true); coCtx.kernalContext().config().getClassLoader() : ctx.configuration().getClassLoader(), true);


Object obj0 = reader.deserialize(); Object obj0 = reader.deserialize();


Expand All @@ -807,8 +808,8 @@ private Object deserializeValue(@Nullable CacheObjectContext coCtx) {
* @param ctx Context. * @param ctx Context.
* @return {@code True} need to copy value returned to user. * @return {@code True} need to copy value returned to user.
*/ */
private boolean needCopy(CacheObjectContext ctx) { private boolean needCopy(CacheObjectValueContext ctx) {
return ctx.copyOnGet() && obj != null && !ctx.processor().immutable(obj); return ctx.copyOnGet() && obj != null && !ctx.kernalContext().cacheObjects().immutable(obj);
} }


/** /**
Expand All @@ -819,7 +820,8 @@ private boolean needCopy(CacheObjectContext ctx) {
* @param forUnmarshal {@code True} if reader is need to unmarshal object. * @param forUnmarshal {@code True} if reader is need to unmarshal object.
* @return Reader. * @return Reader.
*/ */
private BinaryReaderExImpl reader(@Nullable BinaryReaderHandles rCtx, @Nullable ClassLoader ldr, boolean forUnmarshal) { private BinaryReaderExImpl reader(@Nullable BinaryReaderHandles rCtx, @Nullable ClassLoader ldr,
boolean forUnmarshal) {
if (ldr == null) if (ldr == null)
ldr = ctx.configuration().getClassLoader(); ldr = ctx.configuration().getClassLoader();


Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream; import org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream;
import org.apache.ignite.internal.processors.cache.CacheObject; import org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.CacheObjectContext; import org.apache.ignite.internal.processors.cache.CacheObjectContext;
import org.apache.ignite.internal.processors.cache.CacheObjectValueContext;
import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.plugin.extensions.communication.MessageReader; import org.apache.ignite.plugin.extensions.communication.MessageReader;
import org.apache.ignite.plugin.extensions.communication.MessageWriter; import org.apache.ignite.plugin.extensions.communication.MessageWriter;
Expand Down Expand Up @@ -444,7 +445,7 @@ else if (fieldOffLen == BinaryUtils.OFFSET_2)


/** {@inheritDoc} */ /** {@inheritDoc} */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) {
return (T)deserializeValue(); return (T)deserializeValue();
} }


Expand Down
Expand Up @@ -43,7 +43,7 @@ public interface CacheObject extends Message {
* @param cpy If {@code true} need to copy value. * @param cpy If {@code true} need to copy value.
* @return Value. * @return Value.
*/ */
@Nullable public <T> T value(CacheObjectContext ctx, boolean cpy); @Nullable public <T> T value(CacheObjectValueContext ctx, boolean cpy);


/** /**
* @param ctx Context. * @param ctx Context.
Expand Down
Expand Up @@ -50,8 +50,8 @@ public abstract class CacheObjectAdapter implements CacheObject, Externalizable
* @param ctx Context. * @param ctx Context.
* @return {@code True} need to copy value returned to user. * @return {@code True} need to copy value returned to user.
*/ */
protected boolean needCopy(CacheObjectContext ctx) { protected boolean needCopy(CacheObjectValueContext ctx) {
return ctx.copyOnGet() && val != null && !ctx.processor().immutable(val); return ctx.copyOnGet() && val != null && !ctx.kernalContext().cacheObjects().immutable(val);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down
Expand Up @@ -62,7 +62,7 @@ public CacheObjectByteArrayImpl(byte[] val) {
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) { @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) {
if (cpy) if (cpy)
return (T)Arrays.copyOf(val, val.length); return (T)Arrays.copyOf(val, val.length);


Expand Down
Expand Up @@ -27,20 +27,16 @@
import org.apache.ignite.cache.affinity.AffinityKeyMapper; import org.apache.ignite.cache.affinity.AffinityKeyMapper;
import org.apache.ignite.internal.GridKernalContext; import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.binary.BinaryUtils; import org.apache.ignite.internal.binary.BinaryUtils;
import org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor;
import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.F;


/** /**
* *
*/ */
@SuppressWarnings("TypeMayBeWeakened") @SuppressWarnings("TypeMayBeWeakened")
public class CacheObjectContext { public class CacheObjectContext implements CacheObjectValueContext {
/** */ /** */
private GridKernalContext kernalCtx; private GridKernalContext kernalCtx;


/** */
private IgniteCacheObjectProcessor proc;

/** */ /** */
private String cacheName; private String cacheName;


Expand All @@ -53,9 +49,6 @@ public class CacheObjectContext {
/** */ /** */
private boolean storeVal; private boolean storeVal;


/** */
private boolean p2pEnabled;

/** */ /** */
private boolean addDepInfo; private boolean addDepInfo;


Expand All @@ -78,9 +71,6 @@ public CacheObjectContext(GridKernalContext kernalCtx,
this.cpyOnGet = cpyOnGet; this.cpyOnGet = cpyOnGet;
this.storeVal = storeVal; this.storeVal = storeVal;
this.addDepInfo = addDepInfo; this.addDepInfo = addDepInfo;

p2pEnabled = kernalCtx.config().isPeerClassLoadingEnabled();
proc = kernalCtx.cacheObjects();
} }


/** /**
Expand All @@ -90,31 +80,18 @@ public String cacheName() {
return cacheName; return cacheName;
} }


/** /** {@inheritDoc} */
* @return {@code True} if peer class loading is enabled. @Override public boolean addDeploymentInfo() {
*/
public boolean p2pEnabled() {
return p2pEnabled;
}

/**
* @return {@code True} if deployment info should be associated with the objects of this cache.
*/
public boolean addDeploymentInfo() {
return addDepInfo; return addDepInfo;
} }


/** /** {@inheritDoc} */
* @return Copy on get flag. @Override public boolean copyOnGet() {
*/
public boolean copyOnGet() {
return cpyOnGet; return cpyOnGet;
} }


/** /** {@inheritDoc} */
* @return {@code True} if should store unmarshalled value in cache. @Override public boolean storeValue() {
*/
public boolean storeValue() {
return storeVal; return storeVal;
} }


Expand All @@ -125,27 +102,14 @@ public AffinityKeyMapper defaultAffMapper() {
return dfltAffMapper; return dfltAffMapper;
} }


/** /** {@inheritDoc} */
* @return Kernal context. @Override public GridKernalContext kernalContext() {
*/
public GridKernalContext kernalContext() {
return kernalCtx; return kernalCtx;
} }


/** /** {@inheritDoc} */
* @return Processor. @Override public boolean binaryEnabled() {
*/ return false;
public IgniteCacheObjectProcessor processor() {
return proc;
}

/**
* @param o Object to unwrap.
* @param keepBinary Keep binary flag.
* @return Unwrapped object.
*/
public Object unwrapBinaryIfNeeded(Object o, boolean keepBinary) {
return unwrapBinaryIfNeeded(o, keepBinary, true);
} }


/** /**
Expand All @@ -158,141 +122,6 @@ public Object unwrapBinaryIfNeeded(Object o, boolean keepBinary, boolean cpy) {
if (o == null) if (o == null)
return null; return null;


return unwrapBinary(o, keepBinary, cpy); return CacheObjectUtils.unwrapBinaryIfNeeded(this, o, keepBinary, cpy);
}

/**
* @param col Collection of objects to unwrap.
* @param keepBinary Keep binary flag.
* @return Unwrapped collection.
*/
public Collection<Object> unwrapBinariesIfNeeded(Collection<Object> col, boolean keepBinary) {
return unwrapBinariesIfNeeded(col, keepBinary, true);
}

/**
* @param col Collection to unwrap.
* @param keepBinary Keep binary flag.
* @param cpy Copy value flag.
* @return Unwrapped collection.
*/
public Collection<Object> unwrapBinariesIfNeeded(Collection<Object> col, boolean keepBinary, boolean cpy) {
Collection<Object> col0 = BinaryUtils.newKnownCollection(col);

if (col0 == null)
col0 = new ArrayList<>(col.size());

for (Object obj : col)
col0.add(unwrapBinary(obj, keepBinary, cpy));

return col0;
}

/**
* @param col Collection to unwrap.
* @param keepBinary Keep binary flag.
* @param cpy Copy flag.
* @return Unwrapped collection.
*/
private Collection<Object> unwrapKnownCollection(Collection<Object> col, boolean keepBinary, boolean cpy) {
Collection<Object> col0 = BinaryUtils.newKnownCollection(col);

for (Object obj : col)
col0.add(unwrapBinary(obj, keepBinary, cpy));

return col0;
}

/**
* Unwrap array of binaries if needed.
*
* @param arr Array.
* @param keepBinary Keep binary flag.
* @param cpy Copy.
* @return Result.
*/
public Object[] unwrapBinariesInArrayIfNeeded(Object[] arr, boolean keepBinary, boolean cpy) {
if (BinaryUtils.knownArray(arr))
return arr;

Object[] res = new Object[arr.length];

for (int i = 0; i < arr.length; i++)
res[i] = unwrapBinary(arr[i], keepBinary, cpy);

return res;
}

/**
* Unwraps map.
*
* @param map Map to unwrap.
* @param keepBinary Keep binary flag.
* @return Unwrapped collection.
*/
private Map<Object, Object> unwrapBinariesIfNeeded(Map<Object, Object> map, boolean keepBinary, boolean cpy) {
if (keepBinary)
return map;

Map<Object, Object> map0 = BinaryUtils.newMap(map);

for (Map.Entry<Object, Object> e : map.entrySet())
map0.put(unwrapBinary(e.getKey(), keepBinary, cpy), unwrapBinary(e.getValue(), keepBinary, cpy));

return map0;
}

/**
* @param o Object to unwrap.
* @return Unwrapped object.
*/
private Object unwrapBinary(Object o, boolean keepBinary, boolean cpy) {
if (o instanceof Map.Entry) {
Map.Entry entry = (Map.Entry)o;

Object key = entry.getKey();

Object uKey = unwrapBinary(key, keepBinary, cpy);

Object val = entry.getValue();

Object uVal = unwrapBinary(val, keepBinary, cpy);

return (key != uKey || val != uVal) ? F.t(uKey, uVal) : o;
}
else if (BinaryUtils.knownCollection(o))
return unwrapKnownCollection((Collection<Object>)o, keepBinary, cpy);
else if (BinaryUtils.knownMap(o))
return unwrapBinariesIfNeeded((Map<Object, Object>)o, keepBinary, cpy);
else if (o instanceof Object[])
return unwrapBinariesInArrayIfNeeded((Object[])o, keepBinary, cpy);
else if (o instanceof CacheObject) {
CacheObject co = (CacheObject)o;

if (!keepBinary || co.isPlatformType())
return unwrapBinary(co.value(this, cpy), keepBinary, cpy);
}

return o;
}

/**
* @param o Object to test.
* @return True if collection should be recursively unwrapped.
*/
private boolean knownCollection(Object o) {
Class<?> cls = o == null ? null : o.getClass();

return cls == ArrayList.class || cls == LinkedList.class || cls == HashSet.class;
}

/**
* @param o Object to test.
* @return True if map should be recursively unwrapped.
*/
private boolean knownMap(Object o) {
Class<?> cls = o == null ? null : o.getClass();

return cls == HashMap.class || cls == LinkedHashMap.class;
} }
} }

0 comments on commit aad3b0c

Please sign in to comment.