You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/07/19 04:04:15 UTC

[GitHub] [iceberg] singhpk234 commented on a diff in pull request #5063: Core: Add MetadataLog metadata table

singhpk234 commented on code in PR #5063:
URL: https://github.com/apache/iceberg/pull/5063#discussion_r924038523


##########
core/src/main/java/org/apache/iceberg/MetadataLogTable.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.util.List;
+import java.util.function.Function;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.SnapshotUtil;
+
+public class MetadataLogTable extends BaseMetadataTable {
+
+  private static final Schema METADATA_LOG_SCHEMA = new Schema(
+      Types.NestedField.required(1, "timestamp_millis", Types.LongType.get()),
+      Types.NestedField.required(2, "file", Types.StringType.get()),
+      Types.NestedField.optional(3, "latest_snapshot_id", Types.LongType.get())
+  );
+
+  MetadataLogTable(TableOperations ops, Table table) {
+    this(ops, table, table.name() + ".metadata_log");
+  }
+
+  MetadataLogTable(TableOperations ops, Table table, String name) {
+    super(ops, table, name);
+  }
+
+  @Override
+  MetadataTableType metadataTableType() {
+    return MetadataTableType.METADATA_LOG;
+  }
+
+  @Override
+  public TableScan newScan() {
+    return new MetadataLogScan(operations(), table());
+  }
+
+  @Override
+  public Schema schema() {
+    return METADATA_LOG_SCHEMA;
+  }
+
+  private DataTask task(TableScan scan) {
+    TableOperations ops = operations();
+    List<TableMetadata.MetadataLogEntry> metadataLogEntries =
+        Lists.newArrayList(ops.current().previousFiles().listIterator());
+    metadataLogEntries.add(new TableMetadata.MetadataLogEntry(ops.current().lastUpdatedMillis(),
+        ops.current().metadataFileLocation()));
+    return StaticDataTask.of(
+        ops.io().newInputFile(ops.current().metadataFileLocation()),
+        schema(),
+        scan.schema(),
+        metadataLogEntries,
+        convertMetadataLogEntryFunc(table())
+    );
+  }
+
+  private class MetadataLogScan extends StaticTableScan {
+    MetadataLogScan(TableOperations ops, Table table) {
+      super(ops, table, METADATA_LOG_SCHEMA, MetadataTableType.METADATA_LOG, MetadataLogTable.this::task);
+    }
+
+    MetadataLogScan(TableOperations ops, Table table, TableScanContext context) {
+      super(ops, table, METADATA_LOG_SCHEMA, MetadataTableType.METADATA_LOG, MetadataLogTable.this::task, context);
+    }
+
+    @Override
+    protected TableScan newRefinedScan(TableOperations ops, Table table, Schema schema, TableScanContext context) {
+      return new MetadataLogScan(ops, table, context);
+    }
+
+    @Override
+    public CloseableIterable<FileScanTask> planFiles() {
+      return CloseableIterable.withNoopClose(MetadataLogTable.this.task(this));
+    }
+  }
+
+  private static Function<TableMetadata.MetadataLogEntry, StaticDataTask.Row> convertMetadataLogEntryFunc(Table table) {
+    return metadataLogEntry -> {
+      long latestSnapshotId;
+      try {
+        latestSnapshotId = SnapshotUtil.snapshotIdAsOfTime(table, metadataLogEntry.timestampMillis());
+      } catch (Exception e) {
+        // implies this metadata file was created at table creation
+        latestSnapshotId = -1;
+      }
+
+      return StaticDataTask.Row.of(
+          metadataLogEntry.timestampMillis() * 1000,
+          metadataLogEntry.file(),
+          // latest snapshot in this file corresponding to the log entry
+          latestSnapshotId == -1 ? null : latestSnapshotId

Review Comment:
   done, thanks !



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org