szetszwo commented on code in PR #9069:
URL: https://github.com/apache/ozone/pull/9069#discussion_r2421932650
##########
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);
Review Comment:
Include the object:
```java
throw new CodecException("Failed to encode " + object, e);
```
##########
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);
Review Comment:
Use int and Preconditions.assertSame(..)
(org.apache.ratis.util.Preconditions)
```java
final int bucketLength = byteBuffer.getShort();
Preconditions.assertSame(ReconConstants.NUM_OF_FILE_SIZE_BINS,
bucketLength, "bucketLength");
```
##########
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();
+ }
Review Comment:
release() is not needed with try-with-resource.
##########
hadoop-ozone/recon/src/test/java/org/apache/hadoop/ozone/recon/codec/TestNSSummaryCodec.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.hadoop.ozone.recon.codec;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.hadoop.hdds.utils.db.Codec;
+import org.apache.hadoop.hdds.utils.db.CodecBuffer;
+import org.apache.hadoop.ozone.recon.ReconConstants;
+import org.apache.hadoop.ozone.recon.api.types.NSSummary;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for NSSummaryCodec CodecBuffer implementation.
+ */
+public class TestNSSummaryCodec {
+
+ private final Codec<NSSummary> codec = NSSummaryCodec.get();
+
+ @Test
+ public void testCodecBufferRoundTrip() throws Exception {
+ NSSummary original = createTestNSSummary();
+
+ CodecBuffer buffer = codec.toCodecBuffer(original,
CodecBuffer.Allocator.DIRECT);
+ try {
+ NSSummary decoded = codec.fromCodecBuffer(buffer);
+ assertNSSummaryEquals(original, decoded);
+ } finally {
+ buffer.close();
+ }
+ }
+
+ @Test
+ public void testCodecBufferEmptyDirectory() throws Exception {
+ NSSummary original = new NSSummary();
+ original.setDirName("empty");
+ original.setParentId(42L);
+
+ CodecBuffer buffer = codec.toCodecBuffer(original,
CodecBuffer.Allocator.DIRECT);
+ try {
+ NSSummary decoded = codec.fromCodecBuffer(buffer);
+ assertNSSummaryEquals(original, decoded);
+ } finally {
+ buffer.close();
+ }
Review Comment:
Add a runTest(..) method and use it in all tests:
```java
private void runTest(NSSummary original) throws Exception {
final byte[] array = codec.toPersistedFormat(original);
try (CodecBuffer encoded = codec.toCodecBuffer(original,
CodecBuffer.Allocator.DIRECT)) {
assertEquals(ByteBuffer.wrap(array), encoded.asReadOnlyByteBuffer());
NSSummary decoded = codec.fromCodecBuffer(encoded);
assertNSSummaryEquals(original, decoded);
}
}
```
##########
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 {
Review Comment:
Use try-with-resource:
```java
try (CodecBuffer dirNameBuffer =
stringCodec.toCodecBuffer(object.getDirName(), allocator)) {
```
##########
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();
Review Comment:
Use the other constructor to avoid array and set copying.
--
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]