Skip to content

Commit

Permalink
IGNITE-141 - Marshallers refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Kulichenko committed Mar 5, 2015
1 parent d4d96c4 commit 2866f02
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1,043 deletions.
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.ignite;
package org.apache.ignite.internal;

import org.apache.ignite.internal.util.typedef.internal.*;

Expand Down Expand Up @@ -97,6 +97,16 @@ private static Collection<Class> classes() throws Exception {
processFile(file, ldr, prefixLen, col);
}

col.add(byte[].class);
col.add(short[].class);
col.add(int[].class);
col.add(long[].class);
col.add(float[].class);
col.add(double[].class);
col.add(char[].class);
col.add(boolean[].class);
col.add(Object[].class);

return col;
}

Expand Down
1,010 changes: 0 additions & 1,010 deletions modules/core/src/main/java/org/apache/ignite/internal/classnames.properties

Large diffs are not rendered by default.

Expand Up @@ -19,6 +19,7 @@

import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.marshaller.*;
import sun.misc.*;
Expand Down Expand Up @@ -166,9 +167,6 @@ class OptimizedClassDescriptor {
/** {@code True} if descriptor is for {@link Class}. */
private boolean isCls;

/** Array component type. */
private Class<?> arrCompType;

/** Enumeration values. */
private Object[] enumVals;

Expand Down Expand Up @@ -293,11 +291,8 @@ else if (cls == char[].class)
type = TYPE_CHAR_ARR;
else if (cls == boolean[].class)
type = TYPE_BOOLEAN_ARR;
else if (cls.isArray()) {
else if (cls.isArray())
type = TYPE_OBJ_ARR;

arrCompType = cls.getComponentType();
}
else if (cls == String.class)
type = TYPE_STR;
else if (cls.isEnum()) {
Expand Down Expand Up @@ -734,6 +729,7 @@ void write(OptimizedObjectOutputStream out, Object obj) throws IOException {
break;

case TYPE_OBJ_ARR:
out.writeUTF(obj.getClass().getComponentType().getName());
out.writeArray((Object[])obj);

break;
Expand Down Expand Up @@ -881,7 +877,7 @@ Object read(OptimizedObjectInputStream in) throws ClassNotFoundException, IOExce
return in.readBooleanArray();

case TYPE_OBJ_ARR:
return in.readArray(arrCompType);
return in.readArray(U.forName(in.readUTF(), in.classLoader()));

case TYPE_STR:
return in.readString();
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.marshaller.*;
import org.apache.ignite.marshaller.jdk.*;
import org.jdk8.backport.*;
import sun.misc.*;

Expand All @@ -46,9 +47,15 @@ class OptimizedMarshallerUtils {
/** Object reference. */
static final byte OBJECT = (byte)0x72;

/** Object marshalled with JDK marshaller. */
static final byte JDK = (byte)0x73;

/** UTF-8 character name. */
static final Charset UTF_8 = Charset.forName("UTF-8");

/** JDK marshaller. */
static final JdkMarshaller JDK_MARSH = new JdkMarshaller();

/** Class descriptors by class. */
private static final ConcurrentMap<Class, OptimizedClassDescriptor> DESC_BY_CLS = new ConcurrentHashMap8<>();

Expand Down
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.marshaller.optimized;

import org.apache.ignite.*;
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.io.*;
import org.apache.ignite.internal.util.typedef.*;
Expand Down Expand Up @@ -155,6 +156,19 @@ public void in(GridDataInput in) {

return desc.read(this);

case JDK:
try {
return JDK_MARSH.unmarshal(this, clsLdr);
}
catch (IgniteCheckedException e) {
IOException ioEx = e.getCause(IOException.class);

if (ioEx != null)
throw ioEx;
else
throw new IOException("Failed to deserialize object with JDK marshaller.", e);
}

default:
SB msg = new SB("Unexpected error occurred during unmarshalling");

Expand Down
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.marshaller.optimized;

import org.apache.ignite.*;
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.io.*;
import org.apache.ignite.internal.util.typedef.*;
Expand Down Expand Up @@ -150,42 +151,59 @@ private void writeObject0(Object obj) throws IOException {
if (obj == null)
writeByte(NULL);
else {
OptimizedClassDescriptor desc = classDescriptor(obj.getClass(), ctx, mapper);
if (obj instanceof Throwable && !(obj instanceof Externalizable)) {
writeByte(JDK);

if (desc.excluded()) {
writeByte(NULL);
try {
JDK_MARSH.marshal(obj, this);
}
catch (IgniteCheckedException e) {
IOException ioEx = e.getCause(IOException.class);

return;
if (ioEx != null)
throw ioEx;
else
throw new IOException("Failed to serialize object with JDK marshaller: " + obj, e);
}
}
else {
OptimizedClassDescriptor desc = classDescriptor(
obj instanceof Object[] ? Object[].class : obj.getClass(), ctx, mapper);

Object obj0 = desc.replace(obj);
if (desc.excluded()) {
writeByte(NULL);

if (obj0 == null) {
writeByte(NULL);
return;
}

return;
}
Object obj0 = desc.replace(obj);

int handle = -1;
if (obj0 == null) {
writeByte(NULL);

if (!desc.isPrimitive() && !desc.isEnum() && !desc.isClass())
handle = handles.lookup(obj);
return;
}

if (obj0 != obj) {
obj = obj0;
int handle = -1;

desc = classDescriptor(obj.getClass(), ctx, mapper);
}
if (!desc.isPrimitive() && !desc.isEnum() && !desc.isClass()) handle = handles.lookup(obj);

if (handle >= 0) {
writeByte(HANDLE);
writeInt(handle);
}
else {
writeByte(OBJECT);
writeInt(desc.typeId());
if (obj0 != obj) {
obj = obj0;

desc = classDescriptor(obj instanceof Object[] ? Object[].class : obj.getClass(), ctx, mapper);
}

desc.write(this, obj);
if (handle >= 0) {
writeByte(HANDLE);
writeInt(handle);
}
else {
writeByte(OBJECT);
writeInt(desc.typeId());

desc.write(this, obj);
}
}
}
}
Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.marshaller.optimized;

import org.apache.ignite.*;
import org.apache.ignite.internal.processors.cache.*;
import org.apache.ignite.internal.util.io.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
Expand Down Expand Up @@ -993,6 +994,20 @@ public void testPutFieldsWithDefaultWriteObject() throws Exception {
}
}

/**
* @throws Exception If failed.
*/
@SuppressWarnings("ThrowableInstanceNeverThrown")
public void testThrowable() throws Exception {
Throwable t = new Throwable("Throwable");

assertEquals(t.getMessage(), ((Throwable)marshalUnmarshal(t)).getMessage());

CacheFlagException flagEx = new CacheFlagException(CacheFlag.CLONE, CacheFlag.READ);

assertEquals(flagEx.flags(), ((CacheFlagException)marshalUnmarshal(flagEx)).flags());
}

/**
* Marshals and unmarshals object.
*
Expand Down

0 comments on commit 2866f02

Please sign in to comment.