Make ObjectIdConverter a top level class

We intend to reuse the methods of ProtoCacheSerializers for other
protobuf conversions unrelated to CacheSerializers. Hence, we intend
to move ProtoCacheSerializers to a more public package.
As ObjectIdConverter is only necessary for CacheSerializers, keep it at
the current package.

Change-Id: I8763f00667b03a4e7bbe32d56a50f7ceaa0bef97
This commit is contained in:
Alice Kober-Sotzek
2018-12-07 16:59:41 +01:00
parent 290f499daf
commit fb51e6aa9f
12 changed files with 136 additions and 97 deletions

View File

@@ -0,0 +1,56 @@
// 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.cache.serialize;
import static com.google.common.base.Preconditions.checkArgument;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
import com.google.protobuf.ByteString;
import org.eclipse.jgit.lib.ObjectId;
/**
* Helper for serializing {@link ObjectId} instances to/from protobuf fields.
*
* <p>Reuse a single instance's {@link #toByteString(ObjectId)} and {@link
* #fromByteString(ByteString)} within a single {@link CacheSerializer#serialize} or {@link
* CacheSerializer#deserialize} method body to minimize allocation of temporary buffers.
*
* <p><strong>Note:</strong> This class is not threadsafe. Instances must not be stored in {@link
* CacheSerializer} fields if the serializer instances will be used from multiple threads.
*/
public class ObjectIdConverter {
public static ObjectIdConverter create() {
return new ObjectIdConverter();
}
private final byte[] buf = new byte[OBJECT_ID_LENGTH];
private ObjectIdConverter() {}
public ByteString toByteString(ObjectId id) {
id.copyRawTo(buf, 0);
return ByteString.copyFrom(buf);
}
public ObjectId fromByteString(ByteString in) {
checkArgument(
in.size() == OBJECT_ID_LENGTH,
"expected ByteString of length %s: %s",
OBJECT_ID_LENGTH,
in);
in.copyTo(buf, 0);
return ObjectId.fromRaw(buf);
}
}

View File

@@ -14,16 +14,12 @@
package com.google.gerrit.server.cache.serialize;
import static com.google.common.base.Preconditions.checkArgument;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
import com.google.gwtorm.protobuf.ProtobufCodec;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import java.io.IOException;
import org.eclipse.jgit.lib.ObjectId;
/** Static utilities for writing protobuf-based {@link CacheSerializer} implementations. */
public class ProtoCacheSerializers {
@@ -88,40 +84,5 @@ public class ProtoCacheSerializers {
}
}
/**
* Helper for serializing {@link ObjectId} instances to/from protobuf fields.
*
* <p>Reuse a single instance's {@link #toByteString(ObjectId)} and {@link
* #fromByteString(ByteString)} within a single {@link CacheSerializer#serialize} or {@link
* CacheSerializer#deserialize} method body to minimize allocation of temporary buffers.
*
* <p><strong>Note:</strong> This class is not threadsafe. Instances must not be stored in {@link
* CacheSerializer} fields if the serializer instances will be used from multiple threads.
*/
public static class ObjectIdConverter {
public static ObjectIdConverter create() {
return new ObjectIdConverter();
}
private final byte[] buf = new byte[OBJECT_ID_LENGTH];
private ObjectIdConverter() {}
public ByteString toByteString(ObjectId id) {
id.copyRawTo(buf, 0);
return ByteString.copyFrom(buf);
}
public ObjectId fromByteString(ByteString in) {
checkArgument(
in.size() == OBJECT_ID_LENGTH,
"expected ByteString of length %s: %s",
OBJECT_ID_LENGTH,
in);
in.copyTo(buf, 0);
return ObjectId.fromRaw(buf);
}
}
private ProtoCacheSerializers() {}
}