|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.parquet.variant; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | + |
| 21 | +/** |
| 22 | + * Builder for creating Variant arrays, used by VariantBuilder. |
| 23 | + */ |
| 24 | +public class VariantArrayBuilder extends VariantBuilder { |
| 25 | + /** The parent VariantBuilder. */ |
| 26 | + private final VariantBuilder parent; |
| 27 | + /** The offsets of the elements in this array. */ |
| 28 | + private final ArrayList<Integer> offsets; |
| 29 | + /** The number of values appended to this array. */ |
| 30 | + protected long numValues = 0; |
| 31 | + |
| 32 | + VariantArrayBuilder(VariantBuilder parent) { |
| 33 | + this.parent = parent; |
| 34 | + this.offsets = new ArrayList<>(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return the list of element offsets in this array |
| 39 | + */ |
| 40 | + ArrayList<Integer> validateAndGetOffsets() { |
| 41 | + if (offsets.size() != numValues) { |
| 42 | + throw new IllegalStateException(String.format( |
| 43 | + "Number of offsets (%d) do not match the number of values (%d).", offsets.size(), numValues)); |
| 44 | + } |
| 45 | + checkMultipleNested("Cannot call endArray() while a nested object/array is still open."); |
| 46 | + return offsets; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void onAppend() { |
| 51 | + checkAppendWhileNested(); |
| 52 | + offsets.add(writePos); |
| 53 | + numValues++; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void onStartNested() { |
| 58 | + checkMultipleNested("Cannot call startObject()/startArray() without calling endObject()/endArray() first."); |
| 59 | + offsets.add(writePos); |
| 60 | + numValues++; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + int addDictionaryKey(String key) { |
| 65 | + // Add to the parent dictionary. |
| 66 | + return parent.addDictionaryKey(key); |
| 67 | + } |
| 68 | +} |
0 commit comments