http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7c947cc/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedMarshallerUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedMarshallerUtils.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedMarshallerUtils.java new file mode 100644 index 0000000..fb163fd --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedMarshallerUtils.java @@ -0,0 +1,450 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.apache.ignite.marshaller.optimized; + +import org.gridgain.grid.util.*; +import org.gridgain.grid.util.typedef.*; +import org.jdk8.backport.*; +import org.jetbrains.annotations.*; +import sun.misc.*; + +import java.io.*; +import java.lang.reflect.*; +import java.nio.charset.*; +import java.security.*; +import java.util.*; +import java.util.concurrent.*; + +import static org.apache.ignite.marshaller.optimized.IgniteOptimizedMarshallable.*; + +/** + * Miscellaneous utility methods to facilitate {@link IgniteOptimizedMarshaller}. + */ +class IgniteOptimizedMarshallerUtils { + /** Unsafe. */ + private static final Unsafe UNSAFE = GridUnsafe.unsafe(); + + /** {@code Null} object reference. */ + static final byte NULL = (byte)0x70; + + /** Handle reference. */ + static final byte HANDLE = (byte)0x71; + + /** Object reference. */ + static final byte OBJECT = (byte)0x72; + + /** UTF-8 character name. */ + static final Charset UTF_8 = Charset.forName("UTF-8"); + + /** Class descriptors cache. */ + private static final ConcurrentMap<Class<?>, IgniteOptimizedClassDescriptor> CLS_DESC_CACHE = + new ConcurrentHashMap8<>(256); + + /** Classes cache by name. */ + private static final ConcurrentHashMap8<ClassLoader, ConcurrentHashMap8<String, Class<?>>> CLS_BY_NAME_CACHE = + new ConcurrentHashMap8<>(); + + /** + * Suppresses default constructor, ensuring non-instantiability. + */ + private IgniteOptimizedMarshallerUtils() { + // No-op. + } + + /** + * Gets class for given name and class loader. + * + * @param name Class name. + * @param ldr Class loader. + * @return Class. + * @throws ClassNotFoundException If class was not found. + */ + static Class<?> forName(String name, ClassLoader ldr) throws ClassNotFoundException { + assert ldr != null; + assert name != null; + + ConcurrentHashMap8<String, Class<?>> cache = CLS_BY_NAME_CACHE.get(ldr); + + Class<?> cls = null; + + if (cache == null) { + cache = new ConcurrentHashMap8<>(); + + ConcurrentHashMap8<String, Class<?>> old = CLS_BY_NAME_CACHE.putIfAbsent(ldr, cache); + + if (old != null) { + cache = old; + + cls = cache.get(name); + } + } + else + cls = cache.get(name); + + if (cls == null) { + cls = Class.forName(name, true, ldr); + + cache.put(name, cls); + } + + return cls; + } + + /** + * Gets descriptor for provided class. + * + * @param cls Class. + * @param obj Object. + * @return Descriptor. + * @throws IOException In case of error. + */ + static IgniteOptimizedClassDescriptor classDescriptor(Class<?> cls, @Nullable Object obj) throws IOException { + if (obj != null) { + if (obj instanceof IgniteOptimizedMarshallable) { + IgniteOptimizedMarshallable m = (IgniteOptimizedMarshallable)obj; + + Object clsId = m.ggClassId(); + + if (clsId != null && !(clsId instanceof IgniteOptimizedClassDescriptor)) + throw new IOException("Method '" + obj.getClass().getName() + ".ggClassId() must return " + + "the value of the field '" + CLS_ID_FIELD_NAME + "'."); + + IgniteOptimizedClassDescriptor desc = (IgniteOptimizedClassDescriptor)clsId; + + if (desc == null) { + desc = new IgniteOptimizedClassDescriptor(cls); + + try { + Field field = obj.getClass().getDeclaredField(CLS_ID_FIELD_NAME); + + field.setAccessible(true); + + Object o = field.get(null); + + if (o == null) { + if ((field.getModifiers() & Modifier.STATIC) == 0) + throw new IOException("Field '" + CLS_ID_FIELD_NAME + "' must be declared static: " + + obj.getClass().getName()); + + field.set(null, desc); + + if (m.ggClassId() == null) + throw new IOException( "Method '" + obj.getClass().getName() + ".ggClassId() must " + + "return the value of the field '" + CLS_ID_FIELD_NAME + "': " + + obj.getClass().getName()); + } + else if (!(o instanceof IgniteOptimizedClassDescriptor)) + throw new IOException("Field '" + CLS_ID_FIELD_NAME + "' must be declared with " + + "null value: " + obj.getClass().getName()); + } + catch (NoSuchFieldException e) { + throw new IOException("GridOptimizedMarshallable classes must have static field declared " + + "[fieldName=" + CLS_ID_FIELD_NAME + ", cls=" + obj.getClass().getName() + ']', e); + } + catch (IllegalAccessException e) { + throw new IOException("Failed to set field '" + CLS_ID_FIELD_NAME + "' on '" + + obj.getClass().getName() + "' class.", e); + } + } + + return desc; + } + } + + IgniteOptimizedClassDescriptor desc = CLS_DESC_CACHE.get(cls); + + if (desc == null) { + IgniteOptimizedClassDescriptor existing = CLS_DESC_CACHE.putIfAbsent(cls, + desc = new IgniteOptimizedClassDescriptor(cls)); + + if (existing != null) + desc = existing; + } + + return desc; + } + + /** + * Undeployment callback. + * + * @param ldr Undeployed class loader. + */ + public static void onUndeploy(ClassLoader ldr) { + CLS_BY_NAME_CACHE.remove(ldr); + + for (Class<?> cls : CLS_DESC_CACHE.keySet()) { + if (ldr.equals(cls.getClassLoader())) + CLS_DESC_CACHE.remove(cls); + } + } + + /** + * Intended for test purposes only. + */ + public static void clearCache() { + CLS_BY_NAME_CACHE.clear(); + CLS_DESC_CACHE.clear(); + } + + /** + * + */ + public static void printMemoryStats() { + X.println(">>>"); + X.println(">>> GridOptimizedMarshallerUtils memory stats:"); + X.println(" Cache size: " + CLS_DESC_CACHE.size()); + + for (Map.Entry<Class<?>, IgniteOptimizedClassDescriptor> e : CLS_DESC_CACHE.entrySet()) + X.println(" " + e.getKey() + " : " + e.getValue()); + } + + /** + * Computes the serial version UID value for the given class. + * The code is taken from {@link ObjectStreamClass#computeDefaultSUID(Class)}. + * + * @param cls A class. + * @param fields Fields. + * @return A serial version UID. + * @throws IOException If failed. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + static Long computeSerialVersionUid(Class cls, List<Field> fields) throws IOException { + if (Serializable.class.isAssignableFrom(cls) && !Enum.class.isAssignableFrom(cls)) + return ObjectStreamClass.lookup(cls).getSerialVersionUID(); + + MessageDigest md; + + try { + md = MessageDigest.getInstance("SHA"); + } + catch (NoSuchAlgorithmException e) { + throw new IOException("Failed to get digest for SHA.", e); + } + + md.update(cls.getName().getBytes(UTF_8)); + + if (!F.isEmpty(fields)) { + for (int i = 0; i < fields.size(); i++) { + Field f = fields.get(i); + + md.update(f.getName().getBytes(UTF_8)); + md.update(f.getType().getName().getBytes(UTF_8)); + } + } + + byte[] hashBytes = md.digest(); + + long hash = 0; + + // Composes a single-long hash from the byte[] hash. + for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) + hash = (hash << 8) | (hashBytes[i] & 0xFF); + + return hash; + } + + /** + * Gets byte field value. + * + * @param obj Object. + * @param off Field offset. + * @return Byte value. + */ + static byte getByte(Object obj, long off) { + return UNSAFE.getByte(obj, off); + } + + /** + * Sets byte field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setByte(Object obj, long off, byte val) { + UNSAFE.putByte(obj, off, val); + } + + /** + * Gets short field value. + * + * @param obj Object. + * @param off Field offset. + * @return Short value. + */ + static short getShort(Object obj, long off) { + return UNSAFE.getShort(obj, off); + } + + /** + * Sets short field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setShort(Object obj, long off, short val) { + UNSAFE.putShort(obj, off, val); + } + + /** + * Gets integer field value. + * + * @param obj Object. + * @param off Field offset. + * @return Integer value. + */ + static int getInt(Object obj, long off) { + return UNSAFE.getInt(obj, off); + } + + /** + * Sets integer field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setInt(Object obj, long off, int val) { + UNSAFE.putInt(obj, off, val); + } + + /** + * Gets long field value. + * + * @param obj Object. + * @param off Field offset. + * @return Long value. + */ + static long getLong(Object obj, long off) { + return UNSAFE.getLong(obj, off); + } + + /** + * Sets long field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setLong(Object obj, long off, long val) { + UNSAFE.putLong(obj, off, val); + } + + /** + * Gets float field value. + * + * @param obj Object. + * @param off Field offset. + * @return Float value. + */ + static float getFloat(Object obj, long off) { + return UNSAFE.getFloat(obj, off); + } + + /** + * Sets float field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setFloat(Object obj, long off, float val) { + UNSAFE.putFloat(obj, off, val); + } + + /** + * Gets double field value. + * + * @param obj Object. + * @param off Field offset. + * @return Double value. + */ + static double getDouble(Object obj, long off) { + return UNSAFE.getDouble(obj, off); + } + + /** + * Sets double field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setDouble(Object obj, long off, double val) { + UNSAFE.putDouble(obj, off, val); + } + + /** + * Gets char field value. + * + * @param obj Object. + * @param off Field offset. + * @return Char value. + */ + static char getChar(Object obj, long off) { + return UNSAFE.getChar(obj, off); + } + + /** + * Sets char field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setChar(Object obj, long off, char val) { + UNSAFE.putChar(obj, off, val); + } + + /** + * Gets boolean field value. + * + * @param obj Object. + * @param off Field offset. + * @return Boolean value. + */ + static boolean getBoolean(Object obj, long off) { + return UNSAFE.getBoolean(obj, off); + } + + /** + * Sets boolean field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setBoolean(Object obj, long off, boolean val) { + UNSAFE.putBoolean(obj, off, val); + } + + /** + * Gets field value. + * + * @param obj Object. + * @param off Field offset. + * @return Value. + */ + static Object getObject(Object obj, long off) { + return UNSAFE.getObject(obj, off); + } + + /** + * Sets field value. + * + * @param obj Object. + * @param off Field offset. + * @param val Value. + */ + static void setObject(Object obj, long off, Object val) { + UNSAFE.putObject(obj, off, val); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7c947cc/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectInputStream.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectInputStream.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectInputStream.java new file mode 100644 index 0000000..b3362de --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectInputStream.java @@ -0,0 +1,1008 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.apache.ignite.marshaller.optimized; + +import org.apache.ignite.lang.*; +import org.gridgain.grid.util.*; +import org.gridgain.grid.util.io.*; +import org.gridgain.grid.util.typedef.*; +import org.gridgain.grid.util.typedef.internal.*; +import sun.misc.*; + +import java.io.*; +import java.lang.reflect.*; +import java.util.*; + +import static org.apache.ignite.marshaller.optimized.IgniteOptimizedMarshallerUtils.*; + +/** + * Optimized object input stream. + */ +class IgniteOptimizedObjectInputStream extends ObjectInputStream { + /** Unsafe. */ + private static final Unsafe UNSAFE = GridUnsafe.unsafe(); + + /** Dummy object for HashSet. */ + private static final Object DUMMY = new Object(); + + /** */ + private final HandleTable handles = new HandleTable(10); + + /** */ + private ClassLoader clsLdr; + + /** */ + private GridDataInput in; + + /** */ + private Object curObj; + + /** */ + private List<T2<IgniteOptimizedFieldType, Long>> curFields; + + /** */ + private List<IgniteBiTuple<Integer, IgniteOptimizedFieldType>> curFieldInfoList; + + /** */ + private Map<String, IgniteBiTuple<Integer, IgniteOptimizedFieldType>> curFieldInfoMap; + + /** */ + private Class<?> curCls; + + /** + * @param in Input. + * @throws IOException In case of error. + */ + IgniteOptimizedObjectInputStream(GridDataInput in) throws IOException { + this.in = in; + } + + /** + * @throws IOException In case of error. + */ + IgniteOptimizedObjectInputStream() throws IOException { + // No-op. + } + + /** + * @param clsLdr Class loader. + */ + void classLoader(ClassLoader clsLdr) { + this.clsLdr = clsLdr; + } + + /** + * @return Class loader. + */ + ClassLoader classLoader() { + return clsLdr; + } + + /** + * @return Input. + */ + public GridDataInput in() { + return in; + } + + /** + * @param in Input. + */ + public void in(GridDataInput in) { + this.in = in; + } + + /** {@inheritDoc} */ + @Override public void close() throws IOException { + reset(); + } + + /** {@inheritDoc} */ + @SuppressWarnings("NonSynchronizedMethodOverridesSynchronizedMethod") + @Override public void reset() throws IOException { + in.reset(); + handles.clear(); + + curObj = null; + curFields = null; + curFieldInfoList = null; + curFieldInfoMap = null; + } + + /** {@inheritDoc} */ + @Override public Object readObjectOverride() throws ClassNotFoundException, IOException { + curObj = null; + curFields = null; + curFieldInfoList = null; + curFieldInfoMap = null; + + byte ref = in.readByte(); + + switch (ref) { + case NULL: + return null; + + case HANDLE: + return handles.lookup(readInt()); + + case OBJECT: + IgniteOptimizedClassDescriptor desc = IgniteOptimizedClassResolver.readClass(this, clsLdr); + + curCls = desc.describedClass(); + + return desc.read(this); + + default: + SB msg = new SB("Unexpected error occurred during unmarshalling"); + + if (curCls != null) + msg.a(" of an instance of the class: ").a(curCls.getName()); + + msg.a(". Check that all nodes are running the same version of GridGain and that all nodes have " + + "GridOptimizedMarshaller configured with identical optimized classes lists, if any " + + "(see setClassNames and setClassNamesPath methods). If your serialized classes implement " + + "java.io.Externalizable interface, verify that serialization logic is correct."); + + throw new IOException(msg.toString()); + } + } + + /** + * Reads array from this stream. + * + * @param compType Array component type. + * @return Array. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + <T> T[] readArray(Class<T> compType) throws ClassNotFoundException, IOException { + int len = in.readInt(); + + T[] arr = (T[])Array.newInstance(compType, len); + + handles.assign(arr); + + for (int i = 0; i < len; i++) + arr[i] = (T)readObject(); + + return arr; + } + + /** + * Reads {@link UUID} from this stream. + * + * @return UUID. + * @throws IOException In case of error. + */ + UUID readUuid() throws IOException { + UUID uuid = new UUID(readLong(), readLong()); + + handles.assign(uuid); + + return uuid; + } + + /** + * Reads {@link Properties} from this stream. + * + * @return Properties. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + Properties readProperties() throws ClassNotFoundException, IOException { + Properties dflts = readBoolean() ? null : (Properties)readObject(); + + Properties props = new Properties(dflts); + + int size = in.readInt(); + + for (int i = 0; i < size; i++) + props.setProperty(readUTF(), readUTF()); + + handles.assign(props); + + return props; + } + + /** + * Reads and sets all non-static and non-transient field values from this stream. + * + * @param obj Object. + * @param fieldOffs Field offsets. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + void readFields(Object obj, List<T2<IgniteOptimizedFieldType, Long>> fieldOffs) throws ClassNotFoundException, + IOException { + for (int i = 0; i < fieldOffs.size(); i++) { + T2<IgniteOptimizedFieldType, Long> t = fieldOffs.get(i); + + switch ((t.get1())) { + case BYTE: + setByte(obj, t.get2(), readByte()); + + break; + + case SHORT: + setShort(obj, t.get2(), readShort()); + + break; + + case INT: + setInt(obj, t.get2(), readInt()); + + break; + + case LONG: + setLong(obj, t.get2(), readLong()); + + break; + + case FLOAT: + setFloat(obj, t.get2(), readFloat()); + + break; + + case DOUBLE: + setDouble(obj, t.get2(), readDouble()); + + break; + + case CHAR: + setChar(obj, t.get2(), readChar()); + + break; + + case BOOLEAN: + setBoolean(obj, t.get2(), readBoolean()); + + break; + + case OTHER: + setObject(obj, t.get2(), readObject()); + } + } + } + + /** + * Reads {@link Externalizable} object. + * + * @param constructor Constructor. + * @param readResolveMtd {@code readResolve} method. + * @return Object. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + Object readExternalizable(Constructor<?> constructor, Method readResolveMtd) + throws ClassNotFoundException, IOException { + Object obj; + + try { + obj = constructor.newInstance(); + } + catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { + throw new IOException(e); + } + + int handle = handles.assign(obj); + + Externalizable extObj = ((Externalizable)obj); + + extObj.readExternal(this); + + if (readResolveMtd != null) { + try { + obj = readResolveMtd.invoke(obj); + + handles.set(handle, obj); + } + catch (IllegalAccessException | InvocationTargetException e) { + throw new IOException(e); + } + } + + return obj; + } + + /** + * Reads serializable object. + * + * @param cls Class. + * @param mtds {@code readObject} methods. + * @param readResolveMtd {@code readResolve} method. + * @param fields class fields details. + * @return Object. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + Object readSerializable(Class<?> cls, List<Method> mtds, Method readResolveMtd, + IgniteOptimizedClassDescriptor.Fields fields) throws ClassNotFoundException, IOException { + Object obj; + + try { + obj = UNSAFE.allocateInstance(cls); + } + catch (InstantiationException e) { + throw new IOException(e); + } + + int handle = handles.assign(obj); + + for (int i = 0; i < mtds.size(); i++) { + Method mtd = mtds.get(i); + + if (mtd != null) { + curObj = obj; + curFields = fields.fieldOffs(i); + curFieldInfoList = fields.fieldInfoList(i); + curFieldInfoMap = fields.fieldInfoMap(i); + + try { + mtd.invoke(obj, this); + } + catch (IllegalAccessException | InvocationTargetException e) { + throw new IOException(e); + } + } + else + readFields(obj, fields.fieldOffs(i)); + } + + if (readResolveMtd != null) { + try { + obj = readResolveMtd.invoke(obj); + + handles.set(handle, obj); + } + catch (IllegalAccessException | InvocationTargetException e) { + throw new IOException(e); + } + } + + return obj; + } + + /** + * Reads {@link ArrayList}. + * + * @return List. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + ArrayList<?> readArrayList() throws ClassNotFoundException, IOException { + int size = readInt(); + + ArrayList<Object> list = new ArrayList<>(size); + + handles.assign(list); + + for (int i = 0; i < size; i++) + list.add(readObject()); + + return list; + } + + /** + * Reads {@link HashMap}. + * + * @param set Whether reading underlying map from {@link HashSet}. + * @return Map. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + HashMap<?, ?> readHashMap(boolean set) throws ClassNotFoundException, IOException { + int size = readInt(); + float loadFactor = readFloat(); + + HashMap<Object, Object> map = new HashMap<>(size, loadFactor); + + if (!set) + handles.assign(map); + + for (int i = 0; i < size; i++) { + Object key = readObject(); + Object val = !set ? readObject() : DUMMY; + + map.put(key, val); + } + + return map; + } + + /** + * Reads {@link HashSet}. + * + * @param mapFieldOff Map field offset. + * @return Set. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + HashSet<?> readHashSet(long mapFieldOff) throws ClassNotFoundException, IOException { + try { + HashSet<Object> set = (HashSet<Object>)UNSAFE.allocateInstance(HashSet.class); + + handles.assign(set); + + setObject(set, mapFieldOff, readHashMap(true)); + + return set; + } + catch (InstantiationException e) { + throw new IOException(e); + } + } + + /** + * Reads {@link LinkedList}. + * + * @return List. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + LinkedList<?> readLinkedList() throws ClassNotFoundException, IOException { + int size = readInt(); + + LinkedList<Object> list = new LinkedList<>(); + + handles.assign(list); + + for (int i = 0; i < size; i++) + list.add(readObject()); + + return list; + } + + /** + * Reads {@link LinkedHashMap}. + * + * @param set Whether reading underlying map from {@link LinkedHashSet}. + * @return Map. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + LinkedHashMap<?, ?> readLinkedHashMap(boolean set) throws ClassNotFoundException, IOException { + int size = readInt(); + float loadFactor = readFloat(); + boolean accessOrder = readBoolean(); + + LinkedHashMap<Object, Object> map = new LinkedHashMap<>(size, loadFactor, accessOrder); + + if (!set) + handles.assign(map); + + for (int i = 0; i < size; i++) { + Object key = readObject(); + Object val = !set ? readObject() : DUMMY; + + map.put(key, val); + } + + return map; + } + + /** + * Reads {@link LinkedHashSet}. + * + * @param mapFieldOff Map field offset. + * @return Set. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + LinkedHashSet<?> readLinkedHashSet(long mapFieldOff) throws ClassNotFoundException, IOException { + try { + LinkedHashSet<Object> set = (LinkedHashSet<Object>)UNSAFE.allocateInstance(LinkedHashSet.class); + + handles.assign(set); + + setObject(set, mapFieldOff, readLinkedHashMap(true)); + + return set; + } + catch (InstantiationException e) { + throw new IOException(e); + } + } + + /** + * Reads {@link Date}. + * + * @return Date. + * @throws ClassNotFoundException If class not found. + * @throws IOException In case of error. + */ + Date readDate() throws ClassNotFoundException, IOException { + Date date = new Date(readLong()); + + handles.assign(date); + + return date; + } + + /** + * Reads array of {@code byte}s. + * + * @return Array. + * @throws IOException In case of error. + */ + byte[] readByteArray() throws IOException { + byte[] arr = in.readByteArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code short}s. + * + * @return Array. + * @throws IOException In case of error. + */ + short[] readShortArray() throws IOException { + short[] arr = in.readShortArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code int}s. + * + * @return Array. + * @throws IOException In case of error. + */ + int[] readIntArray() throws IOException { + int[] arr = in.readIntArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code long}s. + * + * @return Array. + * @throws IOException In case of error. + */ + long[] readLongArray() throws IOException { + long[] arr = in.readLongArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code float}s. + * + * @return Array. + * @throws IOException In case of error. + */ + float[] readFloatArray() throws IOException { + float[] arr = in.readFloatArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code double}s. + * + * @return Array. + * @throws IOException In case of error. + */ + double[] readDoubleArray() throws IOException { + double[] arr = in.readDoubleArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code char}s. + * + * @return Array. + * @throws IOException In case of error. + */ + char[] readCharArray() throws IOException { + char[] arr = in.readCharArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads array of {@code boolean}s. + * + * @return Array. + * @throws IOException In case of error. + */ + boolean[] readBooleanArray() throws IOException { + boolean[] arr = in.readBooleanArray(); + + handles.assign(arr); + + return arr; + } + + /** + * Reads {@link String}. + * + * @return String. + * @throws IOException In case of error. + */ + public String readString() throws IOException { + String str = in.readUTF(); + + handles.assign(str); + + return str; + } + + /** {@inheritDoc} */ + @Override public void readFully(byte[] b) throws IOException { + in.readFully(b); + } + + /** {@inheritDoc} */ + @Override public void readFully(byte[] b, int off, int len) throws IOException { + in.readFully(b, off, len); + } + + /** {@inheritDoc} */ + @Override public int skipBytes(int n) throws IOException { + return in.skipBytes(n); + } + + /** {@inheritDoc} */ + @Override public boolean readBoolean() throws IOException { + return in.readBoolean(); + } + + /** {@inheritDoc} */ + @Override public byte readByte() throws IOException { + return in.readByte(); + } + + /** {@inheritDoc} */ + @Override public int readUnsignedByte() throws IOException { + return in.readUnsignedByte(); + } + + /** {@inheritDoc} */ + @Override public short readShort() throws IOException { + return in.readShort(); + } + + /** {@inheritDoc} */ + @Override public int readUnsignedShort() throws IOException { + return in.readUnsignedShort(); + } + + /** {@inheritDoc} */ + @Override public char readChar() throws IOException { + return in.readChar(); + } + + /** {@inheritDoc} */ + @Override public int readInt() throws IOException { + return in.readInt(); + } + + /** {@inheritDoc} */ + @Override public long readLong() throws IOException { + return in.readLong(); + } + + /** {@inheritDoc} */ + @Override public float readFloat() throws IOException { + return in.readFloat(); + } + + /** {@inheritDoc} */ + @Override public double readDouble() throws IOException { + return in.readDouble(); + } + + /** {@inheritDoc} */ + @Override public int read() throws IOException { + return in.read(); + } + + /** {@inheritDoc} */ + @Override public int read(byte[] b) throws IOException { + return in.read(b); + } + + /** {@inheritDoc} */ + @Override public int read(byte[] b, int off, int len) throws IOException { + return in.read(b, off, len); + } + + /** {@inheritDoc} */ + @SuppressWarnings("deprecation") + @Override public String readLine() throws IOException { + return in.readLine(); + } + + /** {@inheritDoc} */ + @Override public String readUTF() throws IOException { + return in.readUTF(); + } + + /** {@inheritDoc} */ + @Override public Object readUnshared() throws IOException, ClassNotFoundException { + return readObject(); + } + + /** {@inheritDoc} */ + @Override public void defaultReadObject() throws IOException, ClassNotFoundException { + if (curObj == null) + throw new NotActiveException("Not in readObject() call."); + + readFields(curObj, curFields); + } + + /** {@inheritDoc} */ + @Override public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException { + if (curObj == null) + throw new NotActiveException("Not in readObject() call."); + + return new GetFieldImpl(this); + } + + /** {@inheritDoc} */ + @Override public void registerValidation(ObjectInputValidation obj, int pri) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public int available() throws IOException { + return -1; + } + + /** + * Returns objects that were added to handles table. + * Used ONLY for test purposes. + * + * @return Handled objects. + */ + Object[] handledObjects() { + return handles.entries; + } + + /** + * Lightweight identity hash table which maps objects to integer handles, + * assigned in ascending order. + */ + private static class HandleTable { + /** Array mapping handle -> object/exception (depending on status). */ + private Object[] entries; + + /** Number of handles in table. */ + private int size; + + /** + * Creates handle table with the given initial capacity. + * + * @param initCap Initial capacity. + */ + HandleTable(int initCap) { + entries = new Object[initCap]; + } + + /** + * Assigns next available handle to given object, and returns assigned + * handle. + * + * @param obj Object. + * @return Handle. + */ + int assign(Object obj) { + if (size >= entries.length) + grow(); + + entries[size] = obj; + + return size++; + } + + /** + * Assigns new object to existing handle. Old object is forgotten. + * + * @param handle Handle. + * @param obj Object. + */ + void set(int handle, Object obj) { + entries[handle] = obj; + } + + /** + * Looks up and returns object associated with the given handle. + * + * @param handle Handle. + * @return Object. + */ + Object lookup(int handle) { + return entries[handle]; + } + + /** + * Resets table to its initial state. + */ + void clear() { + Arrays.fill(entries, 0, size, null); + + size = 0; + } + + /** + * Expands capacity of internal arrays. + */ + private void grow() { + int newCap = (entries.length << 1) + 1; + + Object[] newEntries = new Object[newCap]; + + System.arraycopy(entries, 0, newEntries, 0, size); + + entries = newEntries; + } + } + + /** + * {@link GetField} implementation. + */ + private static class GetFieldImpl extends GetField { + /** Field info map. */ + private final Map<String, IgniteBiTuple<Integer, IgniteOptimizedFieldType>> fieldInfoMap; + + /** Values. */ + private final Object[] objs; + + /** + * @param in Stream. + * @throws IOException In case of error. + * @throws ClassNotFoundException If class not found. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + private GetFieldImpl(IgniteOptimizedObjectInputStream in) throws IOException, ClassNotFoundException { + fieldInfoMap = in.curFieldInfoMap; + + List<IgniteBiTuple<Integer, IgniteOptimizedFieldType>> infos = in.curFieldInfoList; + + objs = new Object[infos.size()]; + + for (int i = 0; i < infos.size(); i++) { + IgniteBiTuple<Integer, IgniteOptimizedFieldType> t = infos.get(i); + + Object obj = null; + + switch (t.get2()) { + case BYTE: + obj = in.readByte(); + + break; + + case SHORT: + obj = in.readShort(); + + break; + + case INT: + obj = in.readInt(); + + break; + + case LONG: + obj = in.readLong(); + + break; + + case FLOAT: + obj = in.readFloat(); + + break; + + case DOUBLE: + obj = in.readDouble(); + + break; + + case CHAR: + obj = in.readChar(); + + break; + + case BOOLEAN: + obj = in.readBoolean(); + + break; + + case OTHER: + obj = in.readObject(); + } + + objs[t.get1()] = obj; + } + } + + /** {@inheritDoc} */ + @Override public ObjectStreamClass getObjectStreamClass() { + throw new UnsupportedOperationException(); + } + + /** {@inheritDoc} */ + @Override public boolean defaulted(String name) throws IOException { + return objs[fieldInfoMap.get(name).get1()] == null; + } + + /** {@inheritDoc} */ + @Override public boolean get(String name, boolean dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public byte get(String name, byte dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public char get(String name, char dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public short get(String name, short dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public int get(String name, int dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public long get(String name, long dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public float get(String name, float dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public double get(String name, double dflt) throws IOException { + return value(name, dflt); + } + + /** {@inheritDoc} */ + @Override public Object get(String name, Object dflt) throws IOException { + return value(name, dflt); + } + + /** + * @param name Field name. + * @param dflt Default value. + * @return Value. + */ + private <T> T value(String name, T dflt) { + return objs[fieldInfoMap.get(name).get1()] != null ? (T)objs[fieldInfoMap.get(name).get1()] : dflt; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7c947cc/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectOutputStream.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectOutputStream.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectOutputStream.java new file mode 100644 index 0000000..1156bdd --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectOutputStream.java @@ -0,0 +1,831 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.apache.ignite.marshaller.optimized; + +import org.apache.ignite.lang.*; +import org.gridgain.grid.util.*; +import org.gridgain.grid.util.io.*; +import org.gridgain.grid.util.typedef.*; + +import java.io.*; +import java.lang.reflect.*; +import java.util.*; + +import static org.apache.ignite.marshaller.optimized.IgniteOptimizedMarshallerUtils.*; + +/** + * Optimized object output stream. + */ +class IgniteOptimizedObjectOutputStream extends ObjectOutputStream { + /** */ + private static final Collection<String> CONVERTED_ERR = F.asList( + "weblogic/management/ManagementException", + "Externalizable class doesn't have default constructor: class " + + "org.gridgain.grid.kernal.processors.email.GridEmailProcessor$2" + ); + + /** */ + private final GridHandleTable handles = new GridHandleTable(10, 3.00f); + + /** */ + private boolean requireSer; + + /** */ + private GridDataOutput out; + + /** */ + private Object curObj; + + /** */ + private List<T2<IgniteOptimizedFieldType, Long>> curFields; + + /** */ + private Map<String, IgniteBiTuple<Integer, IgniteOptimizedFieldType>> curFieldInfoMap; + + /** */ + private PutFieldImpl curPut; + + + /** + * @throws IOException In case of error. + */ + IgniteOptimizedObjectOutputStream() throws IOException { + // No-op. + } + + /** + * @param out Output. + * @throws IOException In case of error. + */ + IgniteOptimizedObjectOutputStream(GridDataOutput out) throws IOException { + this.out = out; + } + + /** + * @param requireSer Require {@link Serializable} flag. + */ + void requireSerializable(boolean requireSer) { + this.requireSer = requireSer; + } + + /** + * @return Require {@link Serializable} flag. + */ + boolean requireSerializable() { + return requireSer; + } + + /** + * @param out Output. + */ + public void out(GridDataOutput out) { + this.out = out; + } + + /** + * @return Output. + */ + public GridDataOutput out() { + return out; + } + + /** {@inheritDoc} */ + @Override public void close() throws IOException { + reset(); + } + + /** {@inheritDoc} */ + @Override public void write(byte[] b) throws IOException { + out.write(b); + } + + /** {@inheritDoc} */ + @Override public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + } + + /** {@inheritDoc} */ + @Override protected void writeObjectOverride(Object obj) throws IOException { + try { + writeObject0(obj); + } + catch (IOException e) { + Throwable t = e; + + do { + if (CONVERTED_ERR.contains(t.getMessage())) + throw new IOException("You are trying to serialize internal classes that are not supposed " + + "to be serialized. Check that all non-serializable fields are transient. Consider using " + + "static inner classes instead of non-static inner classes and anonymous classes.", e); + } + while ((t = t.getCause()) != null); + + throw e; + } + } + + /** + * Writes object to stream. + * + * @param obj Object. + * @throws IOException In case of error. + */ + private void writeObject0(Object obj) throws IOException { + curObj = null; + curFields = null; + curPut = null; + curFieldInfoMap = null; + + if (obj == null) + writeByte(NULL); + else { + Class<?> cls = obj.getClass(); + + IgniteOptimizedClassDescriptor desc = classDescriptor(cls, obj); + + if (desc.excluded()) { + writeByte(NULL); + + return; + } + + Object obj0 = desc.replace(obj); + + if (obj0 == null) { + writeByte(NULL); + + return; + } + + int handle = -1; + + if (!desc.isPrimitive() && !desc.isEnum() && !desc.isClass()) + handle = handles.lookup(obj); + + if (obj0 != obj) { + obj = obj0; + + desc = classDescriptor(obj.getClass(), obj); + } + + if (handle >= 0) { + writeByte(HANDLE); + writeInt(handle); + } + else { + writeByte(OBJECT); + + IgniteOptimizedClassResolver.writeClass(this, desc); + + desc.write(this, obj); + } + } + } + + /** + * Writes array to this stream. + * + * @param arr Array. + * @throws IOException In case of error. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + void writeArray(Object[] arr) throws IOException { + int len = arr.length; + + writeInt(len); + + for (int i = 0; i < len; i++) { + Object obj = arr[i]; + + writeObject0(obj); + } + } + + /** + * Writes {@link UUID} to this stream. + * + * @param uuid UUID. + * @throws IOException In case of error. + */ + void writeUuid(UUID uuid) throws IOException { + writeLong(uuid.getMostSignificantBits()); + writeLong(uuid.getLeastSignificantBits()); + } + + /** + * Writes {@link Properties} to this stream. + * + * @param props Properties. + * @param dfltsFieldOff Defaults field offset. + * @throws IOException In case of error. + */ + void writeProperties(Properties props, long dfltsFieldOff) throws IOException { + Properties dflts = (Properties)getObject(props, dfltsFieldOff); + + if (dflts == null) + writeBoolean(true); + else { + writeBoolean(false); + + writeObject0(dflts); + } + + Set<String> names = props.stringPropertyNames(); + + writeInt(names.size()); + + for (String name : names) { + writeUTF(name); + writeUTF(props.getProperty(name)); + } + } + + /** + * Writes externalizable object. + * + * @param obj Object. + * @throws IOException In case of error. + */ + void writeExternalizable(Object obj) throws IOException { + Externalizable extObj = (Externalizable)obj; + + extObj.writeExternal(this); + } + + /** + * Writes serializable object. + * + * @param obj Object. + * @param mtds {@code writeObject} methods. + * @param fields class fields details. + * @throws IOException In case of error. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + void writeSerializable(Object obj, List<Method> mtds, IgniteOptimizedClassDescriptor.Fields fields) + throws IOException { + for (int i = 0; i < mtds.size(); i++) { + Method mtd = mtds.get(i); + + if (mtd != null) { + curObj = obj; + curFields = fields.fieldOffs(i); + curFieldInfoMap = fields.fieldInfoMap(i); + + try { + mtd.invoke(obj, this); + } + catch (IllegalAccessException e) { + throw new IOException(e); + } + catch (InvocationTargetException e) { + throw new IOException(e.getCause()); + } + } + else + writeFields(obj, fields.fieldOffs(i)); + } + } + + /** + * Writes {@link ArrayList}. + * + * @param list List. + * @throws IOException In case of error. + */ + @SuppressWarnings({"ForLoopReplaceableByForEach", "TypeMayBeWeakened"}) + void writeArrayList(ArrayList<?> list) throws IOException { + int size = list.size(); + + writeInt(size); + + for (int i = 0; i < size; i++) + writeObject0(list.get(i)); + } + + /** + * Writes {@link HashMap}. + * + * @param map Map. + * @param loadFactorFieldOff Load factor field offset. + * @param set Whether writing underlying map from {@link HashSet}. + * @throws IOException In case of error. + */ + @SuppressWarnings("TypeMayBeWeakened") + void writeHashMap(HashMap<?, ?> map, long loadFactorFieldOff, boolean set) throws IOException { + int size = map.size(); + + writeInt(size); + writeFloat(getFloat(map, loadFactorFieldOff)); + + for (Map.Entry<?, ?> e : map.entrySet()) { + writeObject0(e.getKey()); + + if (!set) + writeObject0(e.getValue()); + } + } + + /** + * Writes {@link HashSet}. + * + * @param set Set. + * @param mapFieldOff Map field offset. + * @param loadFactorFieldOff Load factor field offset. + * @throws IOException In case of error. + */ + void writeHashSet(HashSet<?> set, long mapFieldOff, long loadFactorFieldOff) throws IOException { + writeHashMap((HashMap<?, ?>)getObject(set, mapFieldOff), loadFactorFieldOff, true); + } + + /** + * Writes {@link LinkedList}. + * + * @param list List. + * @throws IOException In case of error. + */ + @SuppressWarnings("TypeMayBeWeakened") + void writeLinkedList(LinkedList<?> list) throws IOException { + int size = list.size(); + + writeInt(size); + + for (Object obj : list) + writeObject0(obj); + } + + /** + * Writes {@link LinkedHashMap}. + * + * @param map Map. + * @param loadFactorFieldOff Load factor field offset. + * @param accessOrderFieldOff access order field offset. + * @param set Whether writing underlying map from {@link LinkedHashSet}. + * @throws IOException In case of error. + */ + @SuppressWarnings("TypeMayBeWeakened") + void writeLinkedHashMap(LinkedHashMap<?, ?> map, long loadFactorFieldOff, long accessOrderFieldOff, boolean set) + throws IOException { + int size = map.size(); + + writeInt(size); + writeFloat(getFloat(map, loadFactorFieldOff)); + + if (accessOrderFieldOff >= 0) + writeBoolean(getBoolean(map, accessOrderFieldOff)); + else + writeBoolean(false); + + for (Map.Entry<?, ?> e : map.entrySet()) { + writeObject0(e.getKey()); + + if (!set) + writeObject0(e.getValue()); + } + } + + /** + * Writes {@link LinkedHashSet}. + * + * @param set Set. + * @param mapFieldOff Map field offset. + * @param loadFactorFieldOff Load factor field offset. + * @throws IOException In case of error. + */ + void writeLinkedHashSet(LinkedHashSet<?> set, long mapFieldOff, long loadFactorFieldOff) throws IOException { + LinkedHashMap<?, ?> map = (LinkedHashMap<?, ?>)getObject(set, mapFieldOff); + + writeLinkedHashMap(map, loadFactorFieldOff, -1, true); + } + + /** + * Writes {@link Date}. + * + * @param date Date. + * @throws IOException In case of error. + */ + void writeDate(Date date) throws IOException { + writeLong(date.getTime()); + } + + /** + * Writes all non-static and non-transient field values to this stream. + * + * @param obj Object. + * @param fieldOffs Field offsets. + * @throws IOException In case of error. + */ + @SuppressWarnings("ForLoopReplaceableByForEach") + private void writeFields(Object obj, List<T2<IgniteOptimizedFieldType, Long>> fieldOffs) throws IOException { + for (int i = 0; i < fieldOffs.size(); i++) { + T2<IgniteOptimizedFieldType, Long> t = fieldOffs.get(i); + + switch (t.get1()) { + case BYTE: + writeByte(getByte(obj, t.get2())); + + break; + + case SHORT: + writeShort(getShort(obj, t.get2())); + + break; + + case INT: + writeInt(getInt(obj, t.get2())); + + break; + + case LONG: + writeLong(getLong(obj, t.get2())); + + break; + + case FLOAT: + writeFloat(getFloat(obj, t.get2())); + + break; + + case DOUBLE: + writeDouble(getDouble(obj, t.get2())); + + break; + + case CHAR: + writeChar(getChar(obj, t.get2())); + + break; + + case BOOLEAN: + writeBoolean(getBoolean(obj, t.get2())); + + break; + + case OTHER: + writeObject0(getObject(obj, t.get2())); + } + } + } + + /** + * Writes array of {@code byte}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeByteArray(byte[] arr) throws IOException { + out.writeByteArray(arr); + } + + /** + * Writes array of {@code short}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeShortArray(short[] arr) throws IOException { + out.writeShortArray(arr); + } + + /** + * Writes array of {@code int}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeIntArray(int[] arr) throws IOException { + out.writeIntArray(arr); + } + + /** + * Writes array of {@code long}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeLongArray(long[] arr) throws IOException { + out.writeLongArray(arr); + } + + /** + * Writes array of {@code float}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeFloatArray(float[] arr) throws IOException { + out.writeFloatArray(arr); + } + + /** + * Writes array of {@code double}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeDoubleArray(double[] arr) throws IOException { + out.writeDoubleArray(arr); + } + + /** + * Writes array of {@code char}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeCharArray(char[] arr) throws IOException { + out.writeCharArray(arr); + } + + /** + * Writes array of {@code boolean}s. + * + * @param arr Array. + * @throws IOException In case of error. + */ + void writeBooleanArray(boolean[] arr) throws IOException { + out.writeBooleanArray(arr); + } + + /** + * Writes {@link String}. + * + * @param str String. + * @throws IOException In case of error. + */ + void writeString(String str) throws IOException { + out.writeUTF(str); + } + + /** {@inheritDoc} */ + @Override public void writeBoolean(boolean v) throws IOException { + out.writeBoolean(v); + } + + /** {@inheritDoc} */ + @Override public void writeByte(int v) throws IOException { + out.writeByte(v); + } + + /** {@inheritDoc} */ + @Override public void writeShort(int v) throws IOException { + out.writeShort(v); + } + + /** {@inheritDoc} */ + @Override public void writeChar(int v) throws IOException { + out.writeChar(v); + } + + /** {@inheritDoc} */ + @Override public void writeInt(int v) throws IOException { + out.writeInt(v); + } + + /** {@inheritDoc} */ + @Override public void writeLong(long v) throws IOException { + out.writeLong(v); + } + + /** {@inheritDoc} */ + @Override public void writeFloat(float v) throws IOException { + out.writeFloat(v); + } + + /** {@inheritDoc} */ + @Override public void writeDouble(double v) throws IOException { + out.writeDouble(v); + } + + /** {@inheritDoc} */ + @Override public void write(int b) throws IOException { + writeByte(b); + } + + /** {@inheritDoc} */ + @Override public void writeBytes(String s) throws IOException { + out.writeBytes(s); + } + + /** {@inheritDoc} */ + @Override public void writeChars(String s) throws IOException { + out.writeChars(s); + } + + /** {@inheritDoc} */ + @Override public void writeUTF(String s) throws IOException { + out.writeUTF(s); + } + + /** {@inheritDoc} */ + @Override public void useProtocolVersion(int ver) throws IOException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void writeUnshared(Object obj) throws IOException { + writeObject0(obj); + } + + /** {@inheritDoc} */ + @Override public void defaultWriteObject() throws IOException { + if (curObj == null) + throw new NotActiveException("Not in writeObject() call."); + + writeFields(curObj, curFields); + } + + /** {@inheritDoc} */ + @Override public ObjectOutputStream.PutField putFields() throws IOException { + if (curObj == null) + throw new NotActiveException("Not in writeObject() call or fields already written."); + + if (curPut == null) + curPut = new PutFieldImpl(this); + + return curPut; + } + + /** {@inheritDoc} */ + @Override public void writeFields() throws IOException { + if (curObj == null) + throw new NotActiveException("Not in writeObject() call."); + + if (curPut == null) + throw new NotActiveException("putFields() was not called."); + + for (IgniteBiTuple<IgniteOptimizedFieldType, Object> t : curPut.objs) { + switch (t.get1()) { + case BYTE: + writeByte((Byte)t.get2()); + + break; + + case SHORT: + writeShort((Short)t.get2()); + + break; + + case INT: + writeInt((Integer)t.get2()); + + break; + + case LONG: + writeLong((Long)t.get2()); + + break; + + case FLOAT: + writeFloat((Float)t.get2()); + + break; + + case DOUBLE: + writeDouble((Double)t.get2()); + + break; + + case CHAR: + writeChar((Character)t.get2()); + + break; + + case BOOLEAN: + writeBoolean((Boolean)t.get2()); + + break; + + case OTHER: + writeObject0(t.get2()); + } + } + } + + /** {@inheritDoc} */ + @Override public void reset() throws IOException { + out.reset(); + handles.clear(); + + curObj = null; + curFields = null; + curPut = null; + curFieldInfoMap = null; + } + + /** {@inheritDoc} */ + @Override public void flush() throws IOException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void drain() throws IOException { + // No-op. + } + + /** + * Returns objects that were added to handles table. + * Used ONLY for test purposes. + * + * @return Handled objects. + */ + Object[] handledObjects() { + return handles.objects(); + } + + /** + * {@link PutField} implementation. + */ + private static class PutFieldImpl extends PutField { + /** Stream. */ + private final IgniteOptimizedObjectOutputStream out; + + /** Field info map. */ + private final Map<String, IgniteBiTuple<Integer, IgniteOptimizedFieldType>> fieldInfoMap; + + /** Values. */ + private final IgniteBiTuple<IgniteOptimizedFieldType, Object>[] objs; + + /** + * @param out Output stream. + * @throws IOException In case of error. + */ + @SuppressWarnings("unchecked") + private PutFieldImpl(IgniteOptimizedObjectOutputStream out) { + this.out = out; + + fieldInfoMap = out.curFieldInfoMap; + + objs = new IgniteBiTuple[fieldInfoMap.size()]; + } + + /** {@inheritDoc} */ + @Override public void put(String name, boolean val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, byte val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, char val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, short val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, int val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, long val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, float val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, double val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void put(String name, Object val) { + value(name, val); + } + + /** {@inheritDoc} */ + @Override public void write(ObjectOutput out) throws IOException { + if (out != this.out) + throw new IllegalArgumentException("Wrong stream."); + + this.out.writeFields(); + } + + /** + * @param name Field name. + * @param val Value. + */ + private void value(String name, Object val) { + IgniteBiTuple<Integer, IgniteOptimizedFieldType> info = fieldInfoMap.get(name); + + objs[info.get1()] = F.t(info.get2(), val); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7c947cc/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectStreamRegistry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectStreamRegistry.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectStreamRegistry.java new file mode 100644 index 0000000..f4e8c8c --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/IgniteOptimizedObjectStreamRegistry.java @@ -0,0 +1,215 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.apache.ignite.marshaller.optimized; + +import org.gridgain.grid.*; +import org.gridgain.grid.util.typedef.internal.*; +import org.gridgain.grid.util.io.*; + +import java.io.*; +import java.util.concurrent.*; + +/** + * Storage for object streams. + */ +class IgniteOptimizedObjectStreamRegistry { + /** Holders. */ + private static final ThreadLocal<StreamHolder> holders = new ThreadLocal<>(); + + /** Holders pool. */ + private static BlockingQueue<StreamHolder> pool; + + /** + * Ensures singleton. + */ + private IgniteOptimizedObjectStreamRegistry() { + // No-op. + } + + /** + * Sets streams pool size. + * + * @param size Streams pool size. + */ + static void poolSize(int size) { + if (size > 0) { + pool = new LinkedBlockingQueue<>(size); + + for (int i = 0; i < size; i++) { + boolean b = pool.offer(new StreamHolder()); + + assert b; + } + } + else + pool = null; + } + + /** + * Gets output stream. + * + * @return Object output stream. + * @throws GridInterruptedException If thread is interrupted while trying to take holder from pool. + */ + static IgniteOptimizedObjectOutputStream out() throws GridInterruptedException { + return holder().acquireOut(); + } + + /** + * Gets input stream. + * + * @return Object input stream. + * @throws GridInterruptedException If thread is interrupted while trying to take holder from pool. + */ + static IgniteOptimizedObjectInputStream in() throws GridInterruptedException { + return holder().acquireIn(); + } + + /** + * Closes and releases output stream. + * + * @param out Object output stream. + */ + static void closeOut(IgniteOptimizedObjectOutputStream out) { + U.close(out, null); + + StreamHolder holder = holders.get(); + + holder.releaseOut(); + + if (pool != null) { + holders.remove(); + + boolean b = pool.offer(holder); + + assert b; + } + } + + /** + * Closes and releases input stream. + * + * @param in Object input stream. + */ + @SuppressWarnings("TypeMayBeWeakened") + static void closeIn(IgniteOptimizedObjectInputStream in) { + U.close(in, null); + + StreamHolder holder = holders.get(); + + holder.releaseIn(); + + if (pool != null) { + holders.remove(); + + boolean b = pool.offer(holder); + + assert b; + } + } + + /** + * Gets holder from pool or thread local. + * + * @return Stream holder. + * @throws GridInterruptedException If thread is interrupted while trying to take holder from pool. + */ + private static StreamHolder holder() throws GridInterruptedException { + StreamHolder holder = holders.get(); + + if (holder == null) { + try { + holders.set(holder = pool != null ? pool.take() : new StreamHolder()); + } + catch (InterruptedException e) { + throw new GridInterruptedException("Failed to take object stream from pool (thread interrupted).", e); + } + } + + return holder; + } + + /** + * Streams holder. + */ + private static class StreamHolder { + /** Output stream. */ + private final IgniteOptimizedObjectOutputStream out = createOut(); + + /** Input stream. */ + private final IgniteOptimizedObjectInputStream in = createIn(); + + /** Output streams counter. */ + private int outAcquireCnt; + + /** Input streams counter. */ + private int inAcquireCnt; + + /** + * Gets output stream. + * + * @return Object output stream. + */ + IgniteOptimizedObjectOutputStream acquireOut() { + return outAcquireCnt++ > 0 ? createOut() : out; + } + + /** + * Gets input stream. + * + * @return Object input stream. + */ + IgniteOptimizedObjectInputStream acquireIn() { + return inAcquireCnt++ > 0 ? createIn() : in; + } + + /** + * Releases output stream. + */ + void releaseOut() { + outAcquireCnt--; + } + + /** + * Releases input stream. + */ + void releaseIn() { + inAcquireCnt--; + } + + /** + * Creates output stream. + * + * @return Object output stream. + */ + private IgniteOptimizedObjectOutputStream createOut() { + try { + return new IgniteOptimizedObjectOutputStream(new GridUnsafeDataOutput(4 * 1024)); + } + catch (IOException e) { + throw new GridRuntimeException("Failed to create object output stream.", e); + } + } + + /** + * Creates input stream. + * + * @return Object input stream. + */ + private IgniteOptimizedObjectInputStream createIn() { + try { + return new IgniteOptimizedObjectInputStream(new GridUnsafeDataInput()); + } + catch (IOException e) { + throw new GridRuntimeException("Failed to create object input stream.", e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7c947cc/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java index e907146..f2dac0d 100644 --- a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java +++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java @@ -216,7 +216,7 @@ public class GridOptimizedMarshallerTest extends GridCommonAbstractTest { Object obj = new SomeSerializable(null); // Clear caches. - ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); + ((Map)U.staticField(IgniteOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); IgniteOptimizedClassResolver.userClasses(null, null); GridMarshaller marsh = new IgniteOptimizedMarshaller(); @@ -224,7 +224,7 @@ public class GridOptimizedMarshallerTest extends GridCommonAbstractTest { int size1 = marsh.marshal(obj).length; // Clear caches. - ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); + ((Map)U.staticField(IgniteOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); IgniteOptimizedClassResolver.userClasses(null, null); IgniteOptimizedMarshaller marshPreregistered = new IgniteOptimizedMarshaller(); @@ -246,7 +246,7 @@ public class GridOptimizedMarshallerTest extends GridCommonAbstractTest { Object obj = new SomeSerializable(null); // Clear caches. - ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); + ((Map)U.staticField(IgniteOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); IgniteOptimizedClassResolver.userClasses(null, null); GridMarshaller marsh = new IgniteOptimizedMarshaller(); @@ -254,7 +254,7 @@ public class GridOptimizedMarshallerTest extends GridCommonAbstractTest { int size1 = marsh.marshal(obj).length; // Clear caches. - ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); + ((Map)U.staticField(IgniteOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear(); IgniteOptimizedClassResolver.userClasses(null, null); IgniteOptimizedMarshaller marshPreregistered = new IgniteOptimizedMarshaller(); @@ -318,7 +318,7 @@ public class GridOptimizedMarshallerTest extends GridCommonAbstractTest { ignite.compute().execute(taskClsName, 2); ConcurrentMap<Class<?>, IgniteOptimizedClassDescriptor> cache = - U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE"); + U.staticField(IgniteOptimizedMarshallerUtils.class, "CLS_DESC_CACHE"); assertTrue(cache.containsKey(jobCls)); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7c947cc/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedObjectStreamSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedObjectStreamSelfTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedObjectStreamSelfTest.java index 3aae159..60c6110 100644 --- a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedObjectStreamSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedObjectStreamSelfTest.java @@ -689,7 +689,7 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testReadLine() throws Exception { - GridOptimizedObjectInputStream in = new GridOptimizedObjectInputStream(new GridUnsafeDataInput()); + IgniteOptimizedObjectInputStream in = new IgniteOptimizedObjectInputStream(new GridUnsafeDataInput()); byte[] bytes = "line1\nline2\r\nli\rne3\nline4".getBytes(); @@ -859,7 +859,7 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testReadToArray() throws Exception { - GridOptimizedObjectInputStream in = GridOptimizedObjectStreamRegistry.in(); + IgniteOptimizedObjectInputStream in = IgniteOptimizedObjectStreamRegistry.in(); try { byte[] arr = new byte[50]; @@ -898,7 +898,7 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { assertEquals(i < 10 ? 40 + i : 0, buf[i]); } finally { - GridOptimizedObjectStreamRegistry.closeIn(in); + IgniteOptimizedObjectStreamRegistry.closeIn(in); } } @@ -978,11 +978,11 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { * @throws Exception In case of error. */ private <T> T marshalUnmarshal(@Nullable Object obj) throws Exception { - GridOptimizedObjectOutputStream out = null; - GridOptimizedObjectInputStream in = null; + IgniteOptimizedObjectOutputStream out = null; + IgniteOptimizedObjectInputStream in = null; try { - out = GridOptimizedObjectStreamRegistry.out(); + out = IgniteOptimizedObjectStreamRegistry.out(); out.requireSerializable(true); @@ -990,7 +990,7 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { byte[] arr = out.out().array(); - in = GridOptimizedObjectStreamRegistry.in(); + in = IgniteOptimizedObjectStreamRegistry.in(); in.classLoader(getClass().getClassLoader()); @@ -1003,8 +1003,8 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { return (T)obj0; } finally { - GridOptimizedObjectStreamRegistry.closeOut(out); - GridOptimizedObjectStreamRegistry.closeIn(in); + IgniteOptimizedObjectStreamRegistry.closeOut(out); + IgniteOptimizedObjectStreamRegistry.closeIn(in); } } @@ -1015,7 +1015,7 @@ public class GridOptimizedObjectStreamSelfTest extends GridCommonAbstractTest { * @param in Input stream. * @throws Exception If failed. */ - private void checkHandles(GridOptimizedObjectOutputStream out, GridOptimizedObjectInputStream in) + private void checkHandles(IgniteOptimizedObjectOutputStream out, IgniteOptimizedObjectInputStream in) throws Exception { Object[] outHandles = out.handledObjects(); Object[] inHandles = in.handledObjects();