ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntikho...@apache.org
Subject [16/50] [abbrv] ignite git commit: IGNITE-2856 Java name for index field, filter of not used fields, support for primitives.
Date Wed, 04 May 2016 08:20:16 GMT
IGNITE-2856 Java name for index field, filter of not used fields, support for primitives.


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

Branch: refs/heads/ignite-2435
Commit: 2912981680ca2f5b9c1156a1017da78edb26ad97
Parents: d9f4f6e
Author: vsisko <vsisko@gridgain.com>
Authored: Wed Apr 13 11:42:31 2016 +0700
Committer: Alexey Kuznetsov <akuznetsov@apache.org>
Committed: Wed Apr 13 11:42:31 2016 +0700

----------------------------------------------------------------------
 .../ignite/schema/generator/CodeGenerator.java  | 20 +------
 .../ignite/schema/generator/GeneratorUtils.java | 53 +++++++++++++++++
 .../ignite/schema/generator/XmlGenerator.java   | 62 +++++++++++++++-----
 3 files changed, 103 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/29129816/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/CodeGenerator.java
----------------------------------------------------------------------
diff --git a/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/CodeGenerator.java
b/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/CodeGenerator.java
index 1f2f2ab..68519d6 100644
--- a/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/CodeGenerator.java
+++ b/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/CodeGenerator.java
@@ -556,21 +556,6 @@ public class CodeGenerator {
     }
 
     /**
-     * Find field by name.
-     *
-     * @param pojo POJO descriptor.
-     * @param name Field name to find.
-     * @return Field descriptor or {@code null} if not found.
-     */
-    private static PojoField findFieldByName(PojoDescriptor pojo, String name) {
-        for (PojoField field: pojo.valueFields(true))
-            if (field.dbName().equals(name))
-                return field;
-
-        return null;
-    }
-
-    /**
      * Generate java snippet for cache configuration with JDBC store and types metadata.
      *
      * @param pojos POJO descriptors.
@@ -671,7 +656,8 @@ public class CodeGenerator {
             add0(src, "");
 
             for (PojoField field : pojo.fields())
-                add2(src, "fields.put(\"" + field.javaName() + "\", \"" + javaTypeName(field)
+ "\");");
+                add2(src, "fields.put(\"" + field.javaName() + "\", \"" +
+                    GeneratorUtils.boxPrimitiveType(field.javaTypeName()) + "\");");
 
             add0(src, "");
             add2(src, "qryEntity.setFields(fields);");
@@ -692,7 +678,7 @@ public class CodeGenerator {
                     List<T2<String, Boolean>> idxFlds = new ArrayList<>(sz);
 
                     for (Map.Entry<String, Boolean> idxFld : dbIdxFlds) {
-                        PojoField field = findFieldByName(pojo, idxFld.getKey());
+                        PojoField field = GeneratorUtils.findFieldByName(pojo.valueFields(true),
idxFld.getKey());
 
                         if (field != null)
                             idxFlds.add(new T2<>(field.javaName(), idxFld.getValue()));

http://git-wip-us.apache.org/repos/asf/ignite/blob/29129816/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/GeneratorUtils.java
----------------------------------------------------------------------
diff --git a/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/GeneratorUtils.java
b/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/GeneratorUtils.java
new file mode 100644
index 0000000..4195b82
--- /dev/null
+++ b/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/GeneratorUtils.java
@@ -0,0 +1,53 @@
+package org.apache.ignite.schema.generator;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.ignite.schema.model.PojoField;
+
+/**
+ * Schema import generator utils.
+ */
+public class GeneratorUtils {
+    /** Map of conformity between primitive type and Java class. */
+    private static final Map<String, String> primitiveToObject = new HashMap<>();
+
+    static {
+        primitiveToObject.put("boolean", "java.lang.Boolean");
+        primitiveToObject.put("byte", "java.lang.Byte");
+        primitiveToObject.put("short", "java.lang.Short");
+        primitiveToObject.put("int", "java.lang.Integer");
+        primitiveToObject.put("long", "java.lang.Long");
+        primitiveToObject.put("float", "java.lang.Float");
+        primitiveToObject.put("double", "java.lang.Double");
+    }
+
+    /**
+     * Convert primitive type to conformity Java class.
+     *
+     * @param type Primitive type.
+     * @return Conformity Java class
+     */
+    public static String boxPrimitiveType(String type) {
+        if (primitiveToObject.containsKey(type))
+            return primitiveToObject.get(type);
+
+        return type;
+    }
+
+    /**
+     * Find field by name.
+     *
+     * @param fields Field descriptors.
+     * @param name Field name to find.
+     * @return Field descriptor or {@code null} if not found.
+     */
+    public static PojoField findFieldByName(Collection<PojoField> fields, String name)
{
+        for (PojoField field: fields)
+            if (field.dbName().equals(name))
+                return field;
+
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/29129816/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/XmlGenerator.java
----------------------------------------------------------------------
diff --git a/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/XmlGenerator.java
b/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/XmlGenerator.java
index 547e7b0..a1665ae 100644
--- a/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/XmlGenerator.java
+++ b/modules/schema-import/src/main/java/org/apache/ignite/schema/generator/XmlGenerator.java
@@ -22,10 +22,13 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -40,6 +43,7 @@ import org.apache.ignite.cache.QueryIndex;
 import org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory;
 import org.apache.ignite.cache.store.jdbc.JdbcType;
 import org.apache.ignite.cache.store.jdbc.JdbcTypeField;
+import org.apache.ignite.internal.util.typedef.T2;
 import org.apache.ignite.schema.model.PojoDescriptor;
 import org.apache.ignite.schema.model.PojoField;
 import org.apache.ignite.schema.ui.ConfirmCallable;
@@ -199,7 +203,8 @@ public class XmlGenerator {
             Element map = addElement(doc, prop, "util:map", "map-class", "java.util.LinkedHashMap");
 
             for (PojoField field : fields)
-                addElement(doc, map, "entry", "key", field.javaName(), "value", field.javaTypeName());
+                addElement(doc, map, "entry", "key", field.javaName(), "value",
+                    GeneratorUtils.boxPrimitiveType(field.javaTypeName()));
         }
     }
 
@@ -210,28 +215,53 @@ public class XmlGenerator {
      * @param parent Parent XML node.
      * @param idxs Indexes.
      */
-    private static void addQueryIndexes(Document doc, Node parent, Collection<QueryIndex>
idxs) {
+    private static void addQueryIndexes(Document doc, Node parent, Collection<PojoField>
fields,
+        Collection<QueryIndex> idxs) {
         if (!idxs.isEmpty()) {
-            Element prop = addProperty(doc, parent, "indexes", null);
+            boolean firstIdx = true;
 
-            Element list = addElement(doc, prop, "list");
+            Element list = null;
 
             for (QueryIndex idx : idxs) {
-                Element idxBean = addBean(doc, list, QueryIndex.class);
+                Set<Map.Entry<String, Boolean>> dbIdxFlds = idx.getFields().entrySet();
+
+                int sz = dbIdxFlds.size();
+
+                List<T2<String, Boolean>> idxFlds = new ArrayList<>(sz);
+
+                for (Map.Entry<String, Boolean> idxFld : dbIdxFlds) {
+                    PojoField field = GeneratorUtils.findFieldByName(fields, idxFld.getKey());
+
+                    if (field != null)
+                        idxFlds.add(new T2<>(field.javaName(), idxFld.getValue()));
+                    else
+                        break;
+                }
 
-                addProperty(doc, idxBean, "name", idx.getName());
+                // Only if all fields present, add index description.
+                if (idxFlds.size() == sz) {
+                    if (firstIdx) {
+                        Element prop = addProperty(doc, parent, "indexes", null);
 
-                Element idxType = addProperty(doc, idxBean, "indexType", null);
-                addElement(doc, idxType, "util:constant", "static-field", "org.apache.ignite.cache.QueryIndexType."
+ idx.getIndexType());
+                        list = addElement(doc, prop, "list");
 
-                Element flds = addProperty(doc, idxBean, "fields", null);
+                        firstIdx = false;
+                    }
 
-                Element fldsMap = addElement(doc, flds, "map");
+                    Element idxBean = addBean(doc, list, QueryIndex.class);
 
-                Map<String, Boolean> idxFlds = idx.getFields();
+                    addProperty(doc, idxBean, "name", idx.getName());
 
-                for (Map.Entry<String, Boolean> fld : idxFlds.entrySet())
-                    addElement(doc, fldsMap, "entry", "key", fld.getKey(), "value", fld.getValue().toString());
+                    Element idxType = addProperty(doc, idxBean, "indexType", null);
+                    addElement(doc, idxType, "util:constant", "static-field", "org.apache.ignite.cache.QueryIndexType."
+ idx.getIndexType());
+
+                    Element flds = addProperty(doc, idxBean, "fields", null);
+
+                    Element fldsMap = addElement(doc, flds, "map");
+
+                    for (T2<String, Boolean> fld : idxFlds)
+                        addElement(doc, fldsMap, "entry", "key", fld.getKey(), "value", fld.getValue().toString());
+                }
             }
         }
     }
@@ -276,9 +306,11 @@ public class XmlGenerator {
 
         addProperty(doc, bean, "valueType", pkg + "." + pojo.valueClassName());
 
-        addQueryFields(doc, bean, pojo.fields());
+        Collection<PojoField> fields = pojo.valueFields(true);
+
+        addQueryFields(doc, bean, fields);
 
-        addQueryIndexes(doc, bean, pojo.indexes());
+        addQueryIndexes(doc, bean, fields, pojo.indexes());
     }
 
     /**


Mime
View raw message