Notedb: Move individual note parsing into a new class

We would like to store additional information besides inline comments
in notes. As a first step towards doing so, encapsulate parsing state
into a new class, RevisionNote. This class reads the note data once
and can parse it into multiple fields (but for now still one).

When updating the note, we copy the immutable RevisionNote into a
mutable RevisionNoteBuilder, which knows how to write itself out to a
note. This shifts a little bit of work from CommentsInNotesUtil to
ChangeUpdate.

Change-Id: Ia55888b043a5624b81a3fdde733363cd1f1a5aee
This commit is contained in:
Dave Borowitz
2016-01-26 09:06:56 -05:00
parent a5416e1a25
commit 3afa3a5da8
6 changed files with 137 additions and 26 deletions

View File

@@ -130,7 +130,11 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
private ImmutableListMultimap<PatchSet.Id, ChangeMessage> changeMessagesByPatchSet;
private ImmutableListMultimap<RevId, PatchLineComment> comments;
private ImmutableSet<String> hashtags;
// Mutable note map state, only used by ChangeUpdate to make in-place editing
// of notes easier.
NoteMap noteMap;
Map<RevId, RevisionNote> revisionNotes;
private final AllUsersName allUsers;
private DraftCommentNotes draftCommentNotes;
@@ -250,6 +254,10 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
return noteMap;
}
Map<RevId, RevisionNote> getRevisionNotes() {
return revisionNotes;
}
@Override
protected String getRefName() {
return ChangeNoteUtil.changeRefName(getChangeId());
@@ -280,7 +288,8 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
changeMessagesByPatchSet = parser.buildMessagesByPatchSet();
allChangeMessages = parser.buildAllMessages();
comments = ImmutableListMultimap.copyOf(parser.comments);
noteMap = parser.commentNoteMap;
noteMap = parser.noteMap;
revisionNotes = parser.revisionNotes;
change.setDest(new Branch.NameKey(getProjectName(), parser.branch));
change.setTopic(Strings.emptyToNull(parser.topic));
change.setCreatedOn(parser.createdOn);

View File

@@ -64,8 +64,10 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.InvalidObjectIdException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.notes.NoteMap;
import org.eclipse.jgit.revwalk.FooterKey;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -96,7 +98,8 @@ class ChangeNotesParser implements AutoCloseable {
final Multimap<RevId, PatchLineComment> comments;
final TreeMap<PatchSet.Id, PatchSet> patchSets;
final Map<PatchSet.Id, PatchSetState> patchSetStates;
NoteMap commentNoteMap;
final Map<RevId, RevisionNote> revisionNotes;
String branch;
Change.Status status;
String topic;
@@ -108,6 +111,7 @@ class ChangeNotesParser implements AutoCloseable {
String originalSubject;
String submissionId;
PatchSet.Id currentPatchSetId;
NoteMap noteMap;
private final Change.Id changeId;
private final ObjectId tip;
@@ -119,8 +123,8 @@ class ChangeNotesParser implements AutoCloseable {
private final Multimap<PatchSet.Id, ChangeMessage> changeMessagesByPatchSet;
ChangeNotesParser(Change change, ObjectId tip, RevWalk walk,
GitRepositoryManager repoManager) throws RepositoryNotFoundException,
IOException {
GitRepositoryManager repoManager)
throws RepositoryNotFoundException, IOException {
this.changeId = change.getId();
this.tip = tip;
this.walk = walk;
@@ -134,6 +138,7 @@ class ChangeNotesParser implements AutoCloseable {
comments = ArrayListMultimap.create();
patchSets = Maps.newTreeMap(ReviewDbUtil.intKeyOrdering());
patchSetStates = Maps.newHashMap();
revisionNotes = Maps.newHashMap();
}
@Override
@@ -146,7 +151,7 @@ class ChangeNotesParser implements AutoCloseable {
for (RevCommit commit : walk) {
parse(commit);
}
parseComments();
parseNotes();
allPastReviewers.addAll(reviewers.keySet());
pruneReviewers();
updatePatchSetStates();
@@ -459,11 +464,20 @@ class ChangeNotesParser implements AutoCloseable {
allChangeMessages.add(changeMessage);
}
private void parseComments()
private void parseNotes()
throws IOException, ConfigInvalidException {
commentNoteMap = CommentsInNotesUtil.parseCommentsFromNotes(repo,
ChangeNoteUtil.changeRefName(changeId), walk, changeId,
comments, PatchLineComment.Status.PUBLISHED);
ObjectReader reader = walk.getObjectReader();
RevCommit tipCommit = walk.parseCommit(tip);
noteMap = NoteMap.read(reader, tipCommit);
for (Note note : noteMap) {
RevisionNote rn = new RevisionNote(changeId, reader, note.getData());
RevId revId = new RevId(note.name());
revisionNotes.put(revId, rn);
for (PatchLineComment plc : rn.comments) {
comments.put(revId, plc);
}
}
}
private void parseApproval(PatchSet.Id psId, Account.Id accountId,

View File

@@ -26,7 +26,7 @@ import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_SUBJECT;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_SUBMISSION_ID;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_SUBMITTED_WITH;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_TOPIC;
import static com.google.gerrit.server.notedb.CommentsInNotesUtil.addCommentToMap;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
@@ -67,10 +67,9 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -397,7 +396,7 @@ public class ChangeUpdate extends AbstractChangeUpdate {
}
/** @return the tree id for the updated tree */
private ObjectId storeCommentsInNotes() throws OrmException, IOException {
private ObjectId storeRevisionNotes() throws OrmException, IOException {
ChangeNotes notes = ctl.getNotes().load();
NoteMap noteMap = notes.getNoteMap();
if (noteMap == null) {
@@ -407,19 +406,22 @@ public class ChangeUpdate extends AbstractChangeUpdate {
return null;
}
Map<RevId, List<PatchLineComment>> allComments = Maps.newHashMap();
for (Map.Entry<RevId, Collection<PatchLineComment>> e
: notes.getComments().asMap().entrySet()) {
List<PatchLineComment> comments = new ArrayList<>();
for (PatchLineComment c : e.getValue()) {
comments.add(c);
}
allComments.put(e.getKey(), comments);
}
Map<RevId, RevisionNoteBuilder> builders = new HashMap<>();
for (PatchLineComment c : comments) {
addCommentToMap(allComments, c);
RevisionNoteBuilder b = builders.get(c.getRevId());
if (b == null) {
b = new RevisionNoteBuilder(notes.getRevisionNotes().get(c.getRevId()));
builders.put(c.getRevId(), b);
}
b.addComment(c);
}
commentsUtil.writeCommentsToNoteMap(noteMap, allComments, inserter);
for (Map.Entry<RevId, RevisionNoteBuilder> e : builders.entrySet()) {
ObjectId data = inserter.insert(
OBJ_BLOB, e.getValue().build(commentsUtil));
noteMap.set(ObjectId.fromString(e.getKey().get()), data);
}
return noteMap.writeTree(inserter);
}
@@ -441,7 +443,7 @@ public class ChangeUpdate extends AbstractChangeUpdate {
IOException {
CommitBuilder builder = new CommitBuilder();
if (migration.writeChanges()) {
ObjectId treeId = storeCommentsInNotes();
ObjectId treeId = storeRevisionNotes();
if (treeId != null) {
builder.setTreeId(treeId);
}

View File

@@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gerrit.server.PatchLineCommentsUtil.PLC_ORDER;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.GERRIT_PLACEHOLDER_HOST;
import static com.google.gerrit.server.notedb.ChangeNotes.parseException;
import static com.google.gerrit.server.notedb.RevisionNote.MAX_NOTE_SZ;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
@@ -87,7 +88,6 @@ public class CommentsInNotesUtil {
private static final String PATCH_SET = "Patch-set";
private static final String REVISION = "Revision";
private static final String UUID = "UUID";
private static final int MAX_NOTE_SZ = 25 << 20;
public static NoteMap parseCommentsFromNotes(Repository repo, String refName,
RevWalk walk, Change.Id changeId,

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.notedb;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import java.io.IOException;
class RevisionNote {
static final int MAX_NOTE_SZ = 25 << 20;
final ImmutableList<PatchLineComment> comments;
RevisionNote(Change.Id changeId, ObjectReader reader, ObjectId noteId)
throws ConfigInvalidException, IOException {
byte[] bytes = reader.open(noteId, OBJ_BLOB).getCachedBytes(MAX_NOTE_SZ);
comments = ImmutableList.copyOf(CommentsInNotesUtil.parseNote(
bytes, changeId, PatchLineComment.Status.PUBLISHED));
}
}

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.notedb;
import static com.google.gerrit.server.PatchLineCommentsUtil.PLC_ORDER;
import com.google.common.collect.Maps;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import java.util.HashMap;
import java.util.Map;
class RevisionNoteBuilder {
private final Map<PatchLineComment.Key, PatchLineComment> comments;
RevisionNoteBuilder(RevisionNote base) {
if (base != null) {
comments = Maps.newHashMapWithExpectedSize(base.comments.size());
for (PatchLineComment c : base.comments) {
addComment(c);
}
} else {
comments = new HashMap<>();
}
}
void addComment(PatchLineComment comment) {
comments.put(comment.getKey(), comment);
}
byte[] build(CommentsInNotesUtil commentsUtil) {
return commentsUtil.buildNote(PLC_ORDER.sortedCopy(comments.values()));
}
}