/** {@inheritDoc} */ @Override public BinaryObject clone() throws CloneNotSupportedException { return heapCopy(); }
/** {@inheritDoc} */ @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) { return (T)deserializeValue(); }
/** {@inheritDoc} */ @Override public int footerStartOffset() { short flags = BinaryPrimitives.readShort(ptr, start + GridBinaryMarshaller.FLAGS_POS); if (!BinaryUtils.hasSchema(flags)) return start + length(); return start + BinaryPrimitives.readInt(ptr, start + GridBinaryMarshaller.SCHEMA_OR_RAW_OFF_POS); }
BinaryObjectOffheapImpl offheapObj = new BinaryObjectOffheapImpl(ctx, ptr, 0, obj.array().length); assertTrue(offheapObj.equals(offheapObj)); assertFalse(offheapObj.equals(null)); assertFalse(offheapObj.equals("str")); assertTrue(offheapObj.equals(obj)); assertTrue(obj.equals(offheapObj)); BinaryObjectOffheapImpl offheapObj1 = new BinaryObjectOffheapImpl(ctx, ptr1, 0, obj.array().length); assertTrue(offheapObj.equals(offheapObj1)); assertTrue(offheapObj1.equals(offheapObj)); assertEquals(obj.type().typeId(), offheapObj.type().typeId()); assertEquals(obj.hashCode(), offheapObj.hashCode()); BinaryObjectOffheapImpl innerOffheapObj = offheapObj.field("inner"); obj = (BinaryObjectImpl)offheapObj.heapCopy(); assertEquals(obj.type().typeId(), offheapObj.type().typeId()); assertEquals(obj.hashCode(), offheapObj.hashCode());
/** * @param obj Instance of the BinaryObjectImpl to offheap marshalling. * @param marsh Binary marshaller. * @return Instance of BinaryObjectOffheapImpl. */ private BinaryObjectOffheapImpl marshalOffHeap(BinaryObjectImpl obj, BinaryMarshaller marsh) { long ptr = copyOffheap(obj); return new BinaryObjectOffheapImpl(binaryContext(marsh), ptr, 0, obj.array().length); }
/** * @return Deserialized value. */ private Object deserializeValue() { return reader(null, true).deserialize(); }
GridUnsafe.freeMemory(binObjOffheap0.offheapAddress()); binObjOffheap0 = null; GridUnsafe.freeMemory(binObjOffheap1.offheapAddress()); binObjOffheap1 = null; GridUnsafe.freeMemory(binObjWithRawOffheap0.offheapAddress()); binObjOffheap1 = null; GridUnsafe.freeMemory(binObjWithRawOffheap1.offheapAddress()); binObjOffheap1 = null; GridUnsafe.freeMemory(binObjRawOffheap0.offheapAddress()); binObjOffheap1 = null; GridUnsafe.freeMemory(binObjRawOffheap1.offheapAddress()); binObjOffheap1 = null;
/** {@inheritDoc} */ @Override protected BinaryObjectExImpl toBinary(BinaryMarshaller marsh, Object obj) throws Exception { byte[] arr = marsh.marshal(obj); long ptr = GridUnsafe.allocateMemory(arr.length); ptrs.add(ptr); GridUnsafe.copyHeapOffheap(arr, GridUnsafe.BYTE_ARR_OFF, ptr, arr.length); return new BinaryObjectOffheapImpl(ctx, ptr, 0, arr.length); } }
/** {@inheritDoc} */ @Override public BinarySchema createSchema() { return reader(null, false).getOrCreateSchema(); }
/** * Convert binary object to it's final state. * * @param obj Object. * @param offheap Offheap flag. * @return Result. */ private BinaryObjectExImpl convert(BinaryObjectExImpl obj, boolean offheap) { if (offheap) { byte[] arr = obj.array(); long ptr = GridUnsafe.allocateMemory(arr.length); ptrs.add(ptr); GridUnsafe.copyMemory(arr, GridUnsafe.BYTE_ARR_OFF, null, ptr, arr.length); obj = new BinaryObjectOffheapImpl(obj.context(), ptr, 0, obj.array().length); } return obj; }
/** {@inheritDoc} */ @Override public void writeTo(BinaryWriterExImpl writer, BinaryBuilderSerializer ctx) { BinaryObject val = binaryObj; if (val instanceof BinaryObjectOffheapImpl) val = ((BinaryObjectOffheapImpl)val).heapCopy(); writer.doWriteBinaryObject((BinaryObjectImpl)val); } }
/** {@inheritDoc} */ @Nullable @Override public <F> F field(int fieldId) throws BinaryObjectException { return (F) reader(null, false).unmarshalField(fieldId); }
/** {@inheritDoc} */ @Nullable @Override public <T> T deserialize() throws BinaryObjectException { return (T)deserializeValue(); }
/** {@inheritDoc} */ @Override public int footerStartOffset() { short flags = BinaryPrimitives.readShort(ptr, start + GridBinaryMarshaller.FLAGS_POS); if (!BinaryUtils.hasSchema(flags)) return start + length(); return start + BinaryPrimitives.readInt(ptr, start + GridBinaryMarshaller.SCHEMA_OR_RAW_OFF_POS); }
/** * Convert binary object to it's final state. * * @param obj Object. * @param offheap Offheap flag. * @return Result. */ private BinaryObjectExImpl convert(BinaryObject obj, boolean offheap) { BinaryObjectExImpl obj0 = (BinaryObjectExImpl)obj; if (offheap) { byte[] arr = obj0.array(); long ptr = GridUnsafe.allocateMemory(arr.length); ptrs.add(ptr); GridUnsafe.copyMemory(arr, GridUnsafe.BYTE_ARR_OFF, null, ptr, arr.length); obj0 = new BinaryObjectOffheapImpl(obj0.context(), ptr, 0, obj0.array().length); } return obj0; }
/** {@inheritDoc} */ @Override public BinaryObjectBuilder toBuilder() throws BinaryObjectException { return BinaryObjectBuilderImpl.wrap(heapCopy()); }
/** {@inheritDoc} */ @Nullable @Override public <F> F field(String fieldName) throws BinaryObjectException { return (F) reader(null, false).unmarshalField(fieldName); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) { return (T)deserializeValue(); }
/** * Perform post-write hash code update if necessary. * * @param clsName Class name. Always null if class is registered. */ public void postWriteHashCode(@Nullable String clsName) { int typeId = clsName == null ? this.typeId : ctx.typeId(clsName); BinaryIdentityResolver identity = ctx.identity(typeId); if (out.hasArray()) { // Heap. byte[] data = out.array(); BinaryObjectImpl obj = new BinaryObjectImpl(ctx, data, start); BinaryPrimitives.writeInt(data, start + GridBinaryMarshaller.HASH_CODE_POS, identity.hashCode(obj)); } else { // Offheap. long ptr = out.rawOffheapPointer(); assert ptr != 0; BinaryObjectOffheapImpl obj = new BinaryObjectOffheapImpl(ctx, ptr, start, out.capacity()); BinaryPrimitives.writeInt(ptr, start + GridBinaryMarshaller.HASH_CODE_POS, identity.hashCode(obj)); } }
/** * Creates builder initialized by specified binary object. * * @param obj Binary object to initialize builder. * @return New builder. */ public static BinaryObjectBuilderImpl wrap(BinaryObject obj) { BinaryObjectImpl heapObj; if (obj instanceof BinaryObjectOffheapImpl) heapObj = (BinaryObjectImpl)((BinaryObjectOffheapImpl)obj).heapCopy(); else heapObj = (BinaryObjectImpl)obj; return new BinaryObjectBuilderImpl(heapObj); }