Skip to content

Commit

Permalink
Merge pull request #1938 from arnaud-lacurie/unique_compliant_name
Browse files Browse the repository at this point in the history
Make random UUID a compliant type name in CorrelationIdentifier.uniqueID
  • Loading branch information
arnaud-lacurie committed Dec 8, 2022
2 parents f153107 + 748fe6d commit f7a3145
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
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 @@ -26,6 +26,7 @@
import com.apple.foundationdb.record.query.plan.cascades.NullableArrayTypeUtils;
import com.apple.foundationdb.record.query.plan.cascades.PromoteValue;
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 @@ -45,7 +46,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 @@ -303,15 +303,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 @@ -891,7 +882,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 @@ -1222,7 +1213,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 @@ -1812,7 +1803,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;
}
}

0 comments on commit f7a3145

Please sign in to comment.