IGNITE-2856 Use java name for index field and add index only if all index 
fields described as query fields.


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

Branch: refs/heads/ignite-2630
Commit: 87f78fb51777cb6cd04710d676487cd7599b49aa
Parents: 922b75f
Author: Alexey Kuznetsov <[email protected]>
Authored: Mon Apr 4 16:17:54 2016 +0700
Committer: Alexey Kuznetsov <[email protected]>
Committed: Mon Apr 4 16:17:54 2016 +0700

----------------------------------------------------------------------
 .../ignite/schema/generator/CodeGenerator.java  | 96 ++++++++++++++------
 1 file changed, 68 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/87f78fb5/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 769c7d9..1f2f2ab 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
@@ -35,7 +35,7 @@ import java.util.Set;
 import java.util.regex.Pattern;
 
 import org.apache.ignite.cache.QueryIndex;
-import org.apache.ignite.internal.util.typedef.F;
+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;
@@ -556,6 +556,21 @@ 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.
@@ -666,48 +681,73 @@ public class CodeGenerator {
             Collection<QueryIndex> idxs = pojo.indexes();
 
             if (!idxs.isEmpty()) {
-                add2(src, "// Indexes for " + tbl + ".");
-                add2(src, "Collection<QueryIndex> idxs = new ArrayList<>();");
-                add0(src, "");
-
+                boolean first = true;
                 boolean firstIdx = true;
 
                 for (QueryIndex idx : idxs) {
-                    if (idx.getFields().size() == 1) {
-                        Map.Entry<String, Boolean> fld = 
F.first(idx.getFields().entrySet());
+                    Set<Map.Entry<String, Boolean>> dbIdxFlds = 
idx.getFields().entrySet();
+
+                    int sz = dbIdxFlds.size();
 
-                        add2(src, "idxs.add(new QueryIndex(\"" + fld.getKey() 
+ "\", " + fld.getValue() + ", \"" +
-                            idx.getName() + "\"));");
-                        add0(src, "");
+                    List<T2<String, Boolean>> idxFlds = new ArrayList<>(sz);
+
+                    for (Map.Entry<String, Boolean> idxFld : dbIdxFlds) {
+                        PojoField field = findFieldByName(pojo, 
idxFld.getKey());
+
+                        if (field != null)
+                            idxFlds.add(new T2<>(field.javaName(), 
idxFld.getValue()));
+                        else
+                            break;
                     }
-                    else {
-                        add2(src, (firstIdx ? "QueryIndex " : "") + "idx = new 
QueryIndex();");
-                        add0(src, "");
 
-                        add2(src, "idx.setName(\"" + idx.getName() + "\");");
-                        add0(src, "");
+                    // Only if all fields present, add index description.
+                    if (idxFlds.size() == sz) {
+                        if (first) {
+                            add2(src, "// Indexes for " + tbl + ".");
+                            add2(src, "Collection<QueryIndex> idxs = new 
ArrayList<>();");
+                            add0(src, "");
+                        }
+
+                        if (sz == 1) {
+                            T2<String, Boolean> idxFld = idxFlds.get(0);
+
+                            add2(src, "idxs.add(new QueryIndex(\"" + 
idxFld.getKey() + "\", " + idxFld.getValue() + ", \"" +
+                                idx.getName() + "\"));");
+                            add0(src, "");
+                        }
+                        else {
+                            add2(src, (firstIdx ? "QueryIndex " : "") + "idx = 
new QueryIndex();");
+                            add0(src, "");
 
-                        add2(src, (firstIdx ? "LinkedHashMap<String, Boolean> 
" : "") +
-                            "idxFlds = new LinkedHashMap<>();");
-                        add0(src, "");
+                            add2(src, "idx.setName(\"" + idx.getName() + 
"\");");
+                            add0(src, "");
 
-                        for (Map.Entry<String, Boolean> idxFld : 
idx.getFields().entrySet())
-                            add2(src, "idxFlds.put(\"" + idxFld.getKey() + 
"\", " + idxFld.getValue() + ");");
+                            add2(src, (firstIdx ? "LinkedHashMap<String, 
Boolean> " : "") +
+                                "idxFlds = new LinkedHashMap<>();");
+                            add0(src, "");
 
-                        add0(src, "");
+                            for (T2<String, Boolean> idxFld : idxFlds)
+                                add2(src, "idxFlds.put(\"" + idxFld.getKey() + 
"\", " + idxFld.getValue() + ");");
 
-                        add2(src, "idx.setFields(idxFlds);");
-                        add0(src, "");
+                            add0(src, "");
 
-                        add2(src, "idxs.add(idx);");
-                        add0(src, "");
+                            add2(src, "idx.setFields(idxFlds);");
+                            add0(src, "");
 
-                        firstIdx = false;
+                            add2(src, "idxs.add(idx);");
+                            add0(src, "");
+
+                            firstIdx = false;
+                        }
+
+                        first = false;
                     }
                 }
 
-                add2(src, "qryEntity.setIndexes(idxs);");
-                add0(src, "");
+                if (!first) {
+                    add2(src, "qryEntity.setIndexes(idxs);");
+                    add0(src, "");
+                }
             }
 
             add2(src, "return qryEntity;");

Reply via email to