use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class ReadWriteCacheConcurrentTest method validateFileContent.
private void validateFileContent(byte version, int k) throws IOException {
String path = storageLocal.getConfiguration().getDirectory() + "/readWriteCacheTest" + k + ".tst";
OFileClassic fileClassic = new OFileClassic(path, "r");
fileClassic.open();
for (int i = 0; i < PAGE_COUNT; i++) {
byte[] content = new byte[8];
fileClassic.read(i * (8 + systemOffset) + systemOffset, content, 8);
Assert.assertEquals(content, new byte[] { version, 2, 3, seed, 5, 6, (byte) k, (byte) (i & 0xFF) }, " i = " + i);
}
fileClassic.close();
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method cacheFileContent.
private OCachePointer[] cacheFileContent(final int intId, final long startPageIndex, final int pageCount, final boolean addNewPages, OModifiableBoolean cacheHit) throws IOException, InterruptedException {
final long fileId = composeFileId(id, intId);
final OClosableEntry<Long, OFileClassic> entry = files.acquire(fileId);
try {
final OFileClassic fileClassic = entry.get();
if (fileClassic == null)
throw new IllegalArgumentException("File with id " + intId + " not found in WOW Cache");
final OLogSequenceNumber lastLsn;
if (writeAheadLog != null)
lastLsn = writeAheadLog.getFlushedLsn();
else
lastLsn = new OLogSequenceNumber(-1, -1);
final long firstPageStartPosition = startPageIndex * pageSize;
final long firstPageEndPosition = firstPageStartPosition + pageSize;
if (fileClassic.getFileSize() >= firstPageEndPosition) {
final OSessionStoragePerformanceStatistic sessionStoragePerformanceStatistic = performanceStatisticManager.getSessionPerformanceStatistic();
if (sessionStoragePerformanceStatistic != null) {
sessionStoragePerformanceStatistic.startPageReadFromFileTimer();
}
int pagesRead = 0;
try {
if (pageCount == 1) {
final ByteBuffer buffer = bufferPool.acquireDirect(false);
fileClassic.read(firstPageStartPosition, buffer);
buffer.position(0);
final OCachePointer dataPointer = new OCachePointer(buffer, bufferPool, lastLsn, fileId, startPageIndex);
pagesRead = 1;
return new OCachePointer[] { dataPointer };
}
final long maxPageCount = (fileClassic.getFileSize() - firstPageStartPosition) / pageSize;
final int realPageCount = Math.min((int) maxPageCount, pageCount);
final ByteBuffer[] buffers = new ByteBuffer[realPageCount];
for (int i = 0; i < buffers.length; i++) {
buffers[i] = bufferPool.acquireDirect(false);
assert buffers[i].position() == 0;
}
fileClassic.read(firstPageStartPosition, buffers);
final OCachePointer[] dataPointers = new OCachePointer[buffers.length];
for (int n = 0; n < buffers.length; n++) {
buffers[n].position(0);
dataPointers[n] = new OCachePointer(buffers[n], bufferPool, lastLsn, fileId, startPageIndex + n);
}
pagesRead = dataPointers.length;
return dataPointers;
} finally {
if (sessionStoragePerformanceStatistic != null) {
sessionStoragePerformanceStatistic.stopPageReadFromFileTimer(pagesRead);
}
}
} else if (addNewPages) {
final int space = (int) (firstPageEndPosition - fileClassic.getFileSize());
if (space > 0)
fileClassic.allocateSpace(space);
freeSpaceCheckAfterNewPageAdd();
final ByteBuffer buffer = bufferPool.acquireDirect(true);
final OCachePointer dataPointer = new OCachePointer(buffer, bufferPool, lastLsn, fileId, startPageIndex);
cacheHit.setValue(true);
return new OCachePointer[] { dataPointer };
} else
return new OCachePointer[0];
} finally {
files.release(entry);
}
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class WOWCacheTest method testNoChecksumFailureIfNoChecksumProvided.
@Test
public void testNoChecksumFailureIfNoChecksumProvided() throws IOException {
wowCache.setChecksumMode(OChecksumMode.Off);
final long fileId = wowCache.addFile(fileName);
OCachePointer cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getSharedBuffer();
buffer.position(systemOffset);
buffer.put(new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();
wowCache.store(fileId, 0, cachePointer);
cachePointer.decrementReadersReferrer();
wowCache.flush();
final String path = storageLocal.getConfiguration().getDirectory() + File.separator + fileName;
final OFileClassic file = new OFileClassic(path, "rw");
file.open();
file.writeByte(systemOffset, (byte) 1);
file.close();
wowCache.setChecksumMode(OChecksumMode.StoreAndThrow);
cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
cachePointer.decrementReadersReferrer();
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class WOWCacheTest method assertFile.
private void assertFile(long pageIndex, byte[] value, OLogSequenceNumber lsn) throws IOException {
String path = storageLocal.getConfiguration().getDirectory() + File.separator + fileName;
OFileClassic fileClassic = new OFileClassic(path, "r");
fileClassic.open();
byte[] content = new byte[8 + systemOffset];
fileClassic.read(pageIndex * (8 + systemOffset), content, 8 + systemOffset);
Assert.assertEquals(Arrays.copyOfRange(content, systemOffset, 8 + systemOffset), value);
long magicNumber = OLongSerializer.INSTANCE.deserializeNative(content, 0);
Assert.assertEquals(magicNumber, OWOWCache.MAGIC_NUMBER_WITH_CHECKSUM);
CRC32 crc32 = new CRC32();
crc32.update(content, OIntegerSerializer.INT_SIZE + OLongSerializer.LONG_SIZE, content.length - OIntegerSerializer.INT_SIZE - OLongSerializer.LONG_SIZE);
int crc = OIntegerSerializer.INSTANCE.deserializeNative(content, OLongSerializer.LONG_SIZE);
Assert.assertEquals(crc, (int) crc32.getValue());
int segment = OIntegerSerializer.INSTANCE.deserializeNative(content, OLongSerializer.LONG_SIZE + OIntegerSerializer.INT_SIZE);
long position = OLongSerializer.INSTANCE.deserializeNative(content, OLongSerializer.LONG_SIZE + 2 * OIntegerSerializer.INT_SIZE);
OLogSequenceNumber readLsn = new OLogSequenceNumber(segment, position);
Assert.assertEquals(readLsn, lsn);
fileClassic.close();
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class WOWCacheTest method testChecksumFailure.
@Test
public void testChecksumFailure() throws IOException {
wowCache.setChecksumMode(OChecksumMode.StoreAndThrow);
final long fileId = wowCache.addFile(fileName);
final OCachePointer cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
cachePointer.acquireExclusiveLock();
final ByteBuffer buffer = cachePointer.getSharedBuffer();
buffer.position(systemOffset);
buffer.put(new byte[buffer.remaining()]);
cachePointer.releaseExclusiveLock();
wowCache.store(fileId, 0, cachePointer);
cachePointer.decrementReadersReferrer();
wowCache.flush();
final String path = storageLocal.getConfiguration().getDirectory() + File.separator + fileName;
final OFileClassic file = new OFileClassic(path, "rw");
file.open();
file.writeByte(systemOffset, (byte) 1);
file.close();
Assert.assertThrows(OStorageException.class, new Assert.ThrowingRunnable() {
@Override
public void run() throws Throwable {
wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true);
}
});
}
Aggregations