use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method loadFile.
public long loadFile(final String fileName) throws IOException {
filesLock.acquireWriteLock();
try {
Integer fileId = nameIdMap.get(fileName);
OFileClassic fileClassic;
// check that file is already registered
if (!(fileId == null || fileId < 0)) {
final long externalId = composeFileId(id, fileId);
fileClassic = files.get(externalId);
if (fileClassic != null)
return externalId;
else
throw new OStorageException("File with given name " + fileName + " only partially registered in storage");
}
fileClassic = createFileInstance(fileName);
if (!fileClassic.exists())
throw new OStorageException("File with name " + fileName + " does not exist in storage " + storageLocal.getName());
else {
// REGISTER THE FILE
OLogManager.instance().debug(this, "File '" + fileName + "' is not registered in 'file name - id' map, but exists in file system. Registering it");
if (fileId == null) {
++fileCounter;
fileId = fileCounter;
} else
fileId = -fileId;
long externalId = composeFileId(id, fileId);
while (files.get(externalId) != null) {
++fileCounter;
fileId = fileCounter;
externalId = composeFileId(id, fileId);
}
openFile(fileClassic);
files.add(externalId, fileClassic);
nameIdMap.put(fileName, fileId);
writeNameIdEntry(new NameFileIdEntry(fileName, fileId), true);
return externalId;
}
} catch (InterruptedException e) {
throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
} finally {
filesLock.releaseWriteLock();
}
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method checkStoredPages.
public OPageDataVerificationError[] checkStoredPages(OCommandOutputListener commandOutputListener) {
final int notificationTimeOut = 5000;
final List<OPageDataVerificationError> errors = new ArrayList<OPageDataVerificationError>();
filesLock.acquireWriteLock();
try {
for (Integer intId : nameIdMap.values()) {
if (intId < 0)
continue;
boolean fileIsCorrect;
final long externalId = composeFileId(id, intId);
final OClosableEntry<Long, OFileClassic> entry = files.acquire(externalId);
final OFileClassic fileClassic = entry.get();
try {
if (commandOutputListener != null)
commandOutputListener.onMessage("Flashing file " + fileClassic.getName() + "... ");
flush(intId);
if (commandOutputListener != null)
commandOutputListener.onMessage("Start verification of content of " + fileClassic.getName() + "file ...");
long time = System.currentTimeMillis();
long filledUpTo = fileClassic.getFileSize();
fileIsCorrect = true;
for (long pos = 0; pos < filledUpTo; pos += pageSize) {
boolean checkSumIncorrect = false;
boolean magicNumberIncorrect = false;
byte[] data = new byte[pageSize];
fileClassic.read(pos, data, data.length);
long magicNumber = OLongSerializer.INSTANCE.deserializeNative(data, MAGIC_NUMBER_OFFSET);
if (magicNumber != MAGIC_NUMBER_WITH_CHECKSUM && magicNumber != MAGIC_NUMBER_WITHOUT_CHECKSUM) {
magicNumberIncorrect = true;
if (commandOutputListener != null)
commandOutputListener.onMessage("Error: Magic number for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " does not match !!!");
fileIsCorrect = false;
}
if (magicNumber != MAGIC_NUMBER_WITHOUT_CHECKSUM) {
final int storedCRC32 = OIntegerSerializer.INSTANCE.deserializeNative(data, CHECKSUM_OFFSET);
final int calculatedCRC32 = calculatePageCrc(data);
if (storedCRC32 != calculatedCRC32) {
checkSumIncorrect = true;
if (commandOutputListener != null)
commandOutputListener.onMessage("Error: Checksum for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " is incorrect !!!");
fileIsCorrect = false;
}
}
if (magicNumberIncorrect || checkSumIncorrect)
errors.add(new OPageDataVerificationError(magicNumberIncorrect, checkSumIncorrect, pos / pageSize, fileClassic.getName()));
if (commandOutputListener != null && System.currentTimeMillis() - time > notificationTimeOut) {
time = notificationTimeOut;
commandOutputListener.onMessage((pos / pageSize) + " pages were processed ...");
}
}
} catch (IOException ioe) {
if (commandOutputListener != null)
commandOutputListener.onMessage("Error: Error during processing of file " + fileClassic.getName() + ". " + ioe.getMessage());
fileIsCorrect = false;
} finally {
files.release(entry);
}
if (!fileIsCorrect) {
if (commandOutputListener != null)
commandOutputListener.onMessage("Verification of file " + fileClassic.getName() + " is finished with errors.");
} else {
if (commandOutputListener != null)
commandOutputListener.onMessage("Verification of file " + fileClassic.getName() + " is successfully finished.");
}
}
return errors.toArray(new OPageDataVerificationError[errors.size()]);
} catch (InterruptedException e) {
throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
} finally {
filesLock.releaseWriteLock();
}
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class WOWCacheTest method testNoChecksumVerificationIfNotRequested.
@Test
public void testNoChecksumVerificationIfNotRequested() throws IOException {
wowCache.setChecksumMode(OChecksumMode.StoreAndThrow);
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();
cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), false)[0];
cachePointer.decrementReadersReferrer();
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class WOWCacheTest method testNoChecksumFailureIfVerificationTurnedOffOnLoad.
@Test
public void testNoChecksumFailureIfVerificationTurnedOffOnLoad() throws IOException {
wowCache.setChecksumMode(OChecksumMode.Store);
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();
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 testNoChecksumFailureIfVerificationTurnedOff.
@Test
public void testNoChecksumFailureIfVerificationTurnedOff() 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();
cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
cachePointer.decrementReadersReferrer();
}
Aggregations