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.diagnostic.Logger;
20 import com.intellij.util.Consumer;
21 import com.intellij.util.containers.ContainerUtil;
22 import com.intellij.util.indexing.DataIndexer;
23 import com.intellij.util.indexing.ScalarIndexExtension;
24 import com.intellij.util.indexing.StorageException;
25 import com.intellij.vcs.log.VcsFullCommitDetails;
26 import com.intellij.vcs.log.VcsUser;
27 import com.intellij.vcs.log.data.VcsUserRegistryImpl;
28 import com.intellij.vcs.log.impl.FatalErrorHandler;
29 import gnu.trove.THashMap;
30 import gnu.trove.TIntHashSet;
31 import org.jetbrains.annotations.NotNull;
34 import java.io.IOException;
35 import java.util.Collection;
36 import java.util.Collections;
40 import static com.intellij.vcs.log.data.index.VcsLogPersistentIndex.getVersion;
42 public class VcsLogUserIndex extends VcsLogFullDetailsIndex<Void> {
43 private static final Logger LOG = Logger.getInstance(VcsLogUserIndex.class);
44 public static final String USERS = "users";
45 @NotNull private final VcsUserRegistryImpl myUserRegistry;
47 public VcsLogUserIndex(@NotNull String logId,
48 @NotNull VcsUserRegistryImpl userRegistry,
49 @NotNull FatalErrorHandler consumer,
50 @NotNull Disposable disposableParent) throws IOException {
51 super(logId, USERS, getVersion(), new UserIndexer(userRegistry), ScalarIndexExtension.VOID_DATA_EXTERNALIZER,
52 consumer, disposableParent);
53 myUserRegistry = userRegistry;
54 ((UserIndexer)myIndexer).setFatalErrorConsumer(e -> consumer.consume(this, e));
57 public TIntHashSet getCommitsForUsers(@NotNull Set<VcsUser> users) throws IOException, StorageException {
58 Set<Integer> ids = ContainerUtil.newHashSet();
59 for (VcsUser user : users) {
60 ids.add(myUserRegistry.getUserId(user));
62 return getCommitsWithAnyKey(ids);
65 private static class UserIndexer implements DataIndexer<Integer, Void, VcsFullCommitDetails> {
66 @NotNull private final VcsUserRegistryImpl myRegistry;
67 @NotNull private Consumer<Exception> myFatalErrorConsumer = LOG::error;
69 public UserIndexer(@NotNull VcsUserRegistryImpl registry) {
70 myRegistry = registry;
75 public Map<Integer, Void> map(@NotNull VcsFullCommitDetails inputData) {
76 Map<Integer, Void> result = new THashMap<>();
79 result.put(myRegistry.getUserId(inputData.getAuthor()), null);
81 catch (IOException e) {
82 myFatalErrorConsumer.consume(e);
88 public void setFatalErrorConsumer(@NotNull Consumer<Exception> fatalErrorConsumer) {
89 myFatalErrorConsumer = fatalErrorConsumer;