prankstrisse commented on code in PR #11499: URL: https://github.com/apache/nifi/pull/11499#discussion_r4094478981
########## nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-protobuf-message-index-writer/src/main/java/org/apache/nifi/confluent/schemaregistry/ProtobufMessageIndexEncoder.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.nifi.confluent.schemaregistry; + +import org.apache.nifi.confluent.schema.ProtobufMessageSchema; +import org.apache.nifi.confluent.schema.VarintUtils; +import org.apache.nifi.schemaregistry.services.MessageName; + +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; + +import static java.lang.String.format; + +/** + * Computes the Confluent wire format message index path for a target message within a parsed + * Protobuf schema, and encodes it to bytes. This is the inverse of the message index decoding + * performed by {@code ConfluentProtobufMessageNameResolver}: given a fully qualified message + * name, it locates the path of declaration-order indexes leading to that message and encodes it + * as zigzag varints, applying the single-byte {@code 0x00} optimization for the common case of + * the first root message. + * <p> + * <a href="https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format">See the Confluent protobuf wire format.</a> + * <p> + * This class has no dependency on the NiFi framework and can be exercised directly with plain + * unit tests. + */ +final class ProtobufMessageIndexEncoder { + + private static final byte[] FIRST_ROOT_MESSAGE_INDEX = {0x00}; + + private ProtobufMessageIndexEncoder() { + } + + /** + * Encodes the message index path for the given message name within the given schema. + * + * @param rootMessages the root messages of the parsed Protobuf schema, in declaration order + * @param messageName the target message name to locate + * @return the encoded message index bytes + * @throws IllegalStateException if the message name cannot be located within the schema + */ + static byte[] encode(final List<ProtobufMessageSchema> rootMessages, final MessageName messageName) { + final List<Integer> messageIndexPath = findMessageIndexPath(rootMessages, messageName); + + if (messageIndexPath.size() == 1 && messageIndexPath.getFirst() == 0) { + return FIRST_ROOT_MESSAGE_INDEX; Review Comment: Yes, done. Side note: ConfluentProtobufMessageIndexWriter also caches the encoded arrays and returns the same array each time. That's safe because callers only write those bytes to the output stream and never change them, so I left the cache as it is -- 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]
