szetszwo commented on code in PR #9076:
URL: https://github.com/apache/ozone/pull/9076#discussion_r2515139338


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/SnapshotDiffJob.java:
##########
@@ -310,6 +314,51 @@ private static final class SnapshotDiffJobCodec
         .setSerializationInclusion(JsonInclude.Include.NON_NULL)
         .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
+    @Override
+    public boolean supportCodecBuffer() {
+      return true;
+    }
+
+    @Override
+    public CodecBuffer toCodecBuffer(@Nonnull SnapshotDiffJob object,
+        CodecBuffer.Allocator allocator) throws CodecException {
+
+      SnapshotDiffJobProto proto = object.toProtoBuf();
+      final int size = proto.getSerializedSize();
+      final CodecBuffer buffer = allocator.apply(size);
+
+      buffer.put(out -> {
+        try {
+          proto.writeTo(out);
+          return size;
+        } catch (IOException e) {
+          throw new IllegalStateException("Failed to write protobuf to 
buffer", e);
+        }
+      });
+      return buffer;
+    }
+
+    @Override
+    public SnapshotDiffJob fromCodecBuffer(@Nonnull CodecBuffer buffer)
+        throws CodecException {
+      
+      try (java.io.InputStream in = buffer.getInputStream()) {
+        SnapshotDiffJobProto proto = SnapshotDiffJobProto.parseFrom(in);
+        return SnapshotDiffJob.getFromProtoBuf(proto);
+      } catch (InvalidProtocolBufferException e) {
+        ByteBuffer bb = buffer.asReadOnlyByteBuffer();
+        byte[] data = new byte[bb.remaining()];
+        bb.get(data);
+        try {
+          return MAPPER.readValue(data, SnapshotDiffJob.class);
+        } catch (IOException ex) {
+          throw new CodecException("Failed to deserialize SnapshotDiffJob from 
JSON", ex);
+        }
+      } catch (IOException e) {
+        throw new CodecException("Failed to read from CodecBuffer", e);
+      }
+    }

Review Comment:
   Use Proto2Codec instead of reimplementing it.



##########
hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmSnapshotDiffJobCodec.java:
##########
@@ -70,4 +69,88 @@ public void testOldJsonSerializedDataCanBeReadByNewCodec() 
throws Exception {
 
     assertEquals(0.0, parsed.getKeysProcessedPct());
   }
+
+  @Test
+  public void testCodecBufferSupport() throws Exception {
+    assertTrue(newCodec.supportCodecBuffer());
+
+    SnapshotDiffJob original = new SnapshotDiffJob(
+        System.currentTimeMillis(),
+        "test-job-buffer",
+        JobStatus.DONE,
+        "testVol",
+        "testBucket",
+        "fromSnap",
+        "toSnap",
+        false,
+        true,
+        500L,
+        SubStatus.OBJECT_ID_MAP_GEN_FSO,
+        75.5
+    );
+
+    // Test with direct allocator
+    try (CodecBuffer buffer = newCodec.toCodecBuffer(original, 
CodecBuffer.Allocator.getDirect())) {
+      SnapshotDiffJob decoded = newCodec.fromCodecBuffer(buffer);
+      assertSnapshotDiffJobEquals(original, decoded);
+    }
+
+    // Test with heap allocator
+    try (CodecBuffer buffer = newCodec.toCodecBuffer(original, 
CodecBuffer.Allocator.getHeap())) {
+      SnapshotDiffJob decoded = newCodec.fromCodecBuffer(buffer);
+      assertSnapshotDiffJobEquals(original, decoded);
+    }
+  }
+
+  @Test
+  public void testCodecBufferBackwardCompatibility() throws Exception {
+
+    SnapshotDiffJob original = new SnapshotDiffJob(
+        987654321L,
+        "compat-job",
+        JobStatus.FAILED,
+        "volX",
+        "buckY",
+        "oldSnap",
+        "newSnap",
+        true,
+        true,
+        0L,
+        null,
+        0.0
+    );
+    original.setReason("Test failure reason");
+
+
+    byte[] jsonData = oldCodec.toPersistedFormatImpl(original);
+
+    // Create a CodecBuffer from the JSON data
+    // This simulates reading old format data from storage
+    try (CodecBuffer buffer = 
CodecBuffer.Allocator.getHeap().apply(jsonData.length)) {
+      buffer.put(ByteBuffer.wrap(jsonData));
+
+      // The new codec should handle JSON fallback in fromCodecBuffer
+      SnapshotDiffJob decoded = newCodec.fromCodecBuffer(buffer);
+
+      assertEquals(original.getJobId(), decoded.getJobId());
+      assertEquals(original.getStatus(), decoded.getStatus());
+      assertEquals(original.getReason(), decoded.getReason());

Review Comment:
   Use `assertSnapshotDiffJobEquals`.



-- 
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]

Reply via email to