varunarya002 commented on code in PR #3996:
URL: https://github.com/apache/polaris/pull/3996#discussion_r3040967749


##########
runtime/service/src/main/java/org/apache/polaris/service/storage/gcs/GcsStorageLocationPreparer.java:
##########
@@ -0,0 +1,367 @@
+/*
+ * 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.polaris.service.storage.gcs;
+
+import com.google.api.gax.rpc.AlreadyExistsException;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.cloud.storage.Bucket;
+import com.google.cloud.storage.BucketInfo;
+import com.google.cloud.storage.Storage;
+import com.google.cloud.storage.StorageOptions;
+import com.google.storage.control.v2.BucketName;
+import com.google.storage.control.v2.CreateFolderRequest;
+import com.google.storage.control.v2.StorageControlClient;
+import com.google.storage.control.v2.StorageControlSettings;
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.iceberg.TableProperties;
+import org.apache.polaris.core.entity.table.IcebergTableLikeEntity;
+import 
org.apache.polaris.core.storage.PolarisStorageConfigurationInfo.StorageType;
+import org.apache.polaris.core.storage.StorageUtil;
+import org.apache.polaris.service.storage.StorageLocationPreparer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * GCS implementation of {@link StorageLocationPreparer}. Detects whether the 
target bucket uses
+ * Hierarchical Namespace (HNS) and, if so, creates the full folder hierarchy 
required before
+ * Iceberg metadata and data files can be written. For flat-namespace buckets, 
this is a no-op.
+ *
+ * <p>The main flow uses an {@link Optional} pipeline pattern to replace 
imperative guard clauses
+ * with a declarative chain: parse location -> resolve HNS status -> create 
folders.
+ */
+public class GcsStorageLocationPreparer implements StorageLocationPreparer {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(GcsStorageLocationPreparer.class);
+  private static final String GCS_SCHEME_PREFIX = 
StorageType.GCS.getPrefixes().getFirst();
+  private static final Predicate<String> NOT_BLANK = 
Predicate.not(String::isEmpty);
+  private static final String PATH_SEPARATOR = "/";
+  private static final String TRAILING_SLASHES_REGEX = "/+$";
+  private static final String DEFAULT_METADATA_FOLDER = "metadata";
+  private static final String DEFAULT_DATA_FOLDER = "data";
+  private static final String GCS_STORAGE_LAYOUT = "_";
+
+  private final Supplier<GoogleCredentials> credentialsSupplier;
+
+  /** Represents a single folder that needs to be created, with its bucket and 
path. */
+  private record FolderToCreate(String bucketName, String folderPath) {}
+
+  public GcsStorageLocationPreparer(Supplier<GoogleCredentials> 
credentialsSupplier) {
+    this.credentialsSupplier = credentialsSupplier;
+  }
+
+  // ── Main Pipeline 
──────────────────────────────────────────────────────────
+
+  @Override
+  public void prepareTableLocation(String tableLocation, Map<String, String> 
tableProperties) {
+    if (tableLocation == null || !tableLocation.startsWith(GCS_SCHEME_PREFIX)) 
{
+      return;
+    }
+
+    List<FolderToCreate> allFolders = resolveAllFoldersToCreate(tableLocation, 
tableProperties);
+    if (allFolders.isEmpty()) {
+      return;
+    }
+
+    // Group folders by bucket and create only for HNS-enabled buckets
+    allFolders.stream()
+        .collect(Collectors.groupingBy(FolderToCreate::bucketName))
+        .forEach(this::createFoldersForBucketIfHnsEnabled);

Review Comment:
   For unpartitioned tables it works fine, but for partitioned table it does 
not create deeper structures like partition subdirectories (e.g., 
data/year=2024/month=01/). On HNS enabled buckets, if Iceberg clients need to 
create partition folders at write time, they'd need folder creation permissions 
in the downscoped token.



-- 
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: [email protected]

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

Reply via email to