From 052918a23a3af78e8ccea41135d410d2815c27e5 Mon Sep 17 00:00:00 2001 From: Martin Fick Date: Tue, 31 Jan 2012 19:20:21 -0700 Subject: [PATCH] Factor out approvals change status syncing The changeOpen status is cached in the PatchSetApprovals table for performance reasons, so the approvals must be updated whenever the change status changes. This update is done in a few places and is likely not ever obvious why the update is being done. Make a function to do the update which makes the code self documenting all while sharing the implementation. Create an new ApprovalsUtil class for this function which can be a good place for more approval manipulating functions in the future. Change-Id: If92f3757963048a141003304f2c96036c915ab89 --- .../google/gerrit/server/ApprovalsUtil.java | 36 +++++++++++++++++++ .../com/google/gerrit/server/ChangeUtil.java | 7 +--- .../gerrit/server/git/ReceiveCommits.java | 8 ++--- 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 gerrit-server/src/main/java/com/google/gerrit/server/ApprovalsUtil.java diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/ApprovalsUtil.java b/gerrit-server/src/main/java/com/google/gerrit/server/ApprovalsUtil.java new file mode 100644 index 0000000000..6997d47526 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/ApprovalsUtil.java @@ -0,0 +1,36 @@ +// Copyright (C) 2009 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; + +import com.google.gerrit.reviewdb.Change; +import com.google.gerrit.reviewdb.PatchSetApproval; +import com.google.gerrit.reviewdb.ReviewDb; +import com.google.gwtorm.client.OrmException; + +import java.util.List; + +public class ApprovalsUtil { + /* Resync the changeOpen status which is cached in the approvals table for + performance reasons*/ + public static void syncChangeStatus(final ReviewDb db, final Change change) + throws OrmException { + final List approvals = + db.patchSetApprovals().byChange(change.getId()).toList(); + for (PatchSetApproval a : approvals) { + a.cache(change); + } + db.patchSetApprovals().update(approvals); + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java b/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java index 3ff336e502..b4ce6f5c43 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java @@ -403,12 +403,7 @@ public class ChangeUtil { } db.changeMessages().insert(Collections.singleton(cmsg)); - final List approvals = - db.patchSetApprovals().byChange(change.getId()).toList(); - for (PatchSetApproval a : approvals) { - a.cache(change); - } - db.patchSetApprovals().update(approvals); + ApprovalsUtil.syncChangeStatus(db, change); // Email the reviewers final ReplyToChangeSender cm = senderFactory.create(change); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java index 3c9e4da2a1..8450dc5aa9 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java @@ -33,6 +33,7 @@ import com.google.gerrit.reviewdb.PatchSetInfo; import com.google.gerrit.reviewdb.Project; import com.google.gerrit.reviewdb.RevId; import com.google.gerrit.reviewdb.ReviewDb; +import com.google.gerrit.server.ApprovalsUtil; import com.google.gerrit.server.ChangeUtil; import com.google.gerrit.server.GerritPersonIdent; import com.google.gerrit.server.IdentifiedUser; @@ -1830,12 +1831,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook { change.setStatus(Change.Status.MERGED); ChangeUtil.updated(change); - final List approvals = - db.patchSetApprovals().byChange(change.getId()).toList(); - for (PatchSetApproval a : approvals) { - a.cache(change); - } - db.patchSetApprovals().update(approvals); + ApprovalsUtil.syncChangeStatus(db, change); final StringBuilder msgBuf = new StringBuilder(); msgBuf.append("Change has been successfully pushed");