timoninmaxim commented on a change in pull request #8490:
URL: https://github.com/apache/ignite/pull/8490#discussion_r601295792



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/SortedSegmentedIndex.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.sorted;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.cache.query.index.Index;
+import org.apache.ignite.internal.util.lang.GridCursor;
+import org.apache.ignite.spi.indexing.IndexingQueryFilter;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Interface for sorted and segmented Ignite indexes.
+ */
+public interface SortedSegmentedIndex extends Index {
+    /**
+     * Finds index rows by specified range in specifed tree segment. Range can 
be bound or unbound.

Review comment:
       fixed

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/IndexFactory.java
##########
@@ -0,0 +1,34 @@
+/*
+ * 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 org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Base interface for Ignite index factories.
+ */
+public interface IndexFactory {
+    /**
+     * Creates index by specifed defition for specified cache.

Review comment:
       fixed

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/SingleCursor.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.util.lang.GridCursor;
+
+/**
+ * Cursor that holds single value only.
+ * @param <T> class of value to return.
+ */
+public class SingleCursor<T> implements GridCursor<T> {
+    /** Value to return */
+    private final T val;
+
+    /** Counter ot check wherther value is already got. */

Review comment:
       fixed

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/defragmentation/DefragIndexFactory.java
##########
@@ -0,0 +1,262 @@
+/*
+ * 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.sorted.defragmentation;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRow;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRowImpl;
+import 
org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler;
+import 
org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandlerFactory;
+import org.apache.ignite.internal.cache.query.index.sorted.MetaPageInfo;
+import 
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexFactory;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexKeyType;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexTree;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineRecommender;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineInnerIO;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineLeafIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineIO;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineInnerIO;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineLeafIO;
+import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import 
org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter;
+import org.apache.ignite.internal.processors.cache.persistence.RootPage;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+
+/**
+ * Creates temporary index to defragment old index.
+ */
+public class DefragIndexFactory extends InlineIndexFactory {
+    /** Temporary offheap manager. */
+    private final IgniteCacheOffheapManager offheap;
+
+    /** Old index. */
+    private final InlineIndex oldIdx;
+
+    /** Temporary cache page memory. */
+    private final PageMemory newCachePageMemory;
+
+    /** */
+    private final InlineIndexRowHandlerFactory rowHndFactory;
+
+    /** */
+    public DefragIndexFactory(IgniteCacheOffheapManager offheap, PageMemory 
newCachePageMemory, InlineIndex oldIdx) {
+        // Row handler factory that produces no-op handler.
+        rowHndFactory = (def, settings) -> oldIdx.segment(0).rowHandler();
+
+        this.offheap = offheap;
+        this.oldIdx = oldIdx;
+        this.newCachePageMemory = newCachePageMemory;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected InlineIndexTree createIndexSegment(GridCacheContext<?, 
?> cctx, SortedIndexDefinition def,
+        RootPage rootPage, IoStatisticsHolder stats, InlineRecommender 
recommender, int segmentNum) throws Exception {
+
+        InlineIndexTree tree = new InlineIndexTree(
+            def,
+            cctx,
+            def.treeName(),
+            offheap,
+            offheap.reuseListForIndex(def.treeName()),
+            newCachePageMemory,
+            // Use old row handler to have access to inline index key types.
+            pageIoResolver(),
+            rootPage.pageId().pageId(),
+            rootPage.isAllocated(),
+            oldIdx.inlineSize(),
+            def.keyTypeSettings(),
+            null,
+            stats,
+            rowHndFactory,
+            null
+        );
+
+        final MetaPageInfo oldInfo = oldIdx.segment(segmentNum).metaInfo();
+
+        tree.copyMetaInfo(oldInfo);
+
+        tree.enableSequentialWriteMode();
+
+        return tree;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected RootPage rootPage(GridCacheContext<?, ?> ctx, String 
treeName, int segment) throws Exception {
+        return offheap.rootPageForIndex(ctx.cacheId(), treeName, segment);
+    }
+
+    /** */
+    private PageIoResolver pageIoResolver() {
+        return pageAddr -> {
+            PageIO io = 
PageIoResolver.DEFAULT_PAGE_IO_RESOLVER.resolve(pageAddr);
+
+            if (io instanceof BPlusMetaIO)
+                return io;
+
+            //noinspection unchecked,rawtypes,rawtypes
+            return wrap((BPlusIO)io, rowHndFactory.create(null, null));
+        };
+    }
+
+    /** */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    static BPlusIO<IndexRow> wrap(BPlusIO<IndexRow> io, InlineIndexRowHandler 
rowHnd) {
+        assert io instanceof InlineIO;
+
+        if (io instanceof BPlusInnerIO) {
+            assert io instanceof AbstractInlineInnerIO
+                || io instanceof InlineInnerIO;
+
+            return new BPlusInnerIoDelegate((BPlusInnerIO<IndexRow>)io, 
rowHnd);
+        }
+        else {
+            assert io instanceof AbstractInlineLeafIO
+                || io instanceof InlineLeafIO;
+
+            return new BPlusLeafIoDelegate((BPlusLeafIO<IndexRow>)io, rowHnd);
+        }
+    }
+
+    /** */
+    private static <T extends BPlusIO<IndexRow> & InlineIO> IndexRow lookupRow(
+        InlineIndexRowHandler rowHnd,
+        long pageAddr,
+        int idx,
+        T io
+    ) {
+        long link = io.link(pageAddr, idx);
+
+        int off = io.offset(idx);
+
+        IndexKey[] keys = new IndexKey[rowHnd.indexKeyDefinitions().size()];
+
+        int fieldOff = 0;
+
+        for (int i = 0; i < rowHnd.inlineIndexKeyTypes().size(); i++) {
+            InlineIndexKeyType keyType = rowHnd.inlineIndexKeyTypes().get(i);
+
+            IndexKey key = keyType.get(pageAddr, off + fieldOff, 
io.inlineSize() - fieldOff);
+
+            fieldOff += keyType.inlineSize(key);
+
+            keys[i] = key;
+        }
+
+        return new IndexRowImpl(rowHnd, new CacheDataRowAdapter(link), keys);
+    }
+
+    /** */
+    private static class BPlusInnerIoDelegate<IO extends 
BPlusInnerIO<IndexRow> & InlineIO>
+        extends BPlusInnerIO<IndexRow> implements InlineIO {
+        /** */
+        private final IO io;
+
+        /** */
+        private final InlineIndexRowHandler rowHnd;
+
+        /** */
+        public BPlusInnerIoDelegate(IO io, InlineIndexRowHandler rowHnd) {

Review comment:
       fixed.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/defragmentation/DefragIndexFactory.java
##########
@@ -0,0 +1,262 @@
+/*
+ * 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.sorted.defragmentation;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRow;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRowImpl;
+import 
org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler;
+import 
org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandlerFactory;
+import org.apache.ignite.internal.cache.query.index.sorted.MetaPageInfo;
+import 
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexFactory;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexKeyType;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexTree;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineRecommender;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineInnerIO;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineLeafIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineIO;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineInnerIO;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineLeafIO;
+import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import 
org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter;
+import org.apache.ignite.internal.processors.cache.persistence.RootPage;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+
+/**
+ * Creates temporary index to defragment old index.
+ */
+public class DefragIndexFactory extends InlineIndexFactory {
+    /** Temporary offheap manager. */
+    private final IgniteCacheOffheapManager offheap;
+
+    /** Old index. */
+    private final InlineIndex oldIdx;
+
+    /** Temporary cache page memory. */
+    private final PageMemory newCachePageMemory;
+
+    /** */
+    private final InlineIndexRowHandlerFactory rowHndFactory;
+
+    /** */
+    public DefragIndexFactory(IgniteCacheOffheapManager offheap, PageMemory 
newCachePageMemory, InlineIndex oldIdx) {
+        // Row handler factory that produces no-op handler.
+        rowHndFactory = (def, settings) -> oldIdx.segment(0).rowHandler();
+
+        this.offheap = offheap;
+        this.oldIdx = oldIdx;
+        this.newCachePageMemory = newCachePageMemory;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected InlineIndexTree createIndexSegment(GridCacheContext<?, 
?> cctx, SortedIndexDefinition def,
+        RootPage rootPage, IoStatisticsHolder stats, InlineRecommender 
recommender, int segmentNum) throws Exception {
+
+        InlineIndexTree tree = new InlineIndexTree(
+            def,
+            cctx,
+            def.treeName(),
+            offheap,
+            offheap.reuseListForIndex(def.treeName()),
+            newCachePageMemory,
+            // Use old row handler to have access to inline index key types.
+            pageIoResolver(),
+            rootPage.pageId().pageId(),
+            rootPage.isAllocated(),
+            oldIdx.inlineSize(),
+            def.keyTypeSettings(),
+            null,
+            stats,
+            rowHndFactory,
+            null
+        );
+
+        final MetaPageInfo oldInfo = oldIdx.segment(segmentNum).metaInfo();
+
+        tree.copyMetaInfo(oldInfo);
+
+        tree.enableSequentialWriteMode();
+
+        return tree;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected RootPage rootPage(GridCacheContext<?, ?> ctx, String 
treeName, int segment) throws Exception {
+        return offheap.rootPageForIndex(ctx.cacheId(), treeName, segment);
+    }
+
+    /** */
+    private PageIoResolver pageIoResolver() {
+        return pageAddr -> {
+            PageIO io = 
PageIoResolver.DEFAULT_PAGE_IO_RESOLVER.resolve(pageAddr);
+
+            if (io instanceof BPlusMetaIO)
+                return io;
+
+            //noinspection unchecked,rawtypes,rawtypes
+            return wrap((BPlusIO)io, rowHndFactory.create(null, null));
+        };
+    }
+
+    /** */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    static BPlusIO<IndexRow> wrap(BPlusIO<IndexRow> io, InlineIndexRowHandler 
rowHnd) {
+        assert io instanceof InlineIO;
+
+        if (io instanceof BPlusInnerIO) {
+            assert io instanceof AbstractInlineInnerIO
+                || io instanceof InlineInnerIO;
+
+            return new BPlusInnerIoDelegate((BPlusInnerIO<IndexRow>)io, 
rowHnd);
+        }
+        else {
+            assert io instanceof AbstractInlineLeafIO
+                || io instanceof InlineLeafIO;
+
+            return new BPlusLeafIoDelegate((BPlusLeafIO<IndexRow>)io, rowHnd);
+        }
+    }
+
+    /** */
+    private static <T extends BPlusIO<IndexRow> & InlineIO> IndexRow lookupRow(
+        InlineIndexRowHandler rowHnd,
+        long pageAddr,
+        int idx,
+        T io
+    ) {
+        long link = io.link(pageAddr, idx);
+
+        int off = io.offset(idx);
+
+        IndexKey[] keys = new IndexKey[rowHnd.indexKeyDefinitions().size()];
+
+        int fieldOff = 0;
+
+        for (int i = 0; i < rowHnd.inlineIndexKeyTypes().size(); i++) {
+            InlineIndexKeyType keyType = rowHnd.inlineIndexKeyTypes().get(i);
+
+            IndexKey key = keyType.get(pageAddr, off + fieldOff, 
io.inlineSize() - fieldOff);
+
+            fieldOff += keyType.inlineSize(key);
+
+            keys[i] = key;
+        }
+
+        return new IndexRowImpl(rowHnd, new CacheDataRowAdapter(link), keys);
+    }
+
+    /** */
+    private static class BPlusInnerIoDelegate<IO extends 
BPlusInnerIO<IndexRow> & InlineIO>
+        extends BPlusInnerIO<IndexRow> implements InlineIO {
+        /** */
+        private final IO io;
+
+        /** */
+        private final InlineIndexRowHandler rowHnd;
+
+        /** */
+        public BPlusInnerIoDelegate(IO io, InlineIndexRowHandler rowHnd) {

Review comment:
       fixed.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/io/AbstractLeafIO.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.sorted.inline.io;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRow;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexRowImpl;
+import 
org.apache.ignite.internal.cache.query.index.sorted.ThreadLocalRowHandlerHolder;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexTree;
+import org.apache.ignite.internal.pagemem.PageUtils;
+import org.apache.ignite.internal.processors.cache.CacheGroupContext;
+import 
org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO;
+
+/**
+ * Leaf page to store index rows.
+ */
+public abstract class AbstractLeafIO extends BPlusLeafIO<IndexRow> implements 
InlineIO {
+    /**
+     * @param type Page type.
+     * @param ver Page format version.
+     * @param itemSize Single item size on page.
+     */
+    AbstractLeafIO(int type, int ver, int itemSize) {
+        super(type, ver, itemSize);
+    }
+

Review comment:
       fixed.




-- 
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:
[email protected]


Reply via email to