From ae599f57acba2262e2e1f5bf343e960fc0bc9b16 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 15 Mar 2018 11:13:05 +0900 Subject: [PATCH] Use ObjectIdSerializer from JGit [redux] Since [1, 2] JGit provides a public ObjectIdSerializer class. Remove the ObjectIdSerialization class and use the new API. [1] https://git.eclipse.org/r/#/c/117831/ [2] https://git.eclipse.org/r/#/c/119456/ Change-Id: Ie2ea535d5b6e190159dffeb98b63f1000c97658a --- .../server/change/ChangeKindCacheImpl.java | 12 ++--- .../server/change/MergeabilityCacheImpl.java | 12 ++--- java/com/google/gerrit/server/git/TagSet.java | 12 ++--- .../gerrit/server/patch/DiffSummaryKey.java | 16 +++--- .../google/gerrit/server/patch/PatchList.java | 16 +++--- .../gerrit/server/patch/PatchListKey.java | 16 +++--- java/org/eclipse/jgit/BUILD | 1 - .../jgit/lib/ObjectIdSerialization.java | 54 ------------------- 8 files changed, 42 insertions(+), 97 deletions(-) delete mode 100644 java/org/eclipse/jgit/lib/ObjectIdSerialization.java diff --git a/java/com/google/gerrit/server/change/ChangeKindCacheImpl.java b/java/com/google/gerrit/server/change/ChangeKindCacheImpl.java index 7a6c209da1..fc9ae4bf6f 100644 --- a/java/com/google/gerrit/server/change/ChangeKindCacheImpl.java +++ b/java/com/google/gerrit/server/change/ChangeKindCacheImpl.java @@ -16,8 +16,8 @@ package com.google.gerrit.server.change; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeNotNull; +import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; +import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; import com.google.common.annotations.VisibleForTesting; import com.google.common.cache.Cache; @@ -172,14 +172,14 @@ public class ChangeKindCacheImpl implements ChangeKindCache { } private void writeObject(ObjectOutputStream out) throws IOException { - writeNotNull(out, prior); - writeNotNull(out, next); + writeWithoutMarker(out, prior); + writeWithoutMarker(out, next); out.writeUTF(strategyName); } private void readObject(ObjectInputStream in) throws IOException { - prior = readNotNull(in); - next = readNotNull(in); + prior = readWithoutMarker(in); + next = readWithoutMarker(in); strategyName = in.readUTF(); } } diff --git a/java/com/google/gerrit/server/change/MergeabilityCacheImpl.java b/java/com/google/gerrit/server/change/MergeabilityCacheImpl.java index 7ba18e858b..4c80eabf28 100644 --- a/java/com/google/gerrit/server/change/MergeabilityCacheImpl.java +++ b/java/com/google/gerrit/server/change/MergeabilityCacheImpl.java @@ -19,8 +19,8 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.gerrit.server.ioutil.BasicSerialization.readString; import static com.google.gerrit.server.ioutil.BasicSerialization.writeString; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeNotNull; +import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; +import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; import com.google.common.base.MoreObjects; import com.google.common.cache.Cache; @@ -155,8 +155,8 @@ public class MergeabilityCacheImpl implements MergeabilityCache { } private void writeObject(ObjectOutputStream out) throws IOException { - writeNotNull(out, commit); - writeNotNull(out, into); + writeWithoutMarker(out, commit); + writeWithoutMarker(out, into); Character c = SUBMIT_TYPES.get(submitType); if (c == null) { throw new IOException("Invalid submit type: " + submitType); @@ -166,8 +166,8 @@ public class MergeabilityCacheImpl implements MergeabilityCache { } private void readObject(ObjectInputStream in) throws IOException { - commit = readNotNull(in); - into = readNotNull(in); + commit = readWithoutMarker(in); + into = readWithoutMarker(in); char t = in.readChar(); submitType = SUBMIT_TYPES.inverse().get(t); if (submitType == null) { diff --git a/java/com/google/gerrit/server/git/TagSet.java b/java/com/google/gerrit/server/git/TagSet.java index 1f54cd008c..118223b6c1 100644 --- a/java/com/google/gerrit/server/git/TagSet.java +++ b/java/com/google/gerrit/server/git/TagSet.java @@ -14,8 +14,8 @@ package com.google.gerrit.server.git; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeNotNull; +import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; +import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.Project; @@ -194,13 +194,13 @@ class TagSet { for (int i = 0; i < refCnt; i++) { String name = in.readUTF(); int flag = in.readInt(); - ObjectId id = readNotNull(in); + ObjectId id = readWithoutMarker(in); refs.put(name, new CachedRef(flag, id)); } int tagCnt = in.readInt(); for (int i = 0; i < tagCnt; i++) { - ObjectId id = readNotNull(in); + ObjectId id = readWithoutMarker(in); BitSet flags = (BitSet) in.readObject(); tags.add(new Tag(id, flags)); } @@ -211,12 +211,12 @@ class TagSet { for (Map.Entry e : refs.entrySet()) { out.writeUTF(e.getKey()); out.writeInt(e.getValue().flag); - writeNotNull(out, e.getValue().get()); + writeWithoutMarker(out, e.getValue().get()); } out.writeInt(tags.size()); for (Tag tag : tags) { - writeNotNull(out, tag); + writeWithoutMarker(out, tag); out.writeObject(tag.refFlags); } } diff --git a/java/com/google/gerrit/server/patch/DiffSummaryKey.java b/java/com/google/gerrit/server/patch/DiffSummaryKey.java index 0a02e36a78..0f4757a8af 100644 --- a/java/com/google/gerrit/server/patch/DiffSummaryKey.java +++ b/java/com/google/gerrit/server/patch/DiffSummaryKey.java @@ -14,10 +14,10 @@ package com.google.gerrit.server.patch; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readCanBeNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeCanBeNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeNotNull; +import static org.eclipse.jgit.lib.ObjectIdSerializer.read; +import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; +import static org.eclipse.jgit.lib.ObjectIdSerializer.write; +import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; import com.google.common.base.Preconditions; import com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace; @@ -93,9 +93,9 @@ public class DiffSummaryKey implements Serializable { } private void writeObject(ObjectOutputStream out) throws IOException { - writeCanBeNull(out, oldId); + write(out, oldId); out.writeInt(parentNum == null ? 0 : parentNum); - writeNotNull(out, newId); + writeWithoutMarker(out, newId); Character c = PatchListKey.WHITESPACE_TYPES.get(whitespace); if (c == null) { throw new IOException("Invalid whitespace type: " + whitespace); @@ -104,10 +104,10 @@ public class DiffSummaryKey implements Serializable { } private void readObject(ObjectInputStream in) throws IOException { - oldId = readCanBeNull(in); + oldId = read(in); int n = in.readInt(); parentNum = n == 0 ? null : Integer.valueOf(n); - newId = readNotNull(in); + newId = readWithoutMarker(in); char t = in.readChar(); whitespace = PatchListKey.WHITESPACE_TYPES.inverse().get(t); if (whitespace == null) { diff --git a/java/com/google/gerrit/server/patch/PatchList.java b/java/com/google/gerrit/server/patch/PatchList.java index 16ede58c1d..cf5df4af52 100644 --- a/java/com/google/gerrit/server/patch/PatchList.java +++ b/java/com/google/gerrit/server/patch/PatchList.java @@ -18,10 +18,10 @@ import static com.google.gerrit.server.ioutil.BasicSerialization.readBytes; import static com.google.gerrit.server.ioutil.BasicSerialization.readVarInt32; import static com.google.gerrit.server.ioutil.BasicSerialization.writeBytes; import static com.google.gerrit.server.ioutil.BasicSerialization.writeVarInt32; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readCanBeNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeCanBeNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeNotNull; +import static org.eclipse.jgit.lib.ObjectIdSerializer.read; +import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; +import static org.eclipse.jgit.lib.ObjectIdSerializer.write; +import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; import com.google.common.annotations.VisibleForTesting; import com.google.gerrit.common.Nullable; @@ -173,8 +173,8 @@ public class PatchList implements Serializable { private void writeObject(ObjectOutputStream output) throws IOException { final ByteArrayOutputStream buf = new ByteArrayOutputStream(); try (DeflaterOutputStream out = new DeflaterOutputStream(buf)) { - writeCanBeNull(out, oldId); - writeNotNull(out, newId); + write(out, oldId); + writeWithoutMarker(out, newId); writeVarInt32(out, isMerge ? 1 : 0); comparisonType.writeTo(out); writeVarInt32(out, insertions); @@ -190,8 +190,8 @@ public class PatchList implements Serializable { private void readObject(ObjectInputStream input) throws IOException { final ByteArrayInputStream buf = new ByteArrayInputStream(readBytes(input)); try (InflaterInputStream in = new InflaterInputStream(buf)) { - oldId = readCanBeNull(in); - newId = readNotNull(in); + oldId = read(in); + newId = readWithoutMarker(in); isMerge = readVarInt32(in) != 0; comparisonType = ComparisonType.readFrom(in); insertions = readVarInt32(in); diff --git a/java/com/google/gerrit/server/patch/PatchListKey.java b/java/com/google/gerrit/server/patch/PatchListKey.java index 73e82a188a..e5be70ca6d 100644 --- a/java/com/google/gerrit/server/patch/PatchListKey.java +++ b/java/com/google/gerrit/server/patch/PatchListKey.java @@ -15,10 +15,10 @@ package com.google.gerrit.server.patch; import static com.google.common.base.Preconditions.checkState; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readCanBeNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.readNotNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeCanBeNull; -import static org.eclipse.jgit.lib.ObjectIdSerialization.writeNotNull; +import static org.eclipse.jgit.lib.ObjectIdSerializer.read; +import static org.eclipse.jgit.lib.ObjectIdSerializer.readWithoutMarker; +import static org.eclipse.jgit.lib.ObjectIdSerializer.write; +import static org.eclipse.jgit.lib.ObjectIdSerializer.writeWithoutMarker; import com.google.common.collect.ImmutableBiMap; import com.google.gerrit.common.Nullable; @@ -186,9 +186,9 @@ public class PatchListKey implements Serializable { } private void writeObject(ObjectOutputStream out) throws IOException { - writeCanBeNull(out, oldId); + write(out, oldId); out.writeInt(parentNum == null ? 0 : parentNum); - writeNotNull(out, newId); + writeWithoutMarker(out, newId); Character c = WHITESPACE_TYPES.get(whitespace); if (c == null) { throw new IOException("Invalid whitespace type: " + whitespace); @@ -198,10 +198,10 @@ public class PatchListKey implements Serializable { } private void readObject(ObjectInputStream in) throws IOException { - oldId = readCanBeNull(in); + oldId = read(in); int n = in.readInt(); parentNum = n == 0 ? null : Integer.valueOf(n); - newId = readNotNull(in); + newId = readWithoutMarker(in); char t = in.readChar(); whitespace = WHITESPACE_TYPES.inverse().get(t); if (whitespace == null) { diff --git a/java/org/eclipse/jgit/BUILD b/java/org/eclipse/jgit/BUILD index b9b807a94a..95fef280c9 100644 --- a/java/org/eclipse/jgit/BUILD +++ b/java/org/eclipse/jgit/BUILD @@ -39,7 +39,6 @@ java_library( srcs = [ "diff/EditDeserializer.java", "diff/ReplaceEdit.java", - "lib/ObjectIdSerialization.java", ], visibility = ["//visibility:public"], deps = [ diff --git a/java/org/eclipse/jgit/lib/ObjectIdSerialization.java b/java/org/eclipse/jgit/lib/ObjectIdSerialization.java deleted file mode 100644 index c98da6485f..0000000000 --- a/java/org/eclipse/jgit/lib/ObjectIdSerialization.java +++ /dev/null @@ -1,54 +0,0 @@ -// 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 org.eclipse.jgit.lib; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import org.eclipse.jgit.util.IO; - -public class ObjectIdSerialization { - public static void writeCanBeNull(OutputStream out, AnyObjectId id) throws IOException { - if (id != null) { - out.write((byte) 1); - writeNotNull(out, id); - } else { - out.write((byte) 0); - } - } - - public static void writeNotNull(OutputStream out, AnyObjectId id) throws IOException { - id.copyRawTo(out); - } - - public static ObjectId readCanBeNull(InputStream in) throws IOException { - switch (in.read()) { - case 0: - return null; - case 1: - return readNotNull(in); - default: - throw new IOException("Invalid flag before ObjectId"); - } - } - - public static ObjectId readNotNull(InputStream in) throws IOException { - final byte[] b = new byte[20]; - IO.readFully(in, b, 0, 20); - return ObjectId.fromRaw(b); - } - - private ObjectIdSerialization() {} -}