LantaoJin commented on a change in pull request #24982: [SPARK-28181][CORE] Add 
a filter interface to KVStore to speed up the entities retrieve
URL: https://github.com/apache/spark/pull/24982#discussion_r298648264
 
 

 ##########
 File path: 
common/kvstore/src/main/java/org/apache/spark/util/kvstore/LevelDBFilterIterator.java
 ##########
 @@ -0,0 +1,102 @@
+/*
+ * 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.spark.util.kvstore;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.function.Predicate;
+
+class LevelDBFilterIterator<T> implements KVStoreIterator<T>  {
+
+  /** The LevelDBIterator being used */
+  private final LevelDBIterator<T> iterator;
+  private final Predicate<? super T> predicate;
+
+  /** The next object in the iteration */
+  private T nextObject;
+  /** Whether the next object has been calculated yet */
+  private boolean nextObjectSet = false;
+
+  LevelDBFilterIterator(LevelDBIterator<T> iterator, Predicate<T> predicate) {
+    this.iterator = iterator;
+    this.predicate = predicate;
+  }
+
+  /**
+   * Returns true if the underlying iterator contains an object that
+   * matches the predicate.
+   */
+  @Override
+  public boolean hasNext() {
+    return nextObjectSet || setNextObject();
+  }
+
+  /**
+   * Returns the next object that matches the predicate.
+   */
+  @Override
+  public T next() {
+    if (!nextObjectSet && !setNextObject()) {
+      throw new NoSuchElementException();
+    }
+    nextObjectSet = false;
+    return nextObject;
+  }
+
+  @Override
+  public List<T> next(int max) {
+    List<T> list = new ArrayList<>(max);
+    while (hasNext() && list.size() < max) {
+      list.add(next());
+    }
+    return list;
+  }
+
+  @Override
+  public boolean skip(long n) {
+    return iterator.skip(n);
 
 Review comment:
   > But, nit, I think you need to return a new iterator each time in the 
lambda here, not return `base` every time, technically.
   
   I thought that is what you mean. Return a new iterator. 
   

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


With regards,
Apache Git Services

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

Reply via email to