Skip to content

Commit

Permalink
Core: Common metadata for TableMetadata and ViewMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nk1506 committed Feb 8, 2024
1 parent 9f979a1 commit d4198ef
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 32 deletions.
61 changes: 61 additions & 0 deletions core/src/main/java/org/apache/iceberg/BaseMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.iceberg;

import java.io.Serializable;
import java.util.Map;
import org.apache.iceberg.util.PropertyUtil;
import org.immutables.value.Value;
import javax.annotation.Nullable;

/**
* A base class for {@link TableMetadata} and {@link org.apache.iceberg.view.ViewMetadata} It
* abstracts out all the common metadata for table ane view.
*/
public interface BaseMetadata extends Serializable {

@Value.Parameter(order = 1003)
String location();

@Value.Parameter(order = 1008)
Map<String, String> properties();

@Value.Parameter(order = 1010)
@Nullable
String metadataFileLocation();

@Value.Parameter(order = 1011)
Schema schema();

default String property(String property, String defaultValue) {
return properties().getOrDefault(property, defaultValue);
}

default boolean propertyAsBoolean(String property, boolean defaultValue) {
return PropertyUtil.propertyAsBoolean(properties(), property, defaultValue);
}

default int propertyAsInt(String property, int defaultValue) {
return PropertyUtil.propertyAsInt(properties(), property, defaultValue);
}

default long propertyAsLong(String property, long defaultValue) {
return PropertyUtil.propertyAsLong(properties(), property, defaultValue);
}
}
23 changes: 5 additions & 18 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.iceberg;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -47,7 +46,7 @@
import org.apache.iceberg.util.SerializableSupplier;

/** Metadata for a table. */
public class TableMetadata implements Serializable {
public class TableMetadata implements BaseMetadata {
static final long INITIAL_SEQUENCE_NUMBER = 0;
static final long INVALID_SEQUENCE_NUMBER = -1;
static final int DEFAULT_TABLE_FORMAT_VERSION = 2;
Expand Down Expand Up @@ -390,6 +389,7 @@ public int formatVersion() {
return formatVersion;
}

@Override
public String metadataFileLocation() {
return metadataFileLocation;
}
Expand All @@ -414,6 +414,7 @@ public int lastColumnId() {
return lastColumnId;
}

@Override
public Schema schema() {
return schemasById.get(currentSchemaId);
}
Expand Down Expand Up @@ -470,30 +471,16 @@ public Map<Integer, SortOrder> sortOrdersById() {
return sortOrdersById;
}

@Override
public String location() {
return location;
}

@Override
public Map<String, String> properties() {
return properties;
}

public String property(String property, String defaultValue) {
return properties.getOrDefault(property, defaultValue);
}

public boolean propertyAsBoolean(String property, boolean defaultValue) {
return PropertyUtil.propertyAsBoolean(properties, property, defaultValue);
}

public int propertyAsInt(String property, int defaultValue) {
return PropertyUtil.propertyAsInt(properties, property, defaultValue);
}

public long propertyAsLong(String property, long defaultValue) {
return PropertyUtil.propertyAsLong(properties, property, defaultValue);
}

public Snapshot snapshot(long snapshotId) {
if (!snapshotsById.containsKey(snapshotId)) {
ensureSnapshotsLoaded();
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/java/org/apache/iceberg/view/ViewMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.iceberg.view;

import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand All @@ -27,7 +26,7 @@
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.iceberg.BaseMetadata;
import org.apache.iceberg.MetadataUpdate;
import org.apache.iceberg.Schema;
import org.apache.iceberg.exceptions.ValidationException;
Expand All @@ -44,17 +43,11 @@
@SuppressWarnings("ImmutablesStyle")
@Value.Immutable(builder = false)
@Value.Style(allParameters = true, visibilityString = "PACKAGE")
public interface ViewMetadata extends Serializable {
public interface ViewMetadata extends BaseMetadata {
Logger LOG = LoggerFactory.getLogger(ViewMetadata.class);
int SUPPORTED_VIEW_FORMAT_VERSION = 1;
int DEFAULT_VIEW_FORMAT_VERSION = 1;

String uuid();

int formatVersion();

String location();

default Integer currentSchemaId() {
// fail when accessing the current schema if ViewMetadata was created through the
// ViewMetadataParser with an invalid schema id
Expand All @@ -68,21 +61,27 @@ default Integer currentSchemaId() {
return currentSchemaId;
}

@Value.Parameter(order = 1001)
String uuid();

@Value.Parameter(order = 1002)
int formatVersion();

@Value.Parameter(order = 1004)
List<Schema> schemas();

@Value.Parameter(order = 1005)
int currentVersionId();

@Value.Parameter(order = 1006)
List<ViewVersion> versions();

@Value.Parameter(order = 1007)
List<ViewHistoryEntry> history();

Map<String, String> properties();

@Value.Parameter(order = 1009)
List<MetadataUpdate> changes();

@Nullable
String metadataFileLocation();

default ViewVersion version(int versionId) {
return versionsById().get(versionId);
}
Expand Down Expand Up @@ -119,6 +118,7 @@ default Map<Integer, Schema> schemasById() {
return builder.build();
}

@Override
default Schema schema() {
return schemasById().get(currentSchemaId());
}
Expand Down

0 comments on commit d4198ef

Please sign in to comment.