Use transactions to handle comments when possible

If the database supports transactions (see gwtorm, not all do)
try to perform some update operations as a single transaction on
the change. This may allow the database to batch together any
record updates, saving some time.

Change-Id: I5af9efe3a541d83515109e3bf9a3497b3d8127de
This commit is contained in:
Shawn O. Pearce
2011-10-28 15:57:51 -07:00
parent d3743450a2
commit 77c684b417
3 changed files with 88 additions and 61 deletions

View File

@@ -110,6 +110,9 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
final AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>() {
public VoidResult run(ReviewDb db) throws OrmException, Failure {
Change.Id id = commentKey.getParentKey().getParentKey().getParentKey();
db.changes().beginTransaction(id);
try {
final PatchLineComment comment = db.patchComments().get(commentKey);
if (comment == null) {
throw new Failure(new NoSuchEntityException());
@@ -121,7 +124,11 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
throw new Failure(new IllegalStateException("Comment published"));
}
db.patchComments().delete(Collections.singleton(comment));
db.commit();
return VoidResult.INSTANCE;
} finally {
db.rollback();
}
}
});
}
@@ -142,6 +149,8 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
Account.Id account = getAccountId();
AccountPatchReview.Key key =
new AccountPatchReview.Key(patchKey, account);
db.accounts().beginTransaction(account);
try {
AccountPatchReview apr = db.accountPatchReviews().get(key);
if (apr == null && reviewed) {
db.accountPatchReviews().insert(
@@ -149,7 +158,11 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
} else if (apr != null && !reviewed) {
db.accountPatchReviews().delete(Collections.singleton(apr));
}
db.commit();
return VoidResult.INSTANCE;
} finally {
db.rollback();
}
}
});
}

View File

@@ -49,7 +49,6 @@ class SaveDraft extends Handler<PatchLineComment> {
this.changeControlFactory = changeControlFactory;
this.db = db;
this.currentUser = currentUser;
this.comment = comment;
}
@@ -62,6 +61,9 @@ class SaveDraft extends Handler<PatchLineComment> {
final Patch.Key patchKey = comment.getKey().getParentKey();
final PatchSet.Id patchSetId = patchKey.getParentKey();
final Change.Id changeId = patchKey.getParentKey().getParentKey();
db.changes().beginTransaction(changeId);
try {
changeControlFactory.validateFor(changeId);
if (db.patchSets().get(patchSetId) == null) {
throw new NoSuchChangeException(changeId);
@@ -89,6 +91,7 @@ class SaveDraft extends Handler<PatchLineComment> {
nc.setSide(comment.getSide());
nc.setMessage(comment.getMessage());
db.patchComments().insert(Collections.singleton(nc));
db.commit();
return nc;
} else {
@@ -97,7 +100,11 @@ class SaveDraft extends Handler<PatchLineComment> {
}
comment.updated();
db.patchComments().update(Collections.singleton(comment));
db.commit();
return comment;
}
} finally {
db.rollback();
}
}
}

View File

@@ -116,18 +116,25 @@ public class PublishComments implements Callable<VoidResult> {
}
drafts = drafts();
db.changes().beginTransaction(changeId);
try {
publishDrafts();
final boolean isCurrent = patchSetId.equals(change.currentPatchSetId());
if (isCurrent && change.getStatus().isOpen()) {
publishApprovals(ctl);
} else if (! approvals.isEmpty()) {
} else if (!approvals.isEmpty()) {
throw new InvalidChangeOperationException("Change is closed");
} else {
publishMessageOnly();
}
touchChange();
db.commit();
} finally {
db.rollback();
}
email();
fireHook();
return VoidResult.INSTANCE;