Skip to content

Commit

Permalink
refactor exsting code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenSammi committed Nov 12, 2024
1 parent 1afacb0 commit e96c28a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.hadoop.hdds.annotation.InterfaceAudience;
import org.apache.hadoop.hdds.annotation.InterfaceStability;
import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableUtils;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMTokenProto;
Expand Down Expand Up @@ -89,35 +88,6 @@ public Text getKind() {
return KIND_NAME;
}

/** Instead of relying on proto serialization, this
* provides explicit serialization for OzoneTokenIdentifier.
* @return byte[]
*/
public byte[] toUniqueSerializedKey() {
DataOutputBuffer buf = new DataOutputBuffer();
try {
super.write(buf);
WritableUtils.writeVInt(buf, getTokenType().getNumber());
// Set s3 specific fields.
if (getTokenType().equals(S3AUTHINFO)) {
WritableUtils.writeString(buf, getAwsAccessId());
WritableUtils.writeString(buf, getSignature());
WritableUtils.writeString(buf, getStrToSign());
} else {
if (StringUtils.isNotEmpty(getOmCertSerialId())) {
WritableUtils.writeString(buf, getOmCertSerialId());
} else {
WritableUtils.writeString(buf, getSecretKeyId());
}
WritableUtils.writeString(buf, getOmServiceId());
}
} catch (java.io.IOException e) {
throw new IllegalArgumentException(
"Can't encode the the raw data ", e);
}
return buf.getData();
}

/** Instead of relying on proto deserialization, this
* provides explicit deserialization for OzoneTokenIdentifier.
* @return byte[]
Expand Down Expand Up @@ -148,14 +118,7 @@ public OzoneTokenIdentifier fromUniqueSerializedKey(byte[] rawData)
return this;
}

/**
* Overrides default implementation to write using Protobuf.
*
* @param out output stream
* @throws IOException
*/
@Override
public void write(DataOutput out) throws IOException {
public OMTokenProto toProtoBuf() throws IOException {
OMTokenProto.Builder builder = OMTokenProto.newBuilder()
.setMaxDate(getMaxDate())
.setType(getTokenType())
Expand All @@ -182,9 +145,18 @@ public void write(DataOutput out) throws IOException {
builder.setOmServiceId(getOmServiceId());
}
}
return builder.build();
}

OMTokenProto token = builder.build();
out.write(token.toByteArray());
/**
* Overrides default implementation to write using Protobuf.
*
* @param out output stream
* @throws IOException
*/
@Override
public void write(DataOutput out) throws IOException {
out.write(toProtoBuf().toByteArray());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ message OMTokenProto {
optional string accessKeyId = 12;
optional string signature = 13;
optional string strToSign = 14;
optional string omServiceId = 15;
optional string omServiceId = 15 [deprecated = true];
optional string secretKeyId = 16;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public Class<OzoneTokenIdentifier> getTypeClass() {
}

@Override
public byte[] toPersistedFormat(OzoneTokenIdentifier object) {
public byte[] toPersistedFormat(OzoneTokenIdentifier object) throws IOException {
Preconditions
.checkNotNull(object, "Null object can't be converted to byte array.");
return object.toUniqueSerializedKey();
return object.toProtoBuf().toByteArray();
}

@Override
Expand All @@ -60,11 +60,11 @@ public OzoneTokenIdentifier fromPersistedFormat(byte[] rawData)
Preconditions.checkNotNull(rawData,
"Null byte array can't converted to real object.");
try {
OzoneTokenIdentifier object = OzoneTokenIdentifier.newInstance();
return object.fromUniqueSerializedKey(rawData);
return OzoneTokenIdentifier.readProtoBuf(rawData);
} catch (IOException ex) {
try {
return OzoneTokenIdentifier.readProtoBuf(rawData);
OzoneTokenIdentifier object = OzoneTokenIdentifier.newInstance();
return object.fromUniqueSerializedKey(rawData);
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(
"Can't encode the the raw data from the byte array", e);
Expand Down

0 comments on commit e96c28a

Please sign in to comment.