Github user jsoltren commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17902#discussion_r118757763
  
    --- Diff: 
common/kvstore/src/test/java/org/apache/spark/kvstore/DBIteratorSuite.java ---
    @@ -0,0 +1,498 @@
    +/*
    + * 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.kvstore;
    +
    +import java.util.Arrays;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.Comparator;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Random;
    +
    +import com.google.common.base.Predicate;
    +import com.google.common.collect.Iterables;
    +import com.google.common.collect.Iterators;
    +import com.google.common.collect.Lists;
    +import org.apache.commons.io.FileUtils;
    +import org.junit.AfterClass;
    +import org.junit.Before;
    +import org.junit.Test;
    +import static org.junit.Assert.*;
    +
    +public abstract class DBIteratorSuite {
    +
    +  private static final int MIN_ENTRIES = 42;
    +  private static final int MAX_ENTRIES = 1024;
    +  private static final Random RND = new Random();
    +
    +  private static List<CustomType1> allEntries;
    +  private static List<CustomType1> clashingEntries;
    +  private static KVStore db;
    +
    +  private static interface BaseComparator extends Comparator<CustomType1> {
    +    /**
    +     * Returns a comparator that falls back to natural order if this 
comparator's ordering
    +     * returns equality for two elements. Used to mimic how the index 
sorts things internally.
    +     */
    +    default BaseComparator fallback() {
    +      return (t1, t2) -> {
    +        int result = BaseComparator.this.compare(t1, t2);
    +        if (result != 0) {
    +          return result;
    +        }
    +
    +        return t1.key.compareTo(t2.key);
    +      };
    +    }
    +
    +    /** Reverses the order of this comparator. */
    +    default BaseComparator reverse() {
    +      return (t1, t2) -> -BaseComparator.this.compare(t1, t2);
    +    }
    +  }
    +
    +  private static final BaseComparator NATURAL_ORDER = (t1, t2) -> 
t1.key.compareTo(t2.key);
    +  private static final BaseComparator REF_INDEX_ORDER = (t1, t2) -> 
t1.id.compareTo(t2.id);
    +  private static final BaseComparator COPY_INDEX_ORDER = (t1, t2) -> 
t1.name.compareTo(t2.name);
    +  private static final BaseComparator NUMERIC_INDEX_ORDER = (t1, t2) -> 
t1.num - t2.num;
    +  private static final BaseComparator CHILD_INDEX_ORDER = (t1, t2) -> 
t1.child.compareTo(t2.child);
    +
    +  /**
    +   * Implementations should override this method; it is called only once, 
before all tests are
    +   * run. Any state can be safely stored in static variables and cleaned 
up in a @AfterClass
    +   * handler.
    +   */
    +  protected abstract KVStore createStore() throws Exception;
    +
    +  @AfterClass
    +  public static void cleanupData() throws Exception {
    +    allEntries = null;
    +    db = null;
    +  }
    +
    +  @Before
    +  public void setup() throws Exception {
    +    if (db != null) {
    +      return;
    +    }
    +
    +    db = createStore();
    +
    +    int count = RND.nextInt(MAX_ENTRIES) + MIN_ENTRIES;
    +
    +    // Instead of generating sequential IDs, generate random unique IDs to 
avoid the insertion
    +    // order matching the natural ordering. Just in case.
    +    boolean[] usedIDs = new boolean[count];
    +
    +    allEntries = new ArrayList<>(count);
    +    for (int i = 0; i < count; i++) {
    +      CustomType1 t = new CustomType1();
    +
    +      int id;
    +      do {
    +        id = RND.nextInt(count);
    +      } while (usedIDs[id]);
    +
    +      usedIDs[id] = true;
    +      t.key = "key" + id;
    +      t.id = "id" + i;
    +      t.name = "name" + RND.nextInt(MAX_ENTRIES);
    +      t.num = RND.nextInt(MAX_ENTRIES);
    +      t.child = "child" + (i % MIN_ENTRIES);
    +      allEntries.add(t);
    +      db.write(t);
    +    }
    +
    +    // Pick the first generated value, and forcefully create a few entries 
that will clash
    +    // with the indexed values (id and name), to make sure the index 
behaves correctly when
    +    // multiple entities are indexed by the same value.
    +    //
    +    // This also serves as a test for the test code itself, to make sure 
it's sorting indices
    +    // the same way the store is expected to.
    +    CustomType1 first = allEntries.get(0);
    +    clashingEntries = new ArrayList<>();
    +    for (int i = 0; i < RND.nextInt(MIN_ENTRIES) + 1; i++) {
    --- End diff --
    
    Why the + 1? Does it have special meaning or are you trying to avoid <= ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to