use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Multimap in project beam by apache.
the class RenameFields method renameSchema.
// Apply the user-specified renames to the input schema.
@VisibleForTesting
static void renameSchema(Schema inputSchema, Collection<RenamePair> renames, Map<UUID, Schema> renamedSchemasMap, Map<UUID, BitSet> nestedFieldRenamedMap) {
// The mapping of renames to apply at this level of the schema.
Map<Integer, String> topLevelRenames = Maps.newHashMap();
// For nested schemas, collect all applicable renames here.
Multimap<Integer, RenamePair> nestedRenames = ArrayListMultimap.create();
for (RenamePair rename : renames) {
FieldAccessDescriptor access = rename.getFieldAccessDescriptor();
if (!access.fieldIdsAccessed().isEmpty()) {
// This references a field at this level of the schema.
Integer fieldId = Iterables.getOnlyElement(access.fieldIdsAccessed());
topLevelRenames.put(fieldId, rename.getNewName());
} else {
// This references a nested field.
Map.Entry<Integer, FieldAccessDescriptor> nestedAccess = Iterables.getOnlyElement(access.nestedFieldsById().entrySet());
nestedFieldRenamedMap.computeIfAbsent(inputSchema.getUUID(), s -> new BitSet(inputSchema.getFieldCount())).set(nestedAccess.getKey());
nestedRenames.put(nestedAccess.getKey(), RenamePair.of(nestedAccess.getValue(), rename.getNewName()));
}
}
Schema.Builder builder = Schema.builder();
for (int i = 0; i < inputSchema.getFieldCount(); ++i) {
Field field = inputSchema.getField(i);
FieldType fieldType = field.getType();
String newName = topLevelRenames.getOrDefault(i, field.getName());
Collection<RenamePair> nestedFieldRenames = nestedRenames.asMap().getOrDefault(i, Collections.emptyList());
builder.addField(newName, renameFieldType(fieldType, nestedFieldRenames, renamedSchemasMap, nestedFieldRenamedMap));
}
renamedSchemasMap.put(inputSchema.getUUID(), builder.build());
}
use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Multimap in project beam by apache.
the class ProtoByteBuddyUtils method createGetter.
private static <ProtoT> FieldValueGetter createGetter(FieldValueTypeInformation fieldValueTypeInformation, TypeConversionsFactory typeConversionsFactory, Class clazz, Multimap<String, Method> methods, Field field, FieldValueTypeSupplier fieldValueTypeSupplier) {
if (field.getType().isLogicalType(OneOfType.IDENTIFIER)) {
OneOfType oneOfType = field.getType().getLogicalType(OneOfType.class);
// The case accessor method in the proto is named getOneOfNameCase.
Method caseMethod = getProtoGetter(methods, field.getName() + "_case", FieldType.logicalType(oneOfType.getCaseEnumType()));
// Create a map of case enum value to getter. This must be sorted, so store in a TreeMap.
TreeMap<Integer, FieldValueGetter<ProtoT, OneOfType.Value>> oneOfGetters = Maps.newTreeMap();
Map<String, FieldValueTypeInformation> oneOfFieldTypes = fieldValueTypeSupplier.get(clazz, oneOfType.getOneOfSchema()).stream().collect(Collectors.toMap(FieldValueTypeInformation::getName, f -> f));
for (Field oneOfField : oneOfType.getOneOfSchema().getFields()) {
int protoFieldIndex = getFieldNumber(oneOfField);
FieldValueGetter oneOfFieldGetter = createGetter(oneOfFieldTypes.get(oneOfField.getName()), typeConversionsFactory, clazz, methods, oneOfField, fieldValueTypeSupplier);
oneOfGetters.put(protoFieldIndex, oneOfFieldGetter);
}
return createOneOfGetter(fieldValueTypeInformation, oneOfGetters, clazz, oneOfType, caseMethod);
} else {
return JavaBeanUtils.createGetter(fieldValueTypeInformation, typeConversionsFactory);
}
}
Aggregations