From fcf9052f3a5b189496d4ae352976901ac1d8a33d Mon Sep 17 00:00:00 2001 From: Anton Makeev Date: Thu, 24 Nov 2011 10:28:52 +0100 Subject: [PATCH] LocalHistory: small optimization --- .../history/core/tree/DirectoryEntry.java | 18 +++++++++++---- .../com/intellij/history/core/tree/Entry.java | 5 ++++ .../history/integration/IdeaGateway.java | 23 ++++++++++--------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/platform/lvcs-impl/src/com/intellij/history/core/tree/DirectoryEntry.java b/platform/lvcs-impl/src/com/intellij/history/core/tree/DirectoryEntry.java index 67af22eab46e..d6d088ab976f 100644 --- a/platform/lvcs-impl/src/com/intellij/history/core/tree/DirectoryEntry.java +++ b/platform/lvcs-impl/src/com/intellij/history/core/tree/DirectoryEntry.java @@ -24,6 +24,7 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; public class DirectoryEntry extends Entry { @@ -64,21 +65,30 @@ public class DirectoryEntry extends Entry { @Override public void addChild(Entry child) { - checkDoesNotExist(child, child.getName()); + if (!checkDoesNotExist(child, child.getName())) return; unsafeAddChild(child); } + public void addChildren(Collection children) { + myChildren.ensureCapacity(myChildren.size() + children.size()); + for (Entry each : children) { + unsafeAddChild(each); + } + } + private void unsafeAddChild(Entry child) { myChildren.add(child); child.setParent(this); } - protected void checkDoesNotExist(Entry e, String name) { + protected boolean checkDoesNotExist(Entry e, String name) { Entry found = findChild(name); - if (found == null || found == e) return; + if (found == null) return true; + if (found == e) return false; removeChild(found); LocalHistoryLog.LOG.warn(String.format("entry '%s' already exists in '%s'", name, getPath())); + return true; } @Override @@ -171,7 +181,7 @@ public class DirectoryEntry extends Entry { @Override protected void collectDeletedDifferences(List result) { result.add(new Difference(false, this, null)); - + for (Entry child : myChildren) { child.collectDeletedDifferences(result); } diff --git a/platform/lvcs-impl/src/com/intellij/history/core/tree/Entry.java b/platform/lvcs-impl/src/com/intellij/history/core/tree/Entry.java index 55670bae567f..f8f9b5675337 100644 --- a/platform/lvcs-impl/src/com/intellij/history/core/tree/Entry.java +++ b/platform/lvcs-impl/src/com/intellij/history/core/tree/Entry.java @@ -26,6 +26,7 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; @@ -114,6 +115,10 @@ public abstract class Entry { throw new UnsupportedOperationException(formatAddRemove(child)); } + public void addChildren(Collection children) { + throw new UnsupportedOperationException(); + } + public void removeChild(Entry child) { throw new UnsupportedOperationException(formatAddRemove(child)); } diff --git a/platform/lvcs-impl/src/com/intellij/history/integration/IdeaGateway.java b/platform/lvcs-impl/src/com/intellij/history/integration/IdeaGateway.java index c7392ff3de74..26c99517ea82 100644 --- a/platform/lvcs-impl/src/com/intellij/history/integration/IdeaGateway.java +++ b/platform/lvcs-impl/src/com/intellij/history/integration/IdeaGateway.java @@ -37,16 +37,14 @@ import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.*; import com.intellij.openapi.vfs.newvfs.ManagingFS; import com.intellij.openapi.vfs.newvfs.NewVirtualFile; +import com.intellij.util.NullableFunction; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; public class IdeaGateway { private static final Key SAVED_DOCUMENT_CONTENT_AND_STAMP_KEY @@ -137,8 +135,8 @@ public class IdeaGateway { return result; } - public static Iterable iterateDBChildren(VirtualFile f) { - if (!(f instanceof NewVirtualFile)) return ContainerUtil.emptyIterable(); + public static Collection iterateDBChildren(VirtualFile f) { + if (!(f instanceof NewVirtualFile)) return Collections.emptyList(); NewVirtualFile nf = (NewVirtualFile)f; return nf.getCachedChildren(); } @@ -184,11 +182,14 @@ public class IdeaGateway { return newDir; } - private void doCreateChildren(DirectoryEntry parent, Iterable children, boolean forDeletion) { - for (VirtualFile each : children) { - Entry child = doCreateEntry(each, forDeletion); - if (child != null) parent.addChild(child); - } + private void doCreateChildren(DirectoryEntry parent, Collection children, final boolean forDeletion) { + List entries = ContainerUtil.mapNotNull(children, new NullableFunction() { + @Override + public Entry fun(VirtualFile each) { + return doCreateEntry(each, forDeletion); + } + }); + parent.addChildren(entries); } public void registerUnsavedDocuments(final LocalHistoryFacade vcs) {