You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/08/24 14:04:57 UTC
[34/50] [abbrv] ignite git commit: IGNITE-2649 - Fix NPE.
IGNITE-2649 - Fix NPE.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/b5a3f9e3
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/b5a3f9e3
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/b5a3f9e3
Branch: refs/heads/ignite-2649
Commit: b5a3f9e3c60483448bb1181952dbb96fc0f432c8
Parents: 087f84f
Author: dkarachentsev <dk...@gridgain.com>
Authored: Thu Aug 4 11:26:00 2016 +0300
Committer: dkarachentsev <dk...@gridgain.com>
Committed: Thu Aug 4 11:26:00 2016 +0300
----------------------------------------------------------------------
.../ignite/marshaller/MarshallerUtils.java | 114 ++++--
.../GridBinaryCacheSerializationTest.java | 373 -------------------
.../processors/query/h2/IgniteH2Indexing.java | 14 +-
3 files changed, 86 insertions(+), 415 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/b5a3f9e3/modules/core/src/main/java/org/apache/ignite/marshaller/MarshallerUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/MarshallerUtils.java b/modules/core/src/main/java/org/apache/ignite/marshaller/MarshallerUtils.java
index cfb45ad..629ce6a 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/MarshallerUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/MarshallerUtils.java
@@ -30,7 +30,7 @@ import java.nio.ByteBuffer;
/**
* Util class that sets and discards thread local
- * ignite configuration in {@link IgnitionEx} class.
+ * ignite grid name in {@link IgnitionEx} class.
*/
public final class MarshallerUtils {
/**
@@ -40,12 +40,13 @@ public final class MarshallerUtils {
}
/**
+ * Marshal object to stream and set grid name thread local.
*
- * @param marshaller marshaller.
- * @param obj object.
- * @param out output stream.
+ * @param marshaller Marshaller.
+ * @param obj Object to marshal.
+ * @param out Output stream.
* @param gridName Grid name.
- * @throws IgniteCheckedException
+ * @throws IgniteCheckedException If fail.
*/
public static void marshal(final Marshaller marshaller, final @Nullable Object obj,
final OutputStream out, final String gridName) throws IgniteCheckedException {
@@ -64,12 +65,13 @@ public final class MarshallerUtils {
}
/**
+ * Marshal object and set grid name thread local.
*
- * @param marshaller marshaller.
- * @param obj object.
+ * @param marshaller Marshaller.
+ * @param obj Object to marshal.
* @param gridName Grid name.
- * @return serialized.
- * @throws IgniteCheckedException
+ * @return Binary data.
+ * @throws IgniteCheckedException If fail.
*/
public static byte[] marshal(final Marshaller marshaller, @Nullable Object obj,
final String gridName) throws IgniteCheckedException {
@@ -88,13 +90,28 @@ public final class MarshallerUtils {
}
/**
+ * Marshal object.
+ * <p>Use only when grid name is not available, f.e. in tests.</p>
+ *
+ * @param marshaller Marshaller.
+ * @param obj Object to marshal.
+ * @return Binary data.
+ * @throws IgniteCheckedException If fail.
+ */
+ public static byte[] marshal(final Marshaller marshaller, final @Nullable Object obj) throws IgniteCheckedException {
+ // This method used to keep marshaller usages in one place.
+ return marshaller.marshal(obj);
+ }
+
+ /**
+ * Unmarshal object from stream and set grid name thread local.
*
- * @param marshaller marshaller.
- * @param in input stream.
- * @param clsLdr class loader.
+ * @param marshaller Marshaller.
+ * @param in Input stream.
+ * @param clsLdr Class loader.
* @param gridName Grid name.
- * @param <T> target type.
- * @return deserialized object.
+ * @param <T> Target type.
+ * @return Deserialized object.
* @throws IgniteCheckedException
*/
public static <T> T unmarshal(final Marshaller marshaller, InputStream in, @Nullable ClassLoader clsLdr,
@@ -114,14 +131,32 @@ public final class MarshallerUtils {
}
/**
+ * Unmarshal object.
+ * <p>Use only when grid name is not available, f.e. in tests.</p>
+ *
+ * @param marshaller Marshaller.
+ * @param arr Bianry data.
+ * @param clsLdr Class loader.
+ * @param <T> Target type.
+ * @return Unmarshalled object.
+ * @throws IgniteCheckedException If fail.
+ */
+ public static <T> T unmarshal(final Marshaller marshaller, byte[] arr, @Nullable ClassLoader clsLdr)
+ throws IgniteCheckedException {
+ // This method used to keep marshaller usages in one place.
+ return marshaller.unmarshal(arr, clsLdr);
+ }
+
+ /**
+ * Unmarshal object and set grid name thread local.
*
- * @param marshaller marshaller.
- * @param arr byte array.
- * @param clsLdr class loader.
+ * @param marshaller Marshaller.
+ * @param arr Bianry data.
+ * @param clsLdr Class loader.
* @param gridName Grid name.
- * @param <T> target type
- * @return deserialized object.
- * @throws IgniteCheckedException
+ * @param <T> Target type
+ * @return Deserialized object.
+ * @throws IgniteCheckedException If fail.
*/
public static <T> T unmarshal(final Marshaller marshaller, byte[] arr, @Nullable ClassLoader clsLdr,
final String gridName) throws IgniteCheckedException {
@@ -140,14 +175,15 @@ public final class MarshallerUtils {
}
/**
+ * Marshal and unmarshal object.
*
- * @param marshaller marshaller.
- * @param obj object
- * @param clsLdr class loader.
+ * @param marshaller Marshaller.
+ * @param obj Object to clone.
+ * @param clsLdr Class loader.
* @param gridName Grid name.
- * @param <T> target type.
- * @return deserialized value.
- * @throws IgniteCheckedException
+ * @param <T> Target type.
+ * @return Deserialized value.
+ * @throws IgniteCheckedException If fail.
*/
public static <T> T clone(final Marshaller marshaller, T obj, @Nullable ClassLoader clsLdr,
final String gridName) throws IgniteCheckedException {
@@ -166,13 +202,14 @@ public final class MarshallerUtils {
}
/**
+ * Marshal object and set grid name thread local.
*
- * @param gridMarshaller grid marshaller.
- * @param obj object.
- * @param off offset.
- * @param gridName ignite config.
- * @return serialized data.
- * @throws IOException
+ * @param gridMarshaller Grid marshaller.
+ * @param obj Object to marshal.
+ * @param off Offset.
+ * @param gridName Grid name.
+ * @return Serialized data.
+ * @throws IOException If fail.
*/
public static ByteBuffer marshal(GridClientMarshaller gridMarshaller, Object obj, int off,
String gridName) throws IOException {
@@ -191,13 +228,14 @@ public final class MarshallerUtils {
}
/**
+ * Unmarshal object and set grid name thread local.
*
- * @param gridMarshaller grid marshaller.
- * @param bytes byte array.
- * @param gridName ignite config.
- * @param <T> target type.
- * @return deserialized value.
- * @throws IOException
+ * @param gridMarshaller Grid marshaller.
+ * @param bytes Binary data.
+ * @param gridName Grid name.
+ * @param <T> Target type.
+ * @return Deserialized value.
+ * @throws IOException If fail.
*/
public static <T> T unmarshal(GridClientMarshaller gridMarshaller, byte[] bytes,
String gridName) throws IOException {
http://git-wip-us.apache.org/repos/asf/ignite/blob/b5a3f9e3/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridBinaryCacheSerializationTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridBinaryCacheSerializationTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridBinaryCacheSerializationTest.java
deleted file mode 100644
index 31a7258..0000000
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridBinaryCacheSerializationTest.java
+++ /dev/null
@@ -1,373 +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.processors.cache.binary;
-
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-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.configuration.IgniteConfiguration;
-import org.apache.ignite.internal.binary.BinaryMarshaller;
-import org.apache.ignite.internal.util.typedef.internal.U;
-import org.apache.ignite.testframework.GridTestUtils;
-import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
-
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutput;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.util.concurrent.Callable;
-
-/**
- *
- */
-public class GridBinaryCacheSerializationTest extends GridCommonAbstractTest {
- /** */
- private static final String CACHE_NAME = "cache_name";
-
- /** {@inheritDoc} */
- @Override protected IgniteConfiguration getConfiguration(final String gridName) throws Exception {
- final IgniteConfiguration cfg = super.getConfiguration(gridName);
-
- if (gridName != null && gridName.startsWith("binary"))
- cfg.setMarshaller(new BinaryMarshaller());
-
- return cfg;
- }
-
- /**
- * Test that calling {@link Ignition#localIgnite()}
- * is safe for binary marshaller.
- *
- * @throws Exception
- */
- public void testPutGetSimple() throws Exception {
- testPutGet(new SimpleTestObject("one"), null);
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testPutGetSerializable() throws Exception {
- testPutGet(new SerializableTestObject("test"), null);
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testPutGetExternalizable() throws Exception {
- testPutGet(new ExternalizableTestObject("test"), null);
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testPutGetBinarylizable() throws Exception {
- testPutGet(new BinarylizableTestObject("test"), "binaryIgnite");
- }
-
- /**
- * @throws Exception If failed.
- */
- private void testPutGet(final TestObject obj, final String gridName) throws Exception {
- // Run async to emulate user thread.
- GridTestUtils.runAsync(new Callable<Object>() {
- @Override public Object call() throws Exception {
- try (final Ignite ignite = startGrid(gridName)) {
- final IgniteCache<Integer, TestObject> cache = ignite.getOrCreateCache(CACHE_NAME);
-
- assertNull(obj.ignite());
-
- cache.put(1, obj);
-
- assertNotNull(obj.ignite());
-
- final TestObject loadedObj = cache.get(1);
-
- assertNotNull(loadedObj.ignite());
-
- assertEquals(obj, loadedObj);
- }
-
- return null;
- }
- }).get();
- }
-
- /**
- *
- */
- private interface TestObject {
- /**
- * @return Ignite instance.
- */
- Ignite ignite();
- }
-
- /**
- * Test object.
- */
- private static class SimpleTestObject implements TestObject {
- /** */
- private final String val;
-
- /** */
- private transient Ignite ignite;
-
- /** */
- private SimpleTestObject(final String val) {
- this.val = val;
- }
-
- /**
- * @return Object.
- */
- @SuppressWarnings("unused")
- private Object readResolve() {
- ignite = Ignition.localIgnite();
-
- return this;
- }
-
- /**
- * @return Object.
- */
- @SuppressWarnings("unused")
- private Object writeReplace() {
- ignite = Ignition.localIgnite();
-
- return this;
- }
-
- /** */
- @Override public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- final SimpleTestObject simpleTestObj = (SimpleTestObject) o;
-
- return val != null ? val.equals(simpleTestObj.val) : simpleTestObj.val == null;
-
- }
-
- /** */
- @Override public int hashCode() {
- return val != null ? val.hashCode() : 0;
- }
-
- /** {@inheritDoc} */
- @Override public Ignite ignite() {
- return ignite;
- }
- }
-
- /**
- *
- */
- private static class SerializableTestObject implements Serializable, TestObject {
- /** */
- private static final long serialVersionUID = 0L;
-
- /** */
- private String val;
-
- /** */
- private transient Ignite ignite;
-
- /**
- *
- */
- public SerializableTestObject() {
- }
-
- /**
- * @param val Value
- */
- public SerializableTestObject(final String val) {
- this.val = val;
- }
-
- /**
- * @param out Object output.
- * @throws IOException If fail.
- */
- private void writeObject(ObjectOutputStream out) throws IOException {
- U.writeString(out, val);
-
- ignite = Ignition.localIgnite();
- }
-
- /**
- * @param in Object input.
- * @throws IOException If fail.
- */
- private void readObject(ObjectInputStream in) throws IOException {
- val = U.readString(in);
-
- ignite = Ignition.localIgnite();
- }
-
- /** {@inheritDoc} */
- @Override public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- final SerializableTestObject that = (SerializableTestObject) o;
-
- return val != null ? val.equals(that.val) : that.val == null;
-
- }
-
- /** {@inheritDoc} */
- @Override public int hashCode() {
- return val != null ? val.hashCode() : 0;
- }
-
- /** {@inheritDoc} */
- @Override public Ignite ignite() {
- return ignite;
- }
- }
-
- /**
- *
- */
- private static class ExternalizableTestObject implements Externalizable, TestObject {
- /** */
- private static final long serialVersionUID = 0L;
-
- /** */
- private String val;
-
- /** */
- private transient Ignite ignite;
-
- /**
- *
- */
- public ExternalizableTestObject() {
- }
-
- /**
- * @param val Value.
- */
- public ExternalizableTestObject(final String val) {
- this.val = val;
- }
-
- /** {@inheritDoc} */
- @Override public void writeExternal(final ObjectOutput out) throws IOException {
- U.writeString(out, val);
-
- ignite = Ignition.localIgnite();
- }
-
- /** {@inheritDoc} */
- @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
- val = U.readString(in);
-
- ignite = Ignition.localIgnite();
- }
-
- /** {@inheritDoc} */
- @Override public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- final ExternalizableTestObject that = (ExternalizableTestObject) o;
-
- return val != null ? val.equals(that.val) : that.val == null;
-
- }
-
- /** {@inheritDoc} */
- @Override public int hashCode() {
- return val != null ? val.hashCode() : 0;
- }
-
- /** {@inheritDoc} */
- @Override public Ignite ignite() {
- return ignite;
- }
- }
-
- /**
- *
- */
- private static class BinarylizableTestObject implements Binarylizable, TestObject {
- /** */
- private String val;
-
- /** */
- private transient Ignite ignite;
-
- /**
- *
- */
- public BinarylizableTestObject() {
- }
-
- /**
- * @param val Value.
- */
- public BinarylizableTestObject(final String val) {
- this.val = val;
- }
-
- /** {@inheritDoc} */
- @Override public void writeBinary(final BinaryWriter writer) throws BinaryObjectException {
- writer.rawWriter().writeString(val);
-
- ignite = Ignition.localIgnite();
- }
-
- /** {@inheritDoc} */
- @Override public void readBinary(final BinaryReader reader) throws BinaryObjectException {
- val = reader.rawReader().readString();
-
- ignite = Ignition.localIgnite();
- }
-
- /** {@inheritDoc} */
- @Override public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- final BinarylizableTestObject that = (BinarylizableTestObject) o;
-
- return val != null ? val.equals(that.val) : that.val == null;
-
- }
-
- /** {@inheritDoc} */
- @Override public int hashCode() {
- return val != null ? val.hashCode() : 0;
- }
-
- /** {@inheritDoc} */
- @Override public Ignite ignite() {
- return ignite;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/b5a3f9e3/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
index a00795f..c3700ab 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
@@ -1536,13 +1536,19 @@ public class IgniteH2Indexing implements GridQueryIndexing {
protected JavaObjectSerializer h2Serializer() {
return new JavaObjectSerializer() {
@Override public byte[] serialize(Object obj) throws Exception {
- return MarshallerUtils.marshal(marshaller, obj, ctx.gridName());
+ if (ctx != null)
+ return MarshallerUtils.marshal(marshaller, obj, ctx.gridName());
+ else
+ return MarshallerUtils.marshal(marshaller, obj);
}
@Override public Object deserialize(byte[] bytes) throws Exception {
- ClassLoader clsLdr = ctx != null ? U.resolveClassLoader(ctx.config()) : null;
-
- return MarshallerUtils.unmarshal(marshaller, bytes, clsLdr, ctx.gridName());
+ if (ctx != null) {
+ return MarshallerUtils.unmarshal(marshaller, bytes,
+ U.resolveClassLoader(ctx.config()), ctx.gridName());
+ }
+ else
+ return MarshallerUtils.unmarshal(marshaller, bytes, null);
}
};
}