Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DEA31200C5C for ; Thu, 20 Apr 2017 09:48:43 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id DD6E5160B91; Thu, 20 Apr 2017 07:48:43 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id CF9C8160BBA for ; Thu, 20 Apr 2017 09:48:39 +0200 (CEST) Received: (qmail 18576 invoked by uid 500); 20 Apr 2017 07:48:38 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 16716 invoked by uid 99); 20 Apr 2017 07:48:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Apr 2017 07:48:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B971DE93E8; Thu, 20 Apr 2017 07:48:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Thu, 20 Apr 2017 07:49:35 -0000 Message-Id: <705e26c3c4c446f3958438dbdf7e23cf@git.apache.org> In-Reply-To: <15b380a870d740a8b3efeb1ffa3a9ef9@git.apache.org> References: <15b380a870d740a8b3efeb1ffa3a9ef9@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [62/70] [abbrv] ignite git commit: IGNITE-4949 - Fixed CacheJdbcPojoStore for BinaryMarshaller. This closes #1803. archived-at: Thu, 20 Apr 2017 07:48:44 -0000 IGNITE-4949 - Fixed CacheJdbcPojoStore for BinaryMarshaller. This closes #1803. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/0cb2c921 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/0cb2c921 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/0cb2c921 Branch: refs/heads/ignite-2893 Commit: 0cb2c92152ea944d3313aa493bfce1f5191bc263 Parents: 1cd3cac Author: Dmitriy Govorukhin Authored: Wed Apr 19 19:44:49 2017 +0300 Committer: Alexey Goncharuk Committed: Wed Apr 19 19:45:40 2017 +0300 ---------------------------------------------------------------------- .../store/jdbc/CacheAbstractJdbcStore.java | 37 +++-- .../cache/store/jdbc/CacheJdbcPojoStore.java | 1 - .../store/jdbc/CacheJdbcPojoStoreTest.java | 147 ++++++++++++++----- .../ignite/testframework/junits/IgniteMock.java | 4 + 4 files changed, 129 insertions(+), 60 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/0cb2c921/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java index 1c7e9dd..625d3cd 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java @@ -537,23 +537,36 @@ public abstract class CacheAbstractJdbcStore implements CacheStore, /** * @param type Type name to check. + * @param binarySupported True if binary marshaller enable. * @return {@code True} if class not found. */ - protected TypeKind kindForName(String type) { + protected TypeKind kindForName(String type, boolean binarySupported) { if (BUILT_IN_TYPES.contains(type)) return TypeKind.BUILT_IN; + if (binarySupported) + return TypeKind.BINARY; + try { Class.forName(type); return TypeKind.POJO; } - catch(ClassNotFoundException ignored) { - return TypeKind.BINARY; + catch (ClassNotFoundException e) { + throw new CacheException("Failed to find class " + type + + " (make sure the class is present in classPath or use BinaryMarshaller)", e); } } /** + * @param type Type name to check. + * @return {@code True} if class not found. + */ + protected TypeKind kindForName(String type) { + return kindForName(type, ignite.configuration().getMarshaller() instanceof BinaryMarshaller); + } + + /** * @param cacheName Cache name to check mappings for. * @return Type mappings for specified cache name. * @throws CacheException If failed to initialize cache mappings. @@ -587,11 +600,7 @@ public abstract class CacheAbstractJdbcStore implements CacheStore, String keyType = type.getKeyType(); String valType = type.getValueType(); - TypeKind keyKind = kindForName(keyType); - - if (!binarySupported && keyKind == TypeKind.BINARY) - throw new CacheException("Key type has no class [cache=" + U.maskName(cacheName) + - ", type=" + keyType + "]"); + TypeKind keyKind = kindForName(keyType, binarySupported); checkTypeConfiguration(cacheName, keyKind, keyType, type.getKeyFields()); @@ -601,21 +610,11 @@ public abstract class CacheAbstractJdbcStore implements CacheStore, throw new CacheException("Key type must be unique in type metadata [cache=" + U.maskName(cacheName) + ", type=" + keyType + "]"); - TypeKind valKind = kindForName(valType); + TypeKind valKind = kindForName(valType, binarySupported); checkTypeConfiguration(cacheName, valKind, valType, type.getValueFields()); entryMappings.put(keyTypeId, new EntryMapping(cacheName, dialect, type, keyKind, valKind, sqlEscapeAll)); - - // Add one more binding to binary typeId for POJOs, - // because object could be passed to store in binary format. - if (binarySupported && keyKind == TypeKind.POJO) { - keyTypeId = typeIdForTypeName(TypeKind.BINARY, keyType); - - valKind = valKind == TypeKind.POJO ? TypeKind.BINARY : valKind; - - entryMappings.put(keyTypeId, new EntryMapping(cacheName, dialect, type, TypeKind.BINARY, valKind, sqlEscapeAll)); - } } Map> mappings = new HashMap<>(cacheMappings); http://git-wip-us.apache.org/repos/asf/ignite/blob/0cb2c921/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java index 00a8ade..8244c6c 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java @@ -23,7 +23,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; http://git-wip-us.apache.org/repos/asf/ignite/blob/0cb2c921/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java index be38541..1a76321 100644 --- a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java +++ b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.cache.store.jdbc; +import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -28,14 +29,16 @@ import java.util.Collection; import java.util.UUID; import java.util.concurrent.ConcurrentLinkedQueue; import javax.cache.integration.CacheWriterException; - +import org.apache.ignite.Ignite; import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.binary.BinaryObjectBuilder; import org.apache.ignite.cache.store.jdbc.dialect.H2Dialect; import org.apache.ignite.cache.store.jdbc.model.Organization; import org.apache.ignite.cache.store.jdbc.model.OrganizationKey; import org.apache.ignite.cache.store.jdbc.model.Person; import org.apache.ignite.cache.store.jdbc.model.PersonComplexKey; import org.apache.ignite.cache.store.jdbc.model.PersonKey; +import org.apache.ignite.internal.binary.BinaryMarshaller; import org.apache.ignite.internal.processors.cache.CacheEntryImpl; import org.apache.ignite.internal.util.typedef.CI2; import org.apache.ignite.internal.util.typedef.internal.U; @@ -57,6 +60,12 @@ public class CacheJdbcPojoStoreTest extends GridAbstractCacheStoreSelfTest orgKeys = new ConcurrentLinkedQueue<>(); - final Collection prnKeys = new ConcurrentLinkedQueue<>(); - final Collection prnComplexKeys = new ConcurrentLinkedQueue<>(); + final Collection orgKeys = new ConcurrentLinkedQueue<>(); + final Collection prnKeys = new ConcurrentLinkedQueue<>(); + final Collection prnComplexKeys = new ConcurrentLinkedQueue<>(); IgniteBiInClosure c = new CI2() { @Override public void apply(Object k, Object v) { - if (k instanceof BinaryObject) - k = ((BinaryObject)k).deserialize(); - - if (v instanceof BinaryObject) - v = ((BinaryObject)k).deserialize(); - - if (k instanceof OrganizationKey && v instanceof Organization) - orgKeys.add((OrganizationKey)k); - else if (k instanceof PersonKey && v instanceof Person) - prnKeys.add((PersonKey)k); - else if (k instanceof PersonComplexKey && v instanceof Person) { - PersonComplexKey key = (PersonComplexKey)k; - - Person val = (Person)v; - - assertTrue("Key ID should be the same as value ID", key.getId() == val.getId()); - assertTrue("Key orgID should be the same as value orgID", key.getOrgId() == val.getOrgId()); - assertEquals("name" + key.getId(), val.getName()); - - prnComplexKeys.add((PersonComplexKey)k); + if (binaryEnable){ + if (k instanceof BinaryObject && v instanceof BinaryObject) { + BinaryObject key = (BinaryObject)k; + BinaryObject val = (BinaryObject)v; + + String keyType = key.type().typeName(); + String valType = val.type().typeName(); + + if (OrganizationKey.class.getName().equals(keyType) + && Organization.class.getName().equals(valType)) + orgKeys.add(key); + + if (PersonKey.class.getName().equals(keyType) + && Person.class.getName().equals(valType)) + prnKeys.add(key); + + if (PersonComplexKey.class.getName().equals(keyType) + && Person.class.getName().equals(valType)) + prnComplexKeys.add(key); + } + } + else { + if (k instanceof OrganizationKey && v instanceof Organization) + orgKeys.add(k); + else if (k instanceof PersonKey && v instanceof Person) + prnKeys.add(k); + else if (k instanceof PersonComplexKey && v instanceof Person) { + PersonComplexKey key = (PersonComplexKey)k; + + Person val = (Person)v; + + assertTrue("Key ID should be the same as value ID", key.getId() == val.getId()); + assertTrue("Key orgID should be the same as value orgID", key.getOrgId() == val.getOrgId()); + assertEquals("name" + key.getId(), val.getName()); + + prnComplexKeys.add(k); + } } } }; @@ -326,15 +358,16 @@ public class CacheJdbcPojoStoreTest extends GridAbstractCacheStoreSelfTest tmpOrgKeys = new ArrayList<>(orgKeys); - Collection tmpPrnKeys = new ArrayList<>(prnKeys); - Collection tmpPrnComplexKeys = new ArrayList<>(prnComplexKeys); + Collection tmpOrgKeys = new ArrayList<>(orgKeys); + Collection tmpPrnKeys = new ArrayList<>(prnKeys); + Collection tmpPrnComplexKeys = new ArrayList<>(prnComplexKeys); orgKeys.clear(); prnKeys.clear(); prnComplexKeys.clear(); - store.loadCache(c, OrganizationKey.class.getName(), "SELECT name, city, id FROM ORGANIZATION", + store.loadCache( + c, OrganizationKey.class.getName(), "SELECT name, city, id FROM ORGANIZATION", PersonKey.class.getName(), "SELECT org_id, id, name FROM Person WHERE id < 1000"); assertEquals(ORGANIZATION_CNT, orgKeys.size()); @@ -384,20 +417,29 @@ public class CacheJdbcPojoStoreTest extends GridAbstractCacheStoreSelfTest prnComplexKeys = new ConcurrentLinkedQueue<>(); + final Collection prnComplexKeys = new ConcurrentLinkedQueue<>(); IgniteBiInClosure c = new CI2() { @Override public void apply(Object k, Object v) { - if (k instanceof BinaryObject) - k = ((BinaryObject)k).deserialize(); - - if (v instanceof BinaryObject) - v = ((BinaryObject)k).deserialize(); - - if (k instanceof PersonComplexKey && v instanceof Person) - prnComplexKeys.add((PersonComplexKey)k); - else - fail("Unexpected entry [key=" + k + ", value=" + v + "]"); + if (binaryEnable) { + if (k instanceof BinaryObject && v instanceof BinaryObject) { + BinaryObject key = (BinaryObject)k; + BinaryObject val = (BinaryObject)v; + + String keyType = key.type().typeName(); + String valType = val.type().typeName(); + + if (PersonComplexKey.class.getName().equals(keyType) + && Person.class.getName().equals(valType)) + prnComplexKeys.add(key); + } + } + else { + if (k instanceof PersonComplexKey && v instanceof Person) + prnComplexKeys.add(k); + else + fail("Unexpected entry [key=" + k + ", value=" + v + "]"); + } } }; @@ -450,7 +492,7 @@ public class CacheJdbcPojoStoreTest extends GridAbstractCacheStoreSelfTest(k1, v1)); + store.write(new CacheEntryImpl<>(wrap(k1), wrap(v1))); fail("CacheWriterException wasn't thrown."); } @@ -479,4 +521,29 @@ public class CacheJdbcPojoStoreTest extends GridAbstractCacheStoreSelfTest cls = obj.getClass(); + + BinaryObjectBuilder builder = ig.binary().builder(cls.getName()); + + for (Field f : cls.getDeclaredFields()) { + if (f.getName().contains("serialVersionUID")) + continue; + + f.setAccessible(true); + + builder.setField(f.getName(), f.get(obj)); + } + + return builder.build(); + } + + return obj; + } + } http://git-wip-us.apache.org/repos/asf/ignite/blob/0cb2c921/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java index ab918e5..ea53345 100644 --- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java +++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java @@ -56,6 +56,7 @@ import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.configuration.NearCacheConfiguration; import org.apache.ignite.internal.binary.BinaryCachingMetadataHandler; import org.apache.ignite.internal.binary.BinaryContext; +import org.apache.ignite.internal.binary.BinaryMarshaller; import org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl; import org.apache.ignite.internal.processors.cacheobject.NoOpBinary; import org.apache.ignite.internal.util.typedef.internal.U; @@ -328,6 +329,9 @@ public class IgniteMock implements Ignite { return typeName.hashCode(); } }; + + if (marshaller instanceof BinaryMarshaller) + ctx.configure((BinaryMarshaller)marshaller, configuration()); } binaryMock = new NoOpBinary() {