|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hudi.common.table.read; |
| 20 | + |
| 21 | +import org.apache.hudi.common.engine.HoodieReaderContext; |
| 22 | +import org.apache.hudi.common.model.DeleteRecord; |
| 23 | +import org.apache.hudi.common.model.HoodieRecord; |
| 24 | +import org.apache.hudi.common.util.Option; |
| 25 | +import org.apache.hudi.exception.HoodieException; |
| 26 | + |
| 27 | +import org.apache.avro.Schema; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.Serializable; |
| 31 | +import java.util.Properties; |
| 32 | + |
| 33 | +import static org.apache.hudi.common.model.HoodieRecord.DEFAULT_ORDERING_VALUE; |
| 34 | + |
| 35 | +/** |
| 36 | + * Buffered Record used by file group reader. |
| 37 | + */ |
| 38 | +public class BufferedRecord<T> implements Serializable { |
| 39 | + private final String recordKey; |
| 40 | + private final String partitionPath; |
| 41 | + private final Comparable orderingValue; |
| 42 | + private T record; |
| 43 | + private final Integer schemaId; |
| 44 | + private final boolean isDelete; |
| 45 | + |
| 46 | + private BufferedRecord(String partitionPath, String recordKey, Comparable orderingValue, T record, Integer schemaId, boolean isDelete) { |
| 47 | + this.partitionPath = partitionPath; |
| 48 | + this.recordKey = recordKey; |
| 49 | + this.orderingValue = orderingValue; |
| 50 | + this.record = record; |
| 51 | + this.schemaId = schemaId; |
| 52 | + this.isDelete = isDelete; |
| 53 | + } |
| 54 | + |
| 55 | + public static <T> BufferedRecord<T> forRecordWithContext(HoodieRecord<T> record, Schema schema, HoodieReaderContext<T> readerContext, Properties props) { |
| 56 | + String recordKey = record.getRecordKey(); |
| 57 | + Integer schemaId = readerContext.encodeAvroSchema(schema); |
| 58 | + boolean isDelete; |
| 59 | + try { |
| 60 | + isDelete = record.isDelete(schema, props); |
| 61 | + } catch (IOException e) { |
| 62 | + throw new HoodieException("Failed to get isDelete from record.", e); |
| 63 | + } |
| 64 | + return new BufferedRecord<>(null, recordKey, record.getOrderingValue(schema, props), record.getData(), schemaId, isDelete); |
| 65 | + } |
| 66 | + |
| 67 | + public static <T> BufferedRecord<T> forRecordWithContext(T record, Schema schema, HoodieReaderContext<T> readerContext, Option<String> orderingFieldName, boolean isDelete) { |
| 68 | + String recordKey = readerContext.getRecordKey(record, schema); |
| 69 | + Integer schemaId = readerContext.encodeAvroSchema(schema); |
| 70 | + Comparable orderingValue = readerContext.getOrderingValue(record, schema, orderingFieldName); |
| 71 | + return new BufferedRecord<>(null, recordKey, orderingValue, record, schemaId, isDelete); |
| 72 | + } |
| 73 | + |
| 74 | + public static <T> BufferedRecord<T> forDeleteRecord(DeleteRecord deleteRecord, Comparable orderingValue) { |
| 75 | + return new BufferedRecord<>( |
| 76 | + deleteRecord.getPartitionPath(), deleteRecord.getRecordKey(), orderingValue, null, null, true); |
| 77 | + } |
| 78 | + |
| 79 | + public String getPartitionPath() { |
| 80 | + return partitionPath; |
| 81 | + } |
| 82 | + |
| 83 | + public String getRecordKey() { |
| 84 | + return recordKey; |
| 85 | + } |
| 86 | + |
| 87 | + public Comparable getOrderingValue() { |
| 88 | + return orderingValue; |
| 89 | + } |
| 90 | + |
| 91 | + public T getRecord() { |
| 92 | + return record; |
| 93 | + } |
| 94 | + |
| 95 | + public Integer getSchemaId() { |
| 96 | + return schemaId; |
| 97 | + } |
| 98 | + |
| 99 | + public boolean isDelete() { |
| 100 | + return isDelete; |
| 101 | + } |
| 102 | + |
| 103 | + public boolean isDeleteRecordWithNaturalOrder() { |
| 104 | + return isDelete && getOrderingValue().equals(DEFAULT_ORDERING_VALUE); |
| 105 | + } |
| 106 | + |
| 107 | + public BufferedRecord<T> toBinary(HoodieReaderContext<T> readerContext) { |
| 108 | + if (record != null) { |
| 109 | + record = readerContext.toBinaryRow(record); |
| 110 | + } |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public BufferedRecord<T> copy(T data) { |
| 115 | + return new BufferedRecord<>(partitionPath, recordKey, orderingValue, data, schemaId, isDelete); |
| 116 | + } |
| 117 | +} |
0 commit comments