From fab04e46148b798f4be7335e81c49bca5bd8d559 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Fri, 18 Mar 2016 20:26:02 +0100 Subject: [PATCH] inmemory keys handling externalized --- .../util/indexing/MapReduceIndex.java | 73 +++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java b/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java index 562e73bddc79..d3b21d917561 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/MapReduceIndex.java @@ -59,10 +59,12 @@ public class MapReduceIndex implements UpdatableIndex> mySnapshotIndexExternalizer; private final boolean myIsPsiBackedIndex; private final IndexExtension myExtension; + private final AtomicBoolean myIsBufferingMode = new AtomicBoolean(); + private final TIntObjectHashMap> myInMemoryKeys = new TIntObjectHashMap>(); - private PersistentHashMap> myInputsIndex; private PersistentHashMap myContents; private PersistentHashMap myInputsSnapshotMapping; + private PersistentHashMap> myInputsIndex; private PersistentHashMap myIndexingTrace; private final ReentrantReadWriteLock myLock = new ReentrantReadWriteLock(); @@ -97,10 +99,26 @@ public class MapReduceIndex implements UpdatableIndex DataExternalizer> createInputsIndexExternalizer(IndexExtension extension, @@ -550,7 +568,7 @@ public class MapReduceIndex implements UpdatableIndex compute() { try { - Collection oldKeys = myInputsIndex.get(inputId); + Collection oldKeys = readInputKeys(inputId); if (oldKeys == null) { return keysForGivenInputId.compute(); } @@ -572,7 +590,7 @@ public class MapReduceIndex implements UpdatableIndex compute() { try { - Collection oldKeys = myInputsIndex.get(inputId); + Collection oldKeys = readInputKeys(inputId); return oldKeys == null? Collections.emptyList() : oldKeys; } catch (IOException e) { @@ -613,6 +631,38 @@ public class MapReduceIndex implements UpdatableIndex readInputKeys(int inputId) throws IOException { + boolean inMemory = myStorage instanceof MemoryIndexStorage && ((MemoryIndexStorage)myStorage).isBufferingEnabled(); + + if (DebugAssertions.DEBUG) { + DebugAssertions.assertTrue(inMemory == myIsBufferingMode.get()); + } + if (inMemory) { + synchronized (myInMemoryKeys) { + Collection keys = myInMemoryKeys.get(inputId); + if (keys != null) { + if (myInputsIndex != null && DebugAssertions.DEBUG) { + DebugAssertions.assertTrue(equals(keys, myInputsIndex.get(inputId))); + } + return keys; + } + } + } + if (myHasSnapshotMapping) { + if (myInputsIndex != null && DebugAssertions.DEBUG) { + DebugAssertions.assertTrue(equals(null, myInputsIndex.get(inputId))); + } + return null; + } + return myInputsIndex.get(inputId); + } + + private static boolean equals(Collection keys, Collection keys2) { + if (keys == null && keys2 == null) return true; + if (keys == null || keys2 == null || keys.size() != keys2.size()) return false; + return new HashSet(keys).equals(new HashSet<>(keys2)); + } + private void checkValuesHaveProperEqualsAndHashCode(Map data) { for(Map.Entry e: data.entrySet()) { final Value value = e.getValue(); @@ -797,10 +847,21 @@ public class MapReduceIndex implements UpdatableIndex newKeys = newData.keySet(); + + if (DebugAssertions.DEBUG) { + DebugAssertions.assertTrue(!physical == myIsBufferingMode.get()); + } + if (myIsBufferingMode.get()) { + synchronized (myInMemoryKeys) { + myInMemoryKeys.put(inputId, newKeys); + } + } + + if (myHasSnapshotMapping && physical) { myInputsSnapshotMapping.put(inputId, savedInputId); } else if (myInputsIndex != null) { - final Set newKeys = newData.keySet(); if (newKeys.size() > 0) { myInputsIndex.put(inputId, newKeys); }