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);
}
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);
}
}
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();
}
}
Aggregations