@SuppressWarnings("unchecked")
 public void insertStringTuples(IIndexTestContext ctx, int numTuples, Random rnd)
     throws Exception {
   int fieldCount = ctx.getFieldCount();
   int numKeyFields = ctx.getKeyFieldCount();
   String[] fieldValues = new String[fieldCount];
   for (int i = 0; i < numTuples; i++) {
     if (LOGGER.isLoggable(Level.INFO)) {
       if ((i + 1) % (numTuples / Math.min(10, numTuples)) == 0) {
         LOGGER.info("Inserting Tuple " + (i + 1) + "/" + numTuples);
       }
     }
     // Set keys.
     for (int j = 0; j < numKeyFields; j++) {
       int length = (Math.abs(rnd.nextInt()) % 10) + 1;
       fieldValues[j] = getRandomString(length, rnd);
     }
     // Set values.
     for (int j = numKeyFields; j < fieldCount; j++) {
       fieldValues[j] = getRandomString(5, rnd);
     }
     TupleUtils.createTuple(
         ctx.getTupleBuilder(), ctx.getTuple(), ctx.getFieldSerdes(), (Object[]) fieldValues);
     try {
       ctx.getIndexAccessor().insert(ctx.getTuple());
       // Set expected values. Do this only after insertion succeeds
       // because we ignore duplicate keys.
       ctx.insertCheckTuple(
           createStringCheckTuple(fieldValues, ctx.getKeyFieldCount()), ctx.getCheckTuples());
     } catch (TreeIndexDuplicateKeyException e) {
       // Ignore duplicate key insertions.
     }
   }
 }
 @Override
 protected boolean checkDiskOrderScanResult(
     ITupleReference tuple, CheckTuple checkTuple, IIndexTestContext ctx)
     throws HyracksDataException {
   @SuppressWarnings("unchecked")
   TreeSet<CheckTuple> checkTuples = (TreeSet<CheckTuple>) ctx.getCheckTuples();
   CheckTuple matchingCheckTuple = checkTuples.floor(checkTuple);
   if (matchingCheckTuple == null) {
     return false;
   }
   compareActualAndExpected(tuple, matchingCheckTuple, ctx.getFieldSerdes());
   return true;
 }
  @SuppressWarnings("unchecked")
  public void insertSortedIntTuples(IIndexTestContext ctx, int numTuples, Random rnd)
      throws Exception {
    int fieldCount = ctx.getFieldCount();
    int numKeyFields = ctx.getKeyFieldCount();
    int[] fieldValues = new int[ctx.getFieldCount()];
    int maxValue = (int) Math.ceil(Math.pow(numTuples, 1.0 / (double) numKeyFields));
    Collection<CheckTuple> tmpCheckTuples = createCheckTuplesCollection();
    for (int i = 0; i < numTuples; i++) {
      // Set keys.
      setIntKeyFields(fieldValues, numKeyFields, maxValue, rnd);
      // Set values.
      setIntPayloadFields(fieldValues, numKeyFields, fieldCount);

      // Set expected values. (We also use these as the pre-sorted stream
      // for ordered indexes bulk loading).
      ctx.insertCheckTuple(
          createIntCheckTuple(fieldValues, ctx.getKeyFieldCount()), tmpCheckTuples);
    }
    insertCheckTuples(ctx, tmpCheckTuples);

    // Add tmpCheckTuples to ctx check tuples for comparing searches.
    for (CheckTuple checkTuple : tmpCheckTuples) {
      ctx.insertCheckTuple(checkTuple, ctx.getCheckTuples());
    }
  }
  public static void insertCheckTuples(IIndexTestContext ctx, Collection<CheckTuple> checkTuples)
      throws HyracksDataException, IndexException {
    int fieldCount = ctx.getFieldCount();
    int numTuples = checkTuples.size();
    ArrayTupleBuilder tupleBuilder = new ArrayTupleBuilder(fieldCount);
    ArrayTupleReference tuple = new ArrayTupleReference();

    int c = 1;
    for (CheckTuple checkTuple : checkTuples) {
      if (LOGGER.isLoggable(Level.INFO)) {
        if (c % (numTuples / 10) == 0) {
          LOGGER.info("Inserting Tuple " + c + "/" + numTuples);
        }
      }
      createTupleFromCheckTuple(checkTuple, tupleBuilder, tuple, ctx.getFieldSerdes());
      ctx.getIndexAccessor().insert(tuple);
      c++;
    }
  }
  @SuppressWarnings("unchecked")
  public void bulkLoadStringTuples(IIndexTestContext ctx, int numTuples, Random rnd)
      throws Exception {
    int fieldCount = ctx.getFieldCount();
    int numKeyFields = ctx.getKeyFieldCount();
    String[] fieldValues = new String[fieldCount];
    TreeSet<CheckTuple> tmpCheckTuples = new TreeSet<CheckTuple>();
    for (int i = 0; i < numTuples; i++) {
      // Set keys.
      for (int j = 0; j < numKeyFields; j++) {
        int length = (Math.abs(rnd.nextInt()) % 10) + 1;
        fieldValues[j] = getRandomString(length, rnd);
      }
      // Set values.
      for (int j = numKeyFields; j < fieldCount; j++) {
        fieldValues[j] = getRandomString(5, rnd);
      }
      // Set expected values. We also use these as the pre-sorted stream
      // for bulk loading.
      ctx.insertCheckTuple(
          createStringCheckTuple(fieldValues, ctx.getKeyFieldCount()), tmpCheckTuples);
    }
    bulkLoadCheckTuples(ctx, tmpCheckTuples);

    // Add tmpCheckTuples to ctx check tuples for comparing searches.
    for (CheckTuple checkTuple : tmpCheckTuples) {
      ctx.insertCheckTuple(checkTuple, ctx.getCheckTuples());
    }
  }
 @SuppressWarnings("unchecked")
 public void checkRangeSearch(
     IIndexTestContext ctx,
     ITupleReference lowKey,
     ITupleReference highKey,
     boolean lowKeyInclusive,
     boolean highKeyInclusive)
     throws Exception {
   if (LOGGER.isLoggable(Level.INFO)) {
     LOGGER.info("Testing Range Search.");
   }
   MultiComparator lowKeyCmp =
       BTreeUtils.getSearchMultiComparator(ctx.getComparatorFactories(), lowKey);
   MultiComparator highKeyCmp =
       BTreeUtils.getSearchMultiComparator(ctx.getComparatorFactories(), highKey);
   IIndexCursor searchCursor = ctx.getIndexAccessor().createSearchCursor(false);
   RangePredicate rangePred =
       new RangePredicate(
           lowKey, highKey, lowKeyInclusive, highKeyInclusive, lowKeyCmp, highKeyCmp);
   ctx.getIndexAccessor().search(searchCursor, rangePred);
   // Get the subset of elements from the expected set within given key
   // range.
   CheckTuple lowKeyCheck =
       createCheckTupleFromTuple(lowKey, ctx.getFieldSerdes(), lowKeyCmp.getKeyFieldCount());
   CheckTuple highKeyCheck =
       createCheckTupleFromTuple(highKey, ctx.getFieldSerdes(), highKeyCmp.getKeyFieldCount());
   SortedSet<CheckTuple> expectedSubset = null;
   if (lowKeyCmp.getKeyFieldCount() < ctx.getKeyFieldCount()
       || highKeyCmp.getKeyFieldCount() < ctx.getKeyFieldCount()) {
     // Searching on a key prefix (low key or high key or both).
     expectedSubset =
         getPrefixExpectedSubset(
             (TreeSet<CheckTuple>) ctx.getCheckTuples(), lowKeyCheck, highKeyCheck);
   } else {
     // Searching on all key fields.
     expectedSubset =
         ((TreeSet<CheckTuple>) ctx.getCheckTuples())
             .subSet(lowKeyCheck, lowKeyInclusive, highKeyCheck, highKeyInclusive);
   }
   Iterator<CheckTuple> checkIter = expectedSubset.iterator();
   int actualCount = 0;
   try {
     while (searchCursor.hasNext()) {
       if (!checkIter.hasNext()) {
         fail(
             "Range search returned more answers than expected.\nExpected: "
                 + expectedSubset.size());
       }
       searchCursor.next();
       CheckTuple expectedTuple = checkIter.next();
       ITupleReference tuple = searchCursor.getTuple();
       compareActualAndExpected(tuple, expectedTuple, ctx.getFieldSerdes());
       actualCount++;
     }
     if (actualCount < expectedSubset.size()) {
       fail(
           "Range search returned fewer answers than expected.\nExpected: "
               + expectedSubset.size()
               + "\nActual  : "
               + actualCount);
     }
   } finally {
     searchCursor.close();
   }
 }
 @Override
 protected ArrayTupleBuilder createDeleteTupleBuilder(IIndexTestContext ctx) {
   return new ArrayTupleBuilder(ctx.getKeyFieldCount());
 }