You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2017/03/15 10:48:24 UTC

ignite git commit: Better index management within QueryTypeDescriptorImpl.

Repository: ignite
Updated Branches:
  refs/heads/ignite-4565-ddl 3fa1652e7 -> e35c8eaf0


Better index management within QueryTypeDescriptorImpl.


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

Branch: refs/heads/ignite-4565-ddl
Commit: e35c8eaf028e56769456a52fa373ae97c577a052
Parents: 3fa1652
Author: devozerov <vo...@gridgain.com>
Authored: Wed Mar 15 13:48:15 2017 +0300
Committer: devozerov <vo...@gridgain.com>
Committed: Wed Mar 15 13:48:15 2017 +0300

----------------------------------------------------------------------
 .../query/QueryIndexDescriptorImpl.java         | 11 +++-
 .../query/QueryTypeDescriptorImpl.java          | 44 +++++----------
 .../internal/processors/query/QueryUtils.java   | 57 ++++++++++++++------
 3 files changed, 63 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e35c8eaf/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryIndexDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryIndexDescriptorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryIndexDescriptorImpl.java
index 75dea0e..7fc52ec 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryIndexDescriptorImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryIndexDescriptorImpl.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.processors.query;
 
+import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.cache.QueryIndexType;
 import org.apache.ignite.internal.util.typedef.T2;
 import org.apache.ignite.internal.util.typedef.internal.S;
@@ -104,8 +105,14 @@ public class QueryIndexDescriptorImpl implements GridQueryIndexDescriptor {
      * @param field Field name.
      * @param orderNum Field order number in this index.
      * @param descending Sort order.
+     * @return This instance for chaining.
+     * @throws IgniteCheckedException If failed.
      */
-    public void addField(String field, int orderNum, boolean descending) {
+    public QueryIndexDescriptorImpl  addField(String field, int orderNum, boolean descending)
+        throws IgniteCheckedException {
+        if (!typDesc.hasField(field))
+            throw new IgniteCheckedException("Field not found: " + field);
+
         fields.add(new T2<>(field, orderNum));
 
         if (descending) {
@@ -114,6 +121,8 @@ public class QueryIndexDescriptorImpl implements GridQueryIndexDescriptor {
 
             descendings.add(field);
         }
+
+        return this;
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/e35c8eaf/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
index c4e006e..8cc9c6c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
@@ -221,21 +221,15 @@ public class QueryTypeDescriptorImpl implements GridQueryTypeDescriptor {
     }
 
     /**
-     * Adds index.
+     * Add index.
      *
-     * @param idxName Index name.
-     * @param type Index type.
-     * @return Index descriptor.
-     * @throws IgniteCheckedException In case of error.
+     * @param idx Index.
+     * @throws IgniteCheckedException If failed.
      */
-    public QueryIndexDescriptorImpl addIndex(String idxName, QueryIndexType type) throws IgniteCheckedException {
+    public void addIndex(QueryIndexDescriptorImpl idx) throws IgniteCheckedException {
         synchronized (idxMux) {
-            QueryIndexDescriptorImpl idx = new QueryIndexDescriptorImpl(this, idxName, type);
-
-            if (idxs.put(idxName, idx) != null)
-                throw new IgniteCheckedException("Index with name '" + idxName + "' already exists.");
-
-            return idx;
+            if (idxs.put(idx.name(), idx) != null)
+                throw new IgniteCheckedException("Index with name '" + idx.name() + "' already exists.");
         }
     }
 
@@ -251,34 +245,22 @@ public class QueryTypeDescriptorImpl implements GridQueryTypeDescriptor {
     }
 
     /**
-     * Adds field to index.
+     * Chedk if particular field exists.
      *
-     * @param idxName Index name.
-     * @param field Field name.
-     * @param orderNum Fields order number in index.
-     * @param descending Sorting order.
-     * @throws IgniteCheckedException If failed.
+     * @param field Field.
+     * @return {@code True} if exists.
      */
-    public void addFieldToIndex(String idxName, String field, int orderNum, boolean descending)
-        throws IgniteCheckedException {
-        synchronized (idxMux) {
-            if (!props.containsKey(field))
-                throw new IgniteCheckedException("Property not found: " + field);
-
-            QueryIndexDescriptorImpl desc = idxs.get(idxName);
-
-            assert desc != null;
-
-            desc.addField(field, orderNum, descending);
-        }
+    public boolean hasField(String field) {
+        return props.containsKey(field);
     }
 
     /**
      * Adds field to text index.
      *
      * @param field Field name.
+     * @throws IgniteCheckedException If failed.
      */
-    public void addFieldToTextIndex(String field) {
+    public void addFieldToTextIndex(String field) throws IgniteCheckedException {
         if (fullTextIdx == null)
             fullTextIdx = new QueryIndexDescriptorImpl(this, null, QueryIndexType.FULLTEXT);
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/e35c8eaf/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java
index 54fa1cd..6a27e94 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java
@@ -46,6 +46,7 @@ import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.sql.Time;
 import java.sql.Timestamp;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -324,14 +325,19 @@ public class QueryUtils {
         assert keyCls != null;
         assert valCls != null;
 
+        Map<String, QueryIndexDescriptorImpl> idxDescs = new HashMap<>();
+
         for (Map.Entry<String, Class<?>> entry : meta.getAscendingFields().entrySet())
-            addToIndex(d, keyCls, valCls, entry.getKey(), entry.getValue(), 0, IndexType.ASC, null, d.aliases(), coCtx);
+            addToIndex(d, idxDescs, keyCls, valCls, entry.getKey(), entry.getValue(), 0, IndexType.ASC, null,
+                d.aliases(), coCtx);
 
         for (Map.Entry<String, Class<?>> entry : meta.getDescendingFields().entrySet())
-            addToIndex(d, keyCls, valCls, entry.getKey(), entry.getValue(), 0, IndexType.DESC, null, d.aliases(), coCtx);
+            addToIndex(d, idxDescs, keyCls, valCls, entry.getKey(), entry.getValue(), 0, IndexType.DESC, null,
+                d.aliases(), coCtx);
 
         for (String txtField : meta.getTextFields())
-            addToIndex(d, keyCls, valCls, txtField, String.class, 0, IndexType.TEXT, null, d.aliases(), coCtx);
+            addToIndex(d, idxDescs, keyCls, valCls, txtField, String.class, 0, IndexType.TEXT, null, d.aliases(),
+                coCtx);
 
         Map<String, LinkedHashMap<String, IgniteBiTuple<Class<?>, Boolean>>> grps = meta.getGroups();
 
@@ -349,7 +355,7 @@ public class QueryUtils {
                     if (descending == null)
                         descending = false;
 
-                    addToIndex(d, keyCls, valCls, idxField.getKey(), idxField.getValue().get1(), order,
+                    addToIndex(d, idxDescs, keyCls, valCls, idxField.getKey(), idxField.getValue().get1(), order,
                         descending ? IndexType.DESC : IndexType.ASC, idxName, d.aliases(), coCtx);
 
                     order++;
@@ -368,10 +374,14 @@ public class QueryUtils {
 
             d.addProperty(prop, false);
         }
+
+        for (QueryIndexDescriptorImpl idxDesc : idxDescs.values())
+            d.addIndex(idxDesc);
     }
     
     /**
      * @param d Type descriptor.
+     * @param idxDescs Index descriptors.
      * @param keyCls Key class.
      * @param valCls Value class.
      * @param pathStr Path string.
@@ -384,6 +394,7 @@ public class QueryUtils {
      */
     private static void addToIndex(
         QueryTypeDescriptorImpl d,
+        Map<String, QueryIndexDescriptorImpl> idxDescs,
         Class<?> keyCls,
         Class<?> valCls,
         String pathStr,
@@ -423,10 +434,20 @@ public class QueryUtils {
             if (idxType == IndexType.TEXT)
                 d.addFieldToTextIndex(propName);
             else {
-                if (idxOrder == 0) // Add index only on the first field.
-                    d.addIndex(idxName, isGeometryClass(propCls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
+                QueryIndexDescriptorImpl idxDesc = idxDescs.get(idxName);
+
+                if (idxDesc == null) {
+                    assert idxOrder == 0;
+
+                    QueryIndexType idxTyp =
+                        isGeometryClass(propCls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED;
+
+                    idxDesc = new QueryIndexDescriptorImpl(d, idxName, idxTyp);
 
-                d.addFieldToIndex(idxName, propName, idxOrder, idxType == IndexType.DESC);
+                    idxDescs.put(idxName, idxDesc);
+                }
+
+                idxDesc.addField(propName, idxOrder, idxType == IndexType.DESC);
             }
         }
     }
@@ -448,10 +469,9 @@ public class QueryUtils {
             d.addProperty(prop, false);
 
             String idxName = prop.name() + "_idx";
+            QueryIndexType idxTyp = isGeometryClass(prop.type()) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED;
 
-            d.addIndex(idxName, isGeometryClass(prop.type()) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
-
-            d.addFieldToIndex(idxName, prop.name(), 0, false);
+            d.addIndex(new QueryIndexDescriptorImpl(d, idxName, idxTyp).addField(prop.name(), 0, false));
         }
 
         for (Map.Entry<String, Class<?>> entry : meta.getDescendingFields().entrySet()) {
@@ -460,10 +480,9 @@ public class QueryUtils {
             d.addProperty(prop, false);
 
             String idxName = prop.name() + "_idx";
+            QueryIndexType idxTyp = isGeometryClass(prop.type()) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED;
 
-            d.addIndex(idxName, isGeometryClass(prop.type()) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
-
-            d.addFieldToIndex(idxName, prop.name(), 0, true);
+            d.addIndex(new QueryIndexDescriptorImpl(d, idxName, idxTyp).addField(prop.name(), 0, true));
         }
 
         for (String txtIdx : meta.getTextFields()) {
@@ -483,7 +502,7 @@ public class QueryUtils {
                 LinkedHashMap<String, IgniteBiTuple<Class<?>, Boolean>> idxFields = entry.getValue();
 
                 if (!idxFields.isEmpty()) {
-                    d.addIndex(idxName, QueryIndexType.SORTED);
+                    QueryIndexDescriptorImpl idxDesc = new QueryIndexDescriptorImpl(d, idxName, QueryIndexType.SORTED);
 
                     int order = 0;
 
@@ -495,10 +514,12 @@ public class QueryUtils {
 
                         Boolean descending = idxField.getValue().get2();
 
-                        d.addFieldToIndex(idxName, prop.name(), order, descending != null && descending);
+                        idxDesc.addField(prop.name(), order, descending != null && descending);
 
                         order++;
                     }
+
+                    d.addIndex(idxDesc);
                 }
             }
         }
@@ -626,7 +647,7 @@ public class QueryUtils {
         QueryIndexType idxTyp = idx.getIndexType();
 
         if (idxTyp == QueryIndexType.SORTED || idxTyp == QueryIndexType.GEOSPATIAL) {
-            d.addIndex(idxName, idxTyp);
+            QueryIndexDescriptorImpl idxDesc = new QueryIndexDescriptorImpl(d, idxName, idxTyp);
 
             int i = 0;
 
@@ -639,8 +660,10 @@ public class QueryUtils {
                 if (alias != null)
                     field = alias;
 
-                d.addFieldToIndex(idxName, field, i++, !asc);
+                idxDesc.addField(field, i++, !asc);
             }
+
+            d.addIndex(idxDesc);
         }
         else if (idxTyp == QueryIndexType.FULLTEXT){
             for (String field : idx.getFields().keySet()) {