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


##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/PageMemoryMvPartitionStorage.java:
##########
@@ -0,0 +1,515 @@
+/*
+ * 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.pagememory.mv;
+
+import static org.apache.ignite.internal.pagememory.PageIdAllocator.FLAG_AUX;
+
+import java.nio.ByteBuffer;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Predicate;
+import org.apache.ignite.configuration.schemas.table.TableView;
+import org.apache.ignite.internal.pagememory.PageMemoryDataRegion;
+import org.apache.ignite.internal.pagememory.datapage.DataPageReader;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.pagememory.util.PageLockListenerNoOp;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.apache.ignite.internal.schema.ByteBufferRow;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.NoUncommittedVersionException;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.storage.StorageException;
+import org.apache.ignite.internal.storage.StorageUtils;
+import org.apache.ignite.internal.storage.TxIdMismatchException;
+import 
org.apache.ignite.internal.storage.pagememory.VolatilePageMemoryDataRegion;
+import org.apache.ignite.internal.tx.Timestamp;
+import org.apache.ignite.internal.util.Cursor;
+import org.apache.ignite.internal.util.IgniteCursor;
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Implementation of {@link MvPartitionStorage} using Page Memory.
+ *
+ * @see MvPartitionStorage
+ */
+public class PageMemoryMvPartitionStorage implements MvPartitionStorage {
+    private static final byte[] TOMBSTONE_PAYLOAD = new byte[0];
+    private static final Predicate<BinaryRow> MATCH_ALL = row -> true;
+
+    private final int partitionId;
+    private final int groupId;
+
+    private final VersionChainFreeList versionChainFreeList;
+    private final VersionChainTree versionChainTree;
+    private final DataPageReader<VersionChain> versionChainDataPageReader;
+    private final RowVersionFreeList rowVersionFreeList;
+    private final DataPageReader<RowVersion> rowVersionDataPageReader;
+
+    /**
+     * Constructor.
+     */
+    public PageMemoryMvPartitionStorage(int partitionId, TableView 
tableConfig, PageMemoryDataRegion dataRegion) {
+        this.partitionId = partitionId;
+
+        groupId = StorageUtils.groupId(tableConfig);
+
+        versionChainFreeList = ((VolatilePageMemoryDataRegion) 
dataRegion).versionChainFreeList();
+        rowVersionFreeList = ((VolatilePageMemoryDataRegion) 
dataRegion).rowVersionFreeList();
+
+        try {
+            versionChainTree = createVersionChainTree(partitionId, 
tableConfig, dataRegion, versionChainFreeList);
+        } catch (IgniteInternalCheckedException e) {
+            throw new StorageException("Error occurred while creating the 
partition storage", e);
+        }
+
+        versionChainDataPageReader = new 
VersionChainDataPageReader(dataRegion.pageMemory(), groupId, 
IoStatisticsHolderNoOp.INSTANCE);
+        rowVersionDataPageReader = new 
RowVersionDataPageReader(dataRegion.pageMemory(), groupId, 
IoStatisticsHolderNoOp.INSTANCE);
+    }
+
+    private VersionChainTree createVersionChainTree(
+            int partitionId,
+            TableView tableConfig,
+            PageMemoryDataRegion dataRegion,
+            VersionChainFreeList versionChainFreeList1
+    ) throws IgniteInternalCheckedException {
+        // TODO: IGNITE-16641 It is necessary to do getting the tree root for 
the persistent case.
+        long metaPageId = 
dataRegion.requiredPageMemory().allocatePage(groupId, partitionId, FLAG_AUX);
+
+        // TODO: IGNITE-16641 It is necessary to take into account the 
persistent case.
+        boolean initNew = true;
+
+        return new VersionChainTree(
+                groupId,
+                tableConfig.name(),
+                dataRegion.pageMemory(),
+                PageLockListenerNoOp.INSTANCE,
+                new AtomicLong(),
+                metaPageId,
+                versionChainFreeList1,
+                partitionId,
+                initNew
+        );
+    }
+
+    @Override
+    public @Nullable BinaryRow read(RowId rowId, UUID txId) throws 
TxIdMismatchException, StorageException {
+        VersionChain versionChain = findVersionChain(rowId);
+        if (versionChain == null) {
+            return null;
+        }
+
+        return findLatestRowVersion(versionChain, txId, MATCH_ALL);
+    }
+
+    @Override
+    public @Nullable BinaryRow read(RowId rowId, Timestamp timestamp) throws 
StorageException {
+        VersionChain versionChain = findVersionChain(rowId);
+        if (versionChain == null) {
+            return null;
+        }
+
+        return findRowVersionByTimestamp(versionChain, timestamp);
+    }
+
+    @Nullable
+    private VersionChain findVersionChain(RowId rowId) {
+        try {
+            return 
versionChainDataPageReader.getRowByLink(versionChainLinkFrom(rowId));
+        } catch (IgniteInternalCheckedException e) {
+            throw new StorageException("Version chain lookup failed", e);
+        }
+    }
+
+    private long versionChainLinkFrom(RowId rowId) {
+        if (rowId.partitionId() != partitionId) {
+            throw new IllegalArgumentException("I own partition " + 
partitionId + " but I was given RowId with partition "
+                    + rowId.partitionId());
+        }
+
+        LinkRowId linkRowId = (LinkRowId) rowId;
+
+        return linkRowId.versionChainLink();
+    }
+
+    @Nullable
+    private ByteBufferRow findLatestRowVersion(VersionChain versionChain, UUID 
txId, Predicate<BinaryRow> keyFilter) {
+        RowVersion rowVersion = findLatestRowVersion(versionChain);
+        ByteBufferRow row = rowVersionToBinaryRow(rowVersion);
+
+        if (!keyFilter.test(row)) {
+            return null;
+        }
+
+        throwIfChainBelongsToAnotherTx(versionChain, txId);
+
+        return row;
+    }
+
+    private RowVersion findLatestRowVersion(VersionChain versionChain) {
+        return findRowVersion(versionChain.headLink());
+    }
+
+    private RowVersion findRowVersion(long nextRowPartitionlessLink) {
+        long nextLink = 
PartitionlessLinks.addPartitionIdToPartititionlessLink(nextRowPartitionlessLink,
 partitionId);
+
+        try {
+            return rowVersionDataPageReader.getRowByLink(nextLink);
+        } catch (IgniteInternalCheckedException e) {
+            throw new StorageException("Row version lookup failed");
+        }
+    }
+
+    private void throwIfChainBelongsToAnotherTx(VersionChain versionChain, 
UUID txId) {
+        if (versionChain.transactionId() != null && 
!txId.equals(versionChain.transactionId())) {
+            throw new TxIdMismatchException();
+        }
+    }
+
+    @Nullable
+    private ByteBufferRow rowVersionToBinaryRow(RowVersion rowVersion) {
+        if (rowVersion.isTombstone()) {
+            return null;
+        }
+
+        return new ByteBufferRow(rowVersion.value());
+    }
+
+    @Nullable
+    private ByteBufferRow findRowVersionInChain(
+            VersionChain versionChain,
+            @Nullable UUID transactionId,
+            @Nullable Timestamp timestamp,
+            Predicate<BinaryRow> keyFilter
+    ) {
+        assert transactionId != null ^ timestamp != null;
+
+        if (transactionId != null) {
+            return findLatestRowVersion(versionChain, transactionId, 
keyFilter);
+        } else {
+            ByteBufferRow row = findRowVersionByTimestamp(versionChain, 
timestamp);
+            return keyFilter.test(row) ? row : null;
+        }
+    }
+
+    @Nullable
+    private ByteBufferRow findRowVersionByTimestamp(VersionChain versionChain, 
Timestamp timestamp) {
+        long nextRowPartitionlessLink = versionChain.headLink();
+
+        while (true) {
+            RowVersion rowVersion = findRowVersion(nextRowPartitionlessLink);
+            Timestamp rowVersionTs = rowVersion.timestamp();
+            if (rowVersionTs != null && 
rowVersionTs.beforeOrEquals(timestamp)) {

Review Comment:
   It's a very natural operation to have on `Timestamp`, it basically pertains 
to the abstraction (note that it uses the domain-specific 'before' instead of 
the general 'less'). We bloat an API when we add an operation that is not too 
cohesive with the type, but here it's not the case. Making a user writing in 
terms of `compareTo(...) <=> x` produce less readable code. I am sure that we 
will need to compare timestamps in '<=' sense a lot, so this method seems 
really useful.
   
   Note that `Instant` has `isBefore()` method.
   
   Storing a timestamp as two longs means breaking encapsulation of Timestamp 
(who knows how will its internals change in the future?). It's an optimization, 
and at this point it's premature for sure.



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