Skip to content

Commit

Permalink
Last usages of #getBufferDuplicate() are removed from tests. Code obs…
Browse files Browse the repository at this point in the history
…ervations were fixed.
  • Loading branch information
andrii0lomakin committed Feb 6, 2024
1 parent 18fe41d commit 573b160
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.orientechnologies.common.directmemory.OPointer;
import com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -44,7 +43,7 @@ public final class OCachePointer {
}

private static final int WRITERS_OFFSET = 32;
private static final int READERS_MASK = 0xFFFFFFFF;
private static final long READERS_MASK = 0xFFFFFFFFL;

private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

Expand Down Expand Up @@ -232,16 +231,6 @@ public OPointer getPointer() {
return pointer;
}

public ByteBuffer getBufferDuplicate() {
if (pointer == null) {
return null;
}
final ByteBuffer duplicate =
pointer.getNativeByteBuffer().duplicate().order(ByteOrder.nativeOrder());
duplicate.rewind();
return duplicate;
}

public void acquireExclusiveLock() {
readWriteLock.writeLock().lock();
version++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,13 @@ public void testDataUpdate() throws Exception {
final OCachePointer cachePointer =
wowCache.load(fileId, pageIndex, new OModifiableBoolean(), false);
cachePointer.acquireExclusiveLock();
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

Assert.assertEquals(
new OLogSequenceNumber(0, 0), ODurablePage.getLogSequenceNumberFromPage(buffer));

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(data);
buffer.put(ODurablePage.NEXT_FREE_POSITION, data);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, pageIndex, cachePointer);
Expand All @@ -396,11 +395,9 @@ public void testDataUpdate() throws Exception {

OCachePointer cachePointer = wowCache.load(fileId, pageIndex, new OModifiableBoolean(), true);
byte[] dataTwo = new byte[8];
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.get(dataTwo);
buffer.get(ODurablePage.NEXT_FREE_POSITION, dataTwo);

cachePointer.decrementReadersReferrer();
Assert.assertArrayEquals(dataTwo, dataOne);
Expand All @@ -420,11 +417,10 @@ public void testDataUpdate() throws Exception {
wowCache.load(fileId, pageIndex, new OModifiableBoolean(), true);

cachePointer.acquireExclusiveLock();
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(data);
buffer.put(ODurablePage.NEXT_FREE_POSITION, data);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, pageIndex, cachePointer);
Expand All @@ -436,11 +432,10 @@ public void testDataUpdate() throws Exception {
byte[] dataOne = entry.getValue();
OCachePointer cachePointer = wowCache.load(fileId, pageIndex, new OModifiableBoolean(), true);
byte[] dataTwo = new byte[8];
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.get(dataTwo);
buffer.get(ODurablePage.NEXT_FREE_POSITION, dataTwo);
cachePointer.decrementReadersReferrer();

Assert.assertArrayEquals(dataTwo, dataOne);
Expand Down Expand Up @@ -529,17 +524,16 @@ public void testDataUpdateEncrypted() throws Exception {

final OCachePointer cachePointer =
wowCache.load(fileId, pageIndex, new OModifiableBoolean(), verifyChecksums);
ByteBuffer bufferDuplicate = cachePointer.getBufferDuplicate();
ByteBuffer bufferDuplicate = cachePointer.getBuffer();
assert bufferDuplicate != null;

Assert.assertEquals(
new OLogSequenceNumber(0, 0), ODurablePage.getLogSequenceNumberFromPage(bufferDuplicate));

cachePointer.acquireExclusiveLock();
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(data);
buffer.put(ODurablePage.NEXT_FREE_POSITION, data);

cachePointer.releaseExclusiveLock();

Expand All @@ -553,11 +547,10 @@ public void testDataUpdateEncrypted() throws Exception {

OCachePointer cachePointer = wowCache.load(fileId, pageIndex, new OModifiableBoolean(), true);
byte[] dataTwo = new byte[8];
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.get(dataTwo);
buffer.get(ODurablePage.NEXT_FREE_POSITION, dataTwo);

cachePointer.decrementReadersReferrer();
Assert.assertArrayEquals(dataTwo, dataOne);
Expand All @@ -577,11 +570,10 @@ public void testDataUpdateEncrypted() throws Exception {
wowCache.load(fileId, pageIndex, new OModifiableBoolean(), true);

cachePointer.acquireExclusiveLock();
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(data);
buffer.put(ODurablePage.NEXT_FREE_POSITION, data);

cachePointer.releaseExclusiveLock();

Expand All @@ -594,11 +586,10 @@ public void testDataUpdateEncrypted() throws Exception {
byte[] dataOne = entry.getValue();
OCachePointer cachePointer = wowCache.load(fileId, pageIndex, new OModifiableBoolean(), true);
byte[] dataTwo = new byte[8];
ByteBuffer buffer = cachePointer.getBufferDuplicate();
ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.get(dataTwo);
buffer.get(ODurablePage.NEXT_FREE_POSITION, dataTwo);
cachePointer.decrementReadersReferrer();

Assert.assertArrayEquals(dataTwo, dataOne);
Expand Down Expand Up @@ -685,11 +676,10 @@ public void testChecksumFailure() throws IOException {
final OCachePointer cachePointer = wowCache.load(fileId, 0, new OModifiableBoolean(), false);

cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getBufferDuplicate();
final ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(new byte[buffer.remaining()]);
buffer.put(ODurablePage.NEXT_FREE_POSITION, new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, 0, cachePointer);
Expand Down Expand Up @@ -725,11 +715,10 @@ public void testMagicFailure() throws IOException {
final OCachePointer cachePointer = wowCache.load(fileId, 0, new OModifiableBoolean(), false);

cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getBufferDuplicate();
final ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(new byte[buffer.remaining()]);
buffer.put(ODurablePage.NEXT_FREE_POSITION, new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, 0, cachePointer);
Expand Down Expand Up @@ -763,11 +752,10 @@ public void testNoChecksumVerificationIfNotRequested() throws IOException {
final OCachePointer cachePointer = wowCache.load(fileId, 0, new OModifiableBoolean(), false);

cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getBufferDuplicate();
final ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(new byte[buffer.remaining()]);
buffer.put(ODurablePage.NEXT_FREE_POSITION, new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, 0, cachePointer);
Expand Down Expand Up @@ -798,11 +786,10 @@ public void testNoChecksumFailureIfVerificationTurnedOff() throws IOException {
final OCachePointer cachePointer = wowCache.load(fileId, 0, new OModifiableBoolean(), true);

cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getBufferDuplicate();
final ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(new byte[buffer.remaining()]);
buffer.put(ODurablePage.NEXT_FREE_POSITION, new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, 0, cachePointer);
Expand Down Expand Up @@ -833,11 +820,10 @@ public void testNoChecksumFailureIfVerificationTurnedOffOnLoad() throws IOExcept
final OCachePointer cachePointer = wowCache.load(fileId, 0, new OModifiableBoolean(), true);

cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getBufferDuplicate();
final ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(new byte[buffer.remaining()]);
buffer.put(ODurablePage.NEXT_FREE_POSITION, new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, 0, cachePointer);
Expand Down Expand Up @@ -868,11 +854,10 @@ public void testNoChecksumFailureIfNoChecksumProvided() throws IOException {
final OCachePointer cachePointer = wowCache.load(fileId, 0, new OModifiableBoolean(), true);

cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getBufferDuplicate();
final ByteBuffer buffer = cachePointer.getBuffer();
assert buffer != null;

buffer.position(ODurablePage.NEXT_FREE_POSITION);
buffer.put(new byte[buffer.remaining()]);
buffer.put(ODurablePage.NEXT_FREE_POSITION, new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();

wowCache.store(fileId, 0, cachePointer);
Expand Down

0 comments on commit 573b160

Please sign in to comment.