public void upsertStringTuples(IIndexTestContext ictx, int numTuples, Random rnd)
     throws Exception {
   OrderedIndexTestContext ctx = (OrderedIndexTestContext) ictx;
   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);
     ctx.getIndexAccessor().upsert(ctx.getTuple());
     ctx.upsertCheckTuple(
         createStringCheckTuple(fieldValues, ctx.getKeyFieldCount()), ctx.getCheckTuples());
   }
 }
 public void upsertIntTuples(IIndexTestContext ictx, int numTuples, Random rnd) throws Exception {
   OrderedIndexTestContext ctx = (OrderedIndexTestContext) ictx;
   int fieldCount = ctx.getFieldCount();
   int numKeyFields = ctx.getKeyFieldCount();
   int[] fieldValues = new int[ctx.getFieldCount()];
   // Scale range of values according to number of keys.
   // For example, for 2 keys we want the square root of numTuples, for 3
   // keys the cube root of numTuples, etc.
   int maxValue = (int) Math.ceil(Math.pow(numTuples, 1.0 / (double) numKeyFields));
   for (int i = 0; i < numTuples; i++) {
     // Set keys.
     setIntKeyFields(fieldValues, numKeyFields, maxValue, rnd);
     // Set values.
     setIntPayloadFields(fieldValues, numKeyFields, fieldCount);
     TupleUtils.createIntegerTuple(ctx.getTupleBuilder(), ctx.getTuple(), fieldValues);
     if (LOGGER.isLoggable(Level.INFO)) {
       if ((i + 1) % (numTuples / Math.min(10, numTuples)) == 0) {
         LOGGER.info("Inserting Tuple " + (i + 1) + "/" + numTuples);
       }
     }
     ctx.getIndexAccessor().upsert(ctx.getTuple());
     ctx.upsertCheckTuple(
         createIntCheckTuple(fieldValues, ctx.getKeyFieldCount()), ctx.getCheckTuples());
   }
 }
 @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.
     }
   }
 }
 public static String printFrameTuples(
     ITreeIndexFrame frame, ISerializerDeserializer[] fieldSerdes) throws HyracksDataException {
   StringBuilder strBuilder = new StringBuilder();
   ITreeIndexTupleReference tuple = frame.createTupleReference();
   for (int i = 0; i < frame.getTupleCount(); i++) {
     tuple.resetByTupleIndex(frame, i);
     String tupleString = TupleUtils.printTuple(tuple, fieldSerdes);
     strBuilder.append(tupleString);
     if (i != frame.getTupleCount() - 1) {
       strBuilder.append(" | ");
     }
   }
   return strBuilder.toString();
 }
  protected void checkPriorityQueue() throws HyracksDataException, IndexException {
    while (!outputPriorityQueue.isEmpty() || needPush == true) {
      if (!outputPriorityQueue.isEmpty()) {
        PriorityQueueElement checkElement = outputPriorityQueue.peek();
        if (proceed && !searchCallback.proceed(checkElement.getTuple())) {
          if (includeMemComponent) {
            PriorityQueueElement inMemElement = null;
            boolean inMemElementFound = false;
            // scan the PQ for the in-memory component's element
            Iterator<PriorityQueueElement> it = outputPriorityQueue.iterator();
            while (it.hasNext()) {
              inMemElement = it.next();
              if (inMemElement.getCursorIndex() == 0) {
                inMemElementFound = true;
                it.remove();
                break;
              }
            }
            if (inMemElementFound) {
              // copy the in-mem tuple
              if (tupleBuilder == null) {
                tupleBuilder = new ArrayTupleBuilder(cmp.getKeyFieldCount());
              }
              TupleUtils.copyTuple(tupleBuilder, inMemElement.getTuple(), cmp.getKeyFieldCount());
              copyTuple.reset(tupleBuilder.getFieldEndOffsets(), tupleBuilder.getByteArray());

              // unlatch/unpin
              rangeCursors[0].reset();

              // reconcile
              if (checkElement.getCursorIndex() == 0) {
                searchCallback.reconcile(copyTuple);
              } else {
                searchCallback.reconcile(checkElement.getTuple());
              }
              // retraverse
              reusablePred.setLowKey(copyTuple, true);
              memBTreeAccessor.search(rangeCursors[0], reusablePred);
              pushIntoPriorityQueue(inMemElement);
              if (cmp.compare(copyTuple, inMemElement.getTuple()) != 0) {
                searchCallback.cancel(copyTuple);
                continue;
              }
            } else {
              // the in-memory cursor is exhausted
              searchCallback.reconcile(checkElement.getTuple());
            }
          } else {
            searchCallback.reconcile(checkElement.getTuple());
          }
        }
        // If there is no previous tuple or the previous tuple can be ignored
        if (outputElement == null) {
          if (isDeleted(checkElement)) {
            // If the key has been deleted then pop it and set needPush to true.
            // We cannot push immediately because the tuple may be
            // modified if hasNext() is called
            outputElement = outputPriorityQueue.poll();
            searchCallback.cancel(checkElement.getTuple());
            needPush = true;
            proceed = false;
          } else {
            break;
          }
        } else {
          // Compare the previous tuple and the head tuple in the PQ
          if (compare(cmp, outputElement.getTuple(), checkElement.getTuple()) == 0) {
            // If the previous tuple and the head tuple are
            // identical
            // then pop the head tuple and push the next tuple from
            // the tree of head tuple

            // the head element of PQ is useless now
            PriorityQueueElement e = outputPriorityQueue.poll();
            pushIntoPriorityQueue(e);
          } else {
            // If the previous tuple and the head tuple are different
            // the info of previous tuple is useless
            if (needPush == true) {
              pushIntoPriorityQueue(outputElement);
              needPush = false;
            }
            proceed = true;
            outputElement = null;
          }
        }
      } else {
        // the priority queue is empty and needPush
        pushIntoPriorityQueue(outputElement);
        needPush = false;
        outputElement = null;
        proceed = true;
      }
    }
  }