rpuch commented on code in PR #3233:
URL: https://github.com/apache/ignite-3/pull/3233#discussion_r1496975372


##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BinaryRowAndRowIdMatcher.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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;
+
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.apache.ignite.internal.schema.BinaryRowMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.hamcrest.TypeSafeMatcher;
+
+/** Matcher for comparing {@link BinaryRowAndRowId}s. */
+public class BinaryRowAndRowIdMatcher extends 
TypeSafeMatcher<BinaryRowAndRowId> {
+    private final RowId rowId;
+
+    private final Matcher<BinaryRow> binaryRowMatcher;
+
+    private BinaryRowAndRowIdMatcher(BinaryRowAndRowId exp) {

Review Comment:
   ```suggestion
       private BinaryRowAndRowIdMatcher(BinaryRowAndRowId expected) {
   ```



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooser.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.ReadResult;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.util.Cursor;
+
+/** {@link BinaryRowAndRowId} chooser for index building. */
+class BuildIndexRowVersionChooser {
+    private final PartitionDataStorage storage;
+
+    /** Timestamp of activation of the catalog version in which the index 
created. */
+    private final long createIndexActivationTs;
+
+    /** Timestamp of activation of the catalog version in which the index 
start building (get {@link CatalogIndexStatus#BUILDING}). */
+    private final long startBuildingIndexActivationTs;
+
+    /**
+     * Constructor.
+     *
+     * @param storage Multi-version partition storage.
+     * @param createIndexActivationTs Timestamp of activation of the catalog 
version in which the index created.
+     * @param startBuildingIndexActivationTs Timestamp of activation of the 
catalog version in which the index start building
+     *      (get {@link CatalogIndexStatus#BUILDING}).
+     */
+    BuildIndexRowVersionChooser(PartitionDataStorage storage, long 
createIndexActivationTs, long startBuildingIndexActivationTs) {
+        this.storage = storage;
+        this.createIndexActivationTs = createIndexActivationTs;
+        this.startBuildingIndexActivationTs = startBuildingIndexActivationTs;
+    }
+
+    /**
+     * Collects binary versions of a row to build an index.
+     *
+     * <p>Index selection algorithm:</p>
+     * <ul>
+     *     <li>For writeCommitted with commitTs > activationTs(BUILDING), we 
will ignore.</li>
+     *     <li>For writeCommitted with commitTs <= activationTs(BUILDING), we 
will take the latest version.</li>

Review Comment:
   ```suggestion
        *     <li>For writeCommitted with commitTs <= activationTs(BUILDING), 
we will take the latest of them.</li>
   ```



##########
modules/table/src/test/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooserTest.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.hlc.HybridTimestamp.hybridTimestamp;
+import static 
org.apache.ignite.internal.storage.BinaryRowAndRowIdMatcher.equalToBinaryRowAndRowId;
+import static org.apache.ignite.internal.storage.RowId.lowestRowId;
+import static org.apache.ignite.internal.tx.TransactionIds.transactionId;
+import static org.apache.ignite.internal.type.NativeTypes.INT32;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.empty;
+import static org.mockito.Mockito.spy;
+
+import java.util.UUID;
+import org.apache.ignite.distributed.TestPartitionDataStorage;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.apache.ignite.internal.schema.Column;
+import org.apache.ignite.internal.schema.SchemaDescriptor;
+import org.apache.ignite.internal.schema.row.RowAssembler;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.storage.impl.TestMvPartitionStorage;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.hamcrest.Matcher;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.Test;
+
+/** For {@link BuildIndexRowVersionChooser} testing. */
+public class BuildIndexRowVersionChooserTest extends IgniteAbstractTest {
+    private static final int TABLE_ID = 1;
+
+    private static final int PARTITION_ID = 0;
+
+    private static final long CREATE_INDEX_ACTIVATION_TS_MILLS = 100;
+
+    private static final long START_BUILDING_INDEX_ACTIVATION_TS_MILLS = 100;

Review Comment:
   Is it ok they are the same? In reality they will always be different



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooser.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.ReadResult;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.util.Cursor;
+
+/** {@link BinaryRowAndRowId} chooser for index building. */

Review Comment:
   It looks like this javadoc can be improved, like 'obtains versions of a row 
to be indexed while building an index'



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooser.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.ReadResult;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.util.Cursor;
+
+/** {@link BinaryRowAndRowId} chooser for index building. */
+class BuildIndexRowVersionChooser {
+    private final PartitionDataStorage storage;
+
+    /** Timestamp of activation of the catalog version in which the index 
created. */

Review Comment:
   ```suggestion
       /** Timestamp of activation of the catalog version in which the index 
was created. */
   ```



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooser.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.ReadResult;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.util.Cursor;
+
+/** {@link BinaryRowAndRowId} chooser for index building. */
+class BuildIndexRowVersionChooser {
+    private final PartitionDataStorage storage;
+
+    /** Timestamp of activation of the catalog version in which the index 
created. */
+    private final long createIndexActivationTs;
+
+    /** Timestamp of activation of the catalog version in which the index 
start building (get {@link CatalogIndexStatus#BUILDING}). */
+    private final long startBuildingIndexActivationTs;
+
+    /**
+     * Constructor.
+     *
+     * @param storage Multi-version partition storage.
+     * @param createIndexActivationTs Timestamp of activation of the catalog 
version in which the index created.
+     * @param startBuildingIndexActivationTs Timestamp of activation of the 
catalog version in which the index start building
+     *      (get {@link CatalogIndexStatus#BUILDING}).
+     */
+    BuildIndexRowVersionChooser(PartitionDataStorage storage, long 
createIndexActivationTs, long startBuildingIndexActivationTs) {
+        this.storage = storage;
+        this.createIndexActivationTs = createIndexActivationTs;
+        this.startBuildingIndexActivationTs = startBuildingIndexActivationTs;
+    }
+
+    /**
+     * Collects binary versions of a row to build an index.
+     *
+     * <p>Index selection algorithm:</p>
+     * <ul>
+     *     <li>For writeCommitted with commitTs > activationTs(BUILDING), we 
will ignore.</li>
+     *     <li>For writeCommitted with commitTs <= activationTs(BUILDING), we 
will take the latest version.</li>
+     *     <li>For writeIntent with beginTs >= activationTs(REGISTERED), we 
will ignore.</li>
+     *     <li>For writeIntent with beginTs < activationTs(REGISTERED), we 
take.</li>
+     * </ul>
+     *
+     * @param rowId Row ID of interest.
+     */
+    List<BinaryRowAndRowId> chooseForBuildIndex(RowId rowId) {
+        try (Cursor<ReadResult> rowVersionCursor = 
storage.scanVersions(rowId)) {
+            List<BinaryRowAndRowId> result = new ArrayList<>();
+
+            boolean takenLatestVersionOfWriteCommitted = false;
+
+            for (ReadResult readResult : rowVersionCursor) {
+                if (readResult.isEmpty()) {
+                    continue;
+                }
+
+                if (readResult.isWriteIntent()) {
+                    if (beginTs(readResult) >= createIndexActivationTs) {
+                        continue;
+                    }
+                } else {
+                    if (commitTs(readResult) > startBuildingIndexActivationTs) 
{
+                        continue;
+                    } else if (takenLatestVersionOfWriteCommitted) {
+                        break;
+                    } else {
+                        takenLatestVersionOfWriteCommitted = true;
+                    }
+                }
+
+                result.add(new BinaryRowAndRowId(readResult.binaryRow(), 
rowId));

Review Comment:
   Here, the tuple is just added to the list, and information about whether 
it's a write intent or not is lost. We'll need to do Write Intent Resolution 
for write intents (which needs full `ReadResult`), so this will have to be 
added back later. How about returning `ReadResult` instead of `BinaryRow`?



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooser.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.ReadResult;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.util.Cursor;
+
+/** {@link BinaryRowAndRowId} chooser for index building. */
+class BuildIndexRowVersionChooser {
+    private final PartitionDataStorage storage;
+
+    /** Timestamp of activation of the catalog version in which the index 
created. */
+    private final long createIndexActivationTs;
+
+    /** Timestamp of activation of the catalog version in which the index 
start building (get {@link CatalogIndexStatus#BUILDING}). */
+    private final long startBuildingIndexActivationTs;
+
+    /**
+     * Constructor.
+     *
+     * @param storage Multi-version partition storage.
+     * @param createIndexActivationTs Timestamp of activation of the catalog 
version in which the index created.
+     * @param startBuildingIndexActivationTs Timestamp of activation of the 
catalog version in which the index start building
+     *      (get {@link CatalogIndexStatus#BUILDING}).
+     */
+    BuildIndexRowVersionChooser(PartitionDataStorage storage, long 
createIndexActivationTs, long startBuildingIndexActivationTs) {
+        this.storage = storage;
+        this.createIndexActivationTs = createIndexActivationTs;
+        this.startBuildingIndexActivationTs = startBuildingIndexActivationTs;
+    }
+
+    /**
+     * Collects binary versions of a row to build an index.
+     *
+     * <p>Index selection algorithm:</p>
+     * <ul>
+     *     <li>For writeCommitted with commitTs > activationTs(BUILDING), we 
will ignore.</li>
+     *     <li>For writeCommitted with commitTs <= activationTs(BUILDING), we 
will take the latest version.</li>
+     *     <li>For writeIntent with beginTs >= activationTs(REGISTERED), we 
will ignore.</li>
+     *     <li>For writeIntent with beginTs < activationTs(REGISTERED), we 
take.</li>
+     * </ul>
+     *
+     * @param rowId Row ID of interest.
+     */
+    List<BinaryRowAndRowId> chooseForBuildIndex(RowId rowId) {
+        try (Cursor<ReadResult> rowVersionCursor = 
storage.scanVersions(rowId)) {
+            List<BinaryRowAndRowId> result = new ArrayList<>();
+
+            boolean takenLatestVersionOfWriteCommitted = false;
+
+            for (ReadResult readResult : rowVersionCursor) {
+                if (readResult.isEmpty()) {
+                    continue;
+                }
+
+                if (readResult.isWriteIntent()) {
+                    if (beginTs(readResult) >= createIndexActivationTs) {
+                        continue;
+                    }
+                } else {
+                    if (commitTs(readResult) > startBuildingIndexActivationTs) 
{
+                        continue;
+                    } else if (takenLatestVersionOfWriteCommitted) {
+                        break;

Review Comment:
   This check (and the break) can be moved after the `add()`, so prevent 
reading one extra `ReadResult` from the cursor



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/BuildIndexRowVersionChooser.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.table.distributed.raft;
+
+import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.catalog.descriptors.CatalogIndexStatus;
+import org.apache.ignite.internal.storage.BinaryRowAndRowId;
+import org.apache.ignite.internal.storage.ReadResult;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.util.Cursor;
+
+/** {@link BinaryRowAndRowId} chooser for index building. */
+class BuildIndexRowVersionChooser {
+    private final PartitionDataStorage storage;
+
+    /** Timestamp of activation of the catalog version in which the index 
created. */
+    private final long createIndexActivationTs;
+
+    /** Timestamp of activation of the catalog version in which the index 
start building (get {@link CatalogIndexStatus#BUILDING}). */
+    private final long startBuildingIndexActivationTs;
+
+    /**
+     * Constructor.
+     *
+     * @param storage Multi-version partition storage.
+     * @param createIndexActivationTs Timestamp of activation of the catalog 
version in which the index created.
+     * @param startBuildingIndexActivationTs Timestamp of activation of the 
catalog version in which the index start building
+     *      (get {@link CatalogIndexStatus#BUILDING}).
+     */
+    BuildIndexRowVersionChooser(PartitionDataStorage storage, long 
createIndexActivationTs, long startBuildingIndexActivationTs) {
+        this.storage = storage;
+        this.createIndexActivationTs = createIndexActivationTs;
+        this.startBuildingIndexActivationTs = startBuildingIndexActivationTs;
+    }
+
+    /**
+     * Collects binary versions of a row to build an index.
+     *
+     * <p>Index selection algorithm:</p>
+     * <ul>
+     *     <li>For writeCommitted with commitTs > activationTs(BUILDING), we 
will ignore.</li>
+     *     <li>For writeCommitted with commitTs <= activationTs(BUILDING), we 
will take the latest version.</li>
+     *     <li>For writeIntent with beginTs >= activationTs(REGISTERED), we 
will ignore.</li>
+     *     <li>For writeIntent with beginTs < activationTs(REGISTERED), we 
take.</li>
+     * </ul>
+     *
+     * @param rowId Row ID of interest.
+     */
+    List<BinaryRowAndRowId> chooseForBuildIndex(RowId rowId) {
+        try (Cursor<ReadResult> rowVersionCursor = 
storage.scanVersions(rowId)) {

Review Comment:
   How about adding a comment saying that the versions are iterated in the 
newest-to-oldest order, to make the code a bit more obvious?



-- 
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]

Reply via email to