You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/05/31 15:17:22 UTC

[GitHub] [ignite] tledkov-gridgain commented on a change in pull request #9118: IGNITE-14699 Add IndexQuery API.

tledkov-gridgain commented on a change in pull request #9118:
URL: https://github.com/apache/ignite/pull/9118#discussion_r642557531



##########
File path: modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/IndexQueryProcessor.java
##########
@@ -0,0 +1,404 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.cache.query.index;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.NoSuchElementException;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.cache.query.IndexQuery;
+import org.apache.ignite.internal.cache.query.IndexCondition;
+import org.apache.ignite.internal.cache.query.RangeIndexCondition;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRow;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRowComparator;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexSearchRowImpl;
+import org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler;
+import org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.IndexQueryContext;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex;
+import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey;
+import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKeyFactory;
+import org.apache.ignite.internal.processors.cache.CacheObjectContext;
+import org.apache.ignite.internal.processors.cache.CacheObjectUtils;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.query.IndexQueryDesc;
+import org.apache.ignite.internal.util.GridCloseableIteratorAdapter;
+import org.apache.ignite.internal.util.lang.GridCloseableIterator;
+import org.apache.ignite.internal.util.lang.GridCursor;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteBiTuple;
+
+/**
+ * Processor of {@link IndexQuery}.
+ */
+public class IndexQueryProcessor {
+    /** */
+    private final IndexProcessor idxProc;
+
+    /** */
+    public IndexQueryProcessor(IndexProcessor idxProc) {
+        this.idxProc = idxProc;
+    }
+
+    /** Run query on local node. */
+    public <K, V> GridCloseableIterator<IgniteBiTuple<K, V>> queryLocal(
+        GridCacheContext<K, V> cctx, IndexQueryDesc idxQryDesc, IndexQueryContext qryCtx, boolean keepBinary)
+        throws IgniteCheckedException {
+
+        Index idx = index(cctx, idxQryDesc);
+
+        if (idx == null)
+            throw new IgniteCheckedException(
+                "No index matches index query. Cache=" + cctx.name() + "; Qry=" + idxQryDesc);
+
+        GridCursor<IndexRow> cursor = query(cctx, idx, idxQryDesc.idxCond(), qryCtx);
+
+        // Map IndexRow to Cache Key-Value pair.
+        return new GridCloseableIteratorAdapter<IgniteBiTuple<K, V>>() {
+            private IndexRow currVal;
+
+            private final CacheObjectContext coctx = cctx.cacheObjectContext();
+
+            /** {@inheritDoc} */
+            @Override protected boolean onHasNext() throws IgniteCheckedException {
+                if (currVal != null)
+                    return true;
+
+                if (!cursor.next())
+                    return false;
+
+                currVal = cursor.get();
+
+                return true;
+            }
+
+            /** {@inheritDoc} */
+            @Override protected IgniteBiTuple<K, V> onNext() {
+                if (currVal == null)
+                    if (!hasNext())
+                        throw new NoSuchElementException();
+
+                IndexRow row = currVal;
+
+                currVal = null;
+
+                K k = (K) CacheObjectUtils.unwrapBinaryIfNeeded(coctx, row.cacheDataRow().key(), keepBinary, false);
+                V v = (V) CacheObjectUtils.unwrapBinaryIfNeeded(coctx, row.cacheDataRow().value(), keepBinary, false);
+
+                return new IgniteBiTuple<>(k, v);
+            }
+        };
+    }
+
+    /** Get index to run query by specified description. */
+    private Index index(GridCacheContext cctx, IndexQueryDesc idxQryDesc) throws IgniteCheckedException {
+        Class<?> valCls = idxQryDesc.valCls() != null ? loadValClass(cctx, idxQryDesc.valCls()) : null;
+
+        String tableName = cctx.kernalContext().query().tableName(cctx.name(), valCls);
+
+        if (tableName == null)
+            return null;
+
+        // Find index by specified name.
+        if (idxQryDesc.idxName() != null) {
+            String name = "_key_PK".equals(idxQryDesc.idxName()) ? "_key_PK" : idxQryDesc.idxName().toUpperCase();
+
+            String schema = idxQryDesc.schema() == null ? cctx.name() : idxQryDesc.schema();
+
+            IndexName idxName = new IndexName(cctx.name(), schema, tableName, name);
+
+            Index idx = idxProc.index(idxName);
+
+            if (idx == null)
+                return null;
+
+            return checkIndex(idxProc.indexDefinition(idx.id()), idxQryDesc.idxCond()) ? idx : null;
+        }
+
+        // Try get index by list of fields to query.

Review comment:
       The logic of choose index by IndexQuery may confuse user in case multiple-filed index. I propose to think about QueryIndex as low-level API for index access. I guess public Index interface and find/scan methods will be more appropriate. 
   
   Let's disallow the creation of a IndexQuery without explicitly specifying the index.
   
   I'll explain my point of view.
   
   The choice of the index is the work of the SKL optimizer. The optimizer can use statistics and other metadata.
   
   Duplicating optimizer logic is a bad idea.
   It may be surprising for the user to get two different indexes in the SQL optimizer and IndexQuery for the same set of conditions.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org