Skip to content

Commit 85f7d61

Browse files
authored
HDDS-14655. Add a generic type to ScmCodec (#9789)
1 parent c9b052a commit 85f7d61

16 files changed

Lines changed: 48 additions & 63 deletions

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmBigIntegerCodec.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
/**
2525
* Codec for type BigInteger.
2626
*/
27-
public class ScmBigIntegerCodec implements ScmCodec {
27+
public class ScmBigIntegerCodec implements ScmCodec<BigInteger> {
2828

2929
@Override
30-
public ByteString serialize(Object object) {
30+
public ByteString serialize(BigInteger object) {
3131
// BigInteger returns a new byte[].
32-
return UnsafeByteOperations.unsafeWrap(((BigInteger) object).toByteArray());
32+
return UnsafeByteOperations.unsafeWrap(object.toByteArray());
3333
}
3434

3535
@Override
36-
public Object deserialize(Class< ? > type, ByteString value) {
36+
public BigInteger deserialize(Class< ? > type, ByteString value) {
3737
return new BigInteger(value.toByteArray());
3838
}
3939
}

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmBooleanCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
/**
2323
* {@link ScmCodec} for {@code Boolean} objects.
2424
*/
25-
public class ScmBooleanCodec implements ScmCodec {
25+
public class ScmBooleanCodec implements ScmCodec<Boolean> {
2626
static final ByteString TRUE = ByteString.copyFromUtf8(Boolean.TRUE.toString());
2727
static final ByteString FALSE = ByteString.copyFromUtf8(Boolean.FALSE.toString());
2828

2929
@Override
30-
public ByteString serialize(Object object) {
31-
return ((Boolean) object) ? TRUE : FALSE;
30+
public ByteString serialize(Boolean object) {
31+
return object ? TRUE : FALSE;
3232
}
3333

3434
@Override

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmByteStringCodec.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@
1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

2020
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
21-
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
2221

2322
/**
2423
* A dummy codec that serializes a ByteString object to ByteString.
2524
*/
26-
public class ScmByteStringCodec implements ScmCodec {
25+
public class ScmByteStringCodec implements ScmCodec<ByteString> {
2726

2827
@Override
29-
public ByteString serialize(Object object)
30-
throws InvalidProtocolBufferException {
31-
return (ByteString) object;
28+
public ByteString serialize(ByteString object) {
29+
return object;
3230
}
3331

3432
@Override
35-
public Object deserialize(Class<?> type, ByteString value)
36-
throws InvalidProtocolBufferException {
33+
public ByteString deserialize(Class<?> type, ByteString value) {
3734
return value;
3835
}
3936
}

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmCodec.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
2222

2323
/**
24-
* Codec interface to marshall/unmarshall data to/from {@link ByteString}.
24+
* To serialize/deserialize Java objects to/from protobuf ByteString for SCM HA.
25+
*
26+
* @param <T>
2527
*/
26-
public interface ScmCodec {
28+
public interface ScmCodec<T> {
2729

28-
ByteString serialize(Object object) throws InvalidProtocolBufferException;
30+
ByteString serialize(T object) throws InvalidProtocolBufferException;
2931

30-
Object deserialize(Class<?> type, ByteString value)
31-
throws InvalidProtocolBufferException;
32+
T deserialize(Class<?> type, ByteString value) throws InvalidProtocolBufferException;
3233

3334
}

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmEnumCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* {@link ScmCodec} for {@link ProtocolMessageEnum} objects.
3030
*/
31-
public class ScmEnumCodec implements ScmCodec {
31+
public class ScmEnumCodec implements ScmCodec<Object> {
3232

3333
@Override
3434
public ByteString serialize(Object object)

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmGeneratedMessageCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* {@link ScmCodec} for {@link Message} objects.
2828
*/
29-
public class ScmGeneratedMessageCodec implements ScmCodec {
29+
public class ScmGeneratedMessageCodec implements ScmCodec<Object> {
3030

3131
@Override
3232
public ByteString serialize(Object object) throws InvalidProtocolBufferException {

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmIntegerCodec.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@
1919

2020
import com.google.common.primitives.Ints;
2121
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
22-
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
2322
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2423

2524
/**
2625
* Encodes/decodes an integer to a byte string.
2726
*/
28-
public class ScmIntegerCodec implements ScmCodec {
27+
public class ScmIntegerCodec implements ScmCodec<Integer> {
2928
@Override
30-
public ByteString serialize(Object object)
31-
throws InvalidProtocolBufferException {
29+
public ByteString serialize(Integer object) {
3230
// toByteArray returns a new array
33-
return UnsafeByteOperations.unsafeWrap(Ints.toByteArray((Integer) object));
31+
return UnsafeByteOperations.unsafeWrap(Ints.toByteArray(object));
3432
}
3533

3634
@Override
37-
public Object deserialize(Class<?> type, ByteString value)
38-
throws InvalidProtocolBufferException {
35+
public Integer deserialize(Class<?> type, ByteString value) {
3936
return Ints.fromByteArray(value.toByteArray());
4037
}
4138
}

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmListCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* {@link ScmCodec} for {@link List} objects.
3030
*/
31-
public class ScmListCodec implements ScmCodec {
31+
public class ScmListCodec implements ScmCodec<Object> {
3232

3333
@Override
3434
public ByteString serialize(Object object)

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmLongCodec.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,21 @@
1919

2020
import com.google.common.primitives.Longs;
2121
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
22-
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
2322
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2423

2524
/**
2625
* {@link ScmCodec} for {@code Long} objects.
2726
*/
28-
public class ScmLongCodec implements ScmCodec {
27+
public class ScmLongCodec implements ScmCodec<Long> {
2928

3029
@Override
31-
public ByteString serialize(Object object)
32-
throws InvalidProtocolBufferException {
30+
public ByteString serialize(Long object) {
3331
// toByteArray returns a new array
34-
return UnsafeByteOperations.unsafeWrap(Longs.toByteArray((Long) object));
32+
return UnsafeByteOperations.unsafeWrap(Longs.toByteArray(object));
3533
}
3634

3735
@Override
38-
public Object deserialize(Class<?> type, ByteString value)
39-
throws InvalidProtocolBufferException {
36+
public Long deserialize(Class<?> type, ByteString value) {
4037
return Longs.fromByteArray(value.toByteArray());
4138
}
4239

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ScmManagedSecretKeyCodec.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
/**
2727
* A codec for {@link ManagedSecretKey} objects.
2828
*/
29-
public class ScmManagedSecretKeyCodec implements ScmCodec {
29+
public class ScmManagedSecretKeyCodec implements ScmCodec<ManagedSecretKey> {
3030
@Override
31-
public ByteString serialize(Object object)
31+
public ByteString serialize(ManagedSecretKey object)
3232
throws InvalidProtocolBufferException {
33-
ManagedSecretKey secretKey = (ManagedSecretKey) object;
33+
ManagedSecretKey secretKey = object;
3434
return UnsafeByteOperations.unsafeWrap(
3535
secretKey.toProtobuf().toByteString().asReadOnlyByteBuffer());
3636
}
3737

3838
@Override
39-
public Object deserialize(Class<?> type, ByteString value)
39+
public ManagedSecretKey deserialize(Class<?> type, ByteString value)
4040
throws InvalidProtocolBufferException {
4141
try {
4242
SCMSecretKeyProtocolProtos.ManagedSecretKey message =

0 commit comments

Comments
 (0)