rpuch commented on code in PR #739: URL: https://github.com/apache/ignite-3/pull/739#discussion_r842713919
########## modules/storage-api/src/test/java/org/apache/ignite/internal/storage/basic/TestSortedIndexMvStorage.java: ########## @@ -0,0 +1,249 @@ +/* + * 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.storage.basic; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableSet; +import java.util.Objects; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.function.IntPredicate; +import java.util.function.ToIntFunction; +import org.apache.ignite.configuration.NamedListView; +import org.apache.ignite.configuration.schemas.table.ColumnView; +import org.apache.ignite.configuration.schemas.table.IndexColumnView; +import org.apache.ignite.configuration.schemas.table.SortedIndexView; +import org.apache.ignite.configuration.schemas.table.TableIndexView; +import org.apache.ignite.configuration.schemas.table.TableView; +import org.apache.ignite.internal.schema.BinaryRow; +import org.apache.ignite.internal.schema.NativeType; +import org.apache.ignite.internal.schema.SchemaDescriptor; +import org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter; +import org.apache.ignite.internal.schema.configuration.SchemaDescriptorConverter; +import org.apache.ignite.internal.schema.row.Row; +import org.apache.ignite.internal.storage.index.IndexRowPrefix; +import org.apache.ignite.internal.storage.index.PrefixComparator; +import org.apache.ignite.internal.storage.index.SortedIndexMvStorage; +import org.apache.ignite.internal.tx.Timestamp; +import org.apache.ignite.internal.util.Cursor; +import org.jetbrains.annotations.Nullable; + +/** + * Test implementation of MV sorted index storage. + */ +public class TestSortedIndexMvStorage implements SortedIndexMvStorage { + private final NavigableSet<BinaryRow> index; + + private final SchemaDescriptor descriptor; + + private final Map<Integer, TestMvPartitionStorage> pk; + + private final int partitions; + + private final IndexColumnView[] indexColumns; + + private final int[] columnIndexes; + + private final NativeType[] nativeTypes; + + /** + * Constructor. + */ + public TestSortedIndexMvStorage( + String name, + TableView tableCfg, + SchemaDescriptor descriptor, + Map<Integer, TestMvPartitionStorage> pk + ) { + this.descriptor = descriptor; + + this.pk = pk; + + partitions = tableCfg.partitions(); + + index = new ConcurrentSkipListSet<>(((Comparator<BinaryRow>) this::compareColumns).thenComparing(BinaryRow::keySlice)); + + // Init columns. + NamedListView<? extends ColumnView> tblColumns = tableCfg.columns(); + + TableIndexView idxCfg = tableCfg.indices().get(name); + + assert idxCfg instanceof SortedIndexView; + + SortedIndexView sortedIdxCfg = (SortedIndexView) idxCfg; + + NamedListView<? extends IndexColumnView> columns = sortedIdxCfg.columns(); + + int length = columns.size(); + + this.indexColumns = new IndexColumnView[length]; + this.columnIndexes = new int[length]; + this.nativeTypes = new NativeType[length]; + + for (int i = 0; i < length; i++) { + IndexColumnView idxColumn = columns.get(i); + + indexColumns[i] = idxColumn; + + int columnIndex = tblColumns.namedListKeys().indexOf(idxColumn.name()); + + columnIndexes[i] = columnIndex; + + nativeTypes[i] = SchemaDescriptorConverter.convert(SchemaConfigurationConverter.convert(tblColumns.get(columnIndex).type())); + } + } + + /** {@inheritDoc} */ + @Override + public boolean supportsBackwardsScan() { + return true; + } + + /** {@inheritDoc} */ + @Override + public boolean supportsIndexOnlyScan() { + return false; + } + + private int compareColumns(BinaryRow l, BinaryRow r) { + Row leftRow = new Row(descriptor, l); + Row rightRow = new Row(descriptor, r); + + for (int i = 0; i < indexColumns.length; i++) { + int columnIndex = columnIndexes[i]; + + int cmp = PrefixComparator.compareColumns(leftRow, columnIndex, nativeTypes[i].spec(), rightRow.value(columnIndex)); + + if (cmp != 0) { + return indexColumns[i].asc() ? cmp : -cmp; + } + } + + return 0; + } + + public void append(BinaryRow row) { + index.add(row); + } + + public void remove(BinaryRow row) { + index.remove(row); + } + + public boolean matches(BinaryRow aborted, BinaryRow existing) { Review Comment: How about generalizing the method (renaming the parameters and make them symmetrical, that is, check both for non-nullability)? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
