Merge "Introduce HexFormat to hold hex formatting previously in IdGenerator.format"

This commit is contained in:
Han-Wen Nienhuys 2018-07-17 09:16:48 +00:00 committed by Gerrit Code Review
commit 82b1cf3b1c
10 changed files with 67 additions and 27 deletions

View File

@ -0,0 +1,27 @@
// Copyright (C) 2018 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.ioutil;
public class HexFormat {
public static String fromInt(int id) {
final char[] r = new char[8];
for (int p = 7; 0 <= p; p--) {
final int h = id & 0xf;
r[p] = h < 10 ? (char) ('0' + h) : (char) ('a' + (h - 10));
id >>= 4;
}
return new String(r);
}
}

View File

@ -23,13 +23,13 @@ import com.google.gerrit.server.config.ConfigResource;
import com.google.gerrit.server.git.WorkQueue;
import com.google.gerrit.server.git.WorkQueue.ProjectTask;
import com.google.gerrit.server.git.WorkQueue.Task;
import com.google.gerrit.server.ioutil.HexFormat;
import com.google.gerrit.server.permissions.GlobalPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.permissions.ProjectPermission;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState;
import com.google.gerrit.server.util.IdGenerator;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@ -133,7 +133,7 @@ public class ListTasks implements RestReadView<ConfigResource> {
public String queueName;
public TaskInfo(Task<?> task) {
this.id = IdGenerator.format(task.getTaskId());
this.id = HexFormat.fromInt(task.getTaskId());
this.state = task.getState();
this.startTime = new Timestamp(task.getStartTime().getTime());
this.delay = task.getDelay(TimeUnit.MILLISECONDS);

View File

@ -29,9 +29,9 @@ import com.google.gerrit.server.git.GarbageCollection;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.LocalDiskRepositoryManager;
import com.google.gerrit.server.git.WorkQueue;
import com.google.gerrit.server.ioutil.HexFormat;
import com.google.gerrit.server.project.ProjectResource;
import com.google.gerrit.server.restapi.project.GarbageCollect.Input;
import com.google.gerrit.server.util.IdGenerator;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@ -98,7 +98,7 @@ public class GarbageCollect
WorkQueue.Task<Void> task = (WorkQueue.Task<Void>) workQueue.getDefaultQueue().submit(job);
String location =
canonicalUrl.get() + "a/config/server/tasks/" + IdGenerator.format(task.getTaskId());
canonicalUrl.get() + "a/config/server/tasks/" + HexFormat.fromInt(task.getTaskId());
return Response.accepted(location);
}

View File

@ -22,16 +22,6 @@ import java.util.concurrent.atomic.AtomicInteger;
/** Simple class to produce 4 billion keys randomly distributed. */
@Singleton
public class IdGenerator {
/** Format an id created by this class as a hex string. */
public static String format(int id) {
final char[] r = new char[8];
for (int p = 7; 0 <= p; p--) {
final int h = id & 0xf;
r[p] = h < 10 ? (char) ('0' + h) : (char) ('a' + (h - 10));
id >>= 4;
}
return new String(r);
}
private final AtomicInteger gen;

View File

@ -27,7 +27,7 @@ import com.google.gerrit.server.config.ConfigKey;
import com.google.gerrit.server.config.ConfigUpdatedEvent;
import com.google.gerrit.server.config.GerritConfigListener;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.util.IdGenerator;
import com.google.gerrit.server.ioutil.HexFormat;
import com.google.gerrit.server.util.SystemLog;
import com.google.gerrit.sshd.SshScope.Context;
import com.google.inject.Inject;
@ -277,7 +277,7 @@ class SshLog implements LifecycleListener, GerritConfigListener {
}
private static String id(int id) {
return IdGenerator.format(id);
return HexFormat.fromInt(id);
}
void audit(Context ctx, Object result, String cmd) {
@ -298,7 +298,7 @@ class SshLog implements LifecycleListener, GerritConfigListener {
created = TimeUtil.nowMs();
} else {
SshSession session = ctx.getSession();
sessionId = IdGenerator.format(session.getSessionId());
sessionId = HexFormat.fromInt(session.getSessionId());
currentUser = session.getUser();
created = ctx.created;
}

View File

@ -22,7 +22,7 @@ import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.util.IdGenerator;
import com.google.gerrit.server.ioutil.HexFormat;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.gerrit.sshd.SshDaemon;
@ -168,7 +168,7 @@ final class ShowConnections extends SshCommand {
}
private static String id(SshSession sd) {
return sd != null ? IdGenerator.format(sd.getSessionId()) : "";
return sd != null ? HexFormat.fromInt(sd.getSessionId()) : "";
}
private static String time(long now, long time) {

View File

@ -0,0 +1,29 @@
// Copyright (C) 2018 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.ioutil;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HexFormatTest {
@Test
public void fromInt() {
assertEquals("0000000f", HexFormat.fromInt(0xf));
assertEquals("801234ab", HexFormat.fromInt(0x801234ab));
assertEquals("deadbeef", HexFormat.fromInt(0xdeadbeef));
}
}

View File

@ -34,11 +34,4 @@ public class IdGeneratorTest {
assertEquals(0xdeadbeef, IdGenerator.unmix(IdGenerator.mix(0xdeadbeef)));
assertEquals(0x0b966b11, IdGenerator.unmix(IdGenerator.mix(0x0b966b11)));
}
@Test
public void format() {
assertEquals("0000000f", IdGenerator.format(0xf));
assertEquals("801234ab", IdGenerator.format(0x801234ab));
assertEquals("deadbeef", IdGenerator.format(0xdeadbeef));
}
}

View File

@ -19,6 +19,7 @@ genrule2(
PLUGIN_API = [
"//java/com/google/gerrit/server",
"//java/com/google/gerrit/server/ioutil",
"//java/com/google/gerrit/server/restapi",
"//java/com/google/gerrit/pgm/init/api",
"//java/com/google/gerrit/httpd",

@ -1 +1 @@
Subproject commit 3a47f8c11ebdbcbc65fc6a58c35d18f1f3c3a74b
Subproject commit 1086faccd0cf2aa53977854767fdc77f048b0253