|
| 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 | +package org.apache.arrow.file; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.apache.arrow.flatbuf.Block; |
| 24 | +import org.apache.arrow.flatbuf.Footer; |
| 25 | +import org.apache.arrow.schema.FBSerializable; |
| 26 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 27 | + |
| 28 | +import com.google.flatbuffers.FlatBufferBuilder; |
| 29 | + |
| 30 | +public class ArrowFooter implements FBSerializable { |
| 31 | + |
| 32 | + private final Schema schema; |
| 33 | + |
| 34 | + private final List<ArrowBlock> dictionaries; |
| 35 | + |
| 36 | + private final List<ArrowBlock> recordBatches; |
| 37 | + |
| 38 | + public ArrowFooter(Schema schema, List<ArrowBlock> dictionaries, List<ArrowBlock> recordBatches) { |
| 39 | + super(); |
| 40 | + this.schema = schema; |
| 41 | + this.dictionaries = dictionaries; |
| 42 | + this.recordBatches = recordBatches; |
| 43 | + } |
| 44 | + |
| 45 | + public ArrowFooter(Footer footer) { |
| 46 | + this( |
| 47 | + Schema.convertSchema(footer.schema()), |
| 48 | + dictionaries(footer), |
| 49 | + recordBatches(footer) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private static List<ArrowBlock> recordBatches(Footer footer) { |
| 54 | + List<ArrowBlock> recordBatches = new ArrayList<>(); |
| 55 | + Block tempBLock = new Block(); |
| 56 | + int recordBatchesLength = footer.recordBatchesLength(); |
| 57 | + for (int i = 0; i < recordBatchesLength; i++) { |
| 58 | + Block block = footer.recordBatches(tempBLock, i); |
| 59 | + recordBatches.add(new ArrowBlock(block.offset(), block.metaDataLength(), block.bodyLength())); |
| 60 | + } |
| 61 | + return recordBatches; |
| 62 | + } |
| 63 | + |
| 64 | + private static List<ArrowBlock> dictionaries(Footer footer) { |
| 65 | + List<ArrowBlock> dictionaries = new ArrayList<>(); |
| 66 | + Block tempBLock = new Block(); |
| 67 | + int dictionariesLength = footer.dictionariesLength(); |
| 68 | + for (int i = 0; i < dictionariesLength; i++) { |
| 69 | + Block block = footer.dictionaries(tempBLock, i); |
| 70 | + dictionaries.add(new ArrowBlock(block.offset(), block.metaDataLength(), block.bodyLength())); |
| 71 | + } |
| 72 | + return dictionaries; |
| 73 | + } |
| 74 | + |
| 75 | + public Schema getSchema() { |
| 76 | + return schema; |
| 77 | + } |
| 78 | + |
| 79 | + public List<ArrowBlock> getDictionaries() { |
| 80 | + return dictionaries; |
| 81 | + } |
| 82 | + |
| 83 | + public List<ArrowBlock> getRecordBatches() { |
| 84 | + return recordBatches; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int writeTo(FlatBufferBuilder builder) { |
| 89 | + int schemaIndex = schema.getSchema(builder); |
| 90 | + Footer.startDictionariesVector(builder, dictionaries.size()); |
| 91 | + int dicsOffset = endVector(builder, dictionaries); |
| 92 | + Footer.startRecordBatchesVector(builder, recordBatches.size()); |
| 93 | + int rbsOffset = endVector(builder, recordBatches); |
| 94 | + Footer.startFooter(builder); |
| 95 | + Footer.addSchema(builder, schemaIndex); |
| 96 | + Footer.addDictionaries(builder, dicsOffset); |
| 97 | + Footer.addRecordBatches(builder, rbsOffset); |
| 98 | + return Footer.endFooter(builder); |
| 99 | + } |
| 100 | + |
| 101 | + private int endVector(FlatBufferBuilder builder, List<ArrowBlock> blocks) { |
| 102 | + for (ArrowBlock block : blocks) { |
| 103 | + block.writeTo(builder); |
| 104 | + } |
| 105 | + return builder.endVector(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int hashCode() { |
| 110 | + final int prime = 31; |
| 111 | + int result = 1; |
| 112 | + result = prime * result + ((dictionaries == null) ? 0 : dictionaries.hashCode()); |
| 113 | + result = prime * result + ((recordBatches == null) ? 0 : recordBatches.hashCode()); |
| 114 | + result = prime * result + ((schema == null) ? 0 : schema.hashCode()); |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean equals(Object obj) { |
| 120 | + if (this == obj) |
| 121 | + return true; |
| 122 | + if (obj == null) |
| 123 | + return false; |
| 124 | + if (getClass() != obj.getClass()) |
| 125 | + return false; |
| 126 | + ArrowFooter other = (ArrowFooter) obj; |
| 127 | + if (dictionaries == null) { |
| 128 | + if (other.dictionaries != null) |
| 129 | + return false; |
| 130 | + } else if (!dictionaries.equals(other.dictionaries)) |
| 131 | + return false; |
| 132 | + if (recordBatches == null) { |
| 133 | + if (other.recordBatches != null) |
| 134 | + return false; |
| 135 | + } else if (!recordBatches.equals(other.recordBatches)) |
| 136 | + return false; |
| 137 | + if (schema == null) { |
| 138 | + if (other.schema != null) |
| 139 | + return false; |
| 140 | + } else if (!schema.equals(other.schema)) |
| 141 | + return false; |
| 142 | + return true; |
| 143 | + } |
| 144 | +} |
0 commit comments