use of jetbrains.exodus.util.SharedRandomAccessFile in project xodus by JetBrains.
the class SharedOpenFilesCache method getCachedFile.
@NotNull
SharedRandomAccessFile getCachedFile(@NotNull final File file) throws IOException {
SharedRandomAccessFile result;
try (CriticalSection ignored = cache.newCriticalSection()) {
result = cache.tryKey(file);
if (result != null && result.employ() > 1) {
result.close();
result = null;
}
}
if (result == null) {
result = openFile(file);
SharedRandomAccessFile obsolete = null;
try (CriticalSection ignored = cache.newCriticalSection()) {
if (cache.getObject(file) == null) {
result.employ();
obsolete = cache.cacheObject(file, result);
}
}
if (obsolete != null) {
obsolete.close();
}
}
return result;
}
use of jetbrains.exodus.util.SharedRandomAccessFile in project xodus by JetBrains.
the class SharedOpenFilesCache method removeDirectory.
void removeDirectory(@NotNull final File dir) throws IOException {
final List<SharedRandomAccessFile> result = new ArrayList<>();
final List<File> obsoleteFiles = new ArrayList<>();
try (CriticalSection ignored = cache.newCriticalSection()) {
final Iterator<File> keys = cache.keys();
while (keys.hasNext()) {
final File file = keys.next();
if (file.getParentFile().equals(dir)) {
obsoleteFiles.add(file);
result.add(cache.getObject(file));
}
}
for (final File file : obsoleteFiles) {
cache.remove(file);
}
}
for (final SharedRandomAccessFile obsolete : result) {
obsolete.close();
}
}
use of jetbrains.exodus.util.SharedRandomAccessFile in project xodus by JetBrains.
the class SharedMappedFilesCache method getFileBuffer.
@NotNull
SharedMappedByteBuffer getFileBuffer(@NotNull final SharedRandomAccessFile file) throws IOException {
try {
SharedMappedByteBuffer result;
final File key = file.getFile();
synchronized (cache) {
result = cache.get(key);
if (result != null) {
// we do employ() in the critical section intentionally, in order to avoid possible
// (though rather theoretical) race with closing this buffer when it becomes obsolete
result.employ();
return result;
}
}
result = new SharedMappedByteBuffer(file);
result.employ();
final SharedMappedByteBuffer obsolete;
synchronized (cache) {
obsolete = cache.put(key, result);
}
if (obsolete != null) {
obsoleteQueue.offer(obsolete);
}
return result;
} finally {
freeObsoleteBuffers();
}
}
use of jetbrains.exodus.util.SharedRandomAccessFile in project xodus by JetBrains.
the class FileDataReader method truncateBlock.
@Override
public void truncateBlock(long blockAddress, long length) {
final FileBlock file = getBlock(blockAddress);
removeFileFromFileCache(file);
setWritable(file);
try {
try (SharedRandomAccessFile f = new SharedRandomAccessFile(file, "rw")) {
f.setLength(length);
}
if (logger.isInfoEnabled()) {
logger.info("Truncated file " + file.getAbsolutePath() + " to length = " + length);
}
} catch (IOException e) {
throw new ExodusException("Failed to truncate file " + file.getAbsolutePath(), e);
}
}
use of jetbrains.exodus.util.SharedRandomAccessFile in project xodus by JetBrains.
the class SharedOpenFilesCache method clear.
private void clear() throws IOException {
final List<SharedRandomAccessFile> openFiles = new ArrayList<>();
try (CriticalSection ignored = cache.newCriticalSection()) {
final Iterator<SharedRandomAccessFile> it = cache.values();
while (it.hasNext()) {
openFiles.add(it.next());
}
cache.clear();
}
for (final SharedRandomAccessFile file : openFiles) {
file.close();
}
}
Aggregations