Search in sources :

Example 36 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method rebuildReviewDb.

@Override
public void rebuildReviewDb(ReviewDb db, Project.NameKey project, Change.Id changeId) throws OrmException {
    // TODO(dborowitz): Fail fast if changes tables are disabled in ReviewDb.
    ChangeNotes notes = notesFactory.create(db, project, changeId);
    ChangeBundle bundle = ChangeBundle.fromNotes(commentsUtil, notes);
    db = ReviewDbUtil.unwrapDb(db);
    db.changes().beginTransaction(changeId);
    try {
        Change c = db.changes().get(changeId);
        PrimaryStorage ps = PrimaryStorage.of(c);
        if (ps != PrimaryStorage.NOTE_DB) {
            throw new OrmException("primary storage of " + changeId + " is " + ps);
        }
        db.changes().upsert(Collections.singleton(c));
        putExactlyEntities(db.changeMessages(), db.changeMessages().byChange(c.getId()), bundle.getChangeMessages());
        putExactlyEntities(db.patchSets(), db.patchSets().byChange(c.getId()), bundle.getPatchSets());
        putExactlyEntities(db.patchSetApprovals(), db.patchSetApprovals().byChange(c.getId()), bundle.getPatchSetApprovals());
        putExactlyEntities(db.patchComments(), db.patchComments().byChange(c.getId()), bundle.getPatchLineComments());
        db.commit();
    } finally {
        db.rollback();
    }
}
Also used : PrimaryStorage(com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage) OrmException(com.google.gwtorm.server.OrmException) ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.reviewdb.client.Change)

Example 37 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method buildUpdates.

@Override
public void buildUpdates(NoteDbUpdateManager manager, ChangeBundle bundle) throws IOException, OrmException {
    manager.setCheckExpectedState(false).setRefLogMessage("Rebuilding change");
    Change change = new Change(bundle.getChange());
    if (bundle.getPatchSets().isEmpty()) {
        throw new NoPatchSetsException(change.getId());
    }
    // We will rebuild all events, except for draft comments, in buckets based
    // on author and timestamp.
    List<Event> events = new ArrayList<>();
    ListMultimap<Account.Id, DraftCommentEvent> draftCommentEvents = MultimapBuilder.hashKeys().arrayListValues().build();
    events.addAll(getHashtagsEvents(change, manager));
    // Delete ref only after hashtags have been read
    deleteChangeMetaRef(change, manager.getChangeRepo().cmds);
    deleteDraftRefs(change, manager.getAllUsersRepo());
    Integer minPsNum = getMinPatchSetNum(bundle);
    TreeMap<PatchSet.Id, PatchSetEvent> patchSetEvents = new TreeMap<>(ReviewDbUtil.intKeyOrdering());
    for (PatchSet ps : bundle.getPatchSets()) {
        PatchSetEvent pse = new PatchSetEvent(change, ps, manager.getChangeRepo().rw);
        patchSetEvents.put(ps.getId(), pse);
        events.add(pse);
        for (Comment c : getComments(bundle, serverId, Status.PUBLISHED, ps)) {
            CommentEvent e = new CommentEvent(c, change, ps, patchListCache);
            events.add(e.addDep(pse));
        }
        for (Comment c : getComments(bundle, serverId, Status.DRAFT, ps)) {
            DraftCommentEvent e = new DraftCommentEvent(c, change, ps, patchListCache);
            draftCommentEvents.put(c.author.getId(), e);
        }
    }
    ensurePatchSetOrder(patchSetEvents);
    for (PatchSetApproval psa : bundle.getPatchSetApprovals()) {
        PatchSetEvent pse = patchSetEvents.get(psa.getPatchSetId());
        if (pse != null) {
            events.add(new ApprovalEvent(psa, change.getCreatedOn()).addDep(pse));
        }
    }
    for (Table.Cell<ReviewerStateInternal, Account.Id, Timestamp> r : bundle.getReviewers().asTable().cellSet()) {
        events.add(new ReviewerEvent(r, change.getCreatedOn()));
    }
    Change noteDbChange = new Change(null, null, null, null, null);
    for (ChangeMessage msg : bundle.getChangeMessages()) {
        Event msgEvent = new ChangeMessageEvent(change, noteDbChange, msg, change.getCreatedOn());
        if (msg.getPatchSetId() != null) {
            PatchSetEvent pse = patchSetEvents.get(msg.getPatchSetId());
            if (pse == null) {
                // Ignore events for missing patch sets.
                continue;
            }
            msgEvent.addDep(pse);
        }
        events.add(msgEvent);
    }
    sortAndFillEvents(change, noteDbChange, bundle.getPatchSets(), events, minPsNum);
    EventList<Event> el = new EventList<>();
    for (Event e : events) {
        if (!el.canAdd(e)) {
            flushEventsToUpdate(manager, el, change);
            checkState(el.canAdd(e));
        }
        el.add(e);
    }
    flushEventsToUpdate(manager, el, change);
    EventList<DraftCommentEvent> plcel = new EventList<>();
    for (Account.Id author : draftCommentEvents.keys()) {
        for (DraftCommentEvent e : Ordering.natural().sortedCopy(draftCommentEvents.get(author))) {
            if (!plcel.canAdd(e)) {
                flushEventsToDraftUpdate(manager, plcel, change);
                checkState(plcel.canAdd(e));
            }
            plcel.add(e);
        }
        flushEventsToDraftUpdate(manager, plcel, change);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ArrayList(java.util.ArrayList) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) Timestamp(java.sql.Timestamp) PatchLineComment(com.google.gerrit.reviewdb.client.PatchLineComment) Comment(com.google.gerrit.reviewdb.client.Comment) Table(com.google.common.collect.Table) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) TreeMap(java.util.TreeMap) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) GerritServerId(com.google.gerrit.server.config.GerritServerId) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 38 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class PrimaryStorageMigrator method setPrimaryStorageReviewDb.

private void setPrimaryStorageReviewDb(Change.Id id, ObjectId newMetaId) throws OrmException, IOException {
    ImmutableMap.Builder<Account.Id, ObjectId> draftIds = ImmutableMap.builder();
    try (Repository repo = repoManager.openRepository(allUsers)) {
        for (Ref draftRef : repo.getRefDatabase().getRefs(RefNames.refsDraftCommentsPrefix(id)).values()) {
            Account.Id accountId = Account.Id.fromRef(draftRef.getName());
            if (accountId != null) {
                draftIds.put(accountId, draftRef.getObjectId().copy());
            }
        }
    }
    NoteDbChangeState newState = new NoteDbChangeState(id, PrimaryStorage.REVIEW_DB, Optional.of(RefState.create(newMetaId, draftIds.build())), Optional.empty());
    db().changes().atomicUpdate(id, new AtomicUpdate<Change>() {

        @Override
        public Change update(Change change) {
            if (PrimaryStorage.of(change) != PrimaryStorage.NOTE_DB) {
                throw new OrmRuntimeException("change " + id + " is not NoteDb primary: " + change.getNoteDbState());
            }
            change.setNoteDbState(newState.toString());
            return change;
        }
    });
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) ImmutableMap(com.google.common.collect.ImmutableMap) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 39 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class ChangeBundleTest method diffChangesSanitizesSubjectsBeforeComparison.

@Test
public void diffChangesSanitizesSubjectsBeforeComparison() throws Exception {
    Change c1 = TestChanges.newChange(new Project.NameKey("project"), new Account.Id(100));
    c1.setCurrentPatchSet(c1.currentPatchSetId(), "Subject\r\rbody", "Original");
    Change c2 = clone(c1);
    c2.setCurrentPatchSet(c2.currentPatchSetId(), "Subject  body", "Original");
    // Both ReviewDb, exact match required
    ChangeBundle b1 = new ChangeBundle(c1, messages(), patchSets(), approvals(), comments(), reviewers(), REVIEW_DB);
    ChangeBundle b2 = new ChangeBundle(c2, messages(), patchSets(), approvals(), comments(), reviewers(), REVIEW_DB);
    assertDiffs(b1, b2, "subject differs for Change.Id " + c1.getId() + ":" + " {Subject\r\rbody} != {Subject  body}");
    // Both NoteDb, exact match required (although it should be impossible to
    // create a NoteDb change with '\r' in the subject).
    b1 = new ChangeBundle(c1, messages(), patchSets(), approvals(), comments(), reviewers(), NOTE_DB);
    b2 = new ChangeBundle(c2, messages(), patchSets(), approvals(), comments(), reviewers(), NOTE_DB);
    assertDiffs(b1, b2, "subject differs for Change.Id " + c1.getId() + ":" + " {Subject\r\rbody} != {Subject  body}");
    // One ReviewDb, one NoteDb, '\r' is normalized to ' '.
    b1 = new ChangeBundle(c1, messages(), patchSets(), approvals(), comments(), reviewers(), REVIEW_DB);
    b2 = new ChangeBundle(c2, messages(), patchSets(), approvals(), comments(), reviewers(), NOTE_DB);
    assertNoDiffs(b1, b2);
    assertNoDiffs(b2, b1);
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Account(com.google.gerrit.reviewdb.client.Account) Change(com.google.gerrit.reviewdb.client.Change) Test(org.junit.Test)

Example 40 with Change

use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.

the class ChangeBundleTest method diffChangesDoesntTrimLeadingNonSpaceWhitespaceFromSubject.

@Test
public void diffChangesDoesntTrimLeadingNonSpaceWhitespaceFromSubject() throws Exception {
    Change c1 = TestChanges.newChange(new Project.NameKey("project"), new Account.Id(100));
    Change c2 = clone(c1);
    c2.setCurrentPatchSet(c1.currentPatchSetId(), "\t" + c1.getSubject(), c1.getOriginalSubject());
    // Both ReviewDb.
    ChangeBundle b1 = new ChangeBundle(c1, messages(), patchSets(), approvals(), comments(), reviewers(), REVIEW_DB);
    ChangeBundle b2 = new ChangeBundle(c2, messages(), patchSets(), approvals(), comments(), reviewers(), REVIEW_DB);
    assertDiffs(b1, b2, "subject differs for Change.Id " + c1.getId() + ":" + " {Change subject} != {\tChange subject}");
    // One NoteDb.
    b1 = new ChangeBundle(c1, messages(), latest(c1), approvals(), comments(), reviewers(), NOTE_DB);
    b2 = new ChangeBundle(c2, messages(), latest(c2), approvals(), comments(), reviewers(), REVIEW_DB);
    assertDiffs(b1, b2, "subject differs for Change.Id " + c1.getId() + ":" + " {Change subject} != {\tChange subject}");
    assertDiffs(b2, b1, "subject differs for Change.Id " + c1.getId() + ":" + " {\tChange subject} != {Change subject}");
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Account(com.google.gerrit.reviewdb.client.Account) Change(com.google.gerrit.reviewdb.client.Change) Test(org.junit.Test)

Aggregations

Change (com.google.gerrit.reviewdb.client.Change)191 Test (org.junit.Test)96 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)53 ObjectId (org.eclipse.jgit.lib.ObjectId)32 Timestamp (java.sql.Timestamp)31 Account (com.google.gerrit.reviewdb.client.Account)30 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)28 OrmException (com.google.gwtorm.server.OrmException)28 Project (com.google.gerrit.reviewdb.client.Project)27 Repository (org.eclipse.jgit.lib.Repository)27 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)23 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)22 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)21 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)19 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)17 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)17 ChangeData (com.google.gerrit.server.query.change.ChangeData)16 RevId (com.google.gerrit.reviewdb.client.RevId)15