dengzhhu653 commented on code in PR #6935:
URL: https://github.com/apache/hive/pull/6935#discussion_r4688429935


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/metastore/PartitionsIterable.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.hadoop.hive.metastore.metastore;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.RawStore;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+import org.apache.hadoop.hive.metastore.client.builder.GetPartitionsArgs;
+import org.apache.hadoop.hive.metastore.metastore.iface.TableStore;
+
+/**
+ * PartitionsIterable - effectively a lazy 
Iterable<List<Partition>>
+ * Similar to {@link org.apache.hadoop.hive.metastore.PartitionIterable}, but
+ * each iteration step returns up to {@code batchSize} partitions. Because a 
metastore
+ * lookup by name can return fewer objects than names requested (missing 
partitions),
+ * the iterator may perform multiple fetches in one {@code next()} until the 
batch
+ * is full or partition names are exhausted.
+ * Note that remove() is an illegal call on this, and will result in an
+ * IllegalStateException.
+ */
+public class PartitionsIterable implements Iterable<List<Partition>> {
+
+  private final TableStore tableStore;
+  private final int batchSize;
+  private final TableName tableName;
+  private final List<String> partNames;
+
+  @Override
+  public Iterator<List<Partition>> iterator() {
+    if (partNames.isEmpty()) {
+      return Collections.emptyIterator();
+    }
+
+    return new Iterator<>() {
+      private final Iterator<String> partitionNamesIter = partNames.iterator();
+      private List<Partition> leftParts;
+
+      @Override
+      public boolean hasNext() {
+        return hasPendingPartitions() || partitionNamesIter.hasNext();
+      }
+
+      private boolean hasPendingPartitions() {
+        return leftParts != null && !leftParts.isEmpty();
+      }
+
+      @Override
+      public List<Partition> next() {
+        if (!hasNext()) {
+          throw new NoSuchElementException();
+        }
+        List<Partition> result = new ArrayList<>(batchSize);
+        if (hasPendingPartitions()) {
+          appendPartitions(result, leftParts);
+          leftParts = null;
+        }
+        while (result.size() < batchSize && partitionNamesIter.hasNext()) {
+          List<String> nameBatch = nextNameBatch();
+          appendPartitions(result, fetchBatch(nameBatch));
+        }
+        return result;
+      }

Review Comment:
   The motivation is the Metastore might drop the partitions during iterating 
over the partition names, it might always give us a "batchSize" number of 
partitions, and we use the returned partitions to perform some batched 
operations, such as "alterPartitions". In worst case it might return an empty 
partition list in the middle of iteration, which might surprise the caller. 
   



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


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

Reply via email to