snuyanzin commented on code in PR #29154: URL: https://github.com/apache/flink/pull/29154#discussion_r3980147181
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/UuidGenerationUtils.java: ########## @@ -0,0 +1,96 @@ +/* + * 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.flink.table.runtime.functions; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.types.logical.UuidType; + +import java.security.SecureRandom; + +/** + * Runtime helpers for generating {@code UUID} values, stored as their 16-byte big-endian encoding. + */ +@Internal +public final class UuidGenerationUtils { + + private UuidGenerationUtils() {} + + /** + * Generates a random RFC 9562 version 4 {@code UUID}. + * + * <p>Fills the bytes directly rather than via {@link java.util.UUID#randomUUID()}. The internal + * representation is already a {@code byte[]}, so this avoids an extra round-trip through a + * {@link java.util.UUID} object. + */ + public static byte[] generateV4() { + final byte[] bytes = new byte[UuidType.BYTE_LENGTH]; + Holder.SECURE_RANDOM.nextBytes(bytes); + + // set the version to 4 + bytes[6] &= 0x0F; + bytes[6] |= 0x40; + + // set the variant to IETF + bytes[8] &= 0x3F; + bytes[8] |= (byte) 0x80; + + return bytes; + } + + /** + * Generates a time-ordered RFC 9562 version 7 {@code UUID}, consisting of a 48-bit big-endian + * Unix epoch millisecond timestamp followed by 74 bits of randomness, with the version and + * variant bits set accordingly. + * + * <p>Implements the layout from the RFC directly rather than delegating to the JDK, since a + * built-in generator for this ({@code java.util.UUID.ofEpochMillis}) only exists starting with Review Comment: do we need absolute name here? -- 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]
