SammyVimes commented on code in PR #1102:
URL: https://github.com/apache/ignite-3/pull/1102#discussion_r975170717


##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageTest.java:
##########
@@ -746,4 +809,168 @@ void testAppliedIndex() {
 
         assertEquals(1, storage.persistedIndex());
     }
+
+    @Test
+    void testReadWithinBeforeAndAfterTwoCommits() {
+        HybridTimestamp before = clock.now();
+
+        RowId rowId = new RowId(PARTITION_ID);
+
+        HybridTimestamp first = clock.now();
+
+        storage.runConsistently(() -> {
+            addWrite(rowId, binaryRow, newTransactionId());
+
+            commitWrite(rowId, first);
+            return null;
+        });
+
+        HybridTimestamp betweenCommits = clock.now();
+
+        HybridTimestamp second = clock.now();
+
+        storage.runConsistently(() -> {
+            addWrite(rowId, binaryRow2, newTransactionId());
+
+            commitWrite(rowId, second);
+            return null;
+        });
+
+        storage.runConsistently(() -> {
+            addWrite(rowId, binaryRow3, newTransactionId());
+
+            return null;
+        });
+
+        HybridTimestamp after = clock.now();
+
+        // Read before commits.
+        ReadResult res = storage.read(rowId, before);
+        assertNull(res.binaryRow());
+
+        // Read at exact time of first commit.
+        res = storage.read(rowId, first);
+
+        assertNotNull(res);
+        assertNull(res.newestCommitTimestamp());
+        assertRowMatches(res.binaryRow(), binaryRow);
+
+        // Read between two commits.
+        res = storage.read(rowId, betweenCommits);
+
+        assertNotNull(res);
+        assertNull(res.newestCommitTimestamp());
+        assertRowMatches(res.binaryRow(), binaryRow);
+
+        // Read at exact time of second commit.
+        res = storage.read(rowId, second);
+
+        assertNotNull(res);
+        assertNull(res.newestCommitTimestamp());
+        assertRowMatches(res.binaryRow(), binaryRow2);
+
+        // Read after second commit (write intent).
+        res = storage.read(rowId, after);
+
+        assertNotNull(res);
+        assertNotNull(res.newestCommitTimestamp());
+        assertEquals(second, res.newestCommitTimestamp());
+        assertRowMatches(res.binaryRow(), binaryRow3);
+    }
+
+    @Test
+    void testWrongPartition() {
+        RowId rowId = commitAbortAndAddUncommitted();
+
+        var row = new RowId(rowId.partitionId() + 1, 
rowId.mostSignificantBits(), rowId.leastSignificantBits());
+
+        assertThrows(AssertionError.class, () -> read(row, clock.now()));
+        assertThrows(AssertionError.class, () -> read(row, UUID.randomUUID()));
+    }
+
+    @Test
+    void testReadingNothingWithLowerRowIdIfHigherRowIdWritesExist() {
+        RowId rowId = commitAbortAndAddUncommitted();
+
+        RowId lowerRowId = getPreviousRowId(rowId);
+
+        assertNull(read(lowerRowId, clock.now()));
+    }
+
+    @Test
+    void testReadingNothingByTxIdWithLowerRowId() {
+        RowId higherRowId = new RowId(PARTITION_ID);
+        RowId lowerRowId = getPreviousRowId(higherRowId);
+
+        UUID txId = UUID.randomUUID();
+
+        storage.runConsistently(() -> {
+            addWrite(higherRowId, binaryRow, txId);
+
+            return null;
+        });
+
+        assertNull(read(lowerRowId, txId));
+    }
+
+    @Test
+    void 
testReadingCorrectWriteIntentByTimestampIfLowerRowIdWriteIntentExists() {
+        RowId higherRowId = new RowId(PARTITION_ID);
+        RowId lowerRowId = getPreviousRowId(higherRowId);
+
+        storage.runConsistently(() -> {
+            addWrite(lowerRowId, binaryRow2, newTransactionId());
+            addWrite(higherRowId, binaryRow, newTransactionId());
+
+            commitWrite(higherRowId, clock.now());
+
+            return null;
+        });
+
+        assertRowMatches(read(higherRowId, clock.now()), binaryRow);
+    }
+
+    @Test
+    void 
testReadingCorrectWriteIntentByTimestampIfHigherRowIdWriteIntentExists() {
+        RowId higherRowId = new RowId(PARTITION_ID);
+        RowId lowerRowId = getPreviousRowId(higherRowId);
+
+        storage.runConsistently(() -> {
+            addWrite(lowerRowId, binaryRow, newTransactionId());
+            addWrite(higherRowId, binaryRow2, newTransactionId());
+
+            return null;
+        });
+
+        assertRowMatches(read(lowerRowId, clock.now()), binaryRow);
+    }
+
+    /**
+     * Returns row id that is lexicographically smaller (by the value of one) 
than the argument.
+     *
+     * @param value Row id.
+     * @return Row id value minus 1.
+     */
+    private RowId getPreviousRowId(RowId value) {
+        Pair<Long, Long> previous128Uint = 
getPrevious128Uint(value.mostSignificantBits(), value.leastSignificantBits());
+
+        return new RowId(value.partitionId(), previous128Uint.getFirst(), 
previous128Uint.getSecond());
+    }
+
+    /**
+     * Performs a decrement operation on a 128-bit unsigned value that is 
represented by two longs.
+     *
+     * @param msb Most significant bytes of 128-bit unsigned integer.
+     * @param lsb Least significant bytes of 128-bit unsigned integer.
+     * @return Less by one value.
+     */
+    private Pair<Long, Long> getPrevious128Uint(long msb, long lsb) {
+        assert (msb | lsb) != 0L : "Cheer up! That was very unlikely";

Review Comment:
   Good idea



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