aokolnychyi commented on a change in pull request #2362:
URL: https://github.com/apache/iceberg/pull/2362#discussion_r601060077



##########
File path: core/src/main/java/org/apache/iceberg/SerializedTable.java
##########
@@ -0,0 +1,290 @@
+/*
+ * 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.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.encryption.EncryptionManager;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.LocationProvider;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+
+public class SerializedTable implements Table, Serializable {
+  private final String name;
+  private final String location;
+  private final String metadataFileLocation;
+  private final Map<String, String> properties;
+  private final Schema schema;
+  private final int defaultSpecId;
+  private final Map<Integer, PartitionSpec> specs;
+  private final int defaultSortOrderId;
+  private final Map<Integer, SortOrder> sortOrders;
+  private final FileIO io;
+  private final EncryptionManager encryption;
+  private final LocationProvider locationProvider;
+
+  // lazy vars loaded on demand
+  private transient volatile Table lazyTable = null;
+
+  public SerializedTable(Table table) {
+    this(table, table.io());
+  }
+
+  public SerializedTable(Table table, FileIO io) {
+    this.name = table.name();
+    this.location = table.location();
+    this.metadataFileLocation = metadataFileLocation(table);
+    this.properties = copy(table.properties());
+    this.schema = table.schema();
+    this.defaultSpecId = table.spec().specId();
+    this.specs = copy(table.specs());
+    this.defaultSortOrderId = table.sortOrder().orderId();
+    this.sortOrders = copy(table.sortOrders());
+    this.io = io;
+    this.encryption = table.encryption();
+    this.locationProvider = table.locationProvider();
+  }
+
+  // needed for serialization
+  public SerializedTable(String name, String location, String 
metadataFileLocation,
+                         Map<String, String> properties, Schema schema,
+                         int defaultSpecId, Map<Integer, PartitionSpec> specs,
+                         int defaultSortOrderId, Map<Integer, SortOrder> 
sortOrders,
+                         FileIO io, EncryptionManager encryption, 
LocationProvider locationProvider) {
+    this.name = name;
+    this.location = location;
+    this.metadataFileLocation = metadataFileLocation;
+    this.properties = properties;
+    this.schema = schema;
+    this.defaultSpecId = defaultSpecId;
+    this.specs = specs;
+    this.defaultSortOrderId = defaultSortOrderId;
+    this.sortOrders = sortOrders;
+    this.io = io;
+    this.encryption = encryption;
+    this.locationProvider = locationProvider;
+  }
+
+  private Table lazyTable() {
+    if (lazyTable == null) {
+      synchronized (this) {
+        if (lazyTable == null) {
+          TableOperations ops = new 
StaticTableOperations(metadataFileLocation, io, locationProvider);
+          this.lazyTable = newTable(ops, name);
+        }
+      }
+    }
+
+    return lazyTable;
+  }
+
+  protected Table newTable(TableOperations ops, String tableName) {
+    return new BaseTable(ops, tableName);
+  }
+
+  private String metadataFileLocation(Table table) {
+    if (table instanceof HasTableOperations) {
+      TableOperations ops = ((HasTableOperations) table).operations();
+      return ops.current().metadataFileLocation();
+
+    } else if (table instanceof BaseMetadataTable) {
+      return ((BaseMetadataTable) table).metadataLocation();
+
+    } else {
+      throw new IllegalArgumentException("Cannot determine metadata file 
location for table " + table.name());
+    }
+  }
+
+  private <K, V> Map<K, V> copy(Map<K, V> map) {
+    Map<K, V> copy = Maps.newHashMap(map);
+    return Collections.unmodifiableMap(copy);
+  }
+
+  @Override
+  public String name() {
+    return name;
+  }
+
+  @Override
+  public String location() {
+    return location;
+  }
+
+  @Override
+  public Map<String, String> properties() {
+    return properties;
+  }
+
+  @Override
+  public Schema schema() {
+    return schema;
+  }
+
+  @Override
+  public PartitionSpec spec() {
+    return specs.get(defaultSpecId);
+  }
+
+  @Override
+  public Map<Integer, PartitionSpec> specs() {
+    return specs;
+  }
+
+  @Override
+  public SortOrder sortOrder() {
+    return sortOrders.get(defaultSortOrderId);
+  }
+
+  @Override
+  public Map<Integer, SortOrder> sortOrders() {
+    return sortOrders;
+  }
+
+  @Override
+  public FileIO io() {
+    return io;
+  }
+
+  @Override
+  public EncryptionManager encryption() {
+    return encryption;
+  }
+
+  @Override
+  public LocationProvider locationProvider() {
+    return locationProvider;
+  }
+
+  @Override
+  public void refresh() {
+    throw new UnsupportedOperationException(errorMsg("refresh"));
+  }
+
+  @Override
+  public TableScan newScan() {
+    return lazyTable().newScan();

Review comment:
       Should support scans now.




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to