Copilot commented on code in PR #10603:
URL: https://github.com/apache/ozone/pull/10603#discussion_r3571852510


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffValueParser.java:
##########
@@ -0,0 +1,359 @@
+/*
+ * 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.om.snapshot;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.CodedInputStream;
+import com.google.protobuf.WireFormat;
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.KeyValue;
+import org.apache.hadoop.ozone.OzoneConsts;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.DirectoryInfo;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyInfo;
+
+/**
+ * Parses snapshot diff values without full deserialization.
+ */
+public final class SnapshotDiffValueParser {
+  private static final int INT_BYTES = 4;
+  private static final int LONG_BYTES = 8;
+  private static final int HSYNC_METADATA_PRESENT_TAG = 1001;
+  private static final String DIGEST_ALGORITHM = "SHA-256";
+
+  private SnapshotDiffValueParser() {
+  }
+
+  public static ParsedRequiredInfo parseKeyInfoRequiredFields(byte[] value, 
boolean includeUpdateId)
+      throws IOException {
+    CodedInputStream input = CodedInputStream.newInstance(value);
+    long updateId = 0L;
+    long objectId = 0L;
+    long parentId = 0L;
+    boolean hasUpdateId = false;
+    String keyName = null;
+
+    int tag;
+    while ((tag = input.readTag()) != 0) {
+      int fieldNumber = WireFormat.getTagFieldNumber(tag);
+      switch (fieldNumber) {
+      case KeyInfo.KEYNAME_FIELD_NUMBER:
+        keyName = input.readString();
+        break;
+      case KeyInfo.OBJECTID_FIELD_NUMBER:
+        objectId = input.readUInt64();
+        break;
+      case KeyInfo.UPDATEID_FIELD_NUMBER:
+        if (includeUpdateId) {
+          updateId = input.readUInt64();
+          hasUpdateId = true;
+        } else {
+          input.skipField(tag);
+        }
+        break;
+      case KeyInfo.PARENTID_FIELD_NUMBER:
+        parentId = input.readUInt64();
+        break;
+      default:
+        input.skipField(tag);
+        break;
+      }
+    }
+
+    return new ParsedRequiredInfo(updateId, hasUpdateId, objectId, parentId, 
keyName);
+  }
+
+  public static byte[] computeKeyInfoCompareSignature(byte[] value) throws 
IOException {
+    CodedInputStream input = CodedInputStream.newInstance(value);
+    MessageDigest digest = newDigest();
+    AtomicBoolean hasHsyncMetadata = new AtomicBoolean(false);
+    int keyLocationListCount = 0;
+    ByteString latestKeyLocationList = null;
+    List<byte[]> metadataSignatures = new ArrayList<>();
+    List<byte[]> tagSignatures = new ArrayList<>();
+
+    int tag;
+    while ((tag = input.readTag()) != 0) {
+      int fieldNumber = WireFormat.getTagFieldNumber(tag);
+      switch (fieldNumber) {
+      case KeyInfo.DATASIZE_FIELD_NUMBER:
+        updateDigestWithLong(digest, fieldNumber, input.readUInt64());
+        break;
+      case KeyInfo.KEYLOCATIONLIST_FIELD_NUMBER:
+        latestKeyLocationList = input.readBytes();
+        keyLocationListCount++;
+        break;
+      case KeyInfo.METADATA_FIELD_NUMBER:
+        byte[] metadataDigest = 
parseKeyValueDigest(input.readBytes().toByteArray(), true, hasHsyncMetadata);
+        if (metadataDigest != null) {
+          metadataSignatures.add(metadataDigest);
+        }
+        break;
+      case KeyInfo.FILECHECKSUM_FIELD_NUMBER:
+      case KeyInfo.ACLS_FIELD_NUMBER:
+        updateDigestWithBytes(digest, fieldNumber, input.readBytes());
+        break;
+      case KeyInfo.TAGS_FIELD_NUMBER:
+        byte[] tagDigest = 
parseKeyValueDigest(input.readBytes().toByteArray(), false, null);
+        if (tagDigest != null) {
+          tagSignatures.add(tagDigest);
+        }
+        break;
+      default:
+        input.skipField(tag);
+        break;
+      }
+    }
+
+    if (latestKeyLocationList != null) {
+      updateDigestWithBytes(digest, KeyInfo.KEYLOCATIONLIST_FIELD_NUMBER, 
latestKeyLocationList);
+    }
+    addCanonicalizedDigest(digest, KeyInfo.METADATA_FIELD_NUMBER, 
metadataSignatures);
+    addCanonicalizedDigest(digest, KeyInfo.TAGS_FIELD_NUMBER, tagSignatures);
+    updateDigestWithBoolean(digest, HSYNC_METADATA_PRESENT_TAG, 
hasHsyncMetadata.get());
+    updateDigestWithInt(digest, keyLocationListCount);
+
+    return digest.digest();
+  }
+
+  public static ParsedRequiredInfo parseDirectoryInfoRequiredFields(byte[] 
value, boolean includeUpdateId)
+      throws IOException {
+    CodedInputStream input = CodedInputStream.newInstance(value);
+    long updateId = 0L;
+    long objectId = 0L;
+    long parentId = 0L;
+    boolean hasUpdateId = false;
+    String name = null;
+
+    int tag;
+    while ((tag = input.readTag()) != 0) {
+      int fieldNumber = WireFormat.getTagFieldNumber(tag);
+      switch (fieldNumber) {
+      case DirectoryInfo.NAME_FIELD_NUMBER:
+        name = input.readString();
+        break;
+      case DirectoryInfo.OBJECTID_FIELD_NUMBER:
+        objectId = input.readUInt64();
+        break;
+      case DirectoryInfo.UPDATEID_FIELD_NUMBER:
+        if (includeUpdateId) {
+          updateId = input.readUInt64();
+          hasUpdateId = true;
+        } else {
+          input.skipField(tag);
+        }
+        break;
+      case DirectoryInfo.PARENTID_FIELD_NUMBER:
+        parentId = input.readUInt64();
+        break;
+      default:
+        input.skipField(tag);
+        break;
+      }
+    }
+
+    return new ParsedRequiredInfo(updateId, hasUpdateId, objectId, parentId, 
name);
+  }
+
+  public static byte[] computeDirectoryInfoCompareSignature(byte[] value) 
throws IOException {
+    CodedInputStream input = CodedInputStream.newInstance(value);
+    MessageDigest digest = newDigest();
+    List<byte[]> metadataSignatures = new ArrayList<>();
+
+    int tag;
+    while ((tag = input.readTag()) != 0) {
+      int fieldNumber = WireFormat.getTagFieldNumber(tag);
+      switch (fieldNumber) {
+      case DirectoryInfo.METADATA_FIELD_NUMBER:
+        byte[] metadataDigest = 
parseKeyValueDigest(input.readBytes().toByteArray(), false, null);
+        if (metadataDigest != null) {
+          metadataSignatures.add(metadataDigest);
+        }
+        break;
+      case DirectoryInfo.ACLS_FIELD_NUMBER:
+        updateDigestWithBytes(digest, fieldNumber, input.readBytes());
+        break;
+      default:
+        input.skipField(tag);
+        break;
+      }
+    }
+
+    addCanonicalizedDigest(digest, KeyInfo.METADATA_FIELD_NUMBER, 
metadataSignatures);

Review Comment:
   In computeDirectoryInfoCompareSignature(), the canonicalized metadata digest 
is tagged with KeyInfo.METADATA_FIELD_NUMBER. DirectoryInfo.metadata is field 4 
(see OmClientProtocol.proto), so this should use 
DirectoryInfo.METADATA_FIELD_NUMBER to avoid hashing DirectoryInfo metadata 
under the wrong tag and to prevent future field-number collisions if 
DirectoryInfo gains new fields.



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