|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.puffin; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.StringWriter; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; |
| 30 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; |
| 31 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; |
| 32 | +import org.apache.iceberg.util.JsonUtil; |
| 33 | + |
| 34 | +public final class FileMetadataParser { |
| 35 | + |
| 36 | + private FileMetadataParser() { |
| 37 | + } |
| 38 | + |
| 39 | + private static final String BLOBS = "blobs"; |
| 40 | + private static final String PROPERTIES = "properties"; |
| 41 | + |
| 42 | + private static final String TYPE = "type"; |
| 43 | + private static final String FIELDS = "fields"; |
| 44 | + private static final String OFFSET = "offset"; |
| 45 | + private static final String LENGTH = "length"; |
| 46 | + private static final String COMPRESSION_CODEC = "compression-codec"; |
| 47 | + |
| 48 | + public static String toJson(FileMetadata fileMetadata, boolean pretty) { |
| 49 | + try { |
| 50 | + StringWriter writer = new StringWriter(); |
| 51 | + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); |
| 52 | + if (pretty) { |
| 53 | + generator.useDefaultPrettyPrinter(); |
| 54 | + } |
| 55 | + toJson(fileMetadata, generator); |
| 56 | + generator.flush(); |
| 57 | + return writer.toString(); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new UncheckedIOException("Failed to write json for: " + fileMetadata, e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static FileMetadata fromJson(String json) { |
| 64 | + try { |
| 65 | + return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class)); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new UncheckedIOException(e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static FileMetadata fromJson(JsonNode json) { |
| 72 | + return fileMetadataFromJson(json); |
| 73 | + } |
| 74 | + |
| 75 | + static void toJson(FileMetadata fileMetadata, JsonGenerator generator) throws IOException { |
| 76 | + generator.writeStartObject(); |
| 77 | + |
| 78 | + generator.writeArrayFieldStart(BLOBS); |
| 79 | + for (BlobMetadata blobMetadata : fileMetadata.blobs()) { |
| 80 | + toJson(blobMetadata, generator); |
| 81 | + } |
| 82 | + generator.writeEndArray(); |
| 83 | + |
| 84 | + generator.writeObjectFieldStart(PROPERTIES); |
| 85 | + for (Map.Entry<String, String> entry : fileMetadata.properties().entrySet()) { |
| 86 | + generator.writeStringField(entry.getKey(), entry.getValue()); |
| 87 | + } |
| 88 | + generator.writeEndObject(); |
| 89 | + |
| 90 | + generator.writeEndObject(); |
| 91 | + } |
| 92 | + |
| 93 | + static FileMetadata fileMetadataFromJson(JsonNode json) { |
| 94 | + |
| 95 | + ImmutableList.Builder<BlobMetadata> blobs = ImmutableList.builder(); |
| 96 | + JsonNode blobsJson = json.get(BLOBS); |
| 97 | + Preconditions.checkArgument(blobsJson != null && blobsJson.isArray(), |
| 98 | + "Cannot parse blobs from non-array: %s", blobsJson); |
| 99 | + for (JsonNode blobJson : blobsJson) { |
| 100 | + blobs.add(blobMetadataFromJson(blobJson)); |
| 101 | + } |
| 102 | + |
| 103 | + Map<String, String> properties = ImmutableMap.of(); |
| 104 | + JsonNode propertiesJson = json.get(PROPERTIES); |
| 105 | + if (propertiesJson != null) { |
| 106 | + properties = JsonUtil.getStringMap(PROPERTIES, json); |
| 107 | + } |
| 108 | + |
| 109 | + return new FileMetadata( |
| 110 | + blobs.build(), |
| 111 | + properties); |
| 112 | + } |
| 113 | + |
| 114 | + static void toJson(BlobMetadata blobMetadata, JsonGenerator generator) throws IOException { |
| 115 | + generator.writeStartObject(); |
| 116 | + |
| 117 | + generator.writeStringField(TYPE, blobMetadata.type()); |
| 118 | + |
| 119 | + generator.writeArrayFieldStart(FIELDS); |
| 120 | + for (int field : blobMetadata.inputFields()) { |
| 121 | + generator.writeNumber(field); |
| 122 | + } |
| 123 | + generator.writeEndArray(); |
| 124 | + |
| 125 | + generator.writeNumberField(OFFSET, blobMetadata.offset()); |
| 126 | + generator.writeNumberField(LENGTH, blobMetadata.length()); |
| 127 | + |
| 128 | + if (blobMetadata.compressionCodec() != null) { |
| 129 | + generator.writeStringField(COMPRESSION_CODEC, blobMetadata.compressionCodec()); |
| 130 | + } |
| 131 | + |
| 132 | + if (!blobMetadata.properties().isEmpty()) { |
| 133 | + generator.writeObjectFieldStart(PROPERTIES); |
| 134 | + for (Map.Entry<String, String> entry : blobMetadata.properties().entrySet()) { |
| 135 | + generator.writeStringField(entry.getKey(), entry.getValue()); |
| 136 | + } |
| 137 | + generator.writeEndObject(); |
| 138 | + } |
| 139 | + |
| 140 | + generator.writeEndObject(); |
| 141 | + } |
| 142 | + |
| 143 | + static BlobMetadata blobMetadataFromJson(JsonNode json) { |
| 144 | + String type = JsonUtil.getString(TYPE, json); |
| 145 | + List<Integer> fields = JsonUtil.getIntegerList(FIELDS, json); |
| 146 | + long offset = JsonUtil.getLong(OFFSET, json); |
| 147 | + long length = JsonUtil.getLong(LENGTH, json); |
| 148 | + String compressionCodec = JsonUtil.getStringOrNull(COMPRESSION_CODEC, json); |
| 149 | + Map<String, String> properties = ImmutableMap.of(); |
| 150 | + JsonNode propertiesJson = json.get(PROPERTIES); |
| 151 | + if (propertiesJson != null) { |
| 152 | + properties = JsonUtil.getStringMap(PROPERTIES, json); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + return new BlobMetadata( |
| 157 | + type, |
| 158 | + fields, |
| 159 | + offset, |
| 160 | + length, |
| 161 | + compressionCodec, |
| 162 | + properties); |
| 163 | + } |
| 164 | +} |
0 commit comments