use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method fileNameById.
public String fileNameById(long fileId) {
final int intId = extractFileId(fileId);
fileId = composeFileId(id, intId);
filesLock.acquireReadLock();
try {
final OFileClassic f = files.get(fileId);
return f != null ? f.getName() : null;
} finally {
filesLock.releaseReadLock();
}
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method close.
public long[] close() throws IOException {
flush();
if (!commitExecutor.isShutdown()) {
commitExecutor.shutdown();
try {
if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
throw new OWriteCacheException("Background data flush task cannot be stopped.");
} catch (InterruptedException e) {
OLogManager.instance().error(this, "Data flush thread was interrupted");
Thread.currentThread().interrupt();
throw OException.wrapException(new OWriteCacheException("Data flush thread was interrupted"), e);
}
}
final List<Long> result = new ArrayList<Long>();
filesLock.acquireWriteLock();
try {
final Collection<Integer> intIds = nameIdMap.values();
for (Integer intId : intIds) {
if (intId < 0)
continue;
final long fileId = composeFileId(id, intId);
// we remove files because when we reopen storage we will reload them
final OFileClassic fileClassic = files.remove(fileId);
fileClassic.close();
result.add(fileId);
}
if (nameIdMapHolder != null) {
nameIdMapHolder.setLength(0);
for (Map.Entry<String, Integer> entry : nameIdMap.entrySet()) {
writeNameIdEntry(new NameFileIdEntry(entry.getKey(), entry.getValue()), false);
}
nameIdMapHolder.getFD().sync();
nameIdMapHolder.close();
}
nameIdMap.clear();
final long[] ds = new long[result.size()];
int counter = 0;
for (long id : result) {
ds[counter] = id;
counter++;
}
return ds;
} finally {
filesLock.releaseWriteLock();
}
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method addFile.
public long addFile(String fileName, long fileId) throws IOException {
filesLock.acquireWriteLock();
try {
OFileClassic fileClassic;
Integer existingFileId = nameIdMap.get(fileName);
final int intId = extractFileId(fileId);
if (existingFileId != null && existingFileId >= 0) {
if (existingFileId == intId)
throw new OStorageException("File with name '" + fileName + "'' already exists in storage '" + storageLocal.getName() + "'");
else
throw new OStorageException("File with given name already exists but has different id " + existingFileId + " vs. proposed " + fileId);
}
fileId = composeFileId(id, intId);
fileClassic = files.get(fileId);
if (fileClassic != null) {
if (!fileClassic.getName().equals(fileName))
throw new OStorageException("File with given id exists but has different name " + fileClassic.getName() + " vs. proposed " + fileName);
} else {
if (fileCounter < intId)
fileCounter = intId;
fileClassic = createFileInstance(fileName);
createFile(fileClassic);
files.add(fileId, fileClassic);
}
nameIdMap.put(fileName, intId);
writeNameIdEntry(new NameFileIdEntry(fileName, intId), true);
return fileId;
} 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 exists.
public boolean exists(long fileId) {
filesLock.acquireReadLock();
try {
final int intId = extractFileId(fileId);
fileId = composeFileId(id, intId);
final OFileClassic file = files.get(fileId);
if (file == null)
return false;
return file.exists();
} finally {
filesLock.releaseReadLock();
}
}
use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.
the class OWOWCache method renameFile.
public void renameFile(long fileId, String oldFileName, String newFileName) throws IOException {
final int intId = extractFileId(fileId);
fileId = composeFileId(id, intId);
filesLock.acquireWriteLock();
try {
OClosableEntry<Long, OFileClassic> entry = files.acquire(fileId);
if (entry == null)
return;
try {
OFileClassic file = entry.get();
final String osFileName = file.getName();
if (osFileName.startsWith(oldFileName)) {
final File newFile = new File(storageLocal.getStoragePath() + File.separator + newFileName + osFileName.substring(osFileName.lastIndexOf(oldFileName) + oldFileName.length()));
boolean renamed = file.renameTo(newFile);
while (!renamed) {
renamed = file.renameTo(newFile);
}
}
} finally {
files.release(entry);
}
nameIdMap.remove(oldFileName);
nameIdMap.put(newFileName, intId);
writeNameIdEntry(new NameFileIdEntry(oldFileName, -1), false);
writeNameIdEntry(new NameFileIdEntry(newFileName, intId), true);
} catch (InterruptedException e) {
throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
} finally {
filesLock.releaseWriteLock();
}
}
Aggregations