2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.vcs.log.data.index;
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.util.text.TrigramBuilder;
20 import com.intellij.util.indexing.DataIndexer;
21 import com.intellij.util.indexing.ScalarIndexExtension;
22 import com.intellij.util.indexing.StorageException;
23 import com.intellij.util.indexing.ValueContainer;
24 import com.intellij.vcs.log.VcsFullCommitDetails;
25 import com.intellij.vcs.log.impl.FatalErrorHandler;
26 import gnu.trove.THashMap;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
31 import java.io.IOException;
32 import java.util.Collection;
33 import java.util.Collections;
36 import static com.intellij.vcs.log.data.index.VcsLogPersistentIndex.getVersion;
38 public class VcsLogMessagesTrigramIndex extends VcsLogFullDetailsIndex<Void> {
39 public static final String TRIGRAMS = "trigrams";
41 public VcsLogMessagesTrigramIndex(@NotNull String logId,
42 @NotNull FatalErrorHandler fatalErrorHandler,
43 @NotNull Disposable disposableParent) throws IOException {
44 super(logId, TRIGRAMS, getVersion(), new TrigramMessageIndexer(), ScalarIndexExtension.VOID_DATA_EXTERNALIZER,
45 fatalErrorHandler, disposableParent);
49 public ValueContainer.IntIterator getCommitsForSubstring(@NotNull String string) throws StorageException {
50 MyTrigramProcessor trigramProcessor = new MyTrigramProcessor();
51 TrigramBuilder.processTrigrams(string, trigramProcessor);
53 if (trigramProcessor.map.isEmpty()) return null;
55 return getCommitsWithAllKeys(trigramProcessor.map.keySet());
58 public static class TrigramMessageIndexer implements DataIndexer<Integer, Void, VcsFullCommitDetails> {
61 public Map<Integer, Void> map(@NotNull VcsFullCommitDetails inputData) {
62 MyTrigramProcessor trigramProcessor = new MyTrigramProcessor();
63 TrigramBuilder.processTrigrams(inputData.getFullMessage(), trigramProcessor);
65 return trigramProcessor.map;
69 private static class MyTrigramProcessor extends TrigramBuilder.TrigramProcessor {
70 Map<Integer, Void> map;
73 public boolean consumeTrigramsCount(int count) {
74 map = new THashMap<>(count);
79 public boolean execute(int value) {