From e8c62be3e6090796b22fd56723f8fd4ea2ec3e58 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 17 Mar 2016 19:28:23 +0100 Subject: [PATCH] [vfs] minor optimizations and cleanup in LocalFileSystem - watch request management reduced to a single method - read action removed (not needed) - post-test cleanup fix --- .../intellij/openapi/vfs/LocalFileSystem.java | 42 ++++--- .../openapi/vfs/ex/temp/TempFileSystem.java | 15 +-- .../vfs/impl/local/LocalFileSystemImpl.java | 112 ++++++------------ .../vfs/impl/win32/Win32LocalFileSystem.java | 15 +-- .../vfs/newvfs/RefreshSessionImpl.java | 7 +- .../intellij/mock/MockLocalFileSystem.java | 16 +-- 6 files changed, 72 insertions(+), 135 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/LocalFileSystem.java b/platform/platform-api/src/com/intellij/openapi/vfs/LocalFileSystem.java index 5a9163180fab..bdf6857321c9 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/LocalFileSystem.java +++ b/platform/platform-api/src/com/intellij/openapi/vfs/LocalFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -75,36 +75,48 @@ public abstract class LocalFileSystem extends NewVirtualFileSystem { } @Nullable - public WatchRequest addRootToWatch(@NotNull final String rootPath, final boolean watchRecursively) { - final Set result = addRootsToWatch(singleton(rootPath), watchRecursively); + public WatchRequest addRootToWatch(@NotNull String rootPath, boolean watchRecursively) { + Set result = addRootsToWatch(singleton(rootPath), watchRecursively); return result.size() == 1 ? result.iterator().next() : null; } @NotNull - public abstract Set addRootsToWatch(@NotNull final Collection rootPaths, final boolean watchRecursively); + public Set addRootsToWatch(@NotNull Collection rootPaths, boolean watchRecursively) { + if (rootPaths.isEmpty()) { + return Collections.emptySet(); + } + else if (watchRecursively) { + return replaceWatchedRoots(Collections.emptySet(), rootPaths, null); + } + else { + return replaceWatchedRoots(Collections.emptySet(), null, rootPaths); + } + } - public void removeWatchedRoot(@Nullable final WatchRequest watchRequest) { + public void removeWatchedRoot(@Nullable WatchRequest watchRequest) { if (watchRequest != null) { removeWatchedRoots(singleton(watchRequest)); } } - public abstract void removeWatchedRoots(@NotNull final Collection watchRequests); + public void removeWatchedRoots(@NotNull Collection watchRequests) { + if (!watchRequests.isEmpty()) { + replaceWatchedRoots(watchRequests, null, null); + } + } @Nullable - public WatchRequest replaceWatchedRoot(@Nullable final WatchRequest watchRequest, - @NotNull final String rootPath, - final boolean watchRecursively) { - final Set requests = watchRequest != null ? singleton(watchRequest) : Collections.emptySet(); - final Set result = watchRecursively ? replaceWatchedRoots(requests, singleton(rootPath), null) - : replaceWatchedRoots(requests, null, singleton(rootPath)); + public WatchRequest replaceWatchedRoot(@Nullable WatchRequest watchRequest, @NotNull String rootPath, boolean watchRecursively) { + Set requests = watchRequest != null ? singleton(watchRequest) : Collections.emptySet(); + Set roots = singleton(rootPath); + Set result = watchRecursively ? replaceWatchedRoots(requests, roots, null) : replaceWatchedRoots(requests, null, roots); return result.size() == 1 ? result.iterator().next() : null; } @NotNull - public abstract Set replaceWatchedRoots(@NotNull final Collection watchRequests, - @Nullable final Collection recursiveRoots, - @Nullable final Collection flatRoots); + public abstract Set replaceWatchedRoots(@NotNull Collection watchRequests, + @Nullable Collection recursiveRoots, + @Nullable Collection flatRoots); /** * Registers a handler that allows a version control system plugin to intercept file operations in the local file system diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/ex/temp/TempFileSystem.java b/platform/platform-impl/src/com/intellij/openapi/vfs/ex/temp/TempFileSystem.java index ee76887c9c5f..fd88df6f384b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/ex/temp/TempFileSystem.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/ex/temp/TempFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -367,17 +367,6 @@ public class TempFileSystem extends LocalFileSystemBase { return new FileAttributes(item.isDirectory(), false, false, false, length, item.myTimestamp, item.myWritable); } - @NotNull - @Override - public Set addRootsToWatch(@NotNull Collection rootPaths, boolean watchRecursively) { - throw new IncorrectOperationException(); - } - - @Override - public void removeWatchedRoots(@NotNull Collection watchRequests) { - throw new IncorrectOperationException(); - } - @NotNull @Override public Set replaceWatchedRoots(@NotNull Collection watchRequests, @@ -391,4 +380,4 @@ public class TempFileSystem extends LocalFileSystemBase { protected String normalize(@NotNull String path) { return path; } -} +} \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java index e3da2860131c..992ab140c866 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java @@ -116,9 +116,8 @@ public final class LocalFileSystemImpl extends LocalFileSystemBase implements Ap } private List normalizeRootsForRefresh() { - final List result = new ArrayList(); + List result = new ArrayList<>(); - // no need to call for a read action here since we're only called with it on hands already synchronized (myLock) { TreeNode rootNode = new TreeNode(); for (WatchRequestImpl request : myRootsToWatch) { @@ -296,76 +295,23 @@ public final class LocalFileSystemImpl extends LocalFileSystemBase implements Ap } } - private void setUpFileWatcher() { - Application app = ApplicationManager.getApplication(); - app.assertReadAccessAllowed(); - if (app.isDisposeInProgress() || !myWatcher.isOperational()) return; - - synchronized (myLock) { - List recursiveRoots = new ArrayList<>(); - List flatRoots = new ArrayList<>(); - - for (WatchRequestImpl request : normalizeRootsForRefresh()) { - (request.isToWatchRecursively() ? recursiveRoots : flatRoots).add(request.myFSRootPath); - } - - myWatcher.setWatchRoots(recursiveRoots, flatRoots); - } - } - - @Override - @NotNull - public Set addRootsToWatch(@NotNull final Collection rootPaths, final boolean watchRecursively) { - if (rootPaths.isEmpty() || !myWatcher.isOperational()) { - return Collections.emptySet(); - } - else if (watchRecursively) { - return replaceWatchedRoots(Collections.emptySet(), rootPaths, null); - } - else { - return replaceWatchedRoots(Collections.emptySet(), null, rootPaths); - } - } - - @Override - public void removeWatchedRoots(@NotNull final Collection watchRequests) { - if (watchRequests.isEmpty()) return; - - ApplicationManager.getApplication().runReadAction(() -> { - synchronized (myLock) { - boolean update = doRemoveWatchedRoots(watchRequests); - if (update) { - myNormalizedTree = null; - setUpFileWatcher(); - } - } - }); - } - @NotNull @Override public Set replaceWatchedRoots(@NotNull Collection watchRequests, - @Nullable Collection _recursiveRoots, - @Nullable Collection _flatRoots) { - Collection recursiveRoots = ObjectUtils.notNull(_recursiveRoots, Collections.emptyList()); - Collection flatRoots = ObjectUtils.notNull(_flatRoots, Collections.emptyList()); + @Nullable Collection recursiveRoots, + @Nullable Collection flatRoots) { + recursiveRoots = ObjectUtils.notNull(recursiveRoots, Collections.emptyList()); + flatRoots = ObjectUtils.notNull(flatRoots, Collections.emptyList()); - if (recursiveRoots.isEmpty() && flatRoots.isEmpty() || !myWatcher.isOperational()) { - removeWatchedRoots(watchRequests); - return Collections.emptySet(); - } - - Set result = new HashSet(); - ApplicationManager.getApplication().runReadAction(() -> { - synchronized (myLock) { - boolean update = doAddRootsToWatch(recursiveRoots, flatRoots, result) | - doRemoveWatchedRoots(watchRequests); - if (update) { - myNormalizedTree = null; - setUpFileWatcher(); - } + Set result = new HashSet<>(); + synchronized (myLock) { + boolean update = doAddRootsToWatch(recursiveRoots, flatRoots, result) | + doRemoveWatchedRoots(watchRequests); + if (update) { + myNormalizedTree = null; + setUpFileWatcher(); } - }); + } return result; } @@ -413,7 +359,7 @@ public final class LocalFileSystemImpl extends LocalFileSystemBase implements Ap return new WatchRequestImpl(rootFile.getAbsolutePath(), recursively); } - private boolean doRemoveWatchedRoots(@NotNull final Collection watchRequests) { + private boolean doRemoveWatchedRoots(@NotNull Collection watchRequests) { boolean update = false; for (WatchRequest watchRequest : watchRequests) { @@ -425,17 +371,26 @@ public final class LocalFileSystemImpl extends LocalFileSystemBase implements Ap return update; } + private void setUpFileWatcher() { + if (!ApplicationManager.getApplication().isDisposeInProgress() && myWatcher.isOperational()) { + List recursiveRoots = new ArrayList<>(); + List flatRoots = new ArrayList<>(); + + for (WatchRequestImpl request : normalizeRootsForRefresh()) { + (request.isToWatchRecursively() ? recursiveRoots : flatRoots).add(request.myFSRootPath); + } + + myWatcher.setWatchRoots(recursiveRoots, flatRoots); + } + } + @Override public void refreshWithoutFileWatcher(final boolean asynchronous) { - Runnable heavyRefresh = new Runnable() { - @Override - public void run() { - for (VirtualFile root : myManagingFS.getRoots(LocalFileSystemImpl.this)) { - ((NewVirtualFile)root).markDirtyRecursively(); - } - - refresh(asynchronous); + Runnable heavyRefresh = () -> { + for (VirtualFile root : myManagingFS.getRoots(this)) { + ((NewVirtualFile)root).markDirtyRecursively(); } + refresh(asynchronous); }; if (asynchronous && myWatcher.isOperational()) { @@ -455,6 +410,9 @@ public final class LocalFileSystemImpl extends LocalFileSystemBase implements Ap public void cleanupForNextTest() { FileDocumentManager.getInstance().saveAllDocuments(); PersistentFS.getInstance().clearIdCache(); - myRootsToWatch.clear(); + synchronized (myLock) { + myRootsToWatch.clear(); + myNormalizedTree = null; + } } } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/win32/Win32LocalFileSystem.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/win32/Win32LocalFileSystem.java index b850cfdedb36..f462437f5e48 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/win32/Win32LocalFileSystem.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/win32/Win32LocalFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,17 +62,6 @@ public class Win32LocalFileSystem extends LocalFileSystemBase { return myFsCache.getAttributes(file); } - @NotNull - @Override - public Set addRootsToWatch(@NotNull Collection rootPaths, boolean watchRecursively) { - throw new UnsupportedOperationException(); - } - - @Override - public void removeWatchedRoots(@NotNull Collection watchRequests) { - throw new UnsupportedOperationException(); - } - @NotNull @Override public Set replaceWatchedRoots(@NotNull Collection watchRequests, @@ -80,4 +69,4 @@ public class Win32LocalFileSystem extends LocalFileSystemBase { @Nullable Collection flatRoots) { throw new UnsupportedOperationException(); } -} +} \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshSessionImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshSessionImpl.java index c2998c03c866..37ede0bc5935 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshSessionImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/RefreshSessionImpl.java @@ -49,6 +49,8 @@ public class RefreshSessionImpl extends RefreshSession { private final boolean myIsRecursive; private final Runnable myFinishRunnable; private final ModalityState myModalityState; + private final DumbModePermission myDumbModePermission; + private final Throwable myStartTrace; private final Semaphore mySemaphore = new Semaphore(); private List myWorkQueue = new ArrayList(); @@ -56,8 +58,6 @@ public class RefreshSessionImpl extends RefreshSession { private volatile boolean iHaveEventsToFire; private volatile RefreshWorker myWorker = null; private volatile boolean myCancelled = false; - private final DumbModePermission myDumbModePermission; - private final Throwable myStartTrace; public RefreshSessionImpl(boolean async, boolean recursive, @Nullable Runnable finishRunnable) { this(async, recursive, finishRunnable, ModalityState.NON_MODAL); @@ -150,7 +150,8 @@ public class RefreshSessionImpl extends RefreshSession { nvf.markDirty(); } - RefreshWorker worker = myWorker = new RefreshWorker(nvf, myIsRecursive); + RefreshWorker worker = new RefreshWorker(nvf, myIsRecursive); + myWorker = worker; worker.scan(); List events = worker.getEvents(); if (myEvents.addAll(events)) { diff --git a/platform/testFramework/src/com/intellij/mock/MockLocalFileSystem.java b/platform/testFramework/src/com/intellij/mock/MockLocalFileSystem.java index 04ac12836403..dff92c61534c 100644 --- a/platform/testFramework/src/com/intellij/mock/MockLocalFileSystem.java +++ b/platform/testFramework/src/com/intellij/mock/MockLocalFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.mock; import com.intellij.openapi.util.io.FileAttributes; @@ -68,17 +67,6 @@ public class MockLocalFileSystem extends LocalFileSystem { public void refreshFiles(@NotNull Iterable files, boolean async, boolean recursive, @Nullable Runnable onFinish) { } - @Override - @NotNull - public Set addRootsToWatch(@NotNull final Collection rootPaths, final boolean watchRecursively) { - throw new UnsupportedOperationException("Not implemented in " + getClass().getName()); - } - - @Override - public void removeWatchedRoots(@NotNull final Collection rootsToWatch) { - throw new UnsupportedOperationException("Not implemented in " + getClass().getName()); - } - @NotNull @Override public Set replaceWatchedRoots(@NotNull Collection watchRequests, @@ -236,4 +224,4 @@ public class MockLocalFileSystem extends LocalFileSystem { public FileAttributes getAttributes(@NotNull VirtualFile file) { return null; } -} +} \ No newline at end of file