Skip to content

Commit

Permalink
HDDS-11556. Add a getTypeClass method to Codec. (apache#7295)
Browse files Browse the repository at this point in the history
  • Loading branch information
szetszwo authored Oct 10, 2024
1 parent 256aad9 commit 5657604
Show file tree
Hide file tree
Showing 49 changed files with 177 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public class DatanodeDetails extends NodeImpl implements
private static final Codec<DatanodeDetails> CODEC = new DelegatedCodec<>(
Proto2Codec.get(ExtendedDatanodeDetailsProto.getDefaultInstance()),
DatanodeDetails::getFromProtoBuf,
DatanodeDetails::getExtendedProtoBufMessage);
DatanodeDetails::getExtendedProtoBufMessage,
DatanodeDetails.class);

public static Codec<DatanodeDetails> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public final class ContainerID implements Comparable<ContainerID> {
private static final Codec<ContainerID> CODEC = new DelegatedCodec<>(
LongCodec.get(), ContainerID::valueOf, c -> c.id,
DelegatedCodec.CopyType.SHALLOW);
ContainerID.class, DelegatedCodec.CopyType.SHALLOW);

public static final ContainerID MIN = ContainerID.valueOf(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public final class ContainerInfo implements Comparable<ContainerInfo> {
private static final Codec<ContainerInfo> CODEC = new DelegatedCodec<>(
Proto2Codec.get(HddsProtos.ContainerInfoProto.getDefaultInstance()),
ContainerInfo::fromProtobuf,
ContainerInfo::getProtobuf);
ContainerInfo::getProtobuf,
ContainerInfo.class);

public static Codec<ContainerInfo> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public final class Pipeline {
Proto2Codec.get(HddsProtos.Pipeline.getDefaultInstance()),
Pipeline::getFromProtobufSetCreationTimestamp,
p -> p.getProtobufMessage(ClientVersion.CURRENT_VERSION),
Pipeline.class,
DelegatedCodec.CopyType.UNSUPPORTED);

public static Codec<Pipeline> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public final class PipelineID {
private static final Codec<PipelineID> CODEC = new DelegatedCodec<>(
UuidCodec.get(), PipelineID::valueOf, c -> c.id,
DelegatedCodec.CopyType.SHALLOW);
PipelineID.class, DelegatedCodec.CopyType.SHALLOW);

public static Codec<PipelineID> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ private BooleanCodec() {
// singleton
}

@Override
public Class<Boolean> getTypeClass() {
return Boolean.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
public interface Codec<T> {
byte[] EMPTY_BYTE_ARRAY = {};

/** @return the class of the {@link T}. */
Class<T> getTypeClass();

/**
* Does this {@link Codec} support the {@link CodecBuffer} methods?
* If this method returns true, this class must implement both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum CopyType {
private final Codec<DELEGATE> delegate;
private final CheckedFunction<DELEGATE, T, IOException> forward;
private final CheckedFunction<T, DELEGATE, IOException> backward;
private final Class<T> clazz;
private final CopyType copyType;

/**
Expand All @@ -60,18 +61,25 @@ public enum CopyType {
public DelegatedCodec(Codec<DELEGATE> delegate,
CheckedFunction<DELEGATE, T, IOException> forward,
CheckedFunction<T, DELEGATE, IOException> backward,
CopyType copyType) {
Class<T> clazz, CopyType copyType) {
this.delegate = delegate;
this.forward = forward;
this.backward = backward;
this.clazz = clazz;
this.copyType = copyType;
}

/** The same as new DelegatedCodec(delegate, forward, backward, DEEP). */
public DelegatedCodec(Codec<DELEGATE> delegate,
CheckedFunction<DELEGATE, T, IOException> forward,
CheckedFunction<T, DELEGATE, IOException> backward) {
this(delegate, forward, backward, CopyType.DEEP);
CheckedFunction<T, DELEGATE, IOException> backward,
Class<T> clazz) {
this(delegate, forward, backward, clazz, CopyType.DEEP);
}

@Override
public Class<T> getTypeClass() {
return clazz;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ private IntegerCodec() {
// singleton
}

@Override
public Class<Integer> getTypeClass() {
return Integer.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static LongCodec get() {

private LongCodec() { }

@Override
public Class<Long> getTypeClass() {
return Long.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ public static <T extends MessageLite> Codec<T> get(T t) {
return (Codec<T>) codec;
}

private final Class<M> clazz;
private final Parser<M> parser;

private Proto2Codec(M m) {
this.clazz = (Class<M>) m.getClass();
this.parser = (Parser<M>) m.getParserForType();
}

@Override
public Class<M> getTypeClass() {
return clazz;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ public static <T extends MessageLite> Codec<T> get(T t) {
return (Codec<T>) codec;
}

private final Class<M> clazz;
private final Parser<M> parser;

private Proto3Codec(M m) {
this.clazz = (Class<M>) m.getClass();
this.parser = (Parser<M>) m.getParserForType();
}

@Override
public Class<M> getTypeClass() {
return clazz;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ private ShortCodec() {
// singleton
}

@Override
public Class<Short> getTypeClass() {
return Short.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ abstract class StringCodecBase implements Codec<String> {
this.fixedLength = max == encoder.averageBytesPerChar();
}

@Override
public final Class<String> getTypeClass() {
return String.class;
}

CharsetEncoder newEncoder() {
return charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static int getSerializedSize() {

private UuidCodec() { }

@Override
public Class<UUID> getTypeClass() {
return UUID.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class BlockData {
private static final Codec<BlockData> CODEC = new DelegatedCodec<>(
Proto3Codec.get(ContainerProtos.BlockData.getDefaultInstance()),
BlockData::getFromProtoBuf,
BlockData::getProtoBufMessage);
BlockData::getProtoBufMessage,
BlockData.class);

public static Codec<BlockData> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ChunkInfoList {
Proto3Codec.get(ContainerProtos.ChunkInfoList.getDefaultInstance()),
ChunkInfoList::getFromProtoBuf,
ChunkInfoList::getProtoBufMessage,
ChunkInfoList.class,
DelegatedCodec.CopyType.SHALLOW);

public static Codec<ChunkInfoList> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ private SchemaOneChunkInfoListCodec() {
// singleton
}

@Override
public Class<ChunkInfoList> getTypeClass() {
return ChunkInfoList.class;
}

@Override
public byte[] toPersistedFormat(ChunkInfoList chunkList) {
return chunkList.getProtoBufMessage().toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ private SchemaOneKeyCodec() {
// singleton
}

@Override
public Class<String> getTypeClass() {
return String.class;
}

@Override
public byte[] toPersistedFormat(String stringObject) throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MoveDataNodePair {
Proto2Codec.get(MoveDataNodePairProto.getDefaultInstance()),
MoveDataNodePair::getFromProtobuf,
pair -> pair.getProtobufMessage(ClientVersion.CURRENT_VERSION),
MoveDataNodePair.class,
DelegatedCodec.CopyType.SHALLOW);

public static Codec<MoveDataNodePair> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public final class CertInfo implements Comparable<CertInfo>, Serializable {
private static final Codec<CertInfo> CODEC = new DelegatedCodec<>(
Proto2Codec.get(CertInfoProto.getDefaultInstance()),
CertInfo::fromProtobuf,
CertInfo::getProtobuf);
CertInfo::getProtobuf,
CertInfo.class);

public static Codec<CertInfo> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class TransactionInfo implements Comparable<TransactionInfo> {
StringCodec.get(),
TransactionInfo::valueOf,
TransactionInfo::toString,
TransactionInfo.class,
DelegatedCodec.CopyType.SHALLOW);

public static Codec<TransactionInfo> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ private ByteArrayCodec() {
// singleton
}

@Override
public Class<byte[]> getTypeClass() {
return byte[].class;
}

@Override
public byte[] toPersistedFormat(byte[] bytes) {
return bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static ByteStringCodec get() {

private ByteStringCodec() { }

@Override
public Class<ByteString> getTypeClass() {
return ByteString.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public final class CompactionLogEntry implements
private static final Codec<CompactionLogEntry> CODEC = new DelegatedCodec<>(
Proto2Codec.get(CompactionLogEntryProto.getDefaultInstance()),
CompactionLogEntry::getFromProtobuf,
CompactionLogEntry::getProtobuf);
CompactionLogEntry::getProtobuf,
CompactionLogEntry.class);

public static Codec<CompactionLogEntry> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ private BigIntegerCodec() {
// singleton
}

@Override
public Class<BigInteger> getTypeClass() {
return BigInteger.class;
}

@Override
public byte[] toPersistedFormat(BigInteger object) throws IOException {
return object.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ private X509CertificateCodec() {
// singleton
}

@Override
public Class<X509Certificate> getTypeClass() {
return X509Certificate.class;
}

@Override
public boolean supportCodecBuffer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
* Codec to serialize / deserialize PipelineID.
*/
public class OldPipelineIDCodecForTesting implements Codec<PipelineID> {
@Override
public Class<PipelineID> getTypeClass() {
return PipelineID.class;
}

@Override
public byte[] toPersistedFormat(PipelineID object) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ private OldX509CertificateCodecForTesting() {
// singleton
}

@Override
public Class<X509Certificate> getTypeClass() {
return X509Certificate.class;
}

@Override
public byte[] toPersistedFormat(X509Certificate object) throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public final class OmBucketInfo extends WithObjectID implements Auditable, CopyO
private static final Codec<OmBucketInfo> CODEC = new DelegatedCodec<>(
Proto2Codec.get(BucketInfo.getDefaultInstance()),
OmBucketInfo::getFromProtobuf,
OmBucketInfo::getProtobuf);
OmBucketInfo::getProtobuf,
OmBucketInfo.class);

public static Codec<OmBucketInfo> getCodec() {
return CODEC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class OmDBAccessIdInfo {
Proto2Codec.get(ExtendedUserAccessIdInfo.getDefaultInstance()),
OmDBAccessIdInfo::getFromProtobuf,
OmDBAccessIdInfo::getProtobuf,
OmDBAccessIdInfo.class,
DelegatedCodec.CopyType.SHALLOW);

public static Codec<OmDBAccessIdInfo> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class OmDBTenantState implements Comparable<OmDBTenantState> {
Proto2Codec.get(TenantState.getDefaultInstance()),
OmDBTenantState::getFromProtobuf,
OmDBTenantState::getProtobuf,
OmDBTenantState.class,
DelegatedCodec.CopyType.SHALLOW);

public static Codec<OmDBTenantState> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
* principal.
*/
public final class OmDBUserPrincipalInfo {
private static final Codec<OmDBUserPrincipalInfo> CODEC
= new DelegatedCodec<>(
Proto2Codec.get(TenantUserPrincipalInfo.getDefaultInstance()),
OmDBUserPrincipalInfo::getFromProtobuf,
OmDBUserPrincipalInfo::getProtobuf);
private static final Codec<OmDBUserPrincipalInfo> CODEC = new DelegatedCodec<>(
Proto2Codec.get(TenantUserPrincipalInfo.getDefaultInstance()),
OmDBUserPrincipalInfo::getFromProtobuf,
OmDBUserPrincipalInfo::getProtobuf,
OmDBUserPrincipalInfo.class);

public static Codec<OmDBUserPrincipalInfo> getCodec() {
return CODEC;
Expand Down
Loading

0 comments on commit 5657604

Please sign in to comment.