korlov42 commented on a change in pull request #8490:
URL: https://github.com/apache/ignite/pull/8490#discussion_r571664421
##########
File path:
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
##########
@@ -464,41 +427,59 @@ public GridKernalContext kernalContext() {
* @param unwrappedCols Unwrapped index columns for complex types.
* @param wrappedCols Index columns as is complex types.
* @param inlineSize Index inline size.
+ * @param cacheVisitor whether index created with new cache or on existing
one.
* @return Index.
*/
@SuppressWarnings("ConstantConditions")
GridH2IndexBase createSortedIndex(String name, GridH2Table tbl, boolean
pk, boolean affinityKey,
- List<IndexColumn> unwrappedCols, List<IndexColumn> wrappedCols, int
inlineSize) {
- try {
- GridCacheContextInfo cacheInfo = tbl.cacheInfo();
-
- if (log.isDebugEnabled())
- log.debug("Creating cache index [cacheId=" +
cacheInfo.cacheId() + ", idxName=" + name + ']');
-
- if (cacheInfo.affinityNode()) {
- final int segments =
tbl.rowDescriptor().context().config().getQueryParallelism();
-
- H2RowCache cache = rowCache.forGroup(cacheInfo.groupId());
-
- return H2TreeIndex.createIndex(
- cacheInfo.cacheContext(),
- cache,
- tbl,
- name,
- pk,
- affinityKey,
- unwrappedCols,
- wrappedCols,
- inlineSize,
- segments,
- log
- );
- }
+ List<IndexColumn> unwrappedCols, List<IndexColumn> wrappedCols, int
inlineSize, @Nullable SchemaIndexCacheVisitor cacheVisitor) {
+ GridCacheContextInfo cacheInfo = tbl.cacheInfo();
+
+ if (log.isDebugEnabled())
+ log.debug("Creating cache index [cacheId=" + cacheInfo.cacheId() +
", idxName=" + name + ']');
+
+ QueryIndexSchema schemaUnwrapped = new QueryIndexSchema(
+ tbl, unwrappedCols.toArray(new IndexColumn[0]));
+
+ QueryIndexSchema schemaWrapped = new QueryIndexSchema(
+ tbl, wrappedCols.toArray(new IndexColumn[0]));
+
+ if (cacheInfo.affinityNode()) {
+ SortedIndexDefinition idxDef = new QueryIndexDefinition(
Review comment:
It's better to have an idxDefProvider at this point, because a final
index definition will be available after metaPage is read (it depends on
flags). Otherwise it bring a compatibility issues that will lead to an index
tree corruption.
Here are some rules to preserve the compatibility:
- if `inlineObjects` flag is set to `false` then columns with type
`JAVA_OBJECT` should be omitted. For example index from `(int, JO, int)`
becomes `(int, int)`
- if there is a type that currently unsupported, index's tail should be
truncated up to this column. For example for index `(int, int, decimal, int,
int)` only first two columns should be preserved.
- if inlining of object's hash is not supported then `JAVA_OBJECT`'s should
be treated as array of bytes
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/InlineIndexTree.java
##########
@@ -0,0 +1,480 @@
+/*
+ * 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;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.cache.query.index.sorted.SortOrder;
+import org.apache.ignite.failure.FailureType;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition;
+import
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.SortedIndexSchema;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexRow;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexSearchRow;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.ThreadLocalSchemaHolder;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.FullPageId;
+import org.apache.ignite.internal.pagemem.PageIdAllocator;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.pagemem.wal.record.PageSnapshot;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException;
+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.IOVersions;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.CANT_BE_COMPARE;
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.COMPARE_UNSUPPORTED;
+
+/**
+ * BPlusTree where nodes stores inlined index keys.
+ */
+public class InlineIndexTree extends BPlusTree<IndexRow, IndexRow> {
+ /** Amount of bytes to store inlined index keys. */
+ private final int inlineSize;
+
+ /** Recommends change inline size if needed. */
+ private final InlineRecommender recommender;
+
+ /** Whether tree is created from scratch or reused from underlying store.
*/
+ private final boolean created;
+
+ /** Definition of index. */
+ private final SortedIndexDefinition def;
+
+ /** Cache context. */
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Statistics holder used by underlying BPlusTree. */
+ private final IoStatisticsHolder stats;
+
+ /**
+ * Constructor.
+ */
+ public InlineIndexTree(
+ SortedIndexDefinition def,
+ GridCacheContext<?, ?> cctx,
+ String treeName,
+ IgniteCacheOffheapManager offheap,
+ ReuseList reuseList,
+ PageMemory pageMemory,
+ PageIoResolver pageIoResolver,
+ long metaPageId,
+ boolean initNew,
+ int configuredInlineSize,
+ IoStatisticsHolder stats,
+ InlineRecommender recommender) throws IgniteCheckedException {
+ super(
+ treeName,
+ cctx.groupId(),
+ cctx.group().name(),
+ pageMemory,
+ cctx.shared().wal(),
+ offheap.globalRemoveId(),
+ metaPageId,
+ reuseList,
+ PageIdAllocator.FLAG_IDX,
+ cctx.shared().kernalContext().failure(),
+ null,
+ pageIoResolver
+ );
+
+ this.stats = stats;
+
+ created = initNew;
+
+ this.def = def;
+
+ if (!initNew) {
+ // Init from metastore
+ // Page is ready - read meta information.
+ MetaPageInfo metaInfo = getMetaInfo();
+
+ this.def.setUseUnwrappedPk(metaInfo.useUnwrappedPk());
+
+ inlineSize = metaInfo.inlineSize();
+
+ boolean inlineObjSupported = inlineSize > 0 &&
metaInfo.inlineObjectSupported();
+
+ if (!metaInfo.flagsSupported())
+ upgradeMetaPage(inlineObjSupported);
+
+ } else {
+ this.def.setUseUnwrappedPk(true);
+
+ inlineSize = computeInlineSize(
+ def.getSchema().getKeyDefinitions(), configuredInlineSize,
cctx.config().getSqlIndexMaxInlineSize());
+ }
+
+ if (inlineSize == 0)
+ setIos(InnerIO.VERSIONS, LeafIO.VERSIONS);
+ else
+ setIos(
+ // -1 is required as payload starts with 1, and indexes in
list of IOs are with 0.
+ (IOVersions<BPlusInnerIO<IndexRow>>)
PageIO.getInnerVersions(inlineSize - 1, false),
+ (IOVersions<BPlusLeafIO<IndexRow>>)
PageIO.getLeafVersions(inlineSize - 1, false));
+
+ initTree(initNew, inlineSize);
+
+ this.recommender = recommender;
+
+ this.cctx = cctx;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected int compare(BPlusIO<IndexRow> io, long pageAddr, int
idx, IndexRow row)
+ throws IgniteCheckedException {
+ IndexSearchRow r = (IndexSearchRow) row;
+
+ int searchKeysLength = r.getSearchKeysCount();
+
+ if (inlineSize == 0)
+ return compareFullRows(getRow(io, pageAddr, idx), row, 0,
searchKeysLength);
+
+ SortedIndexSchema schema = def.getSchema();
+
+ if ((schema.getKeyDefinitions().length != searchKeysLength) &&
r.isFullSchemaSearch())
+ throw new IgniteCheckedException("Find is configured for full
schema search.");
+
+ int fieldOff = 0;
+
+ // Use it when can't compare values (variable length, for example).
+ int lastIdxUsed = searchKeysLength;
+
+ for (int i = 0; i < searchKeysLength; i++) {
+ try {
+ // If a search key is null then skip other keys (consider that
null shows that we should get all
+ // possible keys for that comparison).
+ if (row.getKey(i) == null)
+ return 0;
+
+ // Other keys are not inlined. Should compare as rows.
+ if (i >= schema.getKeyDefinitions().length) {
+ lastIdxUsed = i;
+ break;
+ }
+
+ int maxSize = inlineSize - fieldOff;
+
+ int off = io.offset(idx);
+
+ IndexKeyDefinition keyDef = schema.getKeyDefinitions()[i];
+
+ if
(!InlineIndexKeyTypeRegistry.supportInline(keyDef.getIdxType())) {
+ lastIdxUsed = i;
+ break;
+ }
+
+ int cmp = COMPARE_UNSUPPORTED;
+
+ if
(!row.getKey(i).getClass().isAssignableFrom(keyDef.getIdxClass())) {
+ lastIdxUsed = i;
+ break;
+ }
+
+ InlineIndexKeyType keyType =
InlineIndexKeyTypeRegistry.get(keyDef.getIdxClass(), keyDef.getIdxType());
+
+ // By default do not compare different types.
+ if (InlineIndexKeyTypeRegistry.validate(keyDef.getIdxType(),
row.getKey(i).getClass()))
+ cmp = keyType.compare(pageAddr, off + fieldOff, maxSize,
row.getKey(i));
+
+ // Can't compare as inlined bytes are not enough for
comparation.
+ if (cmp == CANT_BE_COMPARE) {
+ lastIdxUsed = i;
+ break;
+ }
+
+ // Try compare stored values for inlined keys with different
approach?
+ if (cmp == COMPARE_UNSUPPORTED)
+ cmp = def.getRowComparator().compareKey(
+ pageAddr, off + fieldOff, maxSize, row.getKey(i),
keyType.type());
+
+ if (cmp == CANT_BE_COMPARE || cmp == COMPARE_UNSUPPORTED) {
+ lastIdxUsed = i;
+ break;
+ }
+
+ if (cmp != 0)
+ return applySortOrder(cmp,
schema.getKeyDefinitions()[i].getOrder().getSortOrder());
+
+ fieldOff += keyType.inlineSize(pageAddr, off + fieldOff);
+
+ } catch (Exception e) {
+ throw new IgniteException("Failed to store new index row.", e);
+ }
+ }
+
+ if (lastIdxUsed < searchKeysLength) {
+ recommender.recommend(row, inlineSize);
+
+ IndexRow currRow = getRow(io, pageAddr, idx);
+
+ for (int i = lastIdxUsed; i < searchKeysLength; i++) {
+ // If a search key is null then skip other keys (consider that
null shows that we should get all
+ // possible keys for that comparison).
+ if (row.getKey(i) == null)
+ return 0;
+
+ int c = def.getRowComparator().compareKey((IndexSearchRow)
currRow, (IndexSearchRow) row, i);
+
+ if (c != 0)
+ return applySortOrder(Integer.signum(c),
schema.getKeyDefinitions()[i].getOrder().getSortOrder());
+ }
Review comment:
```suggestion
return compareFullRows(currRow, row, lastIdxUsed,
searchKeysLength)
```
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/InlineIndexTree.java
##########
@@ -0,0 +1,480 @@
+/*
+ * 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;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.cache.query.index.sorted.SortOrder;
+import org.apache.ignite.failure.FailureType;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition;
+import
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.SortedIndexSchema;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexRow;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexSearchRow;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.ThreadLocalSchemaHolder;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.FullPageId;
+import org.apache.ignite.internal.pagemem.PageIdAllocator;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.pagemem.wal.record.PageSnapshot;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException;
+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.IOVersions;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.CANT_BE_COMPARE;
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.COMPARE_UNSUPPORTED;
+
+/**
+ * BPlusTree where nodes stores inlined index keys.
+ */
+public class InlineIndexTree extends BPlusTree<IndexRow, IndexRow> {
+ /** Amount of bytes to store inlined index keys. */
+ private final int inlineSize;
+
+ /** Recommends change inline size if needed. */
+ private final InlineRecommender recommender;
+
+ /** Whether tree is created from scratch or reused from underlying store.
*/
+ private final boolean created;
+
+ /** Definition of index. */
+ private final SortedIndexDefinition def;
+
+ /** Cache context. */
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Statistics holder used by underlying BPlusTree. */
+ private final IoStatisticsHolder stats;
+
+ /**
+ * Constructor.
+ */
+ public InlineIndexTree(
+ SortedIndexDefinition def,
+ GridCacheContext<?, ?> cctx,
+ String treeName,
+ IgniteCacheOffheapManager offheap,
+ ReuseList reuseList,
+ PageMemory pageMemory,
+ PageIoResolver pageIoResolver,
+ long metaPageId,
+ boolean initNew,
+ int configuredInlineSize,
+ IoStatisticsHolder stats,
+ InlineRecommender recommender) throws IgniteCheckedException {
+ super(
+ treeName,
+ cctx.groupId(),
+ cctx.group().name(),
+ pageMemory,
+ cctx.shared().wal(),
+ offheap.globalRemoveId(),
+ metaPageId,
+ reuseList,
+ PageIdAllocator.FLAG_IDX,
+ cctx.shared().kernalContext().failure(),
+ null,
+ pageIoResolver
+ );
+
+ this.stats = stats;
+
+ created = initNew;
+
+ this.def = def;
+
+ if (!initNew) {
+ // Init from metastore
+ // Page is ready - read meta information.
+ MetaPageInfo metaInfo = getMetaInfo();
+
+ this.def.setUseUnwrappedPk(metaInfo.useUnwrappedPk());
+
+ inlineSize = metaInfo.inlineSize();
+
+ boolean inlineObjSupported = inlineSize > 0 &&
metaInfo.inlineObjectSupported();
+
+ if (!metaInfo.flagsSupported())
+ upgradeMetaPage(inlineObjSupported);
+
+ } else {
+ this.def.setUseUnwrappedPk(true);
+
+ inlineSize = computeInlineSize(
+ def.getSchema().getKeyDefinitions(), configuredInlineSize,
cctx.config().getSqlIndexMaxInlineSize());
+ }
+
+ if (inlineSize == 0)
+ setIos(InnerIO.VERSIONS, LeafIO.VERSIONS);
+ else
+ setIos(
+ // -1 is required as payload starts with 1, and indexes in
list of IOs are with 0.
+ (IOVersions<BPlusInnerIO<IndexRow>>)
PageIO.getInnerVersions(inlineSize - 1, false),
+ (IOVersions<BPlusLeafIO<IndexRow>>)
PageIO.getLeafVersions(inlineSize - 1, false));
+
+ initTree(initNew, inlineSize);
+
+ this.recommender = recommender;
+
+ this.cctx = cctx;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected int compare(BPlusIO<IndexRow> io, long pageAddr, int
idx, IndexRow row)
+ throws IgniteCheckedException {
+ IndexSearchRow r = (IndexSearchRow) row;
+
+ int searchKeysLength = r.getSearchKeysCount();
+
+ if (inlineSize == 0)
+ return compareFullRows(getRow(io, pageAddr, idx), row, 0,
searchKeysLength);
+
+ SortedIndexSchema schema = def.getSchema();
+
+ if ((schema.getKeyDefinitions().length != searchKeysLength) &&
r.isFullSchemaSearch())
+ throw new IgniteCheckedException("Find is configured for full
schema search.");
+
+ int fieldOff = 0;
+
+ // Use it when can't compare values (variable length, for example).
+ int lastIdxUsed = searchKeysLength;
+
+ for (int i = 0; i < searchKeysLength; i++) {
+ try {
+ // If a search key is null then skip other keys (consider that
null shows that we should get all
+ // possible keys for that comparison).
+ if (row.getKey(i) == null)
+ return 0;
+
+ // Other keys are not inlined. Should compare as rows.
+ if (i >= schema.getKeyDefinitions().length) {
Review comment:
I'm afraid, this will lead to a performance degradation. For now
`SortedIndexSchema#getKeyDefinitions()` return a copy of an array every time,
but `compare` is on a hot path. Furthermore you are doing it several times for
every search keys. It's better 1) to change key definitions to a `List`, 2)
wrap it with `Collections#unmodifiableList` if you want to deny mutations, and
3) move it out from the loop.
BTW, have you conducted any benchmarks to estimate an impact this
refactoring have on a query performance?
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/InlineIndexTree.java
##########
@@ -0,0 +1,480 @@
+/*
+ * 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;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.cache.query.index.sorted.SortOrder;
+import org.apache.ignite.failure.FailureType;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition;
+import
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.SortedIndexSchema;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexRow;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexSearchRow;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.ThreadLocalSchemaHolder;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.FullPageId;
+import org.apache.ignite.internal.pagemem.PageIdAllocator;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.pagemem.wal.record.PageSnapshot;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException;
+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.IOVersions;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.CANT_BE_COMPARE;
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.COMPARE_UNSUPPORTED;
+
+/**
+ * BPlusTree where nodes stores inlined index keys.
+ */
+public class InlineIndexTree extends BPlusTree<IndexRow, IndexRow> {
+ /** Amount of bytes to store inlined index keys. */
+ private final int inlineSize;
+
+ /** Recommends change inline size if needed. */
+ private final InlineRecommender recommender;
+
+ /** Whether tree is created from scratch or reused from underlying store.
*/
+ private final boolean created;
+
+ /** Definition of index. */
+ private final SortedIndexDefinition def;
+
+ /** Cache context. */
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Statistics holder used by underlying BPlusTree. */
+ private final IoStatisticsHolder stats;
+
+ /**
+ * Constructor.
+ */
+ public InlineIndexTree(
+ SortedIndexDefinition def,
+ GridCacheContext<?, ?> cctx,
+ String treeName,
+ IgniteCacheOffheapManager offheap,
+ ReuseList reuseList,
+ PageMemory pageMemory,
+ PageIoResolver pageIoResolver,
+ long metaPageId,
+ boolean initNew,
+ int configuredInlineSize,
+ IoStatisticsHolder stats,
+ InlineRecommender recommender) throws IgniteCheckedException {
+ super(
+ treeName,
+ cctx.groupId(),
+ cctx.group().name(),
+ pageMemory,
+ cctx.shared().wal(),
+ offheap.globalRemoveId(),
+ metaPageId,
+ reuseList,
+ PageIdAllocator.FLAG_IDX,
+ cctx.shared().kernalContext().failure(),
+ null,
+ pageIoResolver
+ );
+
+ this.stats = stats;
+
+ created = initNew;
+
+ this.def = def;
+
+ if (!initNew) {
+ // Init from metastore
+ // Page is ready - read meta information.
+ MetaPageInfo metaInfo = getMetaInfo();
+
+ this.def.setUseUnwrappedPk(metaInfo.useUnwrappedPk());
+
+ inlineSize = metaInfo.inlineSize();
+
+ boolean inlineObjSupported = inlineSize > 0 &&
metaInfo.inlineObjectSupported();
+
+ if (!metaInfo.flagsSupported())
+ upgradeMetaPage(inlineObjSupported);
+
+ } else {
+ this.def.setUseUnwrappedPk(true);
+
+ inlineSize = computeInlineSize(
+ def.getSchema().getKeyDefinitions(), configuredInlineSize,
cctx.config().getSqlIndexMaxInlineSize());
+ }
+
+ if (inlineSize == 0)
+ setIos(InnerIO.VERSIONS, LeafIO.VERSIONS);
+ else
+ setIos(
+ // -1 is required as payload starts with 1, and indexes in
list of IOs are with 0.
+ (IOVersions<BPlusInnerIO<IndexRow>>)
PageIO.getInnerVersions(inlineSize - 1, false),
+ (IOVersions<BPlusLeafIO<IndexRow>>)
PageIO.getLeafVersions(inlineSize - 1, false));
+
+ initTree(initNew, inlineSize);
+
+ this.recommender = recommender;
+
+ this.cctx = cctx;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected int compare(BPlusIO<IndexRow> io, long pageAddr, int
idx, IndexRow row)
+ throws IgniteCheckedException {
+ IndexSearchRow r = (IndexSearchRow) row;
+
+ int searchKeysLength = r.getSearchKeysCount();
+
+ if (inlineSize == 0)
+ return compareFullRows(getRow(io, pageAddr, idx), row, 0,
searchKeysLength);
+
+ SortedIndexSchema schema = def.getSchema();
+
+ if ((schema.getKeyDefinitions().length != searchKeysLength) &&
r.isFullSchemaSearch())
+ throw new IgniteCheckedException("Find is configured for full
schema search.");
+
+ int fieldOff = 0;
+
+ // Use it when can't compare values (variable length, for example).
+ int lastIdxUsed = searchKeysLength;
+
+ for (int i = 0; i < searchKeysLength; i++) {
+ try {
+ // If a search key is null then skip other keys (consider that
null shows that we should get all
+ // possible keys for that comparison).
+ if (row.getKey(i) == null)
+ return 0;
+
+ // Other keys are not inlined. Should compare as rows.
+ if (i >= schema.getKeyDefinitions().length) {
+ lastIdxUsed = i;
+ break;
+ }
+
+ int maxSize = inlineSize - fieldOff;
+
+ int off = io.offset(idx);
Review comment:
looks like `off` is not changed through iteration too, so it's better to
move it outside the loop
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/InlineIndexTree.java
##########
@@ -0,0 +1,480 @@
+/*
+ * 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;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.cache.query.index.sorted.SortOrder;
+import org.apache.ignite.failure.FailureType;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition;
+import
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.SortedIndexSchema;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexRow;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexSearchRow;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.ThreadLocalSchemaHolder;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.FullPageId;
+import org.apache.ignite.internal.pagemem.PageIdAllocator;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.pagemem.wal.record.PageSnapshot;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException;
+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.IOVersions;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.CANT_BE_COMPARE;
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.COMPARE_UNSUPPORTED;
+
+/**
+ * BPlusTree where nodes stores inlined index keys.
+ */
+public class InlineIndexTree extends BPlusTree<IndexRow, IndexRow> {
+ /** Amount of bytes to store inlined index keys. */
+ private final int inlineSize;
+
+ /** Recommends change inline size if needed. */
+ private final InlineRecommender recommender;
+
+ /** Whether tree is created from scratch or reused from underlying store.
*/
+ private final boolean created;
+
+ /** Definition of index. */
+ private final SortedIndexDefinition def;
+
+ /** Cache context. */
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Statistics holder used by underlying BPlusTree. */
+ private final IoStatisticsHolder stats;
+
+ /**
+ * Constructor.
+ */
+ public InlineIndexTree(
+ SortedIndexDefinition def,
+ GridCacheContext<?, ?> cctx,
+ String treeName,
+ IgniteCacheOffheapManager offheap,
+ ReuseList reuseList,
+ PageMemory pageMemory,
+ PageIoResolver pageIoResolver,
+ long metaPageId,
+ boolean initNew,
+ int configuredInlineSize,
+ IoStatisticsHolder stats,
+ InlineRecommender recommender) throws IgniteCheckedException {
+ super(
+ treeName,
+ cctx.groupId(),
+ cctx.group().name(),
+ pageMemory,
+ cctx.shared().wal(),
+ offheap.globalRemoveId(),
+ metaPageId,
+ reuseList,
+ PageIdAllocator.FLAG_IDX,
+ cctx.shared().kernalContext().failure(),
+ null,
+ pageIoResolver
+ );
+
+ this.stats = stats;
+
+ created = initNew;
+
+ this.def = def;
+
+ if (!initNew) {
+ // Init from metastore
+ // Page is ready - read meta information.
+ MetaPageInfo metaInfo = getMetaInfo();
+
+ this.def.setUseUnwrappedPk(metaInfo.useUnwrappedPk());
+
+ inlineSize = metaInfo.inlineSize();
+
+ boolean inlineObjSupported = inlineSize > 0 &&
metaInfo.inlineObjectSupported();
+
+ if (!metaInfo.flagsSupported())
+ upgradeMetaPage(inlineObjSupported);
+
+ } else {
+ this.def.setUseUnwrappedPk(true);
+
+ inlineSize = computeInlineSize(
+ def.getSchema().getKeyDefinitions(), configuredInlineSize,
cctx.config().getSqlIndexMaxInlineSize());
+ }
+
+ if (inlineSize == 0)
+ setIos(InnerIO.VERSIONS, LeafIO.VERSIONS);
+ else
+ setIos(
+ // -1 is required as payload starts with 1, and indexes in
list of IOs are with 0.
+ (IOVersions<BPlusInnerIO<IndexRow>>)
PageIO.getInnerVersions(inlineSize - 1, false),
+ (IOVersions<BPlusLeafIO<IndexRow>>)
PageIO.getLeafVersions(inlineSize - 1, false));
+
+ initTree(initNew, inlineSize);
+
+ this.recommender = recommender;
+
+ this.cctx = cctx;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected int compare(BPlusIO<IndexRow> io, long pageAddr, int
idx, IndexRow row)
+ throws IgniteCheckedException {
+ IndexSearchRow r = (IndexSearchRow) row;
+
+ int searchKeysLength = r.getSearchKeysCount();
+
+ if (inlineSize == 0)
+ return compareFullRows(getRow(io, pageAddr, idx), row, 0,
searchKeysLength);
+
+ SortedIndexSchema schema = def.getSchema();
+
+ if ((schema.getKeyDefinitions().length != searchKeysLength) &&
r.isFullSchemaSearch())
Review comment:
this should be verified only once before actual search. Also message
error is not clear. Who does configure the search? What actually does "full
schema search" means? I'd rather to get rid of this.
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/InlineIndexTree.java
##########
@@ -0,0 +1,480 @@
+/*
+ * 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;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.cache.query.index.sorted.SortOrder;
+import org.apache.ignite.failure.FailureType;
+import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition;
+import
org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition;
+import org.apache.ignite.internal.cache.query.index.sorted.SortedIndexSchema;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexRow;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.IndexSearchRow;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO;
+import
org.apache.ignite.internal.cache.query.index.sorted.inline.io.ThreadLocalSchemaHolder;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagemem.FullPageId;
+import org.apache.ignite.internal.pagemem.PageIdAllocator;
+import org.apache.ignite.internal.pagemem.PageMemory;
+import org.apache.ignite.internal.pagemem.wal.record.PageSnapshot;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager;
+import org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException;
+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.IOVersions;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.CANT_BE_COMPARE;
+import static
org.apache.ignite.internal.cache.query.index.sorted.inline.keys.NullableInlineIndexKeyType.COMPARE_UNSUPPORTED;
+
+/**
+ * BPlusTree where nodes stores inlined index keys.
+ */
+public class InlineIndexTree extends BPlusTree<IndexRow, IndexRow> {
+ /** Amount of bytes to store inlined index keys. */
+ private final int inlineSize;
+
+ /** Recommends change inline size if needed. */
+ private final InlineRecommender recommender;
+
+ /** Whether tree is created from scratch or reused from underlying store.
*/
+ private final boolean created;
+
+ /** Definition of index. */
+ private final SortedIndexDefinition def;
+
+ /** Cache context. */
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Statistics holder used by underlying BPlusTree. */
+ private final IoStatisticsHolder stats;
+
+ /**
+ * Constructor.
+ */
+ public InlineIndexTree(
+ SortedIndexDefinition def,
+ GridCacheContext<?, ?> cctx,
+ String treeName,
+ IgniteCacheOffheapManager offheap,
+ ReuseList reuseList,
+ PageMemory pageMemory,
+ PageIoResolver pageIoResolver,
+ long metaPageId,
+ boolean initNew,
+ int configuredInlineSize,
+ IoStatisticsHolder stats,
+ InlineRecommender recommender) throws IgniteCheckedException {
+ super(
+ treeName,
+ cctx.groupId(),
+ cctx.group().name(),
+ pageMemory,
+ cctx.shared().wal(),
+ offheap.globalRemoveId(),
+ metaPageId,
+ reuseList,
+ PageIdAllocator.FLAG_IDX,
+ cctx.shared().kernalContext().failure(),
+ null,
+ pageIoResolver
+ );
+
+ this.stats = stats;
+
+ created = initNew;
+
+ this.def = def;
+
+ if (!initNew) {
+ // Init from metastore
+ // Page is ready - read meta information.
+ MetaPageInfo metaInfo = getMetaInfo();
+
+ this.def.setUseUnwrappedPk(metaInfo.useUnwrappedPk());
+
+ inlineSize = metaInfo.inlineSize();
+
+ boolean inlineObjSupported = inlineSize > 0 &&
metaInfo.inlineObjectSupported();
+
+ if (!metaInfo.flagsSupported())
+ upgradeMetaPage(inlineObjSupported);
+
+ } else {
+ this.def.setUseUnwrappedPk(true);
+
+ inlineSize = computeInlineSize(
+ def.getSchema().getKeyDefinitions(), configuredInlineSize,
cctx.config().getSqlIndexMaxInlineSize());
+ }
+
+ if (inlineSize == 0)
+ setIos(InnerIO.VERSIONS, LeafIO.VERSIONS);
+ else
+ setIos(
+ // -1 is required as payload starts with 1, and indexes in
list of IOs are with 0.
+ (IOVersions<BPlusInnerIO<IndexRow>>)
PageIO.getInnerVersions(inlineSize - 1, false),
+ (IOVersions<BPlusLeafIO<IndexRow>>)
PageIO.getLeafVersions(inlineSize - 1, false));
+
+ initTree(initNew, inlineSize);
+
+ this.recommender = recommender;
+
+ this.cctx = cctx;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected int compare(BPlusIO<IndexRow> io, long pageAddr, int
idx, IndexRow row)
+ throws IgniteCheckedException {
+ IndexSearchRow r = (IndexSearchRow) row;
+
+ int searchKeysLength = r.getSearchKeysCount();
+
+ if (inlineSize == 0)
+ return compareFullRows(getRow(io, pageAddr, idx), row, 0,
searchKeysLength);
+
+ SortedIndexSchema schema = def.getSchema();
+
+ if ((schema.getKeyDefinitions().length != searchKeysLength) &&
r.isFullSchemaSearch())
+ throw new IgniteCheckedException("Find is configured for full
schema search.");
+
+ int fieldOff = 0;
+
+ // Use it when can't compare values (variable length, for example).
+ int lastIdxUsed = searchKeysLength;
Review comment:
you could track it automatically by using instead of `I` in the loop
----------------------------------------------------------------
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]