szetszwo commented on code in PR #9069:
URL: https://github.com/apache/ozone/pull/9069#discussion_r2422087337
##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/codec/NSSummaryCodec.java:
##########
@@ -56,6 +60,113 @@ public Class<NSSummary> getTypeClass() {
return NSSummary.class;
}
+ @Override
+ public boolean supportCodecBuffer() {
+ return true;
+ }
+
+ @Override
+ public CodecBuffer toCodecBuffer(@Nonnull NSSummary object,
CodecBuffer.Allocator allocator) throws CodecException {
+ CodecBuffer dirNameBuffer = null;
+ try {
+ Set<Long> childDirs = object.getChildDir();
+ int numOfChildDirs = childDirs.size();
+ String dirName = object.getDirName();
+
+ dirNameBuffer = stringCodec.toCodecBuffer(dirName, allocator);
+ int dirNameSize = dirNameBuffer.readableBytes();
+
+ // total size: primitives + childDirs + dirName length + dirName data
+ // Integer Bytes: numFiles + numOfChildDirs + dirNameSize +
fileSizeBucket
+ // Long Bytes: childDirs + sizeOfFiles + parentId
+ // Short Bytes: fileSizeBucket length
+ final int totalSize = Integer.BYTES * (3 +
ReconConstants.NUM_OF_FILE_SIZE_BINS)
+ + Long.BYTES * (numOfChildDirs + 2)
+ + Short.BYTES
+ + dirNameSize;
+
+ CodecBuffer buffer = allocator.apply(totalSize);
+
+ buffer.putInt(object.getNumOfFiles());
+ buffer.putLong(object.getSizeOfFiles());
+ buffer.putShort((short) ReconConstants.NUM_OF_FILE_SIZE_BINS);
+
+ int[] fileSizeBucket = object.getFileSizeBucket();
+ for (int i = 0; i < ReconConstants.NUM_OF_FILE_SIZE_BINS; ++i) {
+ buffer.putInt(fileSizeBucket[i]);
+ }
+
+ buffer.putInt(numOfChildDirs);
+ for (long childDirId : childDirs) {
+ buffer.putLong(childDirId);
+ }
+
+ buffer.putInt(dirNameSize);
+ if (dirNameSize > 0) {
+ buffer.put(dirNameBuffer.asReadOnlyByteBuffer());
+ }
+ buffer.putLong(object.getParentId());
+
+ dirNameBuffer.release();
+ dirNameBuffer = null;
+
+ return buffer;
+ } catch (Exception e) {
+ if (dirNameBuffer != null) {
+ dirNameBuffer.release();
+ }
+ throw new CodecException("Failed to encode NSSummary to CodecBuffer", e);
+ }
+ }
+
+ @Override
+ public NSSummary fromCodecBuffer(@Nonnull CodecBuffer buffer) throws
CodecException {
+ try {
+ ByteBuffer byteBuffer = buffer.asReadOnlyByteBuffer();
+ NSSummary result = new NSSummary();
+
+ result.setNumOfFiles(byteBuffer.getInt());
+ result.setSizeOfFiles(byteBuffer.getLong());
+
+ short bucketLength = byteBuffer.getShort();
+ assert (bucketLength == (short) ReconConstants.NUM_OF_FILE_SIZE_BINS);
+
+ int[] fileSizeBucket = new int[bucketLength];
+ for (int i = 0; i < bucketLength; ++i) {
+ fileSizeBucket[i] = byteBuffer.getInt();
+ }
+ result.setFileSizeBucket(fileSizeBucket);
+
+ int numChildDirs = byteBuffer.getInt();
+ Set<Long> childDirs = new LinkedHashSet<>(numChildDirs);
+ for (int i = 0; i < numChildDirs; ++i) {
+ childDirs.add(byteBuffer.getLong());
+ }
+ result.setChildDir(childDirs);
+
+ int dirNameSize = byteBuffer.getInt();
+ if (dirNameSize == 0) {
+ result.setParentId(byteBuffer.getLong());
+ return result;
+ }
+
+ byte[] dirNameBytes = new byte[dirNameSize];
+ byteBuffer.get(dirNameBytes);
+ CodecBuffer dirNameBuffer = CodecBuffer.wrap(dirNameBytes);
+ try {
+ String dirName = stringCodec.fromCodecBuffer(dirNameBuffer);
+ result.setDirName(dirName);
+ } finally {
+ dirNameBuffer.release();
+ }
Review Comment:
Add a wrap method to avoid creating byte[]:
```diff
+++
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/CodecBuffer.java
@@ -225,6 +225,11 @@ public static CodecBuffer wrap(byte[] array) {
return Factory.newCodecBuffer(Unpooled.wrappedBuffer(array), array);
}
+ /** Wrap the given {@link ByteBuffer}. */
+ public static CodecBuffer wrap(ByteBuffer buffer) {
+ return Factory.newCodecBuffer(Unpooled.wrappedBuffer(buffer), buffer);
+ }
+
/** Wrap the given {@link ByteString}. */
public static CodecBuffer wrap(ByteString bytes) {
return Factory.newCodecBuffer(
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]