mosche commented on a change in pull request #16947: URL: https://github.com/apache/beam/pull/16947#discussion_r826184386
########## File path: sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/values/AwsPojoRow.java ########## @@ -0,0 +1,176 @@ +/* + * 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.beam.sdk.values; + +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static org.apache.beam.sdk.io.aws2.schemas.AwsPojoSchema.CACHE_IDX_OPTION; +import static org.apache.beam.sdk.io.aws2.schemas.AwsPojoSchema.CACHE_SIZE_OPTION; +import static software.amazon.awssdk.core.protocol.MarshallingType.LIST; +import static software.amazon.awssdk.core.protocol.MarshallingType.MAP; +import static software.amazon.awssdk.core.protocol.MarshallingType.SDK_POJO; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.Schema.Field; +import org.apache.beam.sdk.schemas.Schema.FieldType; +import org.apache.commons.lang3.ArrayUtils; +import org.checkerframework.checker.nullness.qual.Nullable; +import software.amazon.awssdk.core.SdkField; +import software.amazon.awssdk.core.SdkPojo; +import software.amazon.awssdk.core.protocol.MarshallingType; +import software.amazon.awssdk.core.traits.ListTrait; +import software.amazon.awssdk.core.traits.MapTrait; + +/** + * A row delegating to an underlying AWS {@link SdkPojo}. + * + * <p>This is following the example of {@link RowWithGetters}, but optimized for usage with {@link + * SdkPojo}s. + */ +public class AwsPojoRow extends Row { + private final SdkPojo pojo; + + // Cache of computed values (if needed) + private final Object[] valueCache; + + public AwsPojoRow(SdkPojo pojo, Schema schema) { + super(schema); // FIXME package private, discuss how to proceed + this.pojo = pojo; + this.valueCache = createValueCache(schema); + } + + private static Object[] createValueCache(Schema schema) { + short cacheSize = schema.getOptions().getValueOrDefault(CACHE_SIZE_OPTION, (short) 0); + return cacheSize > 0 ? new Object[cacheSize] : ArrayUtils.EMPTY_OBJECT_ARRAY; + } + + public SdkPojo getPojo() { + return pojo; + } + + @Override + @SuppressWarnings("TypeParameterUnusedInFormals") + public <T> @Nullable T getValue(int fieldIdx) { + return (T) convertFieldValue(fieldIdx); + } + + @Override + public int getFieldCount() { + return pojo.sdkFields().size(); + } + + @Override + public List<Object> getValues() { + List<Object> list = new ArrayList<>(getFieldCount()); + for (int i = 0; i <= list.size(); i++) { + Object value = convertFieldValue(i); + if (value != null) { + list.add(value); + } + } + return list; + } + + private @Nullable Object convertFieldValue(int idx) { + SdkField<?> sdkField = pojo.sdkFields().get(idx); + Object value = sdkField.getValueOrDefault(pojo); + if (value == null) { + return null; + } else if (isPojo(sdkField)) { + return cachedValue(idx, field -> getRowValue((SdkPojo) value, field)); + } else if (isListOfPojos(sdkField)) { + List<SdkPojo> list = (List) value; + if (list.isEmpty()) { + return list; + } + return cachedValue(idx, field -> getListValue(list, field)); + } else if (isMapOfPojoValues(sdkField)) { + Map<String, SdkPojo> map = (Map) value; + if (map.isEmpty()) { + return map; + } + return cachedValue(idx, field -> getMapValue(map, field)); + } + return value; + } + + private AwsPojoRow getRowValue(SdkPojo pojo, Field field) { + Schema schema = field.getType().getRowSchema(); + if (schema == null) { + throw new IllegalStateException("Row schema required for field " + field.getName()); + } + return new AwsPojoRow(pojo, schema); + } + + private List<AwsPojoRow> getListValue(List<SdkPojo> list, Field field) { + FieldType type = field.getType().getCollectionElementType(); + Schema schema = type != null ? type.getRowSchema() : null; + if (schema == null) { + throw new IllegalStateException( + "Collection element schema required for field " + field.getName()); + } + return list.stream().map(p -> new AwsPojoRow(p, schema)).collect(toList()); + } + + private Map<String, AwsPojoRow> getMapValue(Map<String, SdkPojo> map, Field field) { + FieldType type = field.getType().getMapValueType(); + Schema schema = type != null ? type.getRowSchema() : null; + if (schema == null) { + throw new IllegalStateException("Map value schema required for field " + field.getName()); + } + return map.entrySet().stream() + .collect(toMap(Map.Entry::getKey, e -> new AwsPojoRow(e.getValue(), schema))); + } + + private <T> T cachedValue(int fieldIdx, Function<Field, T> conversion) { + Field field = getSchema().getField(fieldIdx); + if (valueCache.length == 0) { + return conversion.apply(field); + } + short cacheIdx = field.getOptions().getValueOrDefault(CACHE_IDX_OPTION, (short) -1); Review comment: @mosche treat `-1` 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]
