ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject [16/67] [abbrv] ignite git commit: Merged IGNITE-950-new into IGNITE-1282
Date Wed, 11 Nov 2015 08:25:12 GMT
http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
deleted file mode 100644
index 89bb3ff..0000000
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
+++ /dev/null
@@ -1,570 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.internal.portable.builder;
-
-import org.apache.ignite.internal.portable.GridPortableMarshaller;
-import org.apache.ignite.internal.portable.PortableContext;
-import org.apache.ignite.internal.portable.PortableObjectImpl;
-import org.apache.ignite.internal.portable.PortableObjectOffheapImpl;
-import org.apache.ignite.internal.portable.PortableUtils;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
-import org.apache.ignite.internal.processors.cache.portable.CacheObjectPortableProcessorImpl;
-import org.apache.ignite.internal.util.GridArgumentCheck;
-import org.apache.ignite.internal.util.typedef.F;
-import org.apache.ignite.internal.util.typedef.internal.U;
-import org.apache.ignite.lang.IgniteBiTuple;
-import org.apache.ignite.portable.PortableBuilder;
-import org.apache.ignite.portable.PortableException;
-import org.apache.ignite.portable.PortableInvalidClassException;
-import org.apache.ignite.portable.PortableMetadata;
-import org.apache.ignite.portable.PortableObject;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Set;
-
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.DFLT_HDR_LEN;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.FLAGS_POS;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.HASH_CODE_POS;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.PROTO_VER_POS;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.TYPE_ID_POS;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.UNREGISTERED_TYPE_ID;
-
-/**
- *
- */
-public class PortableBuilderImpl implements PortableBuilder {
-    /** */
-    private static final Object REMOVED_FIELD_MARKER = new Object();
-
-    /** */
-    private final PortableContext ctx;
-
-    /** */
-    private final int typeId;
-
-    /** May be null. */
-    private String typeName;
-
-    /** May be null. */
-    private String clsNameToWrite;
-
-    /** */
-    private boolean registeredType = true;
-
-    /** */
-    private Map<String, Object> assignedVals;
-
-    /** */
-    private Map<Integer, Object> readCache;
-
-    /** Position of object in source array, or -1 if object is not created from PortableObject. */
-    private final int start;
-
-    /** Flags. */
-    private final short flags;
-
-    /** Total header length */
-    private final int hdrLen;
-
-    /** Context of PortableObject reading process. Or {@code null} if object is not created from PortableObject. */
-    private final PortableBuilderReader reader;
-
-    /** */
-    private int hashCode;
-
-    /**
-     * @param clsName Class name.
-     * @param ctx Portable context.
-     */
-    public PortableBuilderImpl(PortableContext ctx, String clsName) {
-        this(ctx, ctx.typeId(clsName), PortableContext.typeName(clsName));
-    }
-
-    /**
-     * @param typeId Type ID.
-     * @param ctx Portable context.
-     */
-    public PortableBuilderImpl(PortableContext ctx, int typeId) {
-        this(ctx, typeId, null);
-    }
-
-    /**
-     * @param typeName Type name.
-     * @param ctx Context.
-     * @param typeId Type id.
-     */
-    public PortableBuilderImpl(PortableContext ctx, int typeId, String typeName) {
-        this.typeId = typeId;
-        this.typeName = typeName;
-        this.ctx = ctx;
-
-        start = -1;
-        flags = -1;
-        reader = null;
-        hdrLen = DFLT_HDR_LEN;
-
-        readCache = Collections.emptyMap();
-    }
-
-    /**
-     * @param obj Object to wrap.
-     */
-    public PortableBuilderImpl(PortableObjectImpl obj) {
-        this(new PortableBuilderReader(obj), obj.start());
-
-        reader.registerObject(this);
-    }
-
-    /**
-     * @param reader ctx
-     * @param start Start.
-     */
-    PortableBuilderImpl(PortableBuilderReader reader, int start) {
-        this.reader = reader;
-        this.start = start;
-        this.flags = reader.readShortPositioned(start + FLAGS_POS);
-
-        byte ver = reader.readBytePositioned(start + PROTO_VER_POS);
-
-        PortableUtils.checkProtocolVersion(ver);
-
-        int typeId = reader.readIntPositioned(start + TYPE_ID_POS);
-        ctx = reader.portableContext();
-        hashCode = reader.readIntPositioned(start + HASH_CODE_POS);
-
-        if (typeId == UNREGISTERED_TYPE_ID) {
-            int mark = reader.position();
-
-            reader.position(start + DFLT_HDR_LEN);
-
-            clsNameToWrite = reader.readString();
-
-            Class cls;
-
-            try {
-                // TODO: IGNITE-1272 - Is class loader needed here?
-                cls = U.forName(clsNameToWrite, null);
-            }
-            catch (ClassNotFoundException e) {
-                throw new PortableInvalidClassException("Failed to load the class: " + clsNameToWrite, e);
-            }
-
-            this.typeId = ctx.descriptorForClass(cls).typeId();
-
-            registeredType = false;
-
-            hdrLen = reader.position() - mark;
-
-            reader.position(mark);
-        }
-        else {
-            this.typeId = typeId;
-            hdrLen = DFLT_HDR_LEN;
-        }
-    }
-
-    /** {@inheritDoc} */
-    @Override public PortableObject build() {
-        try (PortableWriterExImpl writer = new PortableWriterExImpl(ctx, typeId, false)) {
-
-            PortableBuilderSerializer serializationCtx = new PortableBuilderSerializer();
-
-            serializationCtx.registerObjectWriting(this, 0);
-
-            serializeTo(writer, serializationCtx);
-
-            byte[] arr = writer.array();
-
-            return new PortableObjectImpl(ctx, arr, 0);
-        }
-    }
-
-    /**
-     * @param writer Writer.
-     * @param serializer Serializer.
-     */
-    void serializeTo(PortableWriterExImpl writer, PortableBuilderSerializer serializer) {
-        try {
-            PortableUtils.writeHeader(writer,
-                true,
-                registeredType ? typeId : UNREGISTERED_TYPE_ID,
-                hashCode,
-                registeredType ? null : clsNameToWrite);
-
-            Set<Integer> remainsFlds = null;
-
-            if (reader != null) {
-                Map<Integer, Object> assignedFldsById;
-
-                if (assignedVals != null) {
-                    assignedFldsById = U.newHashMap(assignedVals.size());
-
-                    for (Map.Entry<String, Object> entry : assignedVals.entrySet()) {
-                        int fldId = ctx.fieldId(typeId, entry.getKey());
-
-                        assignedFldsById.put(fldId, entry.getValue());
-                    }
-
-                    remainsFlds = assignedFldsById.keySet();
-                } else
-                    assignedFldsById = Collections.emptyMap();
-
-                // Get footer details.
-                int fieldOffsetSize = PortableUtils.fieldOffsetSize(flags);
-
-                IgniteBiTuple<Integer, Integer> footer = PortableUtils.footerAbsolute(reader, start, fieldOffsetSize);
-
-                int footerPos = footer.get1();
-                int footerEnd = footer.get2();
-
-                // Get raw position.
-                int rawPos = PortableUtils.rawOffsetAbsolute(reader, start, fieldOffsetSize);
-
-                // Position reader on data.
-                reader.position(start + hdrLen);
-
-                while (reader.position() + 4 < rawPos) {
-                    int fieldId = reader.readIntPositioned(footerPos);
-                    int fieldLen = fieldPositionAndLength(footerPos, footerEnd, rawPos, fieldOffsetSize).get2();
-
-                    int postPos = reader.position() + fieldLen; // Position where reader will be placed afterwards.
-
-                    footerPos += 4 + fieldOffsetSize;
-
-                    if (assignedFldsById.containsKey(fieldId)) {
-                        Object assignedVal = assignedFldsById.remove(fieldId);
-
-                        if (assignedVal != REMOVED_FIELD_MARKER) {
-                            writer.writeFieldId(fieldId);
-
-                            serializer.writeValue(writer, assignedVal);
-                        }
-                    }
-                    else {
-                        int type = fieldLen != 0 ? reader.readByte(0) : 0;
-
-                        if (fieldLen != 0 && !PortableUtils.isPlainArrayType(type) && PortableUtils.isPlainType(type)) {
-                            writer.writeFieldId(fieldId);
-
-                            writer.write(reader.array(), reader.position(), fieldLen);
-                        }
-                        else {
-                            writer.writeFieldId(fieldId);
-
-                            Object val;
-
-                            if (fieldLen == 0)
-                                val = null;
-                            else if (readCache == null) {
-                                val = reader.parseValue();
-
-                                assert reader.position() == postPos;
-                            }
-                            else
-                                val = readCache.get(fieldId);
-
-                            serializer.writeValue(writer, val);
-                        }
-                    }
-
-                    reader.position(postPos);
-                }
-            }
-
-            if (assignedVals != null && (remainsFlds == null || !remainsFlds.isEmpty())) {
-                boolean metadataEnabled = ctx.isMetaDataEnabled(typeId);
-
-                PortableMetadata metadata = null;
-
-                if (metadataEnabled)
-                    metadata = ctx.metaData(typeId);
-
-                Map<String, String> newFldsMetadata = null;
-
-                for (Map.Entry<String, Object> entry : assignedVals.entrySet()) {
-                    Object val = entry.getValue();
-
-                    if (val == REMOVED_FIELD_MARKER)
-                        continue;
-
-                    String name = entry.getKey();
-
-                    int fldId = ctx.fieldId(typeId, name);
-
-                    if (remainsFlds != null && !remainsFlds.contains(fldId))
-                        continue;
-
-                    writer.writeFieldId(fldId);
-
-                    serializer.writeValue(writer, val);
-
-                    if (metadataEnabled) {
-                        String oldFldTypeName = metadata == null ? null : metadata.fieldTypeName(name);
-
-                        String newFldTypeName;
-
-                        if (val instanceof PortableValueWithType)
-                            newFldTypeName = ((PortableValueWithType) val).typeName();
-                        else {
-                            byte type = PortableUtils.typeByClass(val.getClass());
-
-                            newFldTypeName = CacheObjectPortableProcessorImpl.fieldTypeName(type);
-                        }
-
-                        if (oldFldTypeName == null) {
-                            // It's a new field, we have to add it to metadata.
-
-                            if (newFldsMetadata == null)
-                                newFldsMetadata = new HashMap<>();
-
-                            newFldsMetadata.put(name, newFldTypeName);
-                        }
-                        else {
-                            String objTypeName =
-                                CacheObjectPortableProcessorImpl.FIELD_TYPE_NAMES[GridPortableMarshaller.OBJ];
-
-                            if (!objTypeName.equals(oldFldTypeName) && !oldFldTypeName.equals(newFldTypeName)) {
-                                throw new PortableException(
-                                    "Wrong value has been set [" +
-                                        "typeName=" + (typeName == null ? metadata.typeName() : typeName) +
-                                        ", fieldName=" + name +
-                                        ", fieldType=" + oldFldTypeName +
-                                        ", assignedValueType=" + newFldTypeName + ']'
-                                );
-                            }
-                        }
-                    }
-                }
-
-                if (newFldsMetadata != null) {
-                    String typeName = this.typeName;
-
-                    if (typeName == null)
-                        typeName = metadata.typeName();
-
-                    ctx.updateMetaData(typeId, typeName, newFldsMetadata);
-                }
-            }
-
-            if (reader != null) {
-                // Write raw data if any.
-                int fieldOffsetSize = PortableUtils.fieldOffsetSize(flags);
-
-                int rawOff = PortableUtils.rawOffsetAbsolute(reader, start, fieldOffsetSize);
-                int footerStart = PortableUtils.footerStartAbsolute(reader, start);
-
-                if (rawOff < footerStart) {
-                    writer.rawWriter();
-
-                    writer.write(reader.array(), rawOff, footerStart - rawOff);
-                }
-
-                // Shift reader to the end of the object.
-                reader.position(start + PortableUtils.length(reader, start));
-            }
-
-            writer.postWrite(true);
-        }
-        finally {
-            writer.popSchema();
-        }
-    }
-
-    /** {@inheritDoc} */
-    @Override public PortableBuilderImpl hashCode(int hashCode) {
-        this.hashCode = hashCode;
-
-        return this;
-    }
-
-    /**
-     * Get field position and length.
-     *
-     * @param footerPos Field position inside the footer (absolute).
-     * @param footerEnd Footer end (absolute).
-     * @param rawPos Raw data position (absolute).
-     * @param fieldOffsetSize Size of field's offset.
-     * @return Tuple with field position and length.
-     */
-    private IgniteBiTuple<Integer, Integer> fieldPositionAndLength(int footerPos, int footerEnd, int rawPos,
-        int fieldOffsetSize) {
-        // Get field offset first.
-        int fieldOffset = PortableUtils.fieldOffsetRelative(reader, footerPos + 4, fieldOffsetSize);
-        int fieldPos = start + fieldOffset;
-
-        // Get field length.
-        int fieldLen;
-
-        if (footerPos + 4 + fieldOffsetSize == footerEnd)
-            // This is the last field, compare to raw offset.
-            fieldLen = rawPos - fieldPos;
-        else {
-            // Field is somewhere in the middle, get difference with the next offset.
-            int nextFieldOffset = PortableUtils.fieldOffsetRelative(reader, footerPos + 4 + fieldOffsetSize + 4,
-                fieldOffsetSize);
-
-            fieldLen = nextFieldOffset - fieldOffset;
-        }
-
-        return F.t(fieldPos, fieldLen);
-    }
-
-    /**
-     * Initialize read cache if needed.
-     */
-    private void ensureReadCacheInit() {
-        if (readCache == null) {
-            int fieldOffsetSize = PortableUtils.fieldOffsetSize(flags);
-
-            Map<Integer, Object> readCache = new HashMap<>();
-
-            IgniteBiTuple<Integer, Integer> footer = PortableUtils.footerAbsolute(reader, start, fieldOffsetSize);
-
-            int footerPos = footer.get1();
-            int footerEnd = footer.get2();
-
-            int rawPos = PortableUtils.rawOffsetAbsolute(reader, start, fieldOffsetSize);
-
-            while (footerPos + 4 < footerEnd) {
-                int fieldId = reader.readIntPositioned(footerPos);
-
-                IgniteBiTuple<Integer, Integer> posAndLen =
-                    fieldPositionAndLength(footerPos, footerEnd, rawPos, fieldOffsetSize);
-
-                Object val = reader.getValueQuickly(posAndLen.get1(), posAndLen.get2());
-
-                readCache.put(fieldId, val);
-
-                // Shift current footer position.
-                footerPos += 4 + fieldOffsetSize;
-            }
-
-            this.readCache = readCache;
-        }
-    }
-
-    /** {@inheritDoc} */
-    @SuppressWarnings("unchecked")
-    @Override public <T> T getField(String name) {
-        Object val;
-
-        if (assignedVals != null && assignedVals.containsKey(name)) {
-            val = assignedVals.get(name);
-
-            if (val == REMOVED_FIELD_MARKER)
-                return null;
-        }
-        else {
-            ensureReadCacheInit();
-
-            int fldId = ctx.fieldId(typeId, name);
-
-            val = readCache.get(fldId);
-        }
-
-        return (T)PortableUtils.unwrapLazy(val);
-    }
-
-    /** {@inheritDoc} */
-    @Override public PortableBuilder setField(String name, Object val) {
-        GridArgumentCheck.notNull(val, name);
-
-        if (assignedVals == null)
-            assignedVals = new LinkedHashMap<>();
-
-        Object oldVal = assignedVals.put(name, val);
-
-        if (oldVal instanceof PortableValueWithType) {
-            ((PortableValueWithType)oldVal).value(val);
-
-            assignedVals.put(name, oldVal);
-        }
-
-        return this;
-    }
-
-    /** {@inheritDoc} */
-    @Override public <T> PortableBuilder setField(String name, @Nullable T val, Class<? super T> type) {
-        if (assignedVals == null)
-            assignedVals = new LinkedHashMap<>();
-
-        //int fldId = ctx.fieldId(typeId, fldName);
-
-        assignedVals.put(name, new PortableValueWithType(PortableUtils.typeByClass(type), val));
-
-        return this;
-    }
-
-    /** {@inheritDoc} */
-    @Override public PortableBuilder setField(String name, @Nullable PortableBuilder builder) {
-        if (builder == null)
-            return setField(name, null, Object.class);
-        else
-            return setField(name, (Object)builder);
-    }
-
-    /**
-     * Removes field from portable object.
-     *
-     * @param name Field name.
-     * @return {@code this} instance for chaining.
-     */
-    @Override public PortableBuilderImpl removeField(String name) {
-        if (assignedVals == null)
-            assignedVals = new LinkedHashMap<>();
-
-        assignedVals.put(name, REMOVED_FIELD_MARKER);
-
-        return this;
-    }
-
-    /**
-     * Creates builder initialized by specified portable object.
-     *
-     * @param obj Portable object to initialize builder.
-     * @return New builder.
-     */
-    public static PortableBuilderImpl wrap(PortableObject obj) {
-        PortableObjectImpl heapObj;
-
-        if (obj instanceof PortableObjectOffheapImpl)
-            heapObj = (PortableObjectImpl)((PortableObjectOffheapImpl)obj).heapCopy();
-        else
-            heapObj = (PortableObjectImpl)obj;
-
-        return new PortableBuilderImpl(heapObj);
-    }
-
-    /**
-     * @return Object start position in source array.
-     */
-    int start() {
-        return start;
-    }
-
-    /**
-     * @return Object type id.
-     */
-    public int typeId() {
-        return typeId;
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
index 9645ced..d2a3ac2 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
@@ -23,13 +23,13 @@ import java.util.HashMap;
 import java.util.Map;
 import org.apache.ignite.internal.portable.GridPortableMarshaller;
 import org.apache.ignite.internal.portable.PortableContext;
-import org.apache.ignite.internal.portable.PortableObjectImpl;
 import org.apache.ignite.internal.portable.PortablePositionReadable;
+import org.apache.ignite.internal.portable.BinaryObjectImpl;
 import org.apache.ignite.internal.portable.PortablePrimitives;
-import org.apache.ignite.internal.portable.PortableReaderExImpl;
+import org.apache.ignite.internal.portable.BinaryReaderExImpl;
 import org.apache.ignite.internal.portable.PortableUtils;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
-import org.apache.ignite.portable.PortableException;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
+import org.apache.ignite.binary.BinaryObjectException;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.NULL;
@@ -40,13 +40,13 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.STRING;
  */
 public class PortableBuilderReader implements PortablePositionReadable {
     /** */
-    private final Map<Integer, PortableBuilderImpl> objMap = new HashMap<>();
+    private final Map<Integer, BinaryObjectBuilderImpl> objMap = new HashMap<>();
 
     /** */
     private final PortableContext ctx;
 
     /** */
-    private final PortableReaderExImpl reader;
+    private final BinaryReaderExImpl reader;
 
     /** */
     private byte[] arr;
@@ -57,13 +57,13 @@ public class PortableBuilderReader implements PortablePositionReadable {
     /**
      * @param objImpl Portable object
      */
-    PortableBuilderReader(PortableObjectImpl objImpl) {
+    PortableBuilderReader(BinaryObjectImpl objImpl) {
         ctx = objImpl.context();
         arr = objImpl.array();
         pos = objImpl.start();
 
         // TODO: IGNITE-1272 - Is class loader needed here?
-        reader = new PortableReaderExImpl(portableContext(), arr, pos, null);
+        reader = new BinaryReaderExImpl(portableContext(), arr, pos, null);
     }
 
     /**
@@ -76,7 +76,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
     /**
      * @param obj Mutable portable object.
      */
-    public void registerObject(PortableBuilderImpl obj) {
+    public void registerObject(BinaryObjectBuilderImpl obj) {
         objMap.put(obj.start(), obj);
     }
 
@@ -170,7 +170,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
             return null;
 
         if (flag != STRING)
-            throw new PortableException("Failed to deserialize String.");
+            throw new BinaryObjectException("Failed to deserialize String.");
 
         boolean convert = readBoolean();
         int len = readInt();
@@ -338,7 +338,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
                 break;
 
             default:
-                throw new PortableException("Invalid flag value: " + type);
+                throw new BinaryObjectException("Invalid flag value: " + type);
         }
 
         pos += len;
@@ -359,10 +359,10 @@ public class PortableBuilderReader implements PortablePositionReadable {
             case GridPortableMarshaller.HANDLE: {
                 int objStart = pos - readIntPositioned(pos + 1);
 
-                PortableBuilderImpl res = objMap.get(objStart);
+                BinaryObjectBuilderImpl res = objMap.get(objStart);
 
                 if (res == null) {
-                    res = new PortableBuilderImpl(this, objStart);
+                    res = new BinaryObjectBuilderImpl(this, objStart);
 
                     objMap.put(objStart, res);
                 }
@@ -371,10 +371,10 @@ public class PortableBuilderReader implements PortablePositionReadable {
             }
 
             case GridPortableMarshaller.OBJ: {
-                PortableBuilderImpl res = objMap.get(pos);
+                BinaryObjectBuilderImpl res = objMap.get(pos);
 
                 if (res == null) {
-                    res = new PortableBuilderImpl(this, pos);
+                    res = new BinaryObjectBuilderImpl(this, pos);
 
                     objMap.put(pos, res);
                 }
@@ -455,13 +455,13 @@ public class PortableBuilderReader implements PortablePositionReadable {
 
                 int start = readIntPositioned(pos + 4 + size);
 
-                PortableObjectImpl portableObj = new PortableObjectImpl(ctx, arr, pos + 4 + start);
+                BinaryObjectImpl portableObj = new BinaryObjectImpl(ctx, arr, pos + 4 + start);
 
                 return new PortablePlainPortableObject(portableObj);
             }
 
             default:
-                throw new PortableException("Invalid flag value: " + type);
+                throw new BinaryObjectException("Invalid flag value: " + type);
         }
     }
 
@@ -484,10 +484,10 @@ public class PortableBuilderReader implements PortablePositionReadable {
             case GridPortableMarshaller.HANDLE: {
                 int objStart = pos - 1 - readInt();
 
-                PortableBuilderImpl res = objMap.get(objStart);
+                BinaryObjectBuilderImpl res = objMap.get(objStart);
 
                 if (res == null) {
-                    res = new PortableBuilderImpl(this, objStart);
+                    res = new BinaryObjectBuilderImpl(this, objStart);
 
                     objMap.put(objStart, res);
                 }
@@ -498,10 +498,10 @@ public class PortableBuilderReader implements PortablePositionReadable {
             case GridPortableMarshaller.OBJ: {
                 pos--;
 
-                PortableBuilderImpl res = objMap.get(pos);
+                BinaryObjectBuilderImpl res = objMap.get(pos);
 
                 if (res == null) {
-                    res = new PortableBuilderImpl(this, pos);
+                    res = new BinaryObjectBuilderImpl(this, pos);
 
                     objMap.put(pos, res);
                 }
@@ -633,7 +633,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
                     if (flag == GridPortableMarshaller.NULL) continue;
 
                     if (flag != GridPortableMarshaller.DATE)
-                        throw new PortableException("Invalid flag value: " + flag);
+                        throw new BinaryObjectException("Invalid flag value: " + flag);
 
                     long time = PortablePrimitives.readLong(arr, pos);
 
@@ -657,7 +657,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
                         continue;
 
                     if (flag != GridPortableMarshaller.TIMESTAMP)
-                        throw new PortableException("Invalid flag value: " + flag);
+                        throw new BinaryObjectException("Invalid flag value: " + flag);
 
                     long time = PortablePrimitives.readLong(arr, pos);
 
@@ -719,7 +719,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
                         return new PortableLazySet(this, size);
                 }
 
-                throw new PortableException("Unknown collection type: " + colType);
+                throw new BinaryObjectException("Unknown collection type: " + colType);
             }
 
             case GridPortableMarshaller.MAP:
@@ -741,14 +741,14 @@ public class PortableBuilderReader implements PortablePositionReadable {
 
                 int start = readInt();
 
-                PortableObjectImpl portableObj = new PortableObjectImpl(ctx, arr,
+                BinaryObjectImpl portableObj = new BinaryObjectImpl(ctx, arr,
                     pos - 4 - size + start);
 
                 return new PortablePlainPortableObject(portableObj);
             }
 
             default:
-                throw new PortableException("Invalid flag value: " + type);
+                throw new BinaryObjectException("Invalid flag value: " + type);
         }
 
         PortableAbstractLazyValue res;
@@ -794,7 +794,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
     /**
      * @return Reader.
      */
-    PortableReaderExImpl reader() {
+    BinaryReaderExImpl reader() {
         return reader;
     }
 
@@ -829,7 +829,7 @@ public class PortableBuilderReader implements PortablePositionReadable {
         }
 
         /** {@inheritDoc} */
-        @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+        @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
             ctx.writeValue(writer, wrappedCollection());
         }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializationAware.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializationAware.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializationAware.java
index 976059a..a750f6c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializationAware.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializationAware.java
@@ -27,5 +27,5 @@ interface PortableBuilderSerializationAware {
      * @param writer Writer.
      * @param ctx Context.
      */
-    public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx);
+    public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx);
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializer.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializer.java
index fa08d79..0e8eaa4 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializer.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderSerializer.java
@@ -17,12 +17,12 @@
 
 package org.apache.ignite.internal.portable.builder;
 
+import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.internal.portable.GridPortableMarshaller;
-import org.apache.ignite.internal.portable.PortableObjectEx;
+import org.apache.ignite.internal.portable.BinaryObjectEx;
 import org.apache.ignite.internal.portable.PortableUtils;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
 import org.apache.ignite.internal.util.*;
-import org.apache.ignite.portable.*;
 
 import java.util.*;
 
@@ -31,16 +31,16 @@ import java.util.*;
  */
 class PortableBuilderSerializer {
     /** */
-    private final Map<PortableBuilderImpl, Integer> objToPos = new IdentityHashMap<>();
+    private final Map<BinaryObjectBuilderImpl, Integer> objToPos = new IdentityHashMap<>();
 
     /** */
-    private Map<PortableObject, PortableBuilderImpl> portableObjToWrapper;
+    private Map<BinaryObject, BinaryObjectBuilderImpl> portableObjToWrapper;
 
     /**
      * @param obj Mutable object.
      * @param posInResArr Object position in the array.
      */
-    public void registerObjectWriting(PortableBuilderImpl obj, int posInResArr) {
+    public void registerObjectWriting(BinaryObjectBuilderImpl obj, int posInResArr) {
         objToPos.put(obj, posInResArr);
     }
 
@@ -48,7 +48,7 @@ class PortableBuilderSerializer {
      * @param writer Writer.
      * @param val Value.
      */
-    public void writeValue(PortableWriterExImpl writer, Object val) {
+    public void writeValue(BinaryWriterExImpl writer, Object val) {
         if (val == null) {
             writer.writeByte(GridPortableMarshaller.NULL);
 
@@ -61,23 +61,23 @@ class PortableBuilderSerializer {
             return;
         }
 
-        if (val instanceof PortableObjectEx) {
+        if (val instanceof BinaryObjectEx) {
             if (portableObjToWrapper == null)
                 portableObjToWrapper = new IdentityHashMap<>();
 
-            PortableBuilderImpl wrapper = portableObjToWrapper.get(val);
+            BinaryObjectBuilderImpl wrapper = portableObjToWrapper.get(val);
 
             if (wrapper == null) {
-                wrapper = PortableBuilderImpl.wrap((PortableObject)val);
+                wrapper = BinaryObjectBuilderImpl.wrap((BinaryObject)val);
 
-                portableObjToWrapper.put((PortableObject)val, wrapper);
+                portableObjToWrapper.put((BinaryObject)val, wrapper);
             }
 
             val = wrapper;
         }
 
-        if (val instanceof PortableBuilderImpl) {
-            PortableBuilderImpl obj = (PortableBuilderImpl)val;
+        if (val instanceof BinaryObjectBuilderImpl) {
+            BinaryObjectBuilderImpl obj = (BinaryObjectBuilderImpl)val;
 
             Integer posInResArr = objToPos.get(obj);
 
@@ -186,7 +186,7 @@ class PortableBuilderSerializer {
      * @param arr The array.
      * @param compTypeId Component type ID.
      */
-    public void writeArray(PortableWriterExImpl writer, byte elementType, Object[] arr, int compTypeId) {
+    public void writeArray(BinaryWriterExImpl writer, byte elementType, Object[] arr, int compTypeId) {
         writer.writeByte(elementType);
         writer.writeInt(compTypeId);
         writer.writeInt(arr.length);
@@ -201,7 +201,7 @@ class PortableBuilderSerializer {
      * @param arr The array.
      * @param clsName Component class name.
      */
-    public void writeArray(PortableWriterExImpl writer, byte elementType, Object[] arr, String clsName) {
+    public void writeArray(BinaryWriterExImpl writer, byte elementType, Object[] arr, String clsName) {
         writer.writeByte(elementType);
         writer.writeInt(GridPortableMarshaller.UNREGISTERED_TYPE_ID);
         writer.writeString(clsName);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableEnumArrayLazyValue.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableEnumArrayLazyValue.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableEnumArrayLazyValue.java
index d864a6e..1e2ebc9 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableEnumArrayLazyValue.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableEnumArrayLazyValue.java
@@ -18,10 +18,10 @@
 package org.apache.ignite.internal.portable.builder;
 
 import org.apache.ignite.internal.portable.GridPortableMarshaller;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
 import org.apache.ignite.internal.util.typedef.internal.U;
-import org.apache.ignite.portable.PortableException;
-import org.apache.ignite.portable.PortableInvalidClassException;
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryInvalidTypeException;
 
 /**
  *
@@ -54,7 +54,7 @@ class PortableEnumArrayLazyValue extends PortableAbstractLazyValue {
                 cls = U.forName(reader.readString(), null);
             }
             catch (ClassNotFoundException e) {
-                throw new PortableInvalidClassException("Failed to load the class: " + clsName, e);
+                throw new BinaryInvalidTypeException("Failed to load the class: " + clsName, e);
             }
 
             compTypeId = reader.portableContext().descriptorForClass(cls).typeId();
@@ -90,7 +90,7 @@ class PortableEnumArrayLazyValue extends PortableAbstractLazyValue {
                 continue;
 
             if (flag != GridPortableMarshaller.ENUM)
-                throw new PortableException("Invalid flag value: " + flag);
+                throw new BinaryObjectException("Invalid flag value: " + flag);
 
             res[i] = new PortableBuilderEnum(reader);
         }
@@ -99,7 +99,7 @@ class PortableEnumArrayLazyValue extends PortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (val != null) {
             if (clsName != null)
                 ctx.writeArray(writer, GridPortableMarshaller.ENUM_ARR, (Object[])val, clsName);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyArrayList.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyArrayList.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyArrayList.java
index f29872e..29bbe85 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyArrayList.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyArrayList.java
@@ -131,7 +131,7 @@ class PortableLazyArrayList extends AbstractList<Object> implements PortableBuil
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (delegate == null) {
             int size = reader.readIntPositioned(off + 1);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyLinkedList.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyLinkedList.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyLinkedList.java
index 4940311..3271aaa 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyLinkedList.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyLinkedList.java
@@ -188,7 +188,7 @@ class PortableLazyLinkedList extends AbstractList<Object> implements PortableBui
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (delegate == null) {
             int size = reader.readIntPositioned(off + 1);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMap.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMap.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMap.java
index 74bd4c4..32d4f44 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMap.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMap.java
@@ -85,7 +85,7 @@ class PortableLazyMap extends AbstractMap<Object, Object> implements PortableBui
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (delegate == null) {
             int size = reader.readIntPositioned(off + 1);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMapEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMapEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMapEntry.java
index bd027f5..5ebb223 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMapEntry.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMapEntry.java
@@ -59,7 +59,7 @@ class PortableLazyMapEntry implements Map.Entry<Object, Object>, PortableBuilder
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         writer.writeByte(GridPortableMarshaller.MAP_ENTRY);
 
         ctx.writeValue(writer, key);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazySet.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazySet.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazySet.java
index c1099eb..7e62aa4 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazySet.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazySet.java
@@ -21,7 +21,7 @@ import java.util.Collection;
 import java.util.Set;
 import org.apache.ignite.internal.portable.GridPortableMarshaller;
 import org.apache.ignite.internal.portable.PortableUtils;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
 import org.apache.ignite.internal.util.typedef.internal.U;
 
 /**
@@ -47,7 +47,7 @@ class PortableLazySet extends PortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (val == null) {
             int size = reader.readIntPositioned(off + 1);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableModifiableLazyValue.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableModifiableLazyValue.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableModifiableLazyValue.java
index 09fb844..2b439c8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableModifiableLazyValue.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableModifiableLazyValue.java
@@ -43,7 +43,7 @@ public class PortableModifiableLazyValue extends PortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (val == null)
             writer.write(reader.array(), valOff, len);
         else

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableObjectArrayLazyValue.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableObjectArrayLazyValue.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableObjectArrayLazyValue.java
index 1126a3c..6634eea 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableObjectArrayLazyValue.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableObjectArrayLazyValue.java
@@ -18,9 +18,9 @@
 package org.apache.ignite.internal.portable.builder;
 
 import org.apache.ignite.internal.portable.GridPortableMarshaller;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
 import org.apache.ignite.internal.util.typedef.internal.U;
-import org.apache.ignite.portable.PortableInvalidClassException;
+import org.apache.ignite.binary.BinaryInvalidTypeException;
 
 /**
  *
@@ -53,7 +53,7 @@ class PortableObjectArrayLazyValue extends PortableAbstractLazyValue {
                 cls = U.forName(reader.readString(), null);
             }
             catch (ClassNotFoundException e) {
-                throw new PortableInvalidClassException("Failed to load the class: " + clsName, e);
+                throw new BinaryInvalidTypeException("Failed to load the class: " + clsName, e);
             }
 
             compTypeId = reader.portableContext().descriptorForClass(cls).typeId();
@@ -82,7 +82,7 @@ class PortableObjectArrayLazyValue extends PortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (clsName == null)
             ctx.writeArray(writer, GridPortableMarshaller.OBJ_ARR, lazyValsArr, compTypeId);
         else

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainLazyValue.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainLazyValue.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainLazyValue.java
index 136958a..14c182b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainLazyValue.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainLazyValue.java
@@ -43,7 +43,7 @@ class PortablePlainLazyValue extends PortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         writer.write(reader.array(), valOff, len);
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainPortableObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainPortableObject.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainPortableObject.java
index 8743fbe..1512b44 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainPortableObject.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortablePlainPortableObject.java
@@ -17,22 +17,22 @@
 
 package org.apache.ignite.internal.portable.builder;
 
-import org.apache.ignite.internal.portable.PortableObjectImpl;
-import org.apache.ignite.internal.portable.PortableObjectOffheapImpl;
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
-import org.apache.ignite.portable.PortableObject;
+import org.apache.ignite.internal.portable.BinaryObjectImpl;
+import org.apache.ignite.internal.portable.BinaryObjectOffheapImpl;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
+import org.apache.ignite.binary.BinaryObject;
 
 /**
  *
  */
 public class PortablePlainPortableObject implements PortableLazyValue {
     /** */
-    private final PortableObject portableObj;
+    private final BinaryObject portableObj;
 
     /**
      * @param portableObj Portable object.
      */
-    public PortablePlainPortableObject(PortableObject portableObj) {
+    public PortablePlainPortableObject(BinaryObject portableObj) {
         this.portableObj = portableObj;
     }
 
@@ -42,12 +42,12 @@ public class PortablePlainPortableObject implements PortableLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
-        PortableObject val = portableObj;
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
+        BinaryObject val = portableObj;
 
-        if (val instanceof PortableObjectOffheapImpl)
-            val = ((PortableObjectOffheapImpl)val).heapCopy();
+        if (val instanceof BinaryObjectOffheapImpl)
+            val = ((BinaryObjectOffheapImpl)val).heapCopy();
 
-        writer.doWritePortableObject((PortableObjectImpl)val);
+        writer.doWritePortableObject((BinaryObjectImpl)val);
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableValueWithType.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableValueWithType.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableValueWithType.java
index 2e031f0..5ea8e62 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableValueWithType.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableValueWithType.java
@@ -17,8 +17,8 @@
 
 package org.apache.ignite.internal.portable.builder;
 
-import org.apache.ignite.internal.portable.PortableWriterExImpl;
-import org.apache.ignite.internal.processors.cache.portable.CacheObjectPortableProcessorImpl;
+import org.apache.ignite.internal.portable.BinaryWriterExImpl;
+import org.apache.ignite.internal.processors.cache.portable.CacheObjectBinaryProcessorImpl;
 import org.apache.ignite.internal.util.typedef.internal.S;
 
 /**
@@ -41,7 +41,7 @@ class PortableValueWithType implements PortableLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(PortableWriterExImpl writer, PortableBuilderSerializer ctx) {
+    @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) {
         if (val instanceof PortableBuilderSerializationAware)
             ((PortableBuilderSerializationAware)val).writeTo(writer, ctx);
         else
@@ -50,7 +50,7 @@ class PortableValueWithType implements PortableLazyValue {
 
     /** {@inheritDoc} */
     public String typeName() {
-        return CacheObjectPortableProcessorImpl.fieldTypeName(type);
+        return CacheObjectBinaryProcessorImpl.fieldTypeName(type);
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableAbstractInputStream.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableAbstractInputStream.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableAbstractInputStream.java
index d7f66c9..2c4864e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableAbstractInputStream.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/streams/PortableAbstractInputStream.java
@@ -17,7 +17,7 @@
 
 package org.apache.ignite.internal.portable.streams;
 
-import org.apache.ignite.portable.PortableException;
+import org.apache.ignite.binary.BinaryObjectException;
 
 /**
  * Portable abstract input stream.
@@ -288,7 +288,7 @@ public abstract class PortableAbstractInputStream extends PortableAbstractStream
     /** {@inheritDoc} */
     @Override public void position(int pos) {
         if (remaining() + this.pos < pos)
-            throw new PortableException("Position is out of bounds: " + pos);
+            throw new BinaryObjectException("Position is out of bounds: " + pos);
         else
             this.pos = pos;
     }
@@ -305,7 +305,7 @@ public abstract class PortableAbstractInputStream extends PortableAbstractStream
      */
     protected void ensureEnoughData(int cnt) {
         if (remaining() < cnt)
-            throw new PortableException("Not enough data to read the value [position=" + pos +
+            throw new BinaryObjectException("Not enough data to read the value [position=" + pos +
                 ", requiredBytes=" + cnt + ", remainingBytes=" + remaining() + ']');
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
index 8bc40cd..d728927 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
@@ -33,6 +33,7 @@ import org.apache.ignite.cache.affinity.AffinityFunction;
 import org.apache.ignite.cache.affinity.AffinityKeyMapper;
 import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.internal.GridNodeOrderComparator;
 import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.processors.cache.CacheObject;
@@ -369,7 +370,7 @@ public class GridAffinityAssignmentCache {
      * @return Affinity key.
      */
     private Object affinityKey(Object key) {
-        if (key instanceof CacheObject)
+        if (key instanceof CacheObject && !(key instanceof BinaryObject))
             key = ((CacheObject)key).value(ctx.cacheObjectContext(), false);
 
         return (key instanceof GridCacheInternal ? ctx.defaultAffMapper() : affMapper).affinityKey(key);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
index 8f66dc1..90306b0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
@@ -38,6 +38,7 @@ import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.events.DiscoveryEvent;
 import org.apache.ignite.events.Event;
+import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener;
@@ -497,7 +498,7 @@ public class GridAffinityProcessor extends GridProcessorAdapter {
      * @throws IgniteCheckedException In case of error.
      */
     private <K> ClusterNode primary(AffinityInfo aff, K key) throws IgniteCheckedException {
-        if (key instanceof CacheObject)
+        if (key instanceof CacheObject && !(key instanceof BinaryObject))
             key = ((CacheObject)key).value(aff.cacheObjCtx, false);
 
         int part = aff.affFunc.partition(aff.mapper.affinityKey(key));
@@ -516,7 +517,7 @@ public class GridAffinityProcessor extends GridProcessorAdapter {
      * @return Primary and backup nodes.
      */
     private <K> List<ClusterNode> primaryAndBackups(AffinityInfo aff, K key) {
-        if (key instanceof CacheObject)
+        if (key instanceof CacheObject && !(key instanceof BinaryObject))
             key = ((CacheObject) key).value(aff.cacheObjCtx, false);
 
         int part = aff.affFunc.partition(aff.mapper.affinityKey(key));

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheInvokeEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheInvokeEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheInvokeEntry.java
index f87dadd..2de26fc 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheInvokeEntry.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheInvokeEntry.java
@@ -49,9 +49,10 @@ public class CacheInvokeEntry<K, V> extends CacheLazyEntry<K, V> implements Muta
     public CacheInvokeEntry(GridCacheContext cctx,
         KeyCacheObject keyObj,
         @Nullable CacheObject valObj,
-        GridCacheVersion ver
+        GridCacheVersion ver,
+        boolean keepPortable
     ) {
-        super(cctx, keyObj, valObj);
+        super(cctx, keyObj, valObj, keepPortable);
 
         this.hadVal = valObj != null;
         this.ver = ver;
@@ -70,8 +71,10 @@ public class CacheInvokeEntry<K, V> extends CacheLazyEntry<K, V> implements Muta
         @Nullable K key,
         @Nullable CacheObject valObj,
         @Nullable V val,
-        GridCacheVersion ver) {
-        super(ctx, keyObj, key, valObj, val);
+        GridCacheVersion ver,
+        boolean keepPortable
+    ) {
+        super(ctx, keyObj, key, valObj, val, keepPortable);
 
         this.hadVal = valObj != null || val != null;
         this.ver = ver;

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLazyEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLazyEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLazyEntry.java
index 1301f1a..47ad30c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLazyEntry.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLazyEntry.java
@@ -20,7 +20,6 @@ package org.apache.ignite.internal.processors.cache;
 import javax.cache.Cache;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.internal.util.tostring.GridToStringInclude;
-import org.apache.ignite.internal.util.typedef.internal.CU;
 import org.apache.ignite.internal.util.typedef.internal.S;
 
 /**
@@ -44,15 +43,19 @@ public class CacheLazyEntry<K, V> implements Cache.Entry<K, V> {
     @GridToStringInclude
     protected V val;
 
+    /** Keep portable flag. */
+    private boolean keepPortable;
+
     /**
      * @param cctx Cache context.
      * @param keyObj Key cache object.
      * @param valObj Cache object value.
      */
-    public CacheLazyEntry(GridCacheContext cctx, KeyCacheObject keyObj, CacheObject valObj) {
+    public CacheLazyEntry(GridCacheContext cctx, KeyCacheObject keyObj, CacheObject valObj, boolean keepPortable) {
+        this.cctx = cctx;
         this.keyObj = keyObj;
         this.valObj = valObj;
-        this.cctx = cctx;
+        this.keepPortable = keepPortable;
     }
 
     /**
@@ -60,10 +63,11 @@ public class CacheLazyEntry<K, V> implements Cache.Entry<K, V> {
      * @param val Value.
      * @param cctx Cache context.
      */
-    public CacheLazyEntry(GridCacheContext cctx, KeyCacheObject keyObj, V val) {
+    public CacheLazyEntry(GridCacheContext cctx, KeyCacheObject keyObj, V val, boolean keepPortable) {
+        this.cctx = cctx;
         this.keyObj = keyObj;
         this.val = val;
-        this.cctx = cctx;
+        this.keepPortable = keepPortable;
     }
 
     /**
@@ -77,18 +81,21 @@ public class CacheLazyEntry<K, V> implements Cache.Entry<K, V> {
         KeyCacheObject keyObj,
         K key,
         CacheObject valObj,
-        V val) {
+        V val,
+        boolean keepPortable
+    ) {
         this.cctx = ctx;
         this.keyObj = keyObj;
         this.key = key;
         this.valObj = valObj;
         this.val = val;
+        this.keepPortable = keepPortable;
     }
 
     /** {@inheritDoc} */
     @Override public K getKey() {
         if (key == null)
-            key = CU.value(keyObj, cctx, true);
+            key = (K)cctx.unwrapPortableIfNeeded(keyObj, keepPortable);
 
         return key;
     }
@@ -96,7 +103,7 @@ public class CacheLazyEntry<K, V> implements Cache.Entry<K, V> {
     /** {@inheritDoc} */
     @Override public V getValue() {
         if (val == null)
-            val = CU.value(valObj, cctx, true);
+            val = (V)cctx.unwrapPortableIfNeeded(valObj, keepPortable);
 
         return val;
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObject.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObject.java
index d870a6c..81129bd 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObject.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObject.java
@@ -48,7 +48,16 @@ public interface CacheObject extends Message {
     /**
      * @return Object type.
      */
-    public byte type();
+    public byte cacheObjectType();
+
+    /**
+     * Gets flag indicating whether object value is a platform type. Platform types will be automatically
+     * deserialized on public API cache operations regardless whether
+     * {@link org.apache.ignite.IgniteCache#withKeepBinary()} is used or not.
+     *
+     * @return Platform type flag.
+     */
+    public boolean isPlatformType();
 
     /**
      * Prepares cache object for cache (e.g. copies user-provided object if needed).

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java
index a3f37f6..df20646 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectAdapter.java
@@ -49,7 +49,7 @@ public abstract class CacheObjectAdapter implements CacheObject, Externalizable
     }
 
     /** {@inheritDoc} */
-    @Override public byte type() {
+    @Override public byte cacheObjectType() {
         return TYPE_REGULAR;
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectByteArrayImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectByteArrayImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectByteArrayImpl.java
index 32f5f88..e961d84 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectByteArrayImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectByteArrayImpl.java
@@ -75,11 +75,16 @@ public class CacheObjectByteArrayImpl implements CacheObject, Externalizable {
     }
 
     /** {@inheritDoc} */
-    @Override public byte type() {
+    @Override public byte cacheObjectType() {
         return TYPE_BYTE_ARR;
     }
 
     /** {@inheritDoc} */
+    @Override public boolean isPlatformType() {
+        return true;
+    }
+
+    /** {@inheritDoc} */
     @Override public CacheObject prepareForCache(CacheObjectContext ctx) {
         return this;
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java
index 44b91a5..d49a029 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectContext.java
@@ -17,15 +17,20 @@
 
 package org.apache.ignite.internal.processors.cache;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
 import org.apache.ignite.cache.affinity.AffinityKeyMapper;
 import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.portable.PortableUtils;
 import org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor;
+import org.apache.ignite.internal.util.typedef.F;
 
 /**
  *
  */
-public class CacheObjectContext {
+@SuppressWarnings("TypeMayBeWeakened") public class CacheObjectContext {
     /** */
     private GridKernalContext kernalCtx;
 
@@ -60,12 +65,12 @@ public class CacheObjectContext {
         boolean storeVal,
         boolean addDepInfo) {
         this.kernalCtx = kernalCtx;
-        this.p2pEnabled = kernalCtx.config().isPeerClassLoadingEnabled();
         this.dfltAffMapper = dfltAffMapper;
         this.cpyOnGet = cpyOnGet;
         this.storeVal = storeVal;
         this.addDepInfo = addDepInfo;
 
+        p2pEnabled = kernalCtx.config().isPeerClassLoadingEnabled();
         proc = kernalCtx.cacheObjects();
     }
 
@@ -118,25 +123,124 @@ public class CacheObjectContext {
         return proc;
     }
 
+    /** {@inheritDoc} */
+    public Object unwrapPortableIfNeeded(Object o, boolean keepPortable) {
+        return unwrapPortableIfNeeded(o, keepPortable, true);
+    }
+
+    /** {@inheritDoc} */
+    public Object unwrapPortableIfNeeded(Object o, boolean keepPortable, boolean cpy) {
+        if (o == null)
+            return null;
+
+        return unwrapPortable(o, keepPortable, cpy);
+    }
+
+    /** {@inheritDoc} */
+    public Collection<Object> unwrapPortablesIfNeeded(Collection<Object> col, boolean keepPortable) {
+        return unwrapPortablesIfNeeded(col, keepPortable, true);
+    }
+
+    /** {@inheritDoc} */
+    public Collection<Object> unwrapPortablesIfNeeded(Collection<Object> col, boolean keepPortable, boolean cpy) {
+        if (col instanceof ArrayList)
+            return unwrapPortables((ArrayList<Object>)col, keepPortable, cpy);
+
+        if (col instanceof Set)
+            return unwrapPortables((Set<Object>)col, keepPortable, cpy);
+
+        Collection<Object> col0 = new ArrayList<>(col.size());
+
+        for (Object obj : col)
+            col0.add(unwrapPortable(obj, keepPortable, cpy));
+
+        return col0;
+    }
+
     /**
-     * Unwraps object.
+     * Unwraps map.
      *
-     * @param o Object to unwrap.
+     * @param map Map to unwrap.
      * @param keepPortable Keep portable flag.
-     * @return Unwrapped object.
+     * @return Unwrapped collection.
      */
-    public Object unwrapPortableIfNeeded(Object o, boolean keepPortable) {
-        return o;
+    private Map<Object, Object> unwrapPortablesIfNeeded(Map<Object, Object> map, boolean keepPortable, boolean cpy) {
+        if (keepPortable)
+            return map;
+
+        Map<Object, Object> map0 = PortableUtils.newMap(map);
+
+        for (Map.Entry<Object, Object> e : map.entrySet())
+            map0.put(unwrapPortable(e.getKey(), keepPortable, cpy), unwrapPortable(e.getValue(), keepPortable, cpy));
+
+        return map0;
     }
 
     /**
-     * Unwraps collection.
+     * Unwraps array list.
      *
-     * @param col Collection to unwrap.
-     * @param keepPortable Keep portable flag.
-     * @return Unwrapped collection.
+     * @param col List to unwrap.
+     * @return Unwrapped list.
      */
-    public Collection<Object> unwrapPortablesIfNeeded(Collection<Object> col, boolean keepPortable) {
+    private Collection<Object> unwrapPortables(ArrayList<Object> col, boolean keepPortable, boolean cpy) {
+        int size = col.size();
+
+        for (int i = 0; i < size; i++) {
+            Object o = col.get(i);
+
+            Object unwrapped = unwrapPortable(o, keepPortable, cpy);
+
+            if (o != unwrapped)
+                col.set(i, unwrapped);
+        }
+
         return col;
     }
+
+    /**
+     * Unwraps set with binary.
+     *
+     * @param set Set to unwrap.
+     * @return Unwrapped set.
+     */
+    private Set<Object> unwrapPortables(Set<Object> set, boolean keepPortable, boolean cpy) {
+        Set<Object> set0 = PortableUtils.newSet(set);
+
+        for (Object obj : set)
+            set0.add(unwrapPortable(obj, keepPortable, cpy));
+
+        return set0;
+    }
+
+    /**
+     * @param o Object to unwrap.
+     * @return Unwrapped object.
+     */
+    private Object unwrapPortable(Object o, boolean keepPortable, boolean cpy) {
+        if (o instanceof Map.Entry) {
+            Map.Entry entry = (Map.Entry)o;
+
+            Object key = entry.getKey();
+
+            Object uKey = unwrapPortable(key, keepPortable, cpy);
+
+            Object val = entry.getValue();
+
+            Object uVal = unwrapPortable(val, keepPortable, cpy);
+
+            return (key != uKey || val != uVal) ? F.t(uKey, uVal) : o;
+        }
+        else if (o instanceof Collection)
+            return unwrapPortablesIfNeeded((Collection<Object>)o, keepPortable, cpy);
+        else if (o instanceof Map)
+            return unwrapPortablesIfNeeded((Map<Object, Object>)o, keepPortable, cpy);
+        else if (o instanceof CacheObject) {
+            CacheObject co = (CacheObject)o;
+
+            if (!keepPortable || co.isPlatformType())
+                return unwrapPortable(co.value(this, true), keepPortable, cpy);
+        }
+
+        return o;
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectImpl.java
index 83a80b3..694027f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheObjectImpl.java
@@ -47,6 +47,11 @@ public class CacheObjectImpl extends CacheObjectAdapter {
     }
 
     /** {@inheritDoc} */
+    @Override public boolean isPlatformType() {
+        return true;
+    }
+
+    /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <T> T value(CacheObjectContext ctx, boolean cpy) {
         cpy = cpy && needCopy(ctx);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheOperationContext.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheOperationContext.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheOperationContext.java
index d551258..3993146 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheOperationContext.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheOperationContext.java
@@ -43,7 +43,7 @@ public class CacheOperationContext implements Serializable {
     private final UUID subjId;
 
     /** Keep portable flag. */
-    private final boolean keepPortable;
+    private final boolean keepBinary;
 
     /** Expiry policy. */
     private final ExpiryPolicy expiryPlc;
@@ -56,7 +56,7 @@ public class CacheOperationContext implements Serializable {
 
         subjId = null;
 
-        keepPortable = false;
+        keepBinary = false;
 
         expiryPlc = null;
 
@@ -66,20 +66,20 @@ public class CacheOperationContext implements Serializable {
     /**
      * @param skipStore Skip store flag.
      * @param subjId Subject ID.
-     * @param keepPortable Keep portable flag.
+     * @param keepBinary Keep portable flag.
      * @param expiryPlc Expiry policy.
      */
     public CacheOperationContext(
         boolean skipStore,
         @Nullable UUID subjId,
-        boolean keepPortable,
+        boolean keepBinary,
         @Nullable ExpiryPolicy expiryPlc,
         boolean noRetries) {
         this.skipStore = skipStore;
 
         this.subjId = subjId;
 
-        this.keepPortable = keepPortable;
+        this.keepBinary = keepBinary;
 
         this.expiryPlc = expiryPlc;
 
@@ -89,8 +89,8 @@ public class CacheOperationContext implements Serializable {
     /**
      * @return Keep portable flag.
      */
-    public boolean isKeepPortable() {
-        return keepPortable;
+    public boolean isKeepBinary() {
+        return keepBinary;
     }
 
     /**
@@ -98,7 +98,7 @@ public class CacheOperationContext implements Serializable {
      *
      * @return New instance of CacheOperationContext with keep portable flag.
      */
-    public CacheOperationContext keepPortable() {
+    public CacheOperationContext keepBinary() {
         return new CacheOperationContext(
             skipStore,
             subjId,
@@ -126,7 +126,7 @@ public class CacheOperationContext implements Serializable {
         return new CacheOperationContext(
             skipStore,
             subjId,
-            keepPortable,
+            keepBinary,
             expiryPlc,
             noRetries);
     }
@@ -148,7 +148,7 @@ public class CacheOperationContext implements Serializable {
         return new CacheOperationContext(
             skipStore,
             subjId,
-            keepPortable,
+            keepBinary,
             expiryPlc,
             noRetries);
     }
@@ -183,7 +183,7 @@ public class CacheOperationContext implements Serializable {
         return new CacheOperationContext(
             skipStore,
             subjId,
-            keepPortable,
+            keepBinary,
             expiryPlc,
             noRetries
         );

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
index 74951b5..0d0d3ff 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
@@ -809,9 +809,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
             else
                 cacheVal = localCachePeek0(cacheKey, modes.heap, modes.offheap, modes.swap, plc);
 
-            Object val = CU.value(cacheVal, ctx, true);
-
-            val = ctx.unwrapPortableIfNeeded(val, ctx.keepPortable());
+            Object val = ctx.unwrapPortableIfNeeded(cacheVal, ctx.keepPortable());
 
             return (V)val;
         }
@@ -945,13 +943,13 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
         if (doomed != null && ctx.events().isRecordable(EVT_CACHE_ENTRY_DESTROYED))
             // Event notification.
             ctx.events().addEvent(doomed.partition(), doomed.key(), locNodeId, (IgniteUuid)null, null,
-                EVT_CACHE_ENTRY_DESTROYED, null, false, null, false, null, null, null);
+                EVT_CACHE_ENTRY_DESTROYED, null, false, null, false, null, null, null, true);
 
         if (created != null) {
             // Event notification.
             if (ctx.events().isRecordable(EVT_CACHE_ENTRY_CREATED))
                 ctx.events().addEvent(created.partition(), created.key(), locNodeId, (IgniteUuid)null, null,
-                    EVT_CACHE_ENTRY_CREATED, null, false, null, false, null, null, null);
+                    EVT_CACHE_ENTRY_CREATED, null, false, null, false, null, null, null, true);
 
             if (touch)
                 ctx.evicts().touch(cur, topVer);
@@ -1046,7 +1044,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
             if (ctx.events().isRecordable(EVT_CACHE_ENTRY_DESTROYED))
                 // Event notification.
                 ctx.events().addEvent(entry.partition(), entry.key(), locNodeId, (IgniteUuid)null, null,
-                    EVT_CACHE_ENTRY_DESTROYED, null, false, null, false, null, null, null);
+                    EVT_CACHE_ENTRY_DESTROYED, null, false, null, false, null, null, null, false);
         }
         else if (log.isDebugEnabled())
             log.debug("Remove will not be done for key (obsolete entry got replaced or removed): " + key);
@@ -1622,7 +1620,8 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
                                 subjId,
                                 null,
                                 taskName,
-                                expiry);
+                                expiry,
+                                !deserializePortable);
 
                             if (res == null) {
                                 if (storeEnabled) {
@@ -1824,7 +1823,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
                 @Override public IgniteInternalFuture<Map<K1, V1>> op(IgniteTxLocalAdapter tx) {
                     return tx.getAllAsync(ctx, keys, deserializePortable, skipVals, false, !readThrough);
                 }
-            });
+            }, ctx.operationContextPerCall());
         }
     }
 
@@ -3988,11 +3987,11 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
 
         IgniteTxLocalAdapter tx = ctx.tm().threadLocalTx(ctx);
 
+        CacheOperationContext opCtx = ctx.operationContextPerCall();
+
         if (tx == null || tx.implicit()) {
             boolean skipStore = ctx.skipStore(); // Save value of thread-local flag.
 
-            CacheOperationContext opCtx = ctx.operationContextPerCall();
-
             int retries = opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES;
 
             if (retries == 1) {
@@ -4006,10 +4005,10 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
                     !skipStore,
                     0);
 
-                return asyncOp(tx, op);
+                return asyncOp(tx, op, opCtx);
             }
             else {
-                AsyncOpRetryFuture<T> fut = new AsyncOpRetryFuture<>(op, skipStore, retries);
+                AsyncOpRetryFuture<T> fut = new AsyncOpRetryFuture<>(op, retries, opCtx);
 
                 fut.execute();
 
@@ -4017,7 +4016,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
             }
         }
         else
-            return asyncOp(tx, op);
+            return asyncOp(tx, op, opCtx);
     }
 
     /**
@@ -4027,7 +4026,11 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
      * @return Future.
      */
     @SuppressWarnings("unchecked")
-    protected <T> IgniteInternalFuture<T> asyncOp(IgniteTxLocalAdapter tx, final AsyncOp<T> op) {
+    protected <T> IgniteInternalFuture<T> asyncOp(
+        IgniteTxLocalAdapter tx,
+        final AsyncOp<T> op,
+        final CacheOperationContext opCtx
+    ) {
         IgniteInternalFuture<T> fail = asyncOpAcquire();
 
         if (fail != null)
@@ -4050,24 +4053,31 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
                                 return new GridFinishedFuture<>(
                                     new IgniteCheckedException("Operation has been cancelled (node is stopping)."));
 
-                            return op.op(tx0).chain(new CX1<IgniteInternalFuture<T>, T>() {
-                                @Override public T applyx(IgniteInternalFuture<T> tFut) throws IgniteCheckedException {
-                                    try {
-                                        return tFut.get();
-                                    }
-                                    catch (IgniteTxRollbackCheckedException e) {
-                                        throw e;
-                                    }
-                                    catch (IgniteCheckedException e1) {
-                                        tx0.rollbackAsync();
+                            ctx.operationContextPerCall(opCtx);
 
-                                        throw e1;
-                                    }
-                                    finally {
-                                        ctx.shared().txContextReset();
+                            try {
+                                return op.op(tx0).chain(new CX1<IgniteInternalFuture<T>, T>() {
+                                    @Override public T applyx(IgniteInternalFuture<T> tFut) throws IgniteCheckedException {
+                                        try {
+                                            return tFut.get();
+                                        }
+                                        catch (IgniteTxRollbackCheckedException e) {
+                                            throw e;
+                                        }
+                                        catch (IgniteCheckedException e1) {
+                                            tx0.rollbackAsync();
+
+                                            throw e1;
+                                        }
+                                        finally {
+                                            ctx.shared().txContextReset();
+                                        }
                                     }
-                                }
-                            });
+                                });
+                            }
+                            finally {
+                                ctx.operationContextPerCall(null);
+                            }
                         }
                     });
 
@@ -4605,7 +4615,8 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
             null,
             null,
             null,
-            null);
+            null,
+            !deserializePortable);
 
         if (val == null)
             return null;
@@ -4631,28 +4642,31 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
         private AsyncOp<T> op;
 
         /** */
-        private boolean skipStore;
-
-        /** */
         private int retries;
 
         /** */
         private IgniteTxLocalAdapter tx;
 
+        /** */
+        private CacheOperationContext opCtx;
+
         /**
          * @param op Operation.
-         * @param skipStore Skip store flag.
          * @param retries Number of retries.
+         * @param opCtx Operation context per call to save.
          */
-        public AsyncOpRetryFuture(AsyncOp<T> op,
-            boolean skipStore,
-            int retries) {
+        public AsyncOpRetryFuture(
+            AsyncOp<T> op,
+            int retries,
+            CacheOperationContext opCtx
+        ) {
             assert retries > 1 : retries;
 
+            tx = null;
+
             this.op = op;
-            this.tx = null;
-            this.skipStore = skipStore;
             this.retries = retries;
+            this.opCtx = opCtx;
         }
 
         /**
@@ -4666,10 +4680,10 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V
                 OPTIMISTIC,
                 READ_COMMITTED,
                 ctx.kernalContext().config().getTransactionConfiguration().getDefaultTxTimeout(),
-                !skipStore,
+                opCtx == null || !opCtx.skipStore(),
                 0);
 
-            IgniteInternalFuture<T> fut = asyncOp(tx, op);
+            IgniteInternalFuture<T> fut = asyncOp(tx, op, opCtx);
 
             fut.listen(new IgniteInClosure<IgniteInternalFuture<T>>() {
                 @Override public void apply(IgniteInternalFuture<T> fut) {

http://git-wip-us.apache.org/repos/asf/ignite/blob/b783d2b7/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java
index 1be7c07..fac704b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMap.java
@@ -1878,7 +1878,7 @@ public class GridCacheConcurrentMap {
          * @return Key iterator.
          */
         Iterator<K> keyIterator() {
-            return new KeyIterator<>(map, filter);
+            return new KeyIterator<>(map, opCtxPerCall != null && opCtxPerCall.isKeepBinary(), filter);
         }
 
         /**
@@ -2156,6 +2156,9 @@ public class GridCacheConcurrentMap {
         /** Hash table iterator. */
         private Iterator0<K, V> it;
 
+        /** Keep binary flag. */
+        private boolean keepBinary;
+
         /**
          * Empty constructor required for {@link Externalizable}.
          */
@@ -2167,8 +2170,9 @@ public class GridCacheConcurrentMap {
          * @param map Cache map.
          * @param filter Filter.
          */
-        private KeyIterator(GridCacheConcurrentMap map, CacheEntryPredicate[] filter) {
+        private KeyIterator(GridCacheConcurrentMap map, boolean keepBinary, CacheEntryPredicate[] filter) {
             it = new Iterator0<>(map, false, filter, -1, -1);
+            this.keepBinary = keepBinary;
         }
 
         /** {@inheritDoc} */
@@ -2178,7 +2182,7 @@ public class GridCacheConcurrentMap {
 
         /** {@inheritDoc} */
         @Override public K next() {
-            return it.next().key().value(it.ctx.cacheObjectContext(), true);
+            return (K)it.ctx.cacheObjectContext().unwrapPortableIfNeeded(it.next().key(), keepBinary, true);
         }
 
         /** {@inheritDoc} */


Mime
View raw message