ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akuznet...@apache.org
Subject [33/33] ignite git commit: IGNITE-1753-1282 WIP reworking.
Date Mon, 02 Nov 2015 16:37:16 GMT
IGNITE-1753-1282 WIP reworking.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/a111137d
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/a111137d
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/a111137d

Branch: refs/heads/ignite-1753-1282
Commit: a111137d540bec8d6a406f37e71a41a6fc9d9a82
Parents: b0c9169
Author: AKuznetsov <akuznetsov@gridgain.com>
Authored: Mon Nov 2 23:36:20 2015 +0700
Committer: AKuznetsov <akuznetsov@gridgain.com>
Committed: Mon Nov 2 23:36:20 2015 +0700

----------------------------------------------------------------------
 .../store/jdbc/CacheAbstractJdbcStore.java      |  83 +++++++++------
 .../cache/store/jdbc/CacheJdbcPojoStore.java    | 102 +++++++++----------
 .../ignite/cache/store/jdbc/JdbcType.java       |  26 -----
 3 files changed, 105 insertions(+), 106 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/a111137d/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 12a7109..e2e79d1 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
@@ -68,7 +68,6 @@ import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgniteBiInClosure;
 import org.apache.ignite.lang.IgnitePredicate;
 import org.apache.ignite.lifecycle.LifecycleAware;
-import org.apache.ignite.marshaller.portable.PortableMarshaller;
 import org.apache.ignite.resources.CacheStoreSessionResource;
 import org.apache.ignite.resources.IgniteInstanceResource;
 import org.apache.ignite.resources.LoggerResource;
@@ -170,8 +169,8 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K,
V>,
     /** 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. */
-    private volatile Map<String, Map<String, Boolean>> keepSerializedTypes =
new HashMap<>();
+    /** Map for quick check whether type is Simple, POJO or Portable. */
+    private volatile Map<String, Map<String, TypeKind>> typeKinds = new HashMap<>();
 
     /** Maximum batch size for writeAll and deleteAll operations. */
     private int batchSz = DFLT_BATCH_SIZE;
@@ -236,12 +235,12 @@ public abstract class CacheAbstractJdbcStore<K, V> implements
CacheStore<K, V>,
     /**
      * Calculate type ID for given type name.
      *
-     * @param keepSerialized If {@code true} then calculate type ID for portable object otherwise
for POJO.
+     * @param kind If {@code true} then calculate type ID for POJO otherwise for portable
object .
      * @param typeName String description of type name.
      * @return Type ID.
      * @throws CacheException If failed to get type ID for given type name.
      */
-    protected abstract Object typeIdForTypeName(boolean keepSerialized, String typeName)
throws CacheException;
+    protected abstract Object typeIdForTypeName(TypeKind kind, String typeName) throws CacheException;
 
     /**
      * Prepare internal store specific builders for provided types metadata.
@@ -561,9 +560,9 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K,
V>,
      * @param fields Fields descriptors.
      * @throws CacheException If failed to check type metadata.
      */
-    private void checkMapping(@Nullable String cacheName, String typeName, JdbcTypeField[]
fields) throws CacheException {
+    private void checkMapping(@Nullable String cacheName, TypeKind kind, String typeName,
JdbcTypeField[] fields) throws CacheException {
         try {
-            if (SIMPLE_TYPES.contains(typeName)) {
+            if (kind == TypeKind.SIMPLE) {
                 if (fields.length != 1)
                     throw new CacheException("More than one field for simple type [cache="
+  U.maskName(cacheName) +
                         ", type=" + typeName + " ]");
@@ -619,25 +618,43 @@ public abstract class CacheAbstractJdbcStore<K, V> implements
CacheStore<K, V>,
     }
 
     /**
-     * Checks for POJO/portable format.
+     * Checks for Simple/POJO/Portable type kind.
      *
      * @param cacheName Cache name to get types settings.
      * @param typeName Type name to check for POJO/portable format.
      * @return {@code true} If portable format configured.
      * @throws CacheException In case of error.
      */
-    protected boolean isKeepSerialized(String cacheName, String typeName) {
-        Map<String, Boolean> cacheTypes = keepSerializedTypes.get(cacheName);
+    protected TypeKind typeKind(String cacheName, String typeName) {
+        Map<String, TypeKind> cacheTypes = typeKinds.get(cacheName);
 
         if (cacheTypes == null)
             throw new CacheException("Failed to find types metadata for cache: " +  U.maskName(cacheName));
 
-        Boolean keepSerialized = cacheTypes.get(typeName);
+        TypeKind kind = cacheTypes.get(typeName);
 
-        if (keepSerialized == null)
+        if (kind == null)
             throw new CacheException("Failed to find type metadata for type: " + typeName);
 
-        return keepSerialized;
+        return kind;
+    }
+
+    /**
+     * @param type Type name to check.
+     * @return {@code True} if class not found.
+     */
+    private TypeKind kindForName(String type) {
+        if (SIMPLE_TYPES.contains(type))
+            return TypeKind.SIMPLE;
+
+        try {
+            Class.forName(type);
+
+            return TypeKind.POJO;
+        }
+        catch(ClassNotFoundException ignored) {
+            return TypeKind.PORTABLE;
+        }
     }
 
     /**
@@ -661,8 +678,6 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K,
V>,
 
             // If no types configured, check CacheTypeMetadata for backward compatibility.
             if (types == null) {
-                boolean keepSerialized = ignite.configuration().getMarshaller() instanceof
PortableMarshaller;
-
                 CacheConfiguration ccfg = ignite.cache(cacheName).getConfiguration(CacheConfiguration.class);
 
                 Collection<CacheTypeMetadata> oldTypes = ccfg.getTypeMetadata();
@@ -674,8 +689,6 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K,
V>,
                 for (CacheTypeMetadata oldType : oldTypes) {
                     JdbcType newType = new JdbcType();
 
-                    newType.setKeepSerialized(keepSerialized);
-
                     newType.setCacheName(cacheName);
 
                     newType.setDatabaseSchema(oldType.getDatabaseSchema());
@@ -703,32 +716,32 @@ public abstract class CacheAbstractJdbcStore<K, V> implements
CacheStore<K, V>,
             entryMappings = U.newHashMap(cacheTypes.size());
 
             if (!cacheTypes.isEmpty()) {
-                Map<String, Boolean> tk = new HashMap<>(cacheTypes.size() * 2);
+                Map<String, TypeKind> cacheKinds = new HashMap<>(cacheTypes.size()
* 2);
 
                 for (JdbcType type : cacheTypes) {
-                    boolean keepSerialized = type.isKeepSerialized();
-
                     String keyType = type.getKeyType();
                     String valType = type.getValueType();
 
-                    tk.put(keyType, keepSerialized);
-                    tk.put(valType, keepSerialized);
+                    TypeKind kind = kindForName(keyType);
+                    checkMapping(cacheName, kind, keyType, type.getKeyFields());
+                    cacheKinds.put(keyType, kind);
 
-                    Object keyTypeId = typeIdForTypeName(keepSerialized, keyType);
+                    Object keyTypeId = typeIdForTypeName(kind, keyType);
 
                     if (entryMappings.containsKey(keyTypeId))
                         throw new CacheException("Key type must be unique in type metadata
[cache=" +
-                            U.maskName(cacheName) + ", key type=" + keyType + "]");
-
-                    checkMapping(cacheName, keyType, type.getKeyFields());
-                    checkMapping(cacheName, valType, type.getValueFields());
+                            U.maskName(cacheName) + ", type=" + keyType + "]");
 
+                    kind = kindForName(valType);
+                    cacheKinds.put(valType, kind);
+                    checkMapping(cacheName, kind, valType, type.getValueFields());
                     entryMappings.put(keyTypeId, new EntryMapping(cacheName, dialect, type));
                 }
 
-                keepSerializedTypes.put(cacheName, tk);
+                typeKinds.put(cacheName, cacheKinds);
 
                 Map<String, Map<Object, EntryMapping>> mappings = new HashMap<>(cacheMappings);
+
                 mappings.put(cacheName, entryMappings);
 
                 prepareBuilders(cacheName, cacheTypes);
@@ -791,7 +804,7 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K,
V>,
 
                     String selQry = args[i + 1].toString();
 
-                    EntryMapping em = entryMapping(cacheName, typeIdForTypeName(isKeepSerialized(cacheName,
keyType),
+                    EntryMapping em = entryMapping(cacheName, typeIdForTypeName(typeKind(cacheName,
keyType),
                         keyType), keyType);
 
                     futs.add(pool.submit(new LoadCacheCustomQueryWorker<>(em, selQry,
clo)));
@@ -1633,6 +1646,18 @@ public abstract class CacheAbstractJdbcStore<K, V> implements
CacheStore<K, V>,
     }
 
     /**
+     * Type kind.
+     */
+    protected enum TypeKind {
+        /** Type is known java build type, like {@link String} */
+        SIMPLE,
+        /** Class for this type is available. */
+        POJO,
+        /** Class for this type is not available. */
+        PORTABLE
+    }
+
+    /**
      * Entry mapping description.
      */
     protected static class EntryMapping {

http://git-wip-us.apache.org/repos/asf/ignite/blob/a111137d/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 7605502..e93e828 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
@@ -57,9 +57,13 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
      */
     @Override @Nullable protected Object extractParameter(@Nullable String cacheName, String
typeName, String fieldName,
         Object obj) throws CacheException {
-        return isKeepSerialized(cacheName, typeName)
-            ? extractPortableParameter(fieldName, obj)
-            : extractPojoParameter(cacheName, typeName, fieldName, obj);
+        switch (typeKind(cacheName, typeName)) {
+            case SIMPLE:
+                return obj;
+            case POJO:
+                return extractPojoParameter(cacheName, typeName, fieldName, obj);
+            default: return extractPortableParameter(fieldName, obj);
+        }
     }
 
     /**
@@ -85,9 +89,6 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
             if (mc == null)
                 throw new CacheException("Failed to find POJO type metadata for type: " +
typeName);
 
-            if (mc.simple)
-                return obj;
-
             Method getter = mc.getters.get(fieldName);
 
             if (getter == null)
@@ -124,9 +125,32 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
     @Override protected <R> R buildObject(@Nullable String cacheName, String typeName,
         JdbcTypeField[] fields, Collection<String> hashFields, Map<String, Integer>
loadColIdxs, ResultSet rs)
         throws CacheLoaderException {
-        return (R)(isKeepSerialized(cacheName, typeName)
-            ? buildPortableObject(cacheName, typeName, fields, hashFields, loadColIdxs, rs)
-            : buildPojoObject(cacheName, typeName, fields, loadColIdxs, rs));
+        switch (typeKind(cacheName, typeName)) {
+            case SIMPLE: return (R)buildSimpleObject(cacheName, typeName, fields, hashFields,
loadColIdxs, rs);
+            case POJO: return (R)buildPojoObject(cacheName, typeName, fields, loadColIdxs,
rs);
+            default: return (R)buildPortableObject(cacheName, typeName, fields, hashFields,
loadColIdxs, rs);
+        }
+    }
+
+    /**
+     *
+     * @param cacheName
+     * @param typeName
+     * @param fields
+     * @param hashFields
+     * @param loadColIdxs
+     * @param rs
+     * @return
+     */
+    private Object buildSimpleObject(@Nullable String cacheName, String typeName, JdbcTypeField[]
fields,
+        Collection<String> hashFields, Map<String, Integer> loadColIdxs, ResultSet
rs) throws CacheLoaderException {
+        try {
+            JdbcTypeField field = fields[0];
+
+            return getColumnValue(rs, loadColIdxs.get(field.getDatabaseFieldName()), field.getJavaFieldType());
+        } catch (SQLException e) {
+            throw new CacheLoaderException("Failed to read object of class: " + typeName,
e);
+        }
     }
 
     /**
@@ -155,12 +179,6 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
             throw new CacheLoaderException("Failed to find POJO type metadata for type: "
+ typeName);
 
         try {
-            if (mc.simple) {
-                JdbcTypeField field = fields[0];
-
-                return getColumnValue(rs, loadColIdxs.get(field.getDatabaseFieldName()),
mc.cls);
-            }
-
             Object obj = mc.ctor.newInstance();
 
             for (JdbcTypeField field : fields) {
@@ -177,19 +195,24 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
                 Integer colIdx = loadColIdxs.get(fldDbName);
 
                 try {
-                    setter.invoke(obj, getColumnValue(rs, colIdx, field.getJavaFieldType()));
+                    Object colVal = getColumnValue(rs, colIdx, field.getJavaFieldType());
+
+                    try {
+                        setter.invoke(obj, colVal);
+                    }
+                    catch (Exception e) {
+                        throw new CacheLoaderException("Failed to set property in POJO class
[clsName=" + typeName +
+                            ", prop=" + fldJavaName + ", col=" + colIdx + ", dbName=" + fldDbName
+ "]", e);
+                    }
                 }
-                catch (Exception e) {
-                    throw new IllegalStateException("Failed to set property in POJO class
[clsName=" + typeName +
+                catch (SQLException e) {
+                    throw new CacheLoaderException("Failed to read object property [clsName=:
" + typeName +
                         ", prop=" + fldJavaName + ", col=" + colIdx + ", dbName=" + fldDbName
+ "]", e);
                 }
             }
 
             return obj;
         }
-        catch (SQLException e) {
-            throw new CacheLoaderException("Failed to read object of class: " + typeName,
e);
-        }
         catch (Exception e) {
             throw new CacheLoaderException("Failed to construct instance of class: " + typeName,
e);
         }
@@ -276,16 +299,9 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
         return obj.getClass();
     }
 
-    /**
-     * Calculate type ID for given type name.
-     *
-     * @param keepSerialized If {@code true} then calculate type ID for portable object otherwise
for POJO.
-     * @param typeName String description of type name.
-     * @return Type ID.
-     * @throws CacheException If failed to get type ID for given type name.
-     */
-    @Override protected Object typeIdForTypeName(boolean keepSerialized, String typeName)
throws CacheException {
-        if (keepSerialized)
+    /** {@inheritDoc} */
+    @Override protected Object typeIdForTypeName(TypeKind kind, String typeName) throws CacheException
{
+        if (kind == TypeKind.PORTABLE)
             return ignite.portables().typeId(typeName);
 
         try {
@@ -322,7 +338,8 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
         Map<String, PojoMethodsCache> typeMethods = U.newHashMap(types.size() * 2);
 
         for (JdbcType type : types) {
-            if (!type.isKeepSerialized()) {
+            // TODO FIX-ME !!!
+            if (typeKind(cacheName, type.getKeyType()) == TypeKind.POJO) {
                 String keyType = type.getKeyType();
 
                 if (typeMethods.containsKey(keyType))
@@ -357,7 +374,8 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
         Map<String, IgniteBiTuple<Boolean, Integer>> typeIds = U.newHashMap(types.size()
* 2);
 
         for (JdbcType type : types) {
-            if (type.isKeepSerialized()) {
+            // TODO FIX-ME !!!
+            if (typeKind(cacheName, type.getKeyType()) == TypeKind.PORTABLE) {
                 Ignite ignite = ignite();
 
                 IgnitePortables portables = ignite.portables();
@@ -389,27 +407,12 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
         /** Constructor for POJO object. */
         private Constructor ctor;
 
-        /** {@code true} if object is a simple type. */
-        private final boolean simple;
-
         /** Cached setters for POJO object. */
         private Map<String, Method> getters;
 
         /** Cached getters for POJO object. */
         private Map<String, Method> setters;
 
-
-        /**
-         * Object is a simple type.
-         *
-         * @param cls Class.
-         * @return {@code True} if object is a simple type.
-         */
-        private boolean simpleType(Class<?> cls) {
-            return (Number.class.isAssignableFrom(cls) || String.class.isAssignableFrom(cls)
||
-                java.util.Date.class.isAssignableFrom(cls) || Boolean.class.isAssignableFrom(cls)
||
-                UUID.class.isAssignableFrom(cls));
-        }
         /**
          * POJO methods cache.
          *
@@ -421,9 +424,6 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K,
V> {
             try {
                 cls = Class.forName(clsName);
 
-                if (simple = simpleType(cls))
-                    return;
-
                 ctor = cls.getDeclaredConstructor();
 
                 if (!ctor.isAccessible())

http://git-wip-us.apache.org/repos/asf/ignite/blob/a111137d/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/JdbcType.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/JdbcType.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/JdbcType.java
index 93ab4b6..dfb9bf5 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/JdbcType.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/JdbcType.java
@@ -51,9 +51,6 @@ public class JdbcType implements Serializable {
     @GridToStringInclude
     private JdbcTypeField[] valFields;
 
-    /** If {@code true} object is stored as IgniteObject. */
-    private boolean keepSerialized;
-
     /** Custom type hasher. */
     private JdbcTypeHasher hasher;
 
@@ -80,8 +77,6 @@ public class JdbcType implements Serializable {
 
         valType = type.getValueType();
         valFields = type.getValueFields();
-
-        keepSerialized = type.isKeepSerialized();
     }
 
     /**
@@ -253,27 +248,6 @@ public class JdbcType implements Serializable {
         return this;
     }
 
-    /**
-     * Gets how value stored in cache.
-     *
-     * @return {@code true} if object is stored as IgniteObject.
-     */
-    public boolean isKeepSerialized() {
-        return keepSerialized;
-    }
-
-    /**
-     * Sets how value stored in cache.
-     *
-     * @param keepSerialized {@code true} if object is stored as IgniteObject.
-     * @return {@code this} for chaining.
-     */
-    public JdbcType setKeepSerialized(boolean keepSerialized) {
-        this.keepSerialized = keepSerialized;
-
-        return this;
-    }
-
     /** {@inheritDoc} */
     @Override public String toString() {
         return S.toString(JdbcType.class, this);


Mime
View raw message