diff --git a/modules/core/src/main/java/org/apache/ignite/binary/BinaryCollectionFactory.java b/modules/core/src/main/java/org/apache/ignite/binary/BinaryCollectionFactory.java new file mode 100644 index 0000000000000..0e5bf83d9e49f --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/binary/BinaryCollectionFactory.java @@ -0,0 +1,33 @@ +/* + * 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.binary; + +import java.util.Collection; + +/** + * Collection factory. + */ +public interface BinaryCollectionFactory { + /** + * Create collection. + * + * @param size Amount of elements in collection. + * @return Collection. + */ + public Collection create(int size); +} diff --git a/modules/core/src/main/java/org/apache/ignite/binary/BinaryMapFactory.java b/modules/core/src/main/java/org/apache/ignite/binary/BinaryMapFactory.java new file mode 100644 index 0000000000000..d514bf970aa7e --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/binary/BinaryMapFactory.java @@ -0,0 +1,33 @@ +/* + * 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.binary; + +import java.util.Map; + +/** + * Map factory. + */ +public interface BinaryMapFactory { + /** + * Create collection. + * + * @param size Amount of elements in collection. + * @return Collection. + */ + public Map create(int size); +} diff --git a/modules/core/src/main/java/org/apache/ignite/binary/BinaryRawReader.java b/modules/core/src/main/java/org/apache/ignite/binary/BinaryRawReader.java index 7ff515a7783c9..ce059d18b2d39 100644 --- a/modules/core/src/main/java/org/apache/ignite/binary/BinaryRawReader.java +++ b/modules/core/src/main/java/org/apache/ignite/binary/BinaryRawReader.java @@ -206,11 +206,11 @@ public interface BinaryRawReader { @Nullable public Collection readCollection() throws BinaryObjectException; /** - * @param colCls Collection class. + * @param factory Collection factory. * @return Collection. * @throws BinaryObjectException In case of error. */ - @Nullable public Collection readCollection(Class> colCls) + @Nullable public Collection readCollection(BinaryCollectionFactory factory) throws BinaryObjectException; /** @@ -220,11 +220,11 @@ public interface BinaryRawReader { @Nullable public Map readMap() throws BinaryObjectException; /** - * @param mapCls Map class. + * @param factory Map factory. * @return Map. * @throws BinaryObjectException In case of error. */ - @Nullable public Map readMap(Class> mapCls) throws BinaryObjectException; + @Nullable public Map readMap(BinaryMapFactory factory) throws BinaryObjectException; /** * @return Value. diff --git a/modules/core/src/main/java/org/apache/ignite/binary/BinaryReader.java b/modules/core/src/main/java/org/apache/ignite/binary/BinaryReader.java index b8183a28aab30..be7a156117e8a 100644 --- a/modules/core/src/main/java/org/apache/ignite/binary/BinaryReader.java +++ b/modules/core/src/main/java/org/apache/ignite/binary/BinaryReader.java @@ -242,11 +242,11 @@ public interface BinaryReader { /** * @param fieldName Field name. - * @param colCls Collection class. + * @param factory Collection factory. * @return Collection. * @throws BinaryObjectException In case of error. */ - public Collection readCollection(String fieldName, Class> colCls) + public Collection readCollection(String fieldName, BinaryCollectionFactory factory) throws BinaryObjectException; /** @@ -258,12 +258,11 @@ public Collection readCollection(String fieldName, Class Map readMap(String fieldName, Class> mapCls) - throws BinaryObjectException; + public Map readMap(String fieldName, BinaryMapFactory factory) throws BinaryObjectException; /** * @param fieldName Field name. diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryFieldAccessor.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryFieldAccessor.java index 2f6b73c62225f..7701fb5e0f991 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryFieldAccessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryFieldAccessor.java @@ -17,6 +17,11 @@ package org.apache.ignite.internal.portable; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.internal.util.GridUnsafe; +import org.apache.ignite.internal.util.typedef.internal.U; +import sun.misc.Unsafe; + import java.lang.reflect.Field; import java.math.BigDecimal; import java.sql.Timestamp; @@ -24,10 +29,6 @@ import java.util.Date; import java.util.Map; import java.util.UUID; -import org.apache.ignite.binary.BinaryObjectException; -import org.apache.ignite.internal.util.GridUnsafe; -import org.apache.ignite.internal.util.typedef.internal.U; -import sun.misc.Unsafe; /** * Field accessor to speedup access. @@ -607,11 +608,6 @@ private static class DefaultFinalClassAccessor extends BinaryFieldAccessor { break; - case MAP_ENTRY: - writer.writeMapEntryField((Map.Entry)val); - - break; - case PORTABLE_OBJ: writer.writePortableObjectField((BinaryObjectImpl)val); @@ -813,11 +809,6 @@ protected Object readFixedType(BinaryReaderExImpl reader) throws BinaryObjectExc break; - case MAP_ENTRY: - val = reader.readMapEntry(id); - - break; - case PORTABLE_OBJ: val = reader.readPortableObject(id); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java index a0aa2e5420303..6a8091fce1e5d 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java @@ -26,8 +26,11 @@ import java.util.Date; import java.util.Map; import java.util.UUID; + +import org.apache.ignite.binary.BinaryCollectionFactory; import org.apache.ignite.binary.BinaryIdMapper; import org.apache.ignite.binary.BinaryInvalidTypeException; +import org.apache.ignite.binary.BinaryMapFactory; import org.apache.ignite.binary.BinaryObject; import org.apache.ignite.binary.BinaryObjectException; import org.apache.ignite.binary.BinaryRawReader; @@ -62,7 +65,6 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.LONG; import static org.apache.ignite.internal.portable.GridPortableMarshaller.LONG_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.MAP; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.MAP_ENTRY; import static org.apache.ignite.internal.portable.GridPortableMarshaller.NULL; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ_ARR; @@ -308,24 +310,6 @@ public Object unmarshal(int offset) throws BinaryObjectException { return findFieldById(fieldId) ? PortableUtils.unmarshal(in, ctx, ldr, this) : null; } - /** - * @param fieldId Field ID. - * @return Value. - * @throws BinaryObjectException On case of error. - */ - @Nullable Map.Entry readMapEntry(int fieldId) throws BinaryObjectException { - if (findFieldById(fieldId)) { - Flag flag = checkFlag(MAP_ENTRY); - - if (flag == Flag.NORMAL) - return PortableUtils.doReadMapEntry(in, ctx, ldr, this, true); - else if (flag == Flag.HANDLE) - return readHandleField(); - } - - return null; - } - /** * @param fieldId Field ID. * @return Portable object. @@ -1236,20 +1220,20 @@ private Object[] readEnumArray0(@Nullable Class cls) throws BinaryObjectExcep } /** {@inheritDoc} */ - @Nullable @Override public Collection readCollection(String fieldName, - Class> colCls) throws BinaryObjectException { - return findFieldByName(fieldName) ? readCollection0(colCls) : null; + @Nullable @Override public Collection readCollection(String fieldName, BinaryCollectionFactory factory) + throws BinaryObjectException { + return findFieldByName(fieldName) ? readCollection0(factory) : null; } /** * @param fieldId Field ID. - * @param colCls Collection class. + * @param factory Collection factory. * @return Value. * @throws BinaryObjectException In case of error. */ - @Nullable Collection readCollection(int fieldId, @Nullable Class colCls) + @Nullable Collection readCollection(int fieldId, @Nullable BinaryCollectionFactory factory) throws BinaryObjectException { - return findFieldById(fieldId) ? (Collection)readCollection0(colCls) : null; + return findFieldById(fieldId) ? (Collection)readCollection0(factory) : null; } /** {@inheritDoc} */ @@ -1258,26 +1242,41 @@ private Object[] readEnumArray0(@Nullable Class cls) throws BinaryObjectExcep } /** {@inheritDoc} */ - @Nullable @Override public Collection readCollection(Class> colCls) + @Nullable @Override public Collection readCollection(BinaryCollectionFactory factory) throws BinaryObjectException { - return readCollection0(colCls); + return readCollection0(factory); } /** * Internal read collection routine. * - * @param cls Collection class. + * @param factory Collection factory. * @return Value. * @throws BinaryObjectException If failed. */ - private Collection readCollection0(@Nullable Class cls) + private Collection readCollection0(@Nullable BinaryCollectionFactory factory) throws BinaryObjectException { switch (checkFlag(COL)) { case NORMAL: - return (Collection)PortableUtils.doReadCollection(in, ctx, ldr, this, true, cls); + return (Collection)PortableUtils.doReadCollection(in, ctx, ldr, this, true, factory); - case HANDLE: - return readHandleField(); + case HANDLE: { + int handlePos = PortableUtils.positionForHandle(in) - in.readInt(); + + Object obj = getHandle(handlePos); + + if (obj == null) { + int retPos = in.position(); + + streamPosition(handlePos); + + obj = readCollection0(factory); + + streamPosition(retPos); + } + + return (Collection)obj; + } default: return null; @@ -1290,19 +1289,19 @@ private Collection readCollection0(@Nullable Class cls) } /** {@inheritDoc} */ - @Nullable @Override public Map readMap(String fieldName, Class> mapCls) + @Nullable @Override public Map readMap(String fieldName, BinaryMapFactory factory) throws BinaryObjectException { - return findFieldByName(fieldName) ? readMap0(mapCls) : null; + return findFieldByName(fieldName) ? readMap0(factory) : null; } /** * @param fieldId Field ID. - * @param mapCls Map class. + * @param factory Factory. * @return Value. * @throws BinaryObjectException In case of error. */ - @Nullable Map readMap(int fieldId, @Nullable Class mapCls) throws BinaryObjectException { - return findFieldById(fieldId) ? readMap0(mapCls) : null; + @Nullable Map readMap(int fieldId, @Nullable BinaryMapFactory factory) throws BinaryObjectException { + return findFieldById(fieldId) ? readMap0(factory) : null; } /** {@inheritDoc} */ @@ -1311,25 +1310,40 @@ private Collection readCollection0(@Nullable Class cls) } /** {@inheritDoc} */ - @Nullable @Override public Map readMap(Class> mapCls) + @Nullable @Override public Map readMap(BinaryMapFactory factory) throws BinaryObjectException { - return readMap0(mapCls); + return readMap0(factory); } /** * Internal read map routine. * - * @param cls Map class. + * @param factory Factory. * @return Value. * @throws BinaryObjectException If failed. */ - private Map readMap0(@Nullable Class cls) throws BinaryObjectException { + private Map readMap0(@Nullable BinaryMapFactory factory) throws BinaryObjectException { switch (checkFlag(MAP)) { case NORMAL: - return (Map)PortableUtils.doReadMap(in, ctx, ldr, this, true, cls); + return (Map)PortableUtils.doReadMap(in, ctx, ldr, this, true, factory); - case HANDLE: - return readHandleField(); + case HANDLE: { + int handlePos = PortableUtils.positionForHandle(in) - in.readInt(); + + Object obj = getHandle(handlePos); + + if (obj == null) { + int retPos = in.position(); + + streamPosition(handlePos); + + obj = readMap0(factory); + + streamPosition(retPos); + } + + return (Map)obj; + } default: return null; @@ -1584,11 +1598,6 @@ else if (flag == NULL) break; - case MAP_ENTRY: - obj = PortableUtils.doReadMapEntry(in, ctx, ldr, this, true); - - break; - case PORTABLE_OBJ: obj = PortableUtils.doReadPortableObject(in, ctx); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriteMode.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriteMode.java index bd73ad0629361..3e0bf6929b2d0 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriteMode.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriteMode.java @@ -132,9 +132,6 @@ public enum BinaryWriteMode { /** */ MAP(GridPortableMarshaller.MAP), - /** */ - MAP_ENTRY(GridPortableMarshaller.MAP_ENTRY), - /** */ PORTABLE_OBJ(GridPortableMarshaller.OBJ), diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java index 6bb493e669680..4139b8fb2ef21 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryWriterExImpl.java @@ -17,6 +17,16 @@ package org.apache.ignite.internal.portable; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.binary.BinaryIdMapper; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.binary.BinaryRawWriter; +import org.apache.ignite.binary.BinaryWriter; +import org.apache.ignite.internal.portable.streams.PortableHeapOutputStream; +import org.apache.ignite.internal.portable.streams.PortableOutputStream; +import org.apache.ignite.internal.util.typedef.internal.A; +import org.jetbrains.annotations.Nullable; + import java.io.IOException; import java.io.ObjectOutput; import java.lang.reflect.InvocationTargetException; @@ -27,15 +37,6 @@ import java.util.Date; import java.util.Map; import java.util.UUID; -import org.apache.ignite.IgniteCheckedException; -import org.apache.ignite.binary.BinaryIdMapper; -import org.apache.ignite.binary.BinaryObjectException; -import org.apache.ignite.binary.BinaryRawWriter; -import org.apache.ignite.binary.BinaryWriter; -import org.apache.ignite.internal.portable.streams.PortableHeapOutputStream; -import org.apache.ignite.internal.portable.streams.PortableOutputStream; -import org.apache.ignite.internal.util.typedef.internal.A; -import org.jetbrains.annotations.Nullable; import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.ignite.internal.portable.GridPortableMarshaller.BOOLEAN; @@ -62,7 +63,6 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.LONG; import static org.apache.ignite.internal.portable.GridPortableMarshaller.LONG_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.MAP; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.MAP_ENTRY; import static org.apache.ignite.internal.portable.GridPortableMarshaller.NULL; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ_ARR; @@ -760,23 +760,6 @@ void doWriteMap(@Nullable Map map) throws BinaryObjectException { } } - /** - * @param e Map entry. - * @throws org.apache.ignite.binary.BinaryObjectException In case of error. - */ - void doWriteMapEntry(@Nullable Map.Entry e) throws BinaryObjectException { - if (e == null) - out.writeByte(NULL); - else { - if (tryWriteAsHandle(e)) - return; - - out.writeByte(MAP_ENTRY); - doWriteObject(e.getKey()); - doWriteObject(e.getValue()); - } - } - /** * @param val Value. */ @@ -1217,14 +1200,6 @@ void writeMapField(@Nullable Map map) throws BinaryObjectException { doWriteMap(map); } - /** - * @param e Map entry. - * @throws org.apache.ignite.binary.BinaryObjectException In case of error. - */ - void writeMapEntryField(@Nullable Map.Entry e) throws BinaryObjectException { - doWriteMapEntry(e); - } - /** * @param val Value. */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java index 5244da8de9a0f..5063a1ec295d9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java @@ -118,9 +118,6 @@ public class GridPortableMarshaller { /** */ public static final byte MAP = 25; - /** */ - public static final byte MAP_ENTRY = 26; - /** */ public static final byte PORTABLE_OBJ = 27; @@ -166,30 +163,12 @@ public class GridPortableMarshaller { /** */ public static final byte LINKED_HASH_SET = 4; - /** */ - public static final byte TREE_SET = 5; - - /** */ - public static final byte CONC_SKIP_LIST_SET = 6; - - /** */ - public static final byte CONC_LINKED_QUEUE = 7; - /** */ public static final byte HASH_MAP = 1; /** */ public static final byte LINKED_HASH_MAP = 2; - /** */ - public static final byte TREE_MAP = 3; - - /** */ - public static final byte CONC_HASH_MAP = 4; - - /** */ - public static final byte PROPERTIES_MAP = 5; - /** */ public static final int OBJECT_TYPE_ID = -1; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java index 50c8bb5b14394..2dda9eb4f0125 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java @@ -205,7 +205,6 @@ public class PortableClassDescriptor { case OBJECT_ARR: case COL: case MAP: - case MAP_ENTRY: case PORTABLE_OBJ: case ENUM: case PORTABLE_ENUM: @@ -536,11 +535,6 @@ void write(Object obj, BinaryWriterExImpl writer) throws BinaryObjectException { break; - case MAP_ENTRY: - writer.doWriteMapEntry((Map.Entry)obj); - - break; - case ENUM: writer.doWriteEnum((Enum)obj); @@ -794,6 +788,7 @@ private Object newInstance() throws BinaryObjectException { * * @return {@code true} if to use, {@code false} otherwise. */ + @SuppressWarnings("unchecked") private boolean initUseOptimizedMarshallerFlag() { for (Class c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) { try { diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java index 2c7e4c3980039..f02867bb71ebb 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java @@ -17,6 +17,33 @@ package org.apache.ignite.internal.portable; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.binary.BinaryIdMapper; +import org.apache.ignite.binary.BinaryInvalidTypeException; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.binary.BinarySerializer; +import org.apache.ignite.binary.BinaryType; +import org.apache.ignite.binary.BinaryTypeConfiguration; +import org.apache.ignite.cache.CacheKeyConfiguration; +import org.apache.ignite.cache.affinity.AffinityKeyMapped; +import org.apache.ignite.configuration.BinaryConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.internal.processors.cache.portable.CacheObjectBinaryProcessorImpl; +import org.apache.ignite.internal.processors.datastructures.CollocatedQueueItemKey; +import org.apache.ignite.internal.processors.datastructures.CollocatedSetItemKey; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.util.lang.GridMapEntry; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.T2; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.marshaller.MarshallerContext; +import org.apache.ignite.marshaller.optimized.OptimizedMarshaller; +import org.jetbrains.annotations.Nullable; +import org.jsr166.ConcurrentHashMap8; + import java.io.Externalizable; import java.io.File; import java.io.IOException; @@ -41,43 +68,11 @@ import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Map; -import java.util.Properties; import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ConcurrentSkipListSet; import java.util.jar.JarEntry; import java.util.jar.JarFile; -import org.apache.ignite.IgniteCheckedException; -import org.apache.ignite.binary.BinaryIdMapper; -import org.apache.ignite.binary.BinaryInvalidTypeException; -import org.apache.ignite.binary.BinaryObjectException; -import org.apache.ignite.binary.BinarySerializer; -import org.apache.ignite.binary.BinaryType; -import org.apache.ignite.binary.BinaryTypeConfiguration; -import org.apache.ignite.cache.CacheKeyConfiguration; -import org.apache.ignite.cache.affinity.AffinityKeyMapped; -import org.apache.ignite.configuration.BinaryConfiguration; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.internal.IgniteKernal; -import org.apache.ignite.internal.IgnitionEx; -import org.apache.ignite.internal.processors.cache.portable.CacheObjectBinaryProcessorImpl; -import org.apache.ignite.internal.processors.datastructures.CollocatedQueueItemKey; -import org.apache.ignite.internal.processors.datastructures.CollocatedSetItemKey; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.internal.util.lang.GridMapEntry; -import org.apache.ignite.internal.util.typedef.F; -import org.apache.ignite.internal.util.typedef.T2; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.apache.ignite.lang.IgniteBiTuple; -import org.apache.ignite.marshaller.MarshallerContext; -import org.apache.ignite.marshaller.optimized.OptimizedMarshaller; -import org.jetbrains.annotations.Nullable; -import org.jsr166.ConcurrentHashMap8; /** * Portable context. @@ -164,16 +159,9 @@ public PortableContext(BinaryMetadataHandler metaHnd, IgniteConfiguration ignite colTypes.put(LinkedList.class, GridPortableMarshaller.LINKED_LIST); colTypes.put(HashSet.class, GridPortableMarshaller.HASH_SET); colTypes.put(LinkedHashSet.class, GridPortableMarshaller.LINKED_HASH_SET); - colTypes.put(TreeSet.class, GridPortableMarshaller.TREE_SET); - colTypes.put(ConcurrentSkipListSet.class, GridPortableMarshaller.CONC_SKIP_LIST_SET); - colTypes.put(ConcurrentLinkedQueue.class, GridPortableMarshaller.CONC_LINKED_QUEUE); mapTypes.put(HashMap.class, GridPortableMarshaller.HASH_MAP); mapTypes.put(LinkedHashMap.class, GridPortableMarshaller.LINKED_HASH_MAP); - mapTypes.put(TreeMap.class, GridPortableMarshaller.TREE_MAP); - mapTypes.put(ConcurrentHashMap.class, GridPortableMarshaller.CONC_HASH_MAP); - mapTypes.put(ConcurrentHashMap8.class, GridPortableMarshaller.CONC_HASH_MAP); - mapTypes.put(Properties.class, GridPortableMarshaller.PROPERTIES_MAP); // IDs range from [0..200] is used by Java SDK API and GridGain legacy API @@ -210,14 +198,9 @@ public PortableContext(BinaryMetadataHandler metaHnd, IgniteConfiguration ignite registerPredefinedType(LinkedList.class, 0); registerPredefinedType(HashSet.class, 0); registerPredefinedType(LinkedHashSet.class, 0); - registerPredefinedType(TreeSet.class, 0); - registerPredefinedType(ConcurrentSkipListSet.class, 0); registerPredefinedType(HashMap.class, 0); registerPredefinedType(LinkedHashMap.class, 0); - registerPredefinedType(TreeMap.class, 0); - registerPredefinedType(ConcurrentHashMap.class, 0); - registerPredefinedType(ConcurrentHashMap8.class, 0); registerPredefinedType(GridMapEntry.class, 60); registerPredefinedType(IgniteBiTuple.class, 61); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java index 5d794ca538d5d..125bb258b55bf 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java @@ -17,15 +17,15 @@ package org.apache.ignite.internal.portable; -import java.util.concurrent.ConcurrentLinkedQueue; import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.binary.BinaryCollectionFactory; import org.apache.ignite.binary.BinaryInvalidTypeException; +import org.apache.ignite.binary.BinaryMapFactory; import org.apache.ignite.binary.BinaryObject; import org.apache.ignite.binary.BinaryObjectException; import org.apache.ignite.binary.Binarylizable; import org.apache.ignite.internal.portable.builder.PortableLazyValue; import org.apache.ignite.internal.portable.streams.PortableInputStream; -import org.apache.ignite.internal.util.lang.GridMapEntry; import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.lang.IgniteBiTuple; @@ -35,8 +35,6 @@ import java.io.ByteArrayInputStream; import java.io.Externalizable; import java.lang.reflect.Array; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Timestamp; @@ -50,7 +48,6 @@ import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Map; -import java.util.Properties; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -68,9 +65,6 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.CHAR_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.CLASS; import static org.apache.ignite.internal.portable.GridPortableMarshaller.COL; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.CONC_HASH_MAP; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.CONC_LINKED_QUEUE; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.CONC_SKIP_LIST_SET; import static org.apache.ignite.internal.portable.GridPortableMarshaller.DATE; import static org.apache.ignite.internal.portable.GridPortableMarshaller.DATE_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.DECIMAL; @@ -92,14 +86,12 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.LONG; import static org.apache.ignite.internal.portable.GridPortableMarshaller.LONG_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.MAP; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.MAP_ENTRY; import static org.apache.ignite.internal.portable.GridPortableMarshaller.NULL; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJECT_TYPE_ID; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.OPTM_MARSH; import static org.apache.ignite.internal.portable.GridPortableMarshaller.PORTABLE_OBJ; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.PROPERTIES_MAP; import static org.apache.ignite.internal.portable.GridPortableMarshaller.PROTO_VER; import static org.apache.ignite.internal.portable.GridPortableMarshaller.SHORT; import static org.apache.ignite.internal.portable.GridPortableMarshaller.SHORT_ARR; @@ -107,8 +99,6 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.STRING_ARR; import static org.apache.ignite.internal.portable.GridPortableMarshaller.TIMESTAMP; import static org.apache.ignite.internal.portable.GridPortableMarshaller.TIMESTAMP_ARR; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.TREE_MAP; -import static org.apache.ignite.internal.portable.GridPortableMarshaller.TREE_SET; import static org.apache.ignite.internal.portable.GridPortableMarshaller.UNREGISTERED_TYPE_ID; import static org.apache.ignite.internal.portable.GridPortableMarshaller.USER_COL; import static org.apache.ignite.internal.portable.GridPortableMarshaller.USER_SET; @@ -271,7 +261,6 @@ public class PortableUtils { FIELD_TYPE_NAMES[PORTABLE_OBJ] = "Object"; FIELD_TYPE_NAMES[COL] = "Collection"; FIELD_TYPE_NAMES[MAP] = "Map"; - FIELD_TYPE_NAMES[MAP_ENTRY] = "Entry"; FIELD_TYPE_NAMES[CLASS] = "Class"; FIELD_TYPE_NAMES[BYTE_ARR] = "byte[]"; FIELD_TYPE_NAMES[SHORT_ARR] = "short[]"; @@ -620,15 +609,12 @@ public static byte typeByClass(Class cls) { if (cls.isArray()) return cls.getComponentType().isEnum() || cls.getComponentType() == Enum.class ? ENUM_ARR : OBJ_ARR; - if (Collection.class.isAssignableFrom(cls)) + if (isSpecialCollection(cls)) return COL; - if (Map.class.isAssignableFrom(cls)) + if (isSpecialMap(cls)) return MAP; - if (Map.Entry.class.isAssignableFrom(cls)) - return MAP_ENTRY; - return OBJ; } @@ -1004,19 +990,16 @@ else if (cls == Date[].class) else if (cls == Timestamp[].class) return BinaryWriteMode.TIMESTAMP_ARR; else if (cls.isArray()) - return cls.getComponentType().isEnum() ? - BinaryWriteMode.ENUM_ARR : BinaryWriteMode.OBJECT_ARR; + return cls.getComponentType().isEnum() ? BinaryWriteMode.ENUM_ARR : BinaryWriteMode.OBJECT_ARR; else if (cls == BinaryObjectImpl.class) return BinaryWriteMode.PORTABLE_OBJ; else if (Binarylizable.class.isAssignableFrom(cls)) return BinaryWriteMode.PORTABLE; else if (Externalizable.class.isAssignableFrom(cls)) return BinaryWriteMode.EXTERNALIZABLE; - else if (Map.Entry.class.isAssignableFrom(cls)) - return BinaryWriteMode.MAP_ENTRY; - else if (Collection.class.isAssignableFrom(cls)) + else if (isSpecialCollection(cls)) return BinaryWriteMode.COL; - else if (Map.class.isAssignableFrom(cls)) + else if (isSpecialMap(cls)) return BinaryWriteMode.MAP; else if (cls.isEnum()) return BinaryWriteMode.ENUM; @@ -1026,6 +1009,27 @@ else if (cls == Class.class) return BinaryWriteMode.OBJECT; } + /** + * Check if class represents a collection which must be treated specially. + * + * @param cls Class. + * @return {@code True} if this is a special collection class. + */ + private static boolean isSpecialCollection(Class cls) { + return ArrayList.class.equals(cls) || LinkedList.class.equals(cls) || + HashSet.class.equals(cls) || LinkedHashSet.class.equals(cls); + } + + /** + * Check if class represents a map which must be treated specially. + * + * @param cls Class. + * @return {@code True} if this is a special map class. + */ + private static boolean isSpecialMap(Class cls) { + return HashMap.class.equals(cls) || LinkedHashMap.class.equals(cls); + } + /** * @return Value. */ @@ -1688,9 +1692,6 @@ public static Object doReadOptimized(PortableInputStream in, PortableContext ctx case MAP: return doReadMap(in, ctx, ldr, handles, false, null); - case MAP_ENTRY: - return doReadMapEntry(in, ctx, ldr, handles, false); - case PORTABLE_OBJ: return doReadPortableObject(in, ctx); @@ -1738,13 +1739,13 @@ public static Object[] doReadObjectArray(PortableInputStream in, PortableContext /** * @param deserialize Deep flag. - * @param cls Collection class. + * @param factory Collection factory. * @return Value. * @throws BinaryObjectException In case of error. */ @SuppressWarnings("unchecked") public static Collection doReadCollection(PortableInputStream in, PortableContext ctx, ClassLoader ldr, - BinaryReaderHandlesHolder handles, boolean deserialize, @Nullable Class cls) + BinaryReaderHandlesHolder handles, boolean deserialize, BinaryCollectionFactory factory) throws BinaryObjectException { int hPos = positionForHandle(in); @@ -1756,20 +1757,8 @@ public static Collection doReadCollection(PortableInputStream in, PortableCon Collection col; - if (cls != null) { - try { - Constructor cons = cls.getConstructor(); - - col = cons.newInstance(); - } - catch (NoSuchMethodException ignored) { - throw new BinaryObjectException("Collection class doesn't have public default constructor: " + - cls.getName()); - } - catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { - throw new BinaryObjectException("Failed to instantiate collection: " + cls.getName(), e); - } - } + if (factory != null) + col = factory.create(size); else { switch (colType) { case ARR_LIST: @@ -1792,21 +1781,6 @@ public static Collection doReadCollection(PortableInputStream in, PortableCon break; - case TREE_SET: - col = new TreeSet<>(); - - break; - - case CONC_SKIP_LIST_SET: - col = new ConcurrentSkipListSet<>(); - - break; - - case CONC_LINKED_QUEUE: - col = new ConcurrentLinkedQueue<>(); - - break; - case USER_SET: col = U.newHashSet(size); @@ -1832,13 +1806,13 @@ public static Collection doReadCollection(PortableInputStream in, PortableCon /** * @param deserialize Deep flag. - * @param cls Map class. + * @param factory Map factory. * @return Value. * @throws BinaryObjectException In case of error. */ @SuppressWarnings("unchecked") public static Map doReadMap(PortableInputStream in, PortableContext ctx, ClassLoader ldr, - BinaryReaderHandlesHolder handles, boolean deserialize, @Nullable Class cls) + BinaryReaderHandlesHolder handles, boolean deserialize, BinaryMapFactory factory) throws BinaryObjectException { int hPos = positionForHandle(in); @@ -1850,20 +1824,8 @@ public static Collection doReadCollection(PortableInputStream in, PortableCon Map map; - if (cls != null) { - try { - Constructor cons = cls.getConstructor(); - - map = cons.newInstance(); - } - catch (NoSuchMethodException ignored) { - throw new BinaryObjectException("Map class doesn't have public default constructor: " + - cls.getName()); - } - catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { - throw new BinaryObjectException("Failed to instantiate map: " + cls.getName(), e); - } - } + if (factory != null) + map = factory.create(size); else { switch (mapType) { case HASH_MAP: @@ -1876,26 +1838,11 @@ public static Collection doReadCollection(PortableInputStream in, PortableCon break; - case TREE_MAP: - map = new TreeMap<>(); - - break; - - case CONC_HASH_MAP: - map = new ConcurrentHashMap<>(size); - - break; - case USER_COL: map = U.newHashMap(size); break; - case PROPERTIES_MAP: - map = new Properties(); - - break; - default: throw new BinaryObjectException("Invalid map type: " + mapType); } @@ -1913,25 +1860,6 @@ public static Collection doReadCollection(PortableInputStream in, PortableCon return map; } - /** - * @param deserialize Deserialize flag flag. - * @return Value. - * @throws BinaryObjectException In case of error. - */ - public static Map.Entry doReadMapEntry(PortableInputStream in, PortableContext ctx, ClassLoader ldr, - BinaryReaderHandlesHolder handles, boolean deserialize) throws BinaryObjectException { - int hPos = positionForHandle(in); - - Object val1 = deserializeOrUnmarshal(in, ctx, ldr, handles, deserialize); - Object val2 = deserializeOrUnmarshal(in, ctx, ldr, handles, deserialize); - - GridMapEntry entry = new GridMapEntry<>(val1, val2); - - handles.setHandle(entry, hPos); - - return entry; - } - /** * Deserialize or unmarshal the object. * 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 cf27da426f76d..0b7e0b54cb994 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 @@ -344,12 +344,6 @@ public void skipValue() { return; } - case GridPortableMarshaller.MAP_ENTRY: - skipValue(); - skipValue(); - - return; - case GridPortableMarshaller.PORTABLE_OBJ: len = readInt() + 4; @@ -448,7 +442,6 @@ public Object getValueQuickly(int pos, int len) { case GridPortableMarshaller.OBJ_ARR: case GridPortableMarshaller.COL: case GridPortableMarshaller.MAP: - case GridPortableMarshaller.MAP_ENTRY: return new LazyCollection(pos); case GridPortableMarshaller.ENUM: { @@ -732,8 +725,6 @@ else if (flag == GridPortableMarshaller.DECIMAL) { case GridPortableMarshaller.HASH_SET: case GridPortableMarshaller.LINKED_HASH_SET: - case GridPortableMarshaller.TREE_SET: - case GridPortableMarshaller.CONC_SKIP_LIST_SET: return new PortableLazySet(this, size); } @@ -749,9 +740,6 @@ else if (flag == GridPortableMarshaller.DECIMAL) { case GridPortableMarshaller.ENUM_ARR: return new PortableEnumArrayLazyValue(this); - case GridPortableMarshaller.MAP_ENTRY: - return new PortableLazyMapEntry(this); - case GridPortableMarshaller.PORTABLE_OBJ: { int size = readInt(); 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 52f84c6f1e9d4..d5923eee68150 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 @@ -19,14 +19,15 @@ import org.apache.ignite.binary.BinaryObject; import org.apache.ignite.internal.portable.BinaryMetadata; -import org.apache.ignite.internal.portable.GridPortableMarshaller; import org.apache.ignite.internal.portable.BinaryObjectExImpl; +import org.apache.ignite.internal.portable.BinaryWriterExImpl; +import org.apache.ignite.internal.portable.GridPortableMarshaller; import org.apache.ignite.internal.portable.PortableContext; import org.apache.ignite.internal.portable.PortableUtils; -import org.apache.ignite.internal.portable.BinaryWriterExImpl; -import org.apache.ignite.internal.util.*; -import java.util.*; +import java.util.Collection; +import java.util.IdentityHashMap; +import java.util.Map; /** * @@ -118,13 +119,7 @@ public void writeValue(BinaryWriterExImpl writer, Object val) { writer.writeByte(GridPortableMarshaller.COL); writer.writeInt(c.size()); - byte colType; - - if (c instanceof GridConcurrentSkipListSet) - colType = GridPortableMarshaller.CONC_SKIP_LIST_SET; - else - colType = writer.context().collectionType(c.getClass()); - + byte colType = writer.context().collectionType(c.getClass()); writer.writeByte(colType); 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 deleted file mode 100644 index 5ebb223d61793..0000000000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableLazyMapEntry.java +++ /dev/null @@ -1,68 +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.*; - -import java.util.Map; - -/** - * - */ -class PortableLazyMapEntry implements Map.Entry, PortableBuilderSerializationAware { - /** */ - private final Object key; - - /** */ - private Object val; - - /** - * @param reader GridMutablePortableReader - */ - PortableLazyMapEntry(PortableBuilderReader reader) { - key = reader.parseValue(); - val = reader.parseValue(); - } - - /** {@inheritDoc} */ - @Override public Object getKey() { - return PortableUtils.unwrapLazy(key); - } - - /** {@inheritDoc} */ - @Override public Object getValue() { - return PortableUtils.unwrapLazy(val); - } - - /** {@inheritDoc} */ - @Override public Object setValue(Object val) { - Object res = getValue(); - - this.val = val; - - return res; - } - - /** {@inheritDoc} */ - @Override public void writeTo(BinaryWriterExImpl writer, PortableBuilderSerializer ctx) { - writer.writeByte(GridPortableMarshaller.MAP_ENTRY); - - ctx.writeValue(writer, key); - ctx.writeValue(writer, val); - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryMarshallerSelfTest.java index d667e073e082d..037adf9580bd8 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryMarshallerSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryMarshallerSelfTest.java @@ -40,6 +40,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Queue; @@ -50,7 +51,9 @@ import java.util.concurrent.ConcurrentSkipListSet; import junit.framework.Assert; import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.binary.BinaryCollectionFactory; import org.apache.ignite.binary.BinaryIdMapper; +import org.apache.ignite.binary.BinaryMapFactory; import org.apache.ignite.binary.BinaryObject; import org.apache.ignite.binary.BinaryObjectBuilder; import org.apache.ignite.binary.BinaryObjectException; @@ -382,6 +385,50 @@ private void testMap(Map map) throws Exception { assertEquals(map, marshalUnmarshal(map)); } + /** + * Test serialization of custom collections. + * + * @throws Exception If failed. + */ + @SuppressWarnings("unchecked") + public void testCustomCollections() throws Exception { + CustomCollections cc = new CustomCollections(); + + cc.list.add(1); + cc.customList.add(2); + + CustomCollections copiedCc = marshalUnmarshal(cc); + + assert copiedCc.customList.getClass().equals(CustomArrayList.class); + + assertEquals(cc.list.size(), copiedCc.list.size()); + assertEquals(cc.customList.size(), copiedCc.customList.size()); + + assertEquals(cc.list.get(0), copiedCc.list.get(0)); + assertEquals(cc.customList.get(0), copiedCc.customList.get(0)); + } + + /** + * Test custom collections with factories. + * + * @throws Exception If failed. + */ + @SuppressWarnings("unchecked") + public void testCustomCollectionsWithFactory() throws Exception { + CustomCollectionsWithFactory cc = new CustomCollectionsWithFactory(); + + cc.list.add(new DummyHolder(1)); + cc.map.put(new DummyHolder(2), new DummyHolder(3)); + + CustomCollectionsWithFactory copiedCc = marshalUnmarshal(cc); + + assertEquals(cc.list.size(), copiedCc.list.size()); + assertEquals(cc.map.size(), copiedCc.map.size()); + + assertEquals(cc.list.get(0), copiedCc.list.get(0)); + assertEquals(cc.map.get(new DummyHolder(2)), copiedCc.map.get(new DummyHolder(2))); + } + /** * @throws Exception If failed. */ @@ -1153,8 +1200,8 @@ public void testCollectionFields() throws Exception { )); Object[] arr = new Object[] {new Value(1), new Value(2), new Value(3)}; - Collection col = Arrays.asList(new Value(4), new Value(5), new Value(6)); - Map map = F.asMap(new Key(10), new Value(10), new Key(20), new Value(20), new Key(30), new Value(30)); + Collection col = new ArrayList<>(Arrays.asList(new Value(4), new Value(5), new Value(6))); + Map map = new HashMap<>(F.asMap(new Key(10), new Value(10), new Key(20), new Value(20), new Key(30), new Value(30))); CollectionFieldsObject obj = new CollectionFieldsObject(arr, col, map); @@ -3459,6 +3506,82 @@ private DynamicObject(int idx, int val1, int val2, int val3) { } } + /** + * Custom array list. + */ + private static class CustomArrayList extends ArrayList { + // No-op. + } + + /** + * Custom hash map. + */ + private static class CustomHashMap extends HashMap { + // No-op. + } + + /** + * Holder for non-stadard collections. + */ + private static class CustomCollections { + public List list = new ArrayList(); + public List customList = new CustomArrayList(); + } + + @SuppressWarnings("unchecked") + private static class CustomCollectionsWithFactory implements Binarylizable { + public List list = new CustomArrayList(); + public Map map = new CustomHashMap(); + + /** {@inheritDoc} */ + @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException { + writer.writeCollection("list", list); + writer.writeMap("map", map); + } + + /** {@inheritDoc} */ + @Override public void readBinary(BinaryReader reader) throws BinaryObjectException { + list = (List)reader.readCollection("list", new BinaryCollectionFactory() { + @Override public Collection create(int size) { + return new CustomArrayList(); + } + }); + + map = reader.readMap("map", new BinaryMapFactory() { + @Override public Map create(int size) { + return new CustomHashMap(); + } + }); + } + } + + /** + * Dummy value holder. + */ + private static class DummyHolder { + /** Value. */ + public int val; + + /** + * Constructor. + * + * @param val Value. + */ + public DummyHolder(int val) { + this.val = val; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + return o != null && o instanceof DummyHolder && ((DummyHolder)o).val == val; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return val; + } + } + /** */ private static class CycleLinkObject { diff --git a/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderAdditionalSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderAdditionalSelfTest.java index cfeb7149b4809..06535d2e60112 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderAdditionalSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderAdditionalSelfTest.java @@ -21,6 +21,24 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import org.apache.ignite.IgniteBinary; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.binary.BinaryObjectBuilder; +import org.apache.ignite.binary.BinaryType; +import org.apache.ignite.configuration.BinaryConfiguration; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.portable.builder.BinaryObjectBuilderImpl; +import org.apache.ignite.internal.portable.builder.PortableBuilderEnum; +import org.apache.ignite.internal.portable.mutabletest.GridBinaryMarshalerAwareTestClass; +import org.apache.ignite.internal.processors.cache.portable.CacheObjectBinaryProcessorImpl; +import org.apache.ignite.internal.processors.cache.portable.IgniteBinaryImpl; +import org.apache.ignite.internal.util.lang.GridMapEntry; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Assert; + import java.lang.reflect.Field; import java.math.BigDecimal; import java.sql.Timestamp; @@ -35,27 +53,11 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; -import org.apache.ignite.IgniteCheckedException; -import org.apache.ignite.IgniteBinary; -import org.apache.ignite.configuration.BinaryConfiguration; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.internal.portable.builder.PortableBuilderEnum; -import org.apache.ignite.internal.portable.builder.BinaryObjectBuilderImpl; -import org.apache.ignite.internal.portable.mutabletest.GridBinaryMarshalerAwareTestClass; -import org.apache.ignite.internal.processors.cache.portable.CacheObjectBinaryProcessorImpl; -import org.apache.ignite.internal.processors.cache.portable.IgniteBinaryImpl; -import org.apache.ignite.internal.util.lang.GridMapEntry; -import org.apache.ignite.binary.BinaryObjectBuilder; -import org.apache.ignite.binary.BinaryType; -import org.apache.ignite.binary.BinaryObject; -import org.apache.ignite.testframework.GridTestUtils; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.junit.Assert; import static org.apache.ignite.cache.CacheMode.REPLICATED; import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.Address; -import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.AddressBook; +import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.Addresses; +import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.Companies; import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.Company; import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.TestObjectAllTypes; import static org.apache.ignite.internal.portable.mutabletest.GridPortableTestClasses.TestObjectArrayList; @@ -148,15 +150,6 @@ public void testSimpleTypeFieldRead() throws Exception { break; } - - case "entry": - assertEquals(((Map.Entry)expVal).getKey(), ((Map.Entry)actVal).getKey()); - assertEquals(((Map.Entry)expVal).getValue(), ((Map.Entry)actVal).getValue()); - break; - - default: - assertTrue(field.getName(), Objects.deepEquals(expVal, actVal)); - break; } } } @@ -920,27 +913,6 @@ public void testCollectionsInCollection() { assertEquals(obj.foo, deserialized.foo); } - /** - * - */ - public void testMapEntryModification() { - TestObjectContainer obj = new TestObjectContainer(); - obj.foo = ImmutableMap.of(1, "a").entrySet().iterator().next(); - - BinaryObjectBuilderImpl mutableObj = wrap(obj); - - Map.Entry entry = mutableObj.getField("foo"); - - assertEquals(1, entry.getKey()); - assertEquals("a", entry.getValue()); - - entry.setValue("b"); - - TestObjectContainer res = mutableObj.build().deserialize(); - - assertEquals(new GridMapEntry<>(1, "b"), res.foo); - } - /** * */ @@ -1116,30 +1088,39 @@ public void testTimestampArrayOverride() { * */ public void testChangeMap() { - AddressBook addrBook = new AddressBook(); + Addresses addrs = new Addresses(); + + addrs.addCompany(new Company(1, "Google inc", 100, + new Address("Saint-Petersburg", "Torzhkovskya", 1, 53), "occupation")); + + addrs.addCompany(new Company(2, "Apple inc", 100, + new Address("Saint-Petersburg", "Torzhkovskya", 1, 54), "occupation")); + + addrs.addCompany(new Company(3, "Microsoft", 100, + new Address("Saint-Petersburg", "Torzhkovskya", 1, 55), "occupation")); + + addrs.addCompany(new Company(4, "Oracle", 100, + new Address("Saint-Petersburg", "Nevskiy", 1, 1), "occupation")); - addrBook.addCompany(new Company(1, "Google inc", 100, new Address("Saint-Petersburg", "Torzhkovskya", 1, 53), "occupation")); - addrBook.addCompany(new Company(2, "Apple inc", 100, new Address("Saint-Petersburg", "Torzhkovskya", 1, 54), "occupation")); - addrBook.addCompany(new Company(3, "Microsoft", 100, new Address("Saint-Petersburg", "Torzhkovskya", 1, 55), "occupation")); - addrBook.addCompany(new Company(4, "Oracle", 100, new Address("Saint-Petersburg", "Nevskiy", 1, 1), "occupation")); + BinaryObjectBuilderImpl binaryAddres = wrap(addrs); - BinaryObjectBuilderImpl mutableObj = wrap(addrBook); + Map map = binaryAddres.getField("companyByStreet"); - Map> map = mutableObj.getField("companyByStreet"); + BinaryObjectBuilderImpl binaryCompanies = map.get("Torzhkovskya"); - List list = map.get("Torzhkovskya"); + List binaryCompaniesList = binaryCompanies.getField("companies"); - BinaryObjectBuilderImpl company = list.get(0); + BinaryObjectBuilderImpl company = binaryCompaniesList.get(0); assert "Google inc".equals(company.getField("name")); - list.remove(0); + binaryCompaniesList.remove(0); - AddressBook res = mutableObj.build().deserialize(); + Addresses res = binaryAddres.build().deserialize(); assertEquals(Arrays.asList("Nevskiy", "Torzhkovskya"), new ArrayList<>(res.getCompanyByStreet().keySet())); - List torzhkovskyaCompanies = res.getCompanyByStreet().get("Torzhkovskya"); + Companies torzhkovskyaCompanies = res.getCompanyByStreet().get("Torzhkovskya"); assertEquals(2, torzhkovskyaCompanies.size()); assertEquals("Apple inc", torzhkovskyaCompanies.get(0).name); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/portable/mutabletest/GridPortableTestClasses.java b/modules/core/src/test/java/org/apache/ignite/internal/portable/mutabletest/GridPortableTestClasses.java index 69687abd385f2..b568cb5005052 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/portable/mutabletest/GridPortableTestClasses.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/portable/mutabletest/GridPortableTestClasses.java @@ -31,6 +31,12 @@ import java.util.Map; import java.util.TreeMap; import java.util.UUID; + +import org.apache.ignite.binary.BinaryMapFactory; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.binary.BinaryReader; +import org.apache.ignite.binary.BinaryWriter; +import org.apache.ignite.binary.Binarylizable; import org.apache.ignite.internal.util.lang.GridMapEntry; import org.apache.ignite.binary.BinaryObject; @@ -315,7 +321,10 @@ public enum TestObjectEnum { /** * */ - public static class Address { + public static class Address implements Serializable { + /** SUID. */ + private static final long serialVersionUID = 0L; + /** City. */ public String city; @@ -354,7 +363,10 @@ public Address(String city, String street, int streetNumber, int flatNumber) { /** * */ - public static class Company { + public static class Company implements Serializable { + /** SUID. */ + private static final long serialVersionUID = 0L; + /** ID. */ public int id; @@ -396,28 +408,50 @@ public Company(int id, String name, int size, Address address, String occupation } /** - * + * Companies. */ - public static class AddressBook { - /** */ - private Map> companyByStreet = new TreeMap<>(); + public static class Companies { + /** Companies. */ + private List companies = new ArrayList<>(); /** - * @param street Street. + * @param idx Index. * @return Company. */ - public List findCompany(String street) { - return companyByStreet.get(street); + public Company get(int idx) { + return companies.get(idx); + } + + /** + * @param company Company. + */ + public void add(Company company) { + companies.add(company); + } + + /** + * @return Size. + */ + public int size() { + return companies.size(); } + } + + /** + * + */ + public static class Addresses implements Binarylizable { + /** */ + private Map companyByStreet = new TreeMap<>(); /** * @param company Company. */ public void addCompany(Company company) { - List list = companyByStreet.get(company.address.street); + Companies list = companyByStreet.get(company.address.street); if (list == null) { - list = new ArrayList<>(); + list = new Companies(); companyByStreet.put(company.address.street, list); } @@ -428,16 +462,23 @@ public void addCompany(Company company) { /** * @return map */ - public Map> getCompanyByStreet() { + public Map getCompanyByStreet() { return companyByStreet; } - /** - * @param companyByStreet map - */ - public void setCompanyByStreet(Map> companyByStreet) { - this.companyByStreet = companyByStreet; + /** {@inheritDoc} */ + @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException { + writer.writeMap("companyByStreet", companyByStreet); } - } + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public void readBinary(BinaryReader reader) throws BinaryObjectException { + companyByStreet = reader.readMap("companyByStreet", new BinaryMapFactory() { + @Override public Map create(int size) { + return new TreeMap<>(); + } + }); + } + } } \ No newline at end of file diff --git a/modules/platforms/cpp/core-test/src/binary_reader_writer_raw_test.cpp b/modules/platforms/cpp/core-test/src/binary_reader_writer_raw_test.cpp index 8d47c249d4609..4e7e2dfcf73cf 100644 --- a/modules/platforms/cpp/core-test/src/binary_reader_writer_raw_test.cpp +++ b/modules/platforms/cpp/core-test/src/binary_reader_writer_raw_test.cpp @@ -1496,7 +1496,7 @@ BOOST_AUTO_TEST_CASE(TestCollectionEmpty) BOOST_AUTO_TEST_CASE(TestCollectionEmptyTyped) { - CollectionType typ = IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET; + CollectionType typ = IGNITE_COLLECTION_LINKED_HASH_SET; CheckRawCollectionEmpty(&typ); } @@ -1508,7 +1508,7 @@ BOOST_AUTO_TEST_CASE(TestCollection) BOOST_AUTO_TEST_CASE(TestCollectionTyped) { - CollectionType typ = IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET; + CollectionType typ = IGNITE_COLLECTION_LINKED_HASH_SET; CheckRawCollection(&typ); } @@ -1520,7 +1520,7 @@ BOOST_AUTO_TEST_CASE(TestCollectionIterators) BOOST_AUTO_TEST_CASE(TestCollectionIteratorsTyped) { - CollectionType typ = IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET; + CollectionType typ = IGNITE_COLLECTION_LINKED_HASH_SET; CheckRawCollectionIterators(&typ); } @@ -1573,7 +1573,7 @@ BOOST_AUTO_TEST_CASE(TestMapEmpty) BOOST_AUTO_TEST_CASE(TestMapEmptyTyped) { - MapType typ = IGNITE_MAP_CONCURRENT_HASH_MAP; + MapType typ = IGNITE_MAP_LINKED_HASH_MAP; CheckRawMapEmpty(&typ); } @@ -1585,7 +1585,7 @@ BOOST_AUTO_TEST_CASE(TestMap) BOOST_AUTO_TEST_CASE(TestMapTyped) { - MapType typ = IGNITE_MAP_CONCURRENT_HASH_MAP; + MapType typ = IGNITE_MAP_LINKED_HASH_MAP; CheckRawMap(&typ); } diff --git a/modules/platforms/cpp/core-test/src/binary_reader_writer_test.cpp b/modules/platforms/cpp/core-test/src/binary_reader_writer_test.cpp index b2551ec2228a6..71177b87d3c40 100644 --- a/modules/platforms/cpp/core-test/src/binary_reader_writer_test.cpp +++ b/modules/platforms/cpp/core-test/src/binary_reader_writer_test.cpp @@ -1891,7 +1891,7 @@ BOOST_AUTO_TEST_CASE(TestCollectionEmpty) BOOST_AUTO_TEST_CASE(TestCollectionEmptyTyped) { - CollectionType typ = IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET; + CollectionType typ = IGNITE_COLLECTION_LINKED_HASH_SET; CheckCollectionEmpty(&typ); } @@ -1903,7 +1903,7 @@ BOOST_AUTO_TEST_CASE(TestCollection) BOOST_AUTO_TEST_CASE(testCollectionTyped) { - CollectionType typ = IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET; + CollectionType typ = IGNITE_COLLECTION_LINKED_HASH_SET; CheckCollection(&typ); } @@ -1915,7 +1915,7 @@ BOOST_AUTO_TEST_CASE(TestCollectionIterators) BOOST_AUTO_TEST_CASE(TestCollectionIteratorsTyped) { - CollectionType typ = IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET; + CollectionType typ = IGNITE_COLLECTION_LINKED_HASH_SET; CheckCollectionIterators(&typ); } @@ -1980,7 +1980,7 @@ BOOST_AUTO_TEST_CASE(TestMapEmpty) BOOST_AUTO_TEST_CASE(TestMapEmptyTyped) { - MapType typ = IGNITE_MAP_CONCURRENT_HASH_MAP; + MapType typ = IGNITE_MAP_LINKED_HASH_MAP; CheckMapEmpty(&typ); } @@ -1992,7 +1992,7 @@ BOOST_AUTO_TEST_CASE(TestMap) BOOST_AUTO_TEST_CASE(TestMapTyped) { - MapType typ = IGNITE_MAP_CONCURRENT_HASH_MAP; + MapType typ = IGNITE_MAP_LINKED_HASH_MAP; CheckMap(&typ); } diff --git a/modules/platforms/cpp/core/include/ignite/binary/binary_consts.h b/modules/platforms/cpp/core/include/ignite/binary/binary_consts.h index 68ed9f98c5ab3..7f0fd1efe043d 100644 --- a/modules/platforms/cpp/core/include/ignite/binary/binary_consts.h +++ b/modules/platforms/cpp/core/include/ignite/binary/binary_consts.h @@ -52,17 +52,7 @@ namespace ignite /** * Linked hash set. Maps to LinkedHashSet in Java. */ - IGNITE_COLLECTION_LINKED_HASH_SET = 4, - - /** - * Tree set. Maps to TreeSet in Java. - */ - IGNITE_COLLECTION_TREE_SET = 5, - - /** - * Concurrent skip list set. Maps to ConcurrentSkipListSet in Java. - */ - IGNITE_COLLECTION_CONCURRENT_SKIP_LIST_SET = 6 + IGNITE_COLLECTION_LINKED_HASH_SET = 4 }; /** @@ -83,22 +73,7 @@ namespace ignite /** * Linked hash map. Maps to LinkedHashMap in Java. */ - IGNITE_MAP_LINKED_HASH_MAP = 2, - - /** - * Tree map. Maps to TreeMap in Java. - */ - IGNITE_MAP_TREE_MAP = 3, - - /** - * Concurrent hash map. Maps to ConcurrentHashMap in Java. - */ - IGNITE_MAP_CONCURRENT_HASH_MAP = 4, - - /** - * Properties map. Maps to Properties in Java. - */ - IGNITE_MAP_PROPERTIES_MAP = 5 + IGNITE_MAP_LINKED_HASH_MAP = 2 }; } } diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs index 7ef62595c292e..f41514f336e2b 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs @@ -972,13 +972,7 @@ internal void ProcessBuilder(IBinaryStream outStream, BinaryObjectBuilder builde } break; - - case BinaryUtils.TypeMapEntry: - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - Mutate0(ctx, inStream, outStream, false, 0, EmptyVals); - - break; - + case BinaryUtils.TypeBinary: TransferArray(inStream, outStream, 1); // Data array. TransferBytes(inStream, outStream, 4); // Offset in array. diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs index 506b56da0483f..7596992fc502f 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs @@ -69,7 +69,6 @@ internal static class BinarySystemHandlers {typeof (Guid?), BinaryUtils.TypeGuid}, {typeof (ArrayList), BinaryUtils.TypeCollection}, {typeof (Hashtable), BinaryUtils.TypeDictionary}, - {typeof (DictionaryEntry), BinaryUtils.TypeMapEntry}, {typeof (bool[]), BinaryUtils.TypeArrayBool}, {typeof (byte[]), BinaryUtils.TypeArrayByte}, {typeof (sbyte[]), BinaryUtils.TypeArrayByte}, @@ -162,11 +161,8 @@ static BinarySystemHandlers() // 13. Arbitrary dictionary. ReadHandlers[BinaryUtils.TypeDictionary] = new BinarySystemReader(ReadDictionary); - - // 15. Map entry. - ReadHandlers[BinaryUtils.TypeMapEntry] = new BinarySystemReader(ReadMapEntry); - // 16. Enum. + // 14. Enum. ReadHandlers[BinaryUtils.TypeArrayEnum] = new BinarySystemReader(ReadEnumArray); } @@ -218,8 +214,7 @@ private static BinarySystemWriteDelegate FindWriteHandler(Type type) return WriteArrayList; if (type == typeof(Hashtable)) return WriteHashtable; - if (type == typeof(DictionaryEntry)) - return WriteMapEntry; + if (type.IsArray) { // We know how to write any array type. @@ -611,16 +606,6 @@ private static void WriteHashtable(BinaryWriter ctx, object obj) BinaryUtils.WriteDictionary((IDictionary)obj, ctx, BinaryUtils.MapHashMap); } - /** - * Write map entry. - */ - private static void WriteMapEntry(BinaryWriter ctx, object obj) - { - ctx.Stream.WriteByte(BinaryUtils.TypeMapEntry); - - BinaryUtils.WriteMapEntry(ctx, (DictionaryEntry)obj); - } - /** * Write binary object. */ @@ -696,14 +681,6 @@ private static object ReadDictionary(BinaryReader ctx, Type type) return BinaryUtils.ReadDictionary(ctx, null); } - /** - * Read map entry. - */ - private static object ReadMapEntry(BinaryReader ctx, Type type) - { - return BinaryUtils.ReadMapEntry(ctx); - } - /** * Read delegate. * Read context. diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs index 4c54deac1653e..1ae5722553a8a 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs @@ -19,7 +19,6 @@ namespace Apache.Ignite.Core.Impl.Binary { using System; using System.Collections; - using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; @@ -133,10 +132,7 @@ internal static class BinaryUtils /** Type: map. */ public const byte TypeDictionary = 25; - - /** Type: map entry. */ - public const byte TypeMapEntry = 26; - + /** Type: binary object. */ public const byte TypeBinary = 27; @@ -199,34 +195,13 @@ internal static class BinaryUtils /** Collection: linked list. */ public const byte CollectionLinkedList = 2; - - /** Collection: hash set. */ - public const byte CollectionHashSet = 3; - - /** Collection: hash set. */ - public const byte CollectionLinkedHashSet = 4; - - /** Collection: sorted set. */ - public const byte CollectionSortedSet = 5; - - /** Collection: concurrent bag. */ - public const byte CollectionConcurrentBag = 6; - + /** Map: custom. */ public const byte MapCustom = 0; /** Map: hash map. */ public const byte MapHashMap = 1; - - /** Map: linked hash map. */ - public const byte MapLinkedHashMap = 2; - - /** Map: sorted map. */ - public const byte MapSortedMap = 3; - - /** Map: concurrent hash map. */ - public const byte MapConcurrentHashMap = 4; - + /** Byte "0". */ public const byte ByteZero = 0; @@ -1096,10 +1071,6 @@ public static void WriteCollection(ICollection val, BinaryWriter ctx) colType = CollectionArrayList; else if (genType == typeof (LinkedList<>)) colType = CollectionLinkedList; - else if (genType == typeof (SortedSet<>)) - colType = CollectionSortedSet; - else if (genType == typeof (ConcurrentBag<>)) - colType = CollectionConcurrentBag; else colType = CollectionCustom; } @@ -1147,10 +1118,6 @@ public static void WriteCollection(ICollection val, BinaryWriter ctx, byte colTy { if (colType == CollectionLinkedList) res = new LinkedList(); - else if (colType == CollectionSortedSet) - res = new SortedSet(); - else if (colType == CollectionConcurrentBag) - res = new ConcurrentBag(); else res = new ArrayList(len); } @@ -1181,14 +1148,7 @@ public static void WriteDictionary(IDictionary val, BinaryWriter ctx) { var genType = valType.GetGenericTypeDefinition(); - if (genType == typeof (Dictionary<,>)) - dictType = MapHashMap; - else if (genType == typeof (SortedDictionary<,>)) - dictType = MapSortedMap; - else if (genType == typeof (ConcurrentDictionary<,>)) - dictType = MapConcurrentHashMap; - else - dictType = MapCustom; + dictType = genType == typeof (Dictionary<,>) ? MapHashMap : MapCustom; } else dictType = valType == typeof (Hashtable) ? MapHashMap : MapCustom; @@ -1221,29 +1181,16 @@ public static void WriteDictionary(IDictionary val, BinaryWriter ctx, byte dictT * Factory delegate. * Dictionary. */ - public static IDictionary ReadDictionary(BinaryReader ctx, - DictionaryFactory factory) + public static IDictionary ReadDictionary(BinaryReader ctx, DictionaryFactory factory) { IBinaryStream stream = ctx.Stream; int len = stream.ReadInt(); - byte colType = ctx.Stream.ReadByte(); - - IDictionary res; - - if (factory == null) - { - if (colType == MapSortedMap) - res = new SortedDictionary(); - else if (colType == MapConcurrentHashMap) - res = new ConcurrentDictionary(Environment.ProcessorCount, len); - else - res = new Hashtable(len); - } - else - res = factory.Invoke(len); + // Skip dictionary type as we can do nothing with it here. + ctx.Stream.ReadByte(); + var res = factory == null ? new Hashtable(len) : factory.Invoke(len); for (int i = 0; i < len; i++) { @@ -1256,30 +1203,6 @@ public static void WriteDictionary(IDictionary val, BinaryWriter ctx, byte dictT return res; } - /** - * Write map entry. - * Write context. - * Value. - */ - public static void WriteMapEntry(BinaryWriter ctx, DictionaryEntry val) - { - ctx.Write(val.Key); - ctx.Write(val.Value); - } - - /** - * Read map entry. - * Context. - * Map entry. - */ - public static DictionaryEntry ReadMapEntry(BinaryReader ctx) - { - object key = ctx.Deserialize(); - object val = ctx.Deserialize(); - - return new DictionaryEntry(key, val); - } - /** * Write binary object. * Stream.