Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make random UUID a compliant type name in CorrelationIdentifier.uniqueID #1938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,6 +22,7 @@

import com.apple.foundationdb.annotation.API;
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger;
import com.apple.foundationdb.record.util.ProtoUtils;
import com.google.common.collect.ImmutableSet;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static CorrelationIdentifier uniqueID(@Nonnull final Class<?> clazz, @Non
final CorrelationIdentifier id =
Debugger.getIndexOptional(clazz)
.map(i -> CorrelationIdentifier.of(prefix + i))
.orElseGet(() -> new CorrelationIdentifier(UUID.randomUUID().toString()));
.orElseGet(() -> new CorrelationIdentifier(ProtoUtils.uniqueCorrelationName()));
Debugger.updateIndex(clazz, i -> i + 1);
return id;
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import com.apple.foundationdb.record.query.plan.cascades.Narrowable;
import com.apple.foundationdb.record.query.plan.cascades.NullableArrayTypeUtils;
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
import com.apple.foundationdb.record.util.ProtoUtils;
import com.google.common.base.Suppliers;
import com.google.common.base.Verify;
import com.google.common.collect.BiMap;
Expand All @@ -44,7 +45,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -280,15 +280,6 @@ static List<Type> fromTyped(@Nonnull List<? extends Typed> typedList) {
.collect(ImmutableList.toImmutableList());
}

/**
* Generates a JVM-wide unique type name.
* @return a unique type name.
*/
static String uniqueCompliantTypeName() {
final var safeUuid = UUID.randomUUID().toString().replace('-', '_');
return "__type__" + safeUuid;
}

/**
* translates a protobuf {@link com.google.protobuf.Descriptors.Descriptor} to a {@link Type}.
*
Expand Down Expand Up @@ -676,7 +667,7 @@ public String getName() {
@Override
public void defineProtoType(@Nonnull final TypeRepository.Builder typeRepositoryBuilder) {
Verify.verify(!isErased());
final var typeName = name == null ? uniqueCompliantTypeName() : name;
final var typeName = name == null ? ProtoUtils.uniqueTypeName() : name;
final var enumDescriptorProtoBuilder = DescriptorProtos.EnumDescriptorProto.newBuilder();
enumDescriptorProtoBuilder.setName(typeName);

Expand Down Expand Up @@ -1007,7 +998,7 @@ boolean isErased() {
@Override
public void defineProtoType(final TypeRepository.Builder typeRepositoryBuilder) {
Objects.requireNonNull(fields);
final var typeName = name == null ? uniqueCompliantTypeName() : name;
final var typeName = name == null ? ProtoUtils.uniqueTypeName() : name;
final var recordMsgBuilder = DescriptorProto.newBuilder();
recordMsgBuilder.setName(typeName);

Expand Down Expand Up @@ -1597,7 +1588,7 @@ boolean isErased() {
@Override
public void defineProtoType(final TypeRepository.Builder typeRepositoryBuilder) {
Objects.requireNonNull(elementType);
final var typeName = uniqueCompliantTypeName();
final var typeName = ProtoUtils.uniqueTypeName();
typeRepositoryBuilder.registerTypeToTypeNameMapping(this, typeName);
if (isNullable && elementType.getTypeCode() != TypeCode.UNKNOWN) {
Type wrapperType = Record.fromFields(List.of(Record.Field.of(new Array(elementType), Optional.of(NullableArrayTypeUtils.getRepeatedFieldName()))));
Expand Down
@@ -0,0 +1,53 @@
/*
* ProtoUtils.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2015-2022 Apple Inc. and the FoundationDB project authors
*
* Licensed 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 com.apple.foundationdb.record.util;

import java.util.UUID;

/**
* Utility functions for interacting with protobuf.
*/
public class ProtoUtils {

private ProtoUtils() {
}

/**
* Generates a JVM-wide unique type name.
* @return a unique type name.
*/
public static String uniqueTypeName() {
return uniqueName("__type__");
}

/**
* Generates a JVM-wide unique correlation name.
* @return a unique type name.
*/
public static String uniqueCorrelationName() {
return uniqueName("__corr__");
}

private static String uniqueName(String prefix) {
final var safeUuid = UUID.randomUUID().toString().replace('-', '_');
return prefix + safeUuid;
}
}