IGNITE-1753 Fixed issue with simple types for portables. Implemented naive hash code generation. Added maskName(cacheName) to all messages.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/b51fd737 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/b51fd737 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/b51fd737 Branch: refs/heads/ignite-1753-1282 Commit: b51fd737b0e6ccaf3cf0b39c068be9fadeabb5f0 Parents: 0c26038 Author: AKuznetsov <akuznet...@gridgain.com> Authored: Wed Oct 28 21:18:12 2015 +0700 Committer: AKuznetsov <akuznet...@gridgain.com> Committed: Wed Oct 28 21:18:12 2015 +0700 ---------------------------------------------------------------------- .../store/jdbc/CacheAbstractJdbcStore.java | 83 +++++++++++--------- .../cache/store/jdbc/CacheJdbcPojoStore.java | 51 ++++++------ .../store/jdbc/CacheJdbcPojoStoreTest.java | 2 +- .../jdbc/CacheJdbcStoreAbstractSelfTest.java | 2 - 4 files changed, 67 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/b51fd737/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 4a81135..787e9e6 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 @@ -105,16 +105,20 @@ import static org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreConfiguration * <h2 class="header">Java Example</h2> * <pre name="code" class="java"> * ... - * CacheConfiguration ccfg = new CacheConfiguration<>(); + * // Create store configuration: + * CacheJdbcPojoStoreConfiguration storeCfg = new CacheJdbcPojoStoreConfiguration(); + * storeCfg.setDataSourceBean("your_data_source_name"); + * storeCfg.setDialect(new H2Dialect()); + * storeCfg.setTypes(array_with_your_types); + * ... + * // Create store factory. + * CacheJdbcPojoStoreFactory storeFactory = new CacheJdbcPojoStoreFactory(); + * storeFactory.setConfiguration(storeCfg); * - * // Configure cache store. - * ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory(ConfigurationSnippet.store())); + * ccfg.setCacheStoreFactory(storeFactory); * ccfg.setReadThrough(true); * ccfg.setWriteThrough(true); * - * // Configure cache types metadata. - * ccfg.setTypeMetadata(ConfigurationSnippet.typeMetadata()); - * * cfg.setCacheConfiguration(ccfg); * ... * </pre> @@ -125,7 +129,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, protected static final String ATTR_CONN_PROP = "JDBC_STORE_CONNECTION"; /** Simple types names. */ - protected static Collection<String> SIMPLE_TYPES = new HashSet<>(); + protected static final Collection<String> SIMPLE_TYPES = new HashSet<>(); static { SIMPLE_TYPES.add("java.math.BigDecimal"); @@ -163,7 +167,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, /** Data source. */ protected DataSource dataSrc; - /** Cache with entry mapping description. (cache name, (key id, mapping description)). */ + /** Cache with entry mapping description. (cache name, (keyID, mapping description)). */ protected volatile Map<String, Map<Object, EntryMapping>> cacheMappings = Collections.emptyMap(); /** Map for quick check whether type is POJO or Portable. */ @@ -572,30 +576,30 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (simpleType(cls)) { if (fields.length != 1) - throw new CacheException("More than one field for simple type [cache name=" + cacheName - + ", type=" + clsName + " ]"); + throw new CacheException("More than one field for simple type [cache=" + U.maskName(cacheName) + + ", type=" + clsName + " ]"); JdbcTypeField field = fields[0]; if (field.getDatabaseFieldName() == null) - throw new CacheException("Missing database name in mapping description [cache name=" + cacheName - + ", type=" + clsName + " ]"); + throw new CacheException("Missing database name in mapping description [cache=" + + U.maskName(cacheName) + ", type=" + clsName + " ]"); field.setJavaFieldType(cls); } else for (JdbcTypeField field : fields) { if (field.getDatabaseFieldName() == null) - throw new CacheException("Missing database name in mapping description [cache name=" + cacheName - + ", type=" + clsName + " ]"); + throw new CacheException("Missing database name in mapping description [cache=" + + U.maskName(cacheName) + ", type=" + clsName + " ]"); if (field.getJavaFieldName() == null) - throw new CacheException("Missing field name in mapping description [cache name=" + cacheName - + ", type=" + clsName + " ]"); + throw new CacheException("Missing field name in mapping description [cache=" + + U.maskName(cacheName) + ", type=" + clsName + " ]"); if (field.getJavaFieldType() == null) - throw new CacheException("Missing field type in mapping description [cache name=" + cacheName - + ", type=" + clsName + " ]"); + throw new CacheException("Missing field type in mapping description [cache=" + + U.maskName(cacheName) + ", type=" + clsName + " ]"); } } catch (ClassNotFoundException e) { @@ -637,7 +641,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, Map<String, Boolean> cacheTypes = keepSerializedTypes.get(cacheName); if (cacheTypes == null) - throw new CacheException("Failed to find types metadata for cache: " + cacheName); + throw new CacheException("Failed to find types metadata for cache: " + U.maskName(cacheName)); Boolean keepSerialized = cacheTypes.get(typeName); @@ -720,8 +724,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, Object keyTypeId = typeIdForTypeName(keepSerialized, keyType); if (entryMappings.containsKey(keyTypeId)) - throw new CacheException("Key type must be unique in type metadata [cache name=" + cacheName + - ", key type=" + keyType + "]"); + throw new CacheException("Key type must be unique in type metadata [cache=" + + U.maskName(cacheName) + ", key type=" + keyType + "]"); if (!keepSerialized) { checkMapping(cacheName, keyType, type.getKeyFields()); @@ -762,7 +766,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, String maskedCacheName = U.maskName(cacheName); throw new CacheException("Failed to find mapping description [key=" + key + - ", cache=" + maskedCacheName + "]. Please configure JdbcType to associate '" + maskedCacheName + "' with JdbcPojoStore."); + ", cache=" + maskedCacheName + "]. Please configure JdbcType to associate '" + maskedCacheName + + "' with JdbcPojoStore."); } return em; @@ -807,7 +812,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, for (EntryMapping em : entryMappings) { if (parallelLoadCacheMinThreshold > 0) { if (log.isDebugEnabled()) - log.debug("Multithread loading entries from db [cache=" + cacheName + + log.debug("Multithread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + " ]"); Connection conn = null; @@ -856,7 +861,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, } else { if (log.isDebugEnabled()) - log.debug("Single thread loading entries from db [cache=" + cacheName + + log.debug("Single thread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + " ]"); futs.add(pool.submit(loadCacheFull(em, clo))); @@ -868,10 +873,10 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, U.get(fut); if (log.isDebugEnabled()) - log.debug("Cache loaded from db: " + cacheName); + log.debug("Cache loaded from db: " + U.maskName(cacheName)); } catch (IgniteCheckedException e) { - throw new CacheLoaderException("Failed to load cache: " + cacheName, e.getCause()); + throw new CacheLoaderException("Failed to load cache: " + U.maskName(cacheName), e.getCause()); } finally { U.shutdownNow(getClass(), pool, log); @@ -1135,7 +1140,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (currKeyTypeId == null || !currKeyTypeId.equals(keyTypeId)) { if (mergeStmt != null) { if (log.isDebugEnabled()) - log.debug("Write entries to db [cache=" + cacheName + + log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]"); executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries); @@ -1160,7 +1165,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (++prepared % batchSz == 0) { if (log.isDebugEnabled()) - log.debug("Write entries to db [cache=" + cacheName + + log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]"); executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries); @@ -1173,7 +1178,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (mergeStmt != null && prepared % batchSz != 0) { if (log.isDebugEnabled()) - log.debug("Write entries to db [cache=" + cacheName + + log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]"); executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries); @@ -1186,8 +1191,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, } else { if (log.isDebugEnabled()) - log.debug("Write entries to db one by one using update and insert statements [cache=" + cacheName + - ", cnt=" + entries.size() + "]"); + log.debug("Write entries to db one by one using update and insert statements [cache=" + + U.maskName(cacheName) + ", cnt=" + entries.size() + "]"); PreparedStatement insStmt = null; @@ -1351,8 +1356,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (!currKeyTypeId.equals(keyTypeId)) { if (log.isDebugEnabled()) - log.debug("Delete entries from db [cache=" + cacheName + ", keyType=" + em.keyType() + - ", cnt=" + prepared + "]"); + log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]"); executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys); @@ -1369,8 +1374,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (++prepared % batchSz == 0) { if (log.isDebugEnabled()) - log.debug("Delete entries from db [cache=" + cacheName + ", keyType=" + em.keyType() + - ", cnt=" + prepared + "]"); + log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]"); executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys); @@ -1382,8 +1387,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, if (delStmt != null && prepared % batchSz != 0) { if (log.isDebugEnabled()) - log.debug("Delete entries from db [cache=" + cacheName + ", keyType=" + em.keyType() + - ", cnt=" + prepared + "]"); + log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]"); executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys); } @@ -1849,8 +1854,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, /** {@inheritDoc} */ @Override public Void call() throws Exception { if (log.isDebugEnabled()) - log.debug("Load cache using custom query [cache= " + em.cacheName + ", keyType=" + em.keyType() + - ", query=" + qry + "]"); + log.debug("Load cache using custom query [cache= " + U.maskName(em.cacheName) + + ", keyType=" + em.keyType() + ", query=" + qry + "]"); Connection conn = null; http://git-wip-us.apache.org/repos/asf/ignite/blob/b51fd737/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 e3be95b..f06427f 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 @@ -17,25 +17,19 @@ package org.apache.ignite.cache.store.jdbc; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import javax.cache.CacheException; -import javax.cache.integration.CacheLoaderException; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgnitePortables; -import org.apache.ignite.cache.store.CacheStore; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.apache.ignite.lang.IgniteBiTuple; -import org.apache.ignite.portable.PortableBuilder; -import org.apache.ignite.portable.PortableObject; -import org.jetbrains.annotations.Nullable; +import org.apache.ignite.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.portable.*; +import org.jetbrains.annotations.*; + +import javax.cache.*; +import javax.cache.integration.*; +import java.lang.reflect.*; +import java.sql.*; +import java.util.*; /** * Implementation of {@link CacheStore} backed by JDBC and POJO via reflection. @@ -60,7 +54,7 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { * @return Field value from object. * @throws CacheException in case of error. */ - @Nullable protected Object extractParameter(@Nullable String cacheName, String typeName, String fieldName, + @Override @Nullable protected Object extractParameter(@Nullable String cacheName, String typeName, String fieldName, Object obj) throws CacheException { return isKeepSerialized(cacheName, typeName) ? extractPortableParameter(fieldName, obj) @@ -83,7 +77,7 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { Map<String, PojoMethodsCache> cacheMethods = pojoMethods.get(cacheName); if (cacheMethods == null) - throw new CacheException("Failed to find POJO type metadata for cache: " + cacheName); + throw new CacheException("Failed to find POJO type metadata for cache: " + U.maskName(cacheName)); PojoMethodsCache mc = cacheMethods.get(typeName); @@ -163,7 +157,7 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { Map<String, PojoMethodsCache> cacheMethods = pojoMethods.get(cacheName); if (cacheMethods == null) - throw new CacheLoaderException("Failed to find POJO types metadata for cache: " + cacheName); + throw new CacheLoaderException("Failed to find POJO types metadata for cache: " + U.maskName(cacheName)); PojoMethodsCache mc = cacheMethods.get(typeName); @@ -223,11 +217,11 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { * @throws CacheLoaderException If failed to construct portable object. */ protected Object buildPortableObject(String cacheName, String typeName, JdbcTypeField[] fields, - Map<String, Integer> loadColIdxs, ResultSet rs) throws CacheException { + Map<String, Integer> loadColIdxs, ResultSet rs) throws CacheLoaderException { Map<String, IgniteBiTuple<Boolean, Integer>> cacheTypeIds = portableTypeIds.get(cacheName); if (cacheTypeIds == null) - throw new CacheLoaderException("Failed to find portable types IDs for cache: " + cacheName); + throw new CacheLoaderException("Failed to find portable types IDs for cache: " + U.maskName(cacheName)); IgniteBiTuple<Boolean, Integer> tuple = cacheTypeIds.get(typeName); @@ -251,15 +245,14 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { else { PortableBuilder builder = ignite.portables().builder(tuple.get2()); - int hashCode = 1; + int hashCode = 0; // TODO: IGNITE-1753 hash code calculation could change !!! for (JdbcTypeField field : fields) { Integer colIdx = loadColIdxs.get(field.getDatabaseFieldName()); Object colVal = getColumnValue(rs, colIdx, field.getJavaFieldType()); - if (colVal != null) - hashCode = 31 * hashCode + colVal.hashCode(); + hashCode = 31 * hashCode + (colVal != null ? colVal.hashCode() : 0); builder.setField(field.getJavaFieldName(), colVal); } @@ -336,7 +329,7 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { String keyType = type.getKeyType(); if (typeMethods.containsKey(keyType)) - throw new CacheException("Found duplicate key type [cache=" + cacheName + + throw new CacheException("Found duplicate key type [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + "]"); typeMethods.put(keyType, new PojoMethodsCache(keyType, type.getKeyFields())); @@ -416,7 +409,7 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { * @param fields Fields. * @throws CacheException If failed to construct type cache. */ - public PojoMethodsCache(String clsName, JdbcTypeField[] fields) throws CacheException { + private PojoMethodsCache(String clsName, JdbcTypeField[] fields) throws CacheException { try { cls = Class.forName(clsName); http://git-wip-us.apache.org/repos/asf/ignite/blob/b51fd737/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 8c4f7d4..cda13e6 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 @@ -158,7 +158,7 @@ public class CacheJdbcPojoStoreTest extends GridAbstractCacheStoreSelfTest<Cache @Override protected void inject(CacheJdbcPojoStore<Object, Object> store) throws Exception { getTestResources().inject(store); - GridTestUtils.setFieldValue(store, CacheJdbcPojoStore.class, "ses", ses); + GridTestUtils.setFieldValue(store, CacheAbstractJdbcStore.class, "ses", ses); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/ignite/blob/b51fd737/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractSelfTest.java index 2b07b0a..bc9e73d 100644 --- a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractSelfTest.java @@ -206,7 +206,5 @@ public abstract class CacheJdbcStoreAbstractSelfTest extends GridCommonAbstractT c1.loadCache(null); info("Cache load finished!"); - - Thread.sleep(1000000000); } }