Refactor the ChangeMessage UUID generation to a common utility
We also now only hit the database for a new change message sequence number once each application startup, or once every 2^31 messages. Since the application startup is going to be the more frequent case we should only hit the database once. Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import com.google.gerrit.client.reviewdb.ChangeMessage;
|
||||
import com.google.gerrit.client.reviewdb.PatchSet;
|
||||
import com.google.gerrit.client.reviewdb.ReviewDb;
|
||||
import com.google.gerrit.client.rpc.Common;
|
||||
import com.google.gerrit.server.ChangeUtil;
|
||||
import com.google.gerrit.server.GerritServer;
|
||||
import com.google.gwtorm.client.OrmException;
|
||||
import com.google.gwtorm.client.Transaction;
|
||||
@@ -39,8 +40,6 @@ import org.spearce.jgit.merge.Merger;
|
||||
import org.spearce.jgit.revwalk.RevCommit;
|
||||
import org.spearce.jgit.revwalk.RevSort;
|
||||
import org.spearce.jgit.revwalk.RevWalk;
|
||||
import org.spearce.jgit.util.Base64;
|
||||
import org.spearce.jgit.util.NB;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -442,9 +441,12 @@ public class MergeOp {
|
||||
}
|
||||
|
||||
private ChangeMessage message(final Change c, final String body) {
|
||||
final byte[] raw = new byte[4];
|
||||
NB.encodeInt32(raw, 0, schema.nextChangeMessageId());
|
||||
final String uuid = Base64.encodeBytes(raw);
|
||||
final String uuid;
|
||||
try {
|
||||
uuid = ChangeUtil.messageUUID(schema);
|
||||
} catch (OrmException e) {
|
||||
return null;
|
||||
}
|
||||
final ChangeMessage m =
|
||||
new ChangeMessage(new ChangeMessage.Key(c.getId(), uuid), null);
|
||||
m.setMessage(body);
|
||||
@@ -486,7 +488,9 @@ public class MergeOp {
|
||||
try {
|
||||
final Transaction txn = schema.beginTransaction();
|
||||
schema.changes().update(Collections.singleton(c), txn);
|
||||
schema.changeMessages().insert(Collections.singleton(msg), txn);
|
||||
if (msg != null) {
|
||||
schema.changeMessages().insert(Collections.singleton(msg), txn);
|
||||
}
|
||||
txn.commit();
|
||||
break;
|
||||
} catch (OrmException e) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2009 Google Inc.
|
||||
//
|
||||
// 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.client.reviewdb.ReviewDb;
|
||||
import com.google.gwtorm.client.OrmException;
|
||||
|
||||
import org.spearce.jgit.util.Base64;
|
||||
import org.spearce.jgit.util.NB;
|
||||
|
||||
public class ChangeUtil {
|
||||
private static int uuidPrefix;
|
||||
private static int uuidSeq;
|
||||
|
||||
/**
|
||||
* Generate a new unique identifier for change message entities.
|
||||
*
|
||||
* @param db the database connection, used to increment the change message
|
||||
* allocation sequence.
|
||||
* @return the new unique identifier.
|
||||
* @throws OrmException the database couldn't be incremented.
|
||||
*/
|
||||
public static String messageUUID(final ReviewDb db) throws OrmException {
|
||||
final byte[] raw = new byte[8];
|
||||
fill(raw, db);
|
||||
return Base64.encodeBytes(raw);
|
||||
}
|
||||
|
||||
private static synchronized void fill(byte[] raw, ReviewDb db)
|
||||
throws OrmException {
|
||||
if (uuidSeq == 0) {
|
||||
uuidPrefix = db.nextChangeMessageId();
|
||||
uuidSeq = Integer.MAX_VALUE;
|
||||
}
|
||||
NB.encodeInt32(raw, 0, uuidPrefix);
|
||||
NB.encodeInt32(raw, 4, uuidSeq--);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user