/** * Unwraps cache object, if needed. * * @param o Object to unwrap. * @return Unwrapped object. */ private Object unwrap(Object o) { return coCtx == null ? o : o instanceof CacheObject ? ((CacheObject)o).value(coCtx, false) : o; }
/** * @param ctx Cache object context. * @return Marshalled size. * @throws IgniteCheckedException If failed. */ public int marshalledSize(CacheObjectContext ctx) throws IgniteCheckedException { int size = 0; if (val != null) size += val.valueBytes(ctx).length; size += key.valueBytes(ctx).length; return SIZE_OVERHEAD + size; }
/** {@inheritDoc} */ @Override public byte[] getBytesNoCopy() { if (obj.cacheObjectType() == CacheObject.TYPE_REGULAR) { // Result must be the same as `marshaller.marshall(obj.value(coctx, false));` try { return obj.valueBytes(valCtx); } catch (IgniteCheckedException e) { throw DbException.convert(e); } } // For user-provided and array types. return JdbcUtils.serialize(obj, null); }
/** * @param cpy Copy flag. * @return Value. */ public Object getObject(boolean cpy) { return obj.isPlatformType() ? obj.value(valCtx, cpy) : obj; }
/** * @param obj Object. * @param ctx Context. * @throws IgniteCheckedException If failed. */ protected final void prepareMarshalCacheObject(CacheObject obj, GridCacheContext ctx) throws IgniteCheckedException { if (obj != null) { obj.prepareMarshal(ctx.cacheObjectContext()); if (addDepInfo) prepareObject(obj.value(ctx.cacheObjectContext(), false), ctx.shared()); } }
CacheObjectContext coctx = objectContext(); Object key = k.isPlatformType() ? k.value(coctx, false) : k; Object val = v.isPlatformType() ? v.value(coctx, false) : v; BytesRef keyByteRef = new BytesRef(k.valueBytes(coctx)); doc.add(new StoredField(VAL_FIELD_NAME, v.valueBytes(coctx)));
/** * This method is called after the whole message is received * and is responsible for unmarshalling state. * * @param ctx Context. * @param ldr Class loader. * @throws IgniteCheckedException If failed. */ public void finishUnmarshal(GridCacheContext ctx, ClassLoader ldr) throws IgniteCheckedException { if (val != null) val.finishUnmarshal(ctx.cacheObjectContext(), ldr); }
/** * @throws Exception If failed. */ @Test public void testPrimitiveValues() throws Exception { IgniteEx ignite = grid(0); IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME); for (int i = 0; i < 10; i++) cache.put(i, String.valueOf(i)); IgniteCacheObjectProcessor co = ignite.context().cacheObjects(); GridCacheAdapter<Object, Object> iCache = ignite.context().cache().internalCache(CACHE_NAME); GridCacheContext<Object, Object> cacheCtx = iCache.context(); CacheObjectContext coCtx = cacheCtx.cacheObjectContext(); ByteBuffer buf = ByteBuffer.allocate(2048); for (int i = 0; i < 10; i++) { KeyCacheObject key = co.toCacheKeyObject(coCtx, cacheCtx, i, false); GridCacheEntryEx entry = iCache.peekEx(key); assertNotNull(entry); assertTrue(entry.key().putValue(buf)); assertTrue(entry.valueBytes().putValue(buf)); } buf.flip(); for (int i = 0; i < 10; i++) { CacheObject co1 = co.toCacheObject(coCtx, buf); assertEquals((Integer)i, co1.value(coCtx, false)); CacheObject co2 = co.toCacheObject(coCtx, buf); assertEquals(String.valueOf(i), co2.value(coCtx, false)); } }
/** * This method is called before the whole message is sent * and is responsible for pre-marshalling state. * * @param ctx Cache object context. * @throws IgniteCheckedException If failed. */ public void prepareMarshal(CacheObjectContext ctx) throws IgniteCheckedException { if (val != null) val.prepareMarshal(ctx); }
/** * @param info Entry to marshal. * @param ctx Context. * @param cacheObjCtx Cache object context. * @throws IgniteCheckedException If failed. */ protected final void marshalInfo(GridCacheEntryInfo info, GridCacheSharedContext ctx, CacheObjectContext cacheObjCtx ) throws IgniteCheckedException { assert ctx != null; if (info != null) { info.marshal(cacheObjCtx); if (addDepInfo) { if (info.key() != null) prepareObject(info.key().value(cacheObjCtx, false), ctx); CacheObject val = info.value(); if (val != null) { val.finishUnmarshal(cacheObjCtx, ctx.deploy().globalLoader()); prepareObject(val.value(cacheObjCtx, false), ctx); } } } }
/** {@inheritDoc} */ @Override public int memorySize() throws IgniteCheckedException { byte[] kb; byte[] vb = null; int extrasSize; lockEntry(); try { key.prepareMarshal(cctx.cacheObjectContext()); kb = key.valueBytes(cctx.cacheObjectContext()); if (val != null) { val.prepareMarshal(cctx.cacheObjectContext()); vb = val.valueBytes(cctx.cacheObjectContext()); } extrasSize = extrasSize(); } finally { unlockEntry(); } return SIZE_OVERHEAD + extrasSize + kb.length + (vb == null ? 1 : vb.length); }
/** {@inheritDoc} */ @Override public int size() throws IgniteCheckedException { int len = key().valueBytesLength(null); len += value().valueBytesLength(null) + CacheVersionIO.size(version(), false) + 8; return len + (cacheId() != 0 ? 4 : 0); }
/** * @param buf Buffer to write to. * @param entry Data entry. */ void putPlainDataEntry(ByteBuffer buf, DataEntry entry) throws IgniteCheckedException { buf.putInt(entry.cacheId()); if (!entry.key().putValue(buf)) throw new AssertionError(); if (entry.value() == null) buf.putInt(-1); else if (!entry.value().putValue(buf)) throw new AssertionError(); buf.put((byte)entry.op().ordinal()); putVersion(buf, entry.nearXidVersion(), true); putVersion(buf, entry.writeVersion(), false); buf.putInt(entry.partitionId()); buf.putLong(entry.partitionCounter()); buf.putLong(entry.expireTime()); }
CacheObjectContext coctx = objectContext(); Object key = k.isPlatformType() ? k.value(coctx, false) : k; Object val = v.isPlatformType() ? v.value(coctx, false) : v; BytesRef keyByteRef = new BytesRef(k.valueBytes(coctx)); doc.add(new StoredField(VAL_FIELD_NAME, v.valueBytes(coctx)));
/** * @param o Object to unwrap. * @return Unwrapped object. */ private static Object unwrapBinary(CacheObjectValueContext ctx, Object o, boolean keepBinary, boolean cpy) { if (o == null) return o; while (BinaryUtils.knownCacheObject(o)) { CacheObject co = (CacheObject)o; if (!co.isPlatformType() && keepBinary) return o; // It may be a collection of binaries o = co.value(ctx, cpy); } if (BinaryUtils.knownCollection(o)) return unwrapKnownCollection(ctx, (Collection<Object>)o, keepBinary, cpy); else if (BinaryUtils.knownMap(o)) return unwrapBinariesIfNeeded(ctx, (Map<Object, Object>)o, keepBinary, cpy); else if (o instanceof Object[]) return unwrapBinariesInArrayIfNeeded(ctx, (Object[])o, keepBinary, cpy); return o; }
/** * @param ctx Context. * @param ldr Loader. * @throws IgniteCheckedException If failed. */ public void unmarshalValue(GridCacheContext<?, ?> ctx, ClassLoader ldr) throws IgniteCheckedException { if (val != null) val.finishUnmarshal(ctx.cacheObjectContext(), ldr); }
/** * @param col Collection. * @param ctx Cache context. * @throws IgniteCheckedException If failed. */ protected final void prepareMarshalCacheObjects(@Nullable Collection<? extends CacheObject> col, GridCacheContext ctx) throws IgniteCheckedException { if (col == null) return; for (CacheObject obj : col) { if (obj != null) { obj.prepareMarshal(ctx.cacheObjectContext()); if (addDepInfo) prepareObject(obj.value(ctx.cacheObjectContext(), false), ctx.shared()); } } }
/** * @throws Exception If failed. */ @Test public void testClassValues() throws Exception { IgniteEx ignite = grid(0); IgniteCache<Object, Object> cache = ignite.cache(CACHE_NAME); for (int i = 0; i < 10; i++) cache.put(new TestValue(i), new TestValue(i)); IgniteCacheObjectProcessor co = ignite.context().cacheObjects(); GridCacheAdapter<Object, Object> iCache = ignite.context().cache().internalCache(CACHE_NAME); GridCacheContext<Object, Object> cacheCtx = iCache.context(); CacheObjectContext coCtx = cacheCtx.cacheObjectContext(); ByteBuffer buf = ByteBuffer.allocate(2048); for (int i = 0; i < 10; i++) { KeyCacheObject key = co.toCacheKeyObject(coCtx, cacheCtx, new TestValue(i), false); GridCacheEntryEx entry = iCache.peekEx(key); assertNotNull(entry); assertTrue(entry.key().putValue(buf)); assertTrue(entry.valueBytes().putValue(buf)); } buf.flip(); for (int i = 0; i < 10; i++) { CacheObject co1 = co.toCacheObject(coCtx, buf); assertEquals(new TestValue(i), co1.value(coCtx, false)); CacheObject co2 = co.toCacheObject(coCtx, buf); assertEquals(new TestValue(i), co2.value(coCtx, false)); } }
/** * @param obj CacheObject to marshal * @param ctx context * @throws IgniteCheckedException if error */ private void prepareMarshalObject(CacheObject obj, GridCacheContext ctx) throws IgniteCheckedException { if (obj != null) obj.prepareMarshal(ctx.cacheObjectContext()); }
/** {@inheritDoc} */ @Override protected void writeFragmentData(CacheDataRow row, ByteBuffer buf, int rowOff, int payloadSize) throws IgniteCheckedException { final int keySize = row.key().valueBytesLength(null); final int valSize = row.value().valueBytesLength(null); int written = writeFragment(row, buf, rowOff, payloadSize, MVCC_INFO, keySize, valSize); written += writeFragment(row, buf, rowOff + written, payloadSize - written, CACHE_ID, keySize, valSize); written += writeFragment(row, buf, rowOff + written, payloadSize - written, KEY, keySize, valSize); written += writeFragment(row, buf, rowOff + written, payloadSize - written, EXPIRE_TIME, keySize, valSize); written += writeFragment(row, buf, rowOff + written, payloadSize - written, VALUE, keySize, valSize); written += writeFragment(row, buf, rowOff + written, payloadSize - written, VERSION, keySize, valSize); assert written == payloadSize; }