public List<CallingLocation> getAllLocations(final MethodNameAndQualifier methodNameAndQualifier) { try { final List<CallingLocation> result = new ArrayList<CallingLocation>(); myIndex.getData(methodNameAndQualifier).forEach(new ValueContainer.ContainerAction<List<CallingLocation>>() { @Override public boolean perform(final int id, final List<CallingLocation> values) { result.addAll(values); return true; } }); return result; } catch (StorageException e) { throw new RuntimeException(e); } }
public Multiset<MethodIncompleteSignature> getLocationsAsParam(final MethodNameAndQualifier methodNameAndQualifier) { final Multiset<MethodIncompleteSignature> result = HashMultiset.create(); try { myIndex.getData(methodNameAndQualifier).forEach(new ValueContainer.ContainerAction<List<CallingLocation>>() { @Override public boolean perform(final int id, final List<CallingLocation> values) { for (final CallingLocation value : values) { if (value.getVariableType().equals(VariableType.METHOD_PARAMETER)) { result.add(value.getMethodIncompleteSignature()); } } return true; } }); } catch (StorageException e) { throw new RuntimeException(e); } return result; }
protected Set<String> getSupers(final String classQName) { try { final ValueContainer<Set<String>> valueContainer = myIndex.getData(classQName); final Ref<Set<String>> setRef = Ref.create(); valueContainer.forEach(new ValueContainer.ContainerAction<Set<String>>() { @Override public boolean perform(final int id, final Set<String> value) { setRef.set(value); return false; } }); final Set<String> supers = setRef.get(); if (supers == null) { return Collections.emptySet(); } return supers; } catch (StorageException e) { throw new RuntimeException(e); } }
protected Set<String> getMethodsNames(final String classQName) { final Ref<Set<String>> methodsRef = Ref.create(); try { myIndex.getData(classQName).forEach(new ValueContainer.ContainerAction<Set<String>>() { @Override public boolean perform(final int id, final Set<String> value) { methodsRef.set(value); return true; } }); final Set<String> methods = methodsRef.get(); return methods == null ? Collections.<String>emptySet() : methods; } catch (StorageException e) { throw new RuntimeException(e); } }
public TreeSet<UsageIndexValue> getValues(final K key) { try { final ValueContainer<Multiset<MethodIncompleteSignature>> valueContainer = myIndex.getData(key); final Multiset<MethodIncompleteSignature> rawValues = HashMultiset.create(); valueContainer.forEach(new ValueContainer.ContainerAction<Multiset<MethodIncompleteSignature>>() { @Override public boolean perform(final int id, final Multiset<MethodIncompleteSignature> values) { for (final Multiset.Entry<MethodIncompleteSignature> entry : values.entrySet()) { rawValues.add(entry.getElement(), entry.getCount()); } return true; } }); return rawValuesToValues(rawValues); } catch (StorageException e) { throw new RuntimeException(); } }
@NotNull public List<Integer> getTwinInfo(final String typeQName) { try { final ValueContainer<List<Integer>> valueContainer = myIndex.getData(typeQName); final List<Integer> result = new ArrayList<Integer>(valueContainer.size()); valueContainer.forEach(new ValueContainer.ContainerAction<List<Integer>>() { @Override public boolean perform(final int id, final List<Integer> value) { result.addAll(value); return true; } }); return result; } catch (StorageException e) { throw new RuntimeException(e); } }
@Nonnull public Set<Integer> addCommitsAndGetRenames(@Nonnull Set<Integer> newPathIds, @Nonnull Set<Integer> allPathIds, @Nonnull TIntHashSet commits) throws StorageException { Set<Integer> renames = ContainerUtil.newHashSet(); for (Integer key : newPathIds) { iterateCommitIdsAndValues(key, (value, commit) -> { commits.add(commit); if (value != null && !allPathIds.contains(value)) { renames.add(value); } }); } return renames; }
private void processAllKeysAsDeleted(final RemovedKeyProcessor<Key> removeProcessor) throws StorageException { if (myMap instanceof THashMap) { final StorageException[] exception = new StorageException[]{null}; ((THashMap<Key, Value>)myMap).forEachEntry(new TObjectObjectProcedure<Key, Value>() { @Override public boolean execute(Key k, Value v) { try { removeProcessor.process(k, myInputId); } catch (StorageException e) { exception[0] = e; return false; } return true; } }); if (exception[0] != null) throw exception[0]; } else { for (Key key : myMap.keySet()) { removeProcessor.process(key, myInputId); } } }
public void clear() { try { myIndex.clear(); } catch (StorageException e) { throw new RuntimeException(e); } }
public TIntHashSet getCommitsForPaths(@Nonnull Collection<FilePath> paths) throws IOException, StorageException { Set<Integer> allPathIds = ContainerUtil.newHashSet(); for (FilePath path : paths) { allPathIds.add(myPathsIndexer.myPathsEnumerator.enumerate(path.getPath())); } TIntHashSet result = new TIntHashSet(); Set<Integer> renames = allPathIds; while (!renames.isEmpty()) { renames = addCommitsAndGetRenames(renames, allPathIds, result); allPathIds.addAll(renames); } return result; }
private void flush() { try { if (myIndexStorage != null) { myIndexStorage.messages.force(); myIndexStorage.trigrams.flush(); myIndexStorage.users.flush(); myIndexStorage.paths.flush(); myIndexStorage.commits.flush(); } } catch (StorageException e) { myFatalErrorsConsumer.consume(this, e); } }
private void processRuntimeException(@Nonnull RuntimeException e) { if (myIndexStorage != null) myIndexStorage.markCorrupted(); if (e.getCause() instanceof IOException || e.getCause() instanceof StorageException) { myFatalErrorsConsumer.consume(this, e); } else { throw new RuntimeException(e); } }
@Nullable public TIntHashSet getCommitsForSubstring(@Nonnull String string) throws StorageException { MyTrigramProcessor trigramProcessor = new MyTrigramProcessor(); TrigramBuilder.processTrigrams(string, trigramProcessor); if (trigramProcessor.map.isEmpty()) return null; return getCommitsWithAllKeys(trigramProcessor.map.keySet()); }
public TIntHashSet getCommitsForUsers(@Nonnull Set<VcsUser> users) throws IOException, StorageException { Set<Integer> ids = ContainerUtil.newHashSet(); for (VcsUser user : users) { ids.add(myUserRegistry.getUserId(user)); } return getCommitsWithAnyKey(ids); }
@Override public void differentiate(@Nonnull Map<Key, Value> newData, @Nonnull final KeyValueUpdateProcessor<Key, Value> addProcessor, @Nonnull KeyValueUpdateProcessor<Key, Value> updateProcessor, @Nonnull RemovedKeyProcessor<Key> removeProcessor) throws StorageException { processKeys(newData, addProcessor, myInputId); }
static <Key, Value >void processKeys(@Nonnull Map<Key, Value> currentData, @Nonnull final KeyValueUpdateProcessor<Key, Value> processor, final int inputId) throws StorageException { if (currentData instanceof THashMap) { final StorageException[] exception = new StorageException[]{null}; ((THashMap<Key, Value>)currentData).forEachEntry(new TObjectObjectProcedure<Key, Value>() { @Override public boolean execute(Key k, Value v) { try { processor.process(k, v, inputId); } catch (StorageException e) { exception[0] = e; return false; } return true; } }); if (exception[0] != null) { throw exception[0]; } } else { for (Map.Entry<Key, Value> entry : currentData.entrySet()) { processor.process(entry.getKey(), entry.getValue(), inputId); } } }
public void iterateKeys(KeyValueUpdateProcessor<Key, Value> addProcessor, KeyValueUpdateProcessor<Key, Value> updateProcessor, RemovedKeyProcessor<Key> removeProcessor) throws StorageException { final InputDataDiffBuilder<Key, Value> currentData; try { currentData = getCurrentDataEvaluator().compute(); } catch (IOException e) { throw new StorageException(e); } currentData.differentiate(myNewData, addProcessor, updateProcessor, removeProcessor); }
@Override @Nonnull public ChangeTrackingValueContainer<Value> read(final Key key) throws StorageException { l.lock(); try { return myCache.get(key); } catch (RuntimeException e) { return unwrapCauseAndRethrow(e); } finally { l.unlock(); } }
@Override public void addValue(final Key key, final int inputId, final Value value) throws StorageException { try { myMap.markDirty(); if (!myKeyIsUniqueForIndexedFile) { read(key).addValue(inputId, value); return; } ChangeTrackingValueContainer<Value> cached; try { l.lock(); cached = myCache.getIfCached(key); } finally { l.unlock(); } if (cached != null) { cached.addValue(inputId, value); return; } // do not pollute the cache with keys unique to indexed file ChangeTrackingValueContainer<Value> valueContainer = new ChangeTrackingValueContainer<Value>(null); valueContainer.addValue(inputId, value); myMap.put(key, valueContainer); } catch (IOException e) { throw new StorageException(e); } }
@Override public void removeAllValues(@Nonnull Key key, int inputId) throws StorageException { try { myMap.markDirty(); // important: assuming the key exists in the index read(key).removeAssociatedValue(inputId); } catch (IOException e) { throw new StorageException(e); } }
protected static <T> T unwrapCauseAndRethrow(RuntimeException e) throws StorageException { final Throwable cause = e.getCause(); if (cause instanceof IOException) { throw new StorageException(cause); } if (cause instanceof StorageException) { throw (StorageException)cause; } throw e; }
@Override public void differentiate(@Nonnull Map<Key, Value> newData, @Nonnull KeyValueUpdateProcessor<Key, Value> addProcessor, @Nonnull KeyValueUpdateProcessor<Key, Value> updateProcessor, @Nonnull RemovedKeyProcessor<Key> removeProcessor) throws StorageException { differentiateWithKeySeq(mySeq, newData, myInputId, addProcessor, removeProcessor); }
static <Key, Value> void differentiateWithKeySeq(@Nonnull Collection<Key> currentData, @Nonnull Map<Key, Value> newData, int inputId, @Nonnull KeyValueUpdateProcessor<Key, Value> addProcessor, @Nonnull RemovedKeyProcessor<Key> removeProcessor) throws StorageException { for (Key key : currentData) { removeProcessor.process(key, inputId); } EmptyInputDataDiffBuilder.processKeys(newData, addProcessor, inputId); }
public List<String> getFilesByWord(@NotNull String word) throws StorageException { return myIndex.getData(word).toValueList(); }
public void update(final String path, @Nullable String content, @Nullable String oldContent) throws StorageException { myIndex.update(Math.abs(path.hashCode()), toInput(path, content)).compute(); }
public void flush() throws StorageException { myIndex.flush(); }
public void testUpdate() throws StorageException, IOException { final File storageFile = FileUtil.createTempFile("indextest", "storage"); final File metaIndexFile = FileUtil.createTempFile("indextest_inputs", "storage"); final MapIndexStorage indexStorage = new MapIndexStorage(storageFile, new EnumeratorStringDescriptor(), new EnumeratorStringDescriptor(), 16 * 1024); final StringIndex index = new StringIndex(indexStorage, new Factory<PersistentHashMap<Integer, Collection<String>>>() { @Override public PersistentHashMap<Integer, Collection<String>> create() { try { return createMetaIndex(metaIndexFile); } catch (IOException e) { throw new RuntimeException(e); } } }); try { // build index index.update("com/ppp/a.java", "a b c d", null); index.update("com/ppp/b.java", "a b g h", null); index.update("com/ppp/c.java", "a z f", null); index.update("com/ppp/d.java", "a a u y z", null); index.update("com/ppp/e.java", "a n chj e c d", null); assertDataEquals(index.getFilesByWord("a"), "com/ppp/a.java", "com/ppp/b.java", "com/ppp/c.java", "com/ppp/d.java", "com/ppp/e.java"); assertDataEquals(index.getFilesByWord("b"), "com/ppp/a.java", "com/ppp/b.java"); assertDataEquals(index.getFilesByWord("c"), "com/ppp/a.java", "com/ppp/e.java"); assertDataEquals(index.getFilesByWord("d"), "com/ppp/a.java", "com/ppp/e.java"); assertDataEquals(index.getFilesByWord("g"), "com/ppp/b.java"); assertDataEquals(index.getFilesByWord("h"), "com/ppp/b.java"); assertDataEquals(index.getFilesByWord("z"), "com/ppp/c.java", "com/ppp/d.java"); assertDataEquals(index.getFilesByWord("f"), "com/ppp/c.java"); assertDataEquals(index.getFilesByWord("u"), "com/ppp/d.java"); assertDataEquals(index.getFilesByWord("y"), "com/ppp/d.java"); assertDataEquals(index.getFilesByWord("n"), "com/ppp/e.java"); assertDataEquals(index.getFilesByWord("chj"), "com/ppp/e.java"); assertDataEquals(index.getFilesByWord("e"), "com/ppp/e.java"); // update index index.update("com/ppp/d.java", "a u y z", "a a u y z"); assertDataEquals(index.getFilesByWord("a"), "com/ppp/a.java", "com/ppp/b.java", "com/ppp/c.java", "com/ppp/d.java", "com/ppp/e.java"); index.update("com/ppp/d.java", "u y z", "a u y z"); assertDataEquals(index.getFilesByWord("a"), "com/ppp/a.java", "com/ppp/b.java", "com/ppp/c.java", "com/ppp/e.java"); index.update("com/ppp/d.java", "a a a u y z", "u y z"); assertDataEquals(index.getFilesByWord("a"), "com/ppp/a.java", "com/ppp/b.java", "com/ppp/c.java", "com/ppp/d.java", "com/ppp/e.java"); index.update("com/ppp/e.java", "a n chj e c d z", "a n chj e c d"); assertDataEquals(index.getFilesByWord("z"), "com/ppp/c.java", "com/ppp/d.java", "com/ppp/e.java"); index.update("com/ppp/b.java", null, "a b g h"); assertDataEquals(index.getFilesByWord("a"), "com/ppp/a.java", "com/ppp/c.java", "com/ppp/d.java", "com/ppp/e.java"); assertDataEquals(index.getFilesByWord("b"), "com/ppp/a.java"); assertDataEquals(index.getFilesByWord("g")); assertDataEquals(index.getFilesByWord("h")); } finally { indexStorage.close(); FileUtil.delete(storageFile); } }
public List<String> getFilesByWord(String word) throws StorageException { return myIndex.getData(word).toValueList(); }
public void update(final String path, @Nullable String content, @Nullable String oldContent) throws StorageException { myIndex.update(path.hashCode(), toInput(path, content)).compute(); }
@Override public void flush() throws StorageException { super.flush(); myPathsIndexer.getPathsEnumerator().force(); }
/** * produce a diff between existing data and newData and consume result to addProcessor, updateProcessor and removeProcessor. */ public abstract void differentiate(@Nonnull Map<Key, Value> newData, @Nonnull KeyValueUpdateProcessor<Key, Value> addProcessor, @Nonnull KeyValueUpdateProcessor<Key, Value> updateProcessor, @Nonnull RemovedKeyProcessor<Key> removeProcessor) throws StorageException;
@Nonnull ValueContainer<Value> read(Key key) throws StorageException;
void process(Key key, Value value, int inputId) throws StorageException;
void addValue(Key key, int inputId, Value value) throws StorageException;
void removeAllValues(@Nonnull Key key, int inputId) throws StorageException;
void clear() throws StorageException;
void close() throws StorageException;
void process(Key key, int inputId) throws StorageException;