Search in sources :

Example 1 with CriticalSection

use of jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection 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 CriticalSection

use of jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection 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 CriticalSection

use of jetbrains.exodus.core.dataStructures.ObjectCacheBase.CriticalSection 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

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