Search in sources :

Example 1 with CommentContext

use of com.google.gerrit.entities.CommentContext in project gerrit by GerritCodeReview.

the class CommentContextSerializerTest method roundTripValue.

@Test
public void roundTripValue() {
    CommentContext commentContext = CommentContext.create(ImmutableMap.of(1, "line_1", 2, "line_2"), "text/x-java");
    byte[] serialized = CommentContextSerializer.INSTANCE.serialize(commentContext);
    CommentContext deserialized = CommentContextSerializer.INSTANCE.deserialize(serialized);
    assertThat(commentContext).isEqualTo(deserialized);
}
Also used : CommentContext(com.google.gerrit.entities.CommentContext) Test(org.junit.Test)

Example 2 with CommentContext

use of com.google.gerrit.entities.CommentContext in project gerrit by GerritCodeReview.

the class CommentContextCacheImpl method getAll.

@Override
public ImmutableMap<CommentContextKey, CommentContext> getAll(Iterable<CommentContextKey> inputKeys) {
    ImmutableMap.Builder<CommentContextKey, CommentContext> result = ImmutableMap.builder();
    // We do two transformations to the input keys: first we adjust the max context padding, and
    // second we hash the file path. The transformed keys are used to request context from the
    // cache. Keeping a map of the original inputKeys to the transformed keys
    Map<CommentContextKey, CommentContextKey> inputKeysToCacheKeys = Streams.stream(inputKeys).collect(Collectors.toMap(Function.identity(), k -> adjustMaxContextPadding(k).toBuilder().path(Loader.hashPath(k.path())).build()));
    try {
        ImmutableMap<CommentContextKey, CommentContext> allContext = contextCache.getAll(inputKeysToCacheKeys.values());
        for (CommentContextKey inputKey : inputKeys) {
            CommentContextKey cacheKey = inputKeysToCacheKeys.get(inputKey);
            result.put(inputKey, allContext.get(cacheKey));
        }
        return result.build();
    } catch (ExecutionException e) {
        throw new StorageException("Failed to retrieve comments' context", e);
    }
}
Also used : Module(com.google.inject.Module) Iterables(com.google.common.collect.Iterables) LoadingCache(com.google.common.cache.LoadingCache) Inject(com.google.inject.Inject) Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) Function(java.util.function.Function) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Change(com.google.gerrit.entities.Change) Weigher(com.google.common.cache.Weigher) AllCommentContextProto(com.google.gerrit.server.cache.proto.Cache.AllCommentContextProto) ImmutableMap(com.google.common.collect.ImmutableMap) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StorageException(com.google.gerrit.exceptions.StorageException) ContextInput(com.google.gerrit.server.comment.CommentContextLoader.ContextInput) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) CommentContext(com.google.gerrit.entities.CommentContext) Collectors(java.util.stream.Collectors) CacheModule(com.google.gerrit.server.cache.CacheModule) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) Protos(com.google.gerrit.proto.Protos) CacheSerializer(com.google.gerrit.server.cache.serialize.CacheSerializer) List(java.util.List) CommentContextProto(com.google.gerrit.server.cache.proto.Cache.AllCommentContextProto.CommentContextProto) Project(com.google.gerrit.entities.Project) Named(com.google.inject.name.Named) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FluentLogger(com.google.common.flogger.FluentLogger) CommentContext(com.google.gerrit.entities.CommentContext) ExecutionException(java.util.concurrent.ExecutionException) StorageException(com.google.gerrit.exceptions.StorageException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with CommentContext

use of com.google.gerrit.entities.CommentContext in project gerrit by GerritCodeReview.

the class CommentContextLoader method getContext.

/**
 * Load the comment context for multiple contextInputs at once. This method will open the
 * repository and read the source files for all necessary contextInputs' file paths.
 *
 * @param contextInputs a list of contextInputs.
 * @return a Map where all entries consist of the input contextInputs and the values are their
 *     corresponding {@link CommentContext}.
 */
public Map<ContextInput, CommentContext> getContext(Collection<ContextInput> contextInputs) throws IOException {
    ImmutableMap.Builder<ContextInput, CommentContext> result = ImmutableMap.builderWithExpectedSize(Iterables.size(contextInputs));
    // Group contextInputs by commit ID so that each commit is parsed only once
    Map<ObjectId, List<ContextInput>> commentsByCommitId = contextInputs.stream().collect(groupingBy(ContextInput::commitId));
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        for (ObjectId commitId : commentsByCommitId.keySet()) {
            RevCommit commit;
            try {
                commit = rw.parseCommit(commitId);
            } catch (IncorrectObjectTypeException | MissingObjectException e) {
                logger.atWarning().log("Commit %s is missing or has an incorrect object type", commitId);
                commentsByCommitId.get(commitId).forEach(contextInput -> result.put(contextInput, CommentContext.empty()));
                continue;
            }
            for (ContextInput contextInput : commentsByCommitId.get(commitId)) {
                Optional<Range> range = getStartAndEndLines(contextInput);
                if (!range.isPresent()) {
                    result.put(contextInput, CommentContext.empty());
                    continue;
                }
                String filePath = contextInput.filePath();
                switch(filePath) {
                    case COMMIT_MSG:
                        result.put(contextInput, getContextForCommitMessage(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
                        break;
                    case MERGE_LIST:
                        result.put(contextInput, getContextForMergeList(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
                        break;
                    default:
                        result.put(contextInput, getContextForFilePath(repo, rw, commit, filePath, range.get(), contextInput.contextPadding()));
                }
            }
        }
        return result.build();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) ComparisonType(com.google.gerrit.server.patch.ComparisonType) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ProjectCache(com.google.gerrit.server.project.ProjectCache) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) SrcContentResolver(com.google.gerrit.server.patch.SrcContentResolver) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) Comment(com.google.gerrit.entities.Comment) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) MimeType(eu.medsea.mimeutil.MimeType) Assisted(com.google.inject.assistedinject.Assisted) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchScript(com.google.gerrit.common.data.PatchScript) ContextLineInfo(com.google.gerrit.extensions.common.ContextLineInfo) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) Map(java.util.Map) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableMap(com.google.common.collect.ImmutableMap) Text(com.google.gerrit.server.patch.Text) Collection(java.util.Collection) FileTypeRegistry(com.google.gerrit.server.mime.FileTypeRegistry) ProjectState(com.google.gerrit.server.project.ProjectState) IOException(java.io.IOException) CommentContext(com.google.gerrit.entities.CommentContext) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) MimeUtil2(eu.medsea.mimeutil.MimeUtil2) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) CommentContext(com.google.gerrit.entities.CommentContext) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ImmutableMap(com.google.common.collect.ImmutableMap) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Repository(org.eclipse.jgit.lib.Repository) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

CommentContext (com.google.gerrit.entities.CommentContext)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Iterables (com.google.common.collect.Iterables)2 FluentLogger (com.google.common.flogger.FluentLogger)2 Comment (com.google.gerrit.entities.Comment)2 Project (com.google.gerrit.entities.Project)2 Inject (com.google.inject.Inject)2 IOException (java.io.IOException)2 List (java.util.List)2 Map (java.util.Map)2 AutoValue (com.google.auto.value.AutoValue)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 Weigher (com.google.common.cache.Weigher)1 ImmutableList (com.google.common.collect.ImmutableList)1 Streams (com.google.common.collect.Streams)1 Hashing (com.google.common.hash.Hashing)1 Nullable (com.google.gerrit.common.Nullable)1 PatchScript (com.google.gerrit.common.data.PatchScript)1