Search in sources :

Example 1 with SharedRandomAccessFile

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;
}
Also used : CriticalSection(jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection) SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SharedRandomAccessFile

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();
    }
}
Also used : CriticalSection(jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection) ArrayList(java.util.ArrayList) SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile) SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile) File(java.io.File)

Example 3 with SharedRandomAccessFile

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();
    }
}
Also used : SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with SharedRandomAccessFile

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);
    }
}
Also used : SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile) IOException(java.io.IOException) ExodusException(jetbrains.exodus.ExodusException)

Example 5 with SharedRandomAccessFile

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();
    }
}
Also used : CriticalSection(jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection) ArrayList(java.util.ArrayList) SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile)

Aggregations

SharedRandomAccessFile (jetbrains.exodus.util.SharedRandomAccessFile)5 CriticalSection (jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 IOException (java.io.IOException)1 ExodusException (jetbrains.exodus.ExodusException)1