private void writeSearchResults(IFrameTupleAccessor leftAccessor, int tIndex) throws Exception {
    while (cursor.hasNext()) {
      tb.reset();
      cursor.next();

      ITupleReference frameTuple = cursor.getTuple();
      for (int i = 0; i < inputRecDesc.getFields().length; i++) {
        int tupleStart = leftAccessor.getTupleStartOffset(tIndex);
        int fieldStart = leftAccessor.getFieldStartOffset(tIndex, i);
        int offset = leftAccessor.getFieldSlotsLength() + tupleStart + fieldStart;
        int len = leftAccessor.getFieldEndOffset(tIndex, i) - fieldStart;
        dos.write(leftAccessor.getBuffer().array(), offset, len);
        tb.addFieldEndOffset();
      }
      for (int i = 0; i < frameTuple.getFieldCount(); i++) {
        dos.write(
            frameTuple.getFieldData(i), frameTuple.getFieldStart(i), frameTuple.getFieldLength(i));
        tb.addFieldEndOffset();
      }

      if (!appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize())) {
        FrameUtils.flushFrame(writeBuffer, writer);
        appender.reset(writeBuffer, true);
        if (!appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize())) {
          throw new IllegalStateException();
        }
      }
    }
  }
  private void loadPartitionInMem(int pid, RunFileWriter wr, int[] buffs)
      throws HyracksDataException {
    RunFileReader r = wr.createReader();
    r.open();
    int counter = 0;
    ByteBuffer mBuff = null;
    reloadBuffer.clear();
    while (r.nextFrame(reloadBuffer)) {
      mBuff = memBuffs[buffs[counter]];
      if (mBuff == null) {
        mBuff = ctx.allocateFrame();
        memBuffs[buffs[counter]] = mBuff;
      }
      FrameUtils.copy(reloadBuffer, mBuff);
      counter++;
      reloadBuffer.clear();
    }

    int curNext = nextBuff[buffs[buffs.length - 1]];
    nextBuff[buffs[buffs.length - 1]] = END_OF_PARTITION;
    nextFreeBuffIx = curNext;

    r.close();
    pStatus.set(pid, false);
    buildRFWriters[pid] = null;
  }
 @Override
 public boolean nextFrame(ByteBuffer buffer) throws HyracksDataException {
   int index = channelReader.findNextSender();
   if (index >= 0) {
     IInputChannel[] channels = channelReader.getChannels();
     ByteBuffer srcFrame = channels[index].getNextBuffer();
     FrameUtils.copy(srcFrame, buffer);
     channels[index].recycleBuffer(srcFrame);
     return true;
   }
   return false;
 }
 @Override
 public void close() throws HyracksDataException {
   try {
     if (appender.getTupleCount() > 0) {
       FrameUtils.flushFrame(writeBuffer, writer);
     }
     writer.close();
     try {
       cursor.close();
     } catch (Exception e) {
       throw new HyracksDataException(e);
     }
   } finally {
     treeIndexOpHelper.close();
   }
 }
  /** write the right result */
  private void writeRightResults(ITupleReference frameTuple) throws Exception {
    tb.reset();
    for (int i = 0; i < frameTuple.getFieldCount(); i++) {
      dos.write(
          frameTuple.getFieldData(i), frameTuple.getFieldStart(i), frameTuple.getFieldLength(i));
      tb.addFieldEndOffset();
    }

    if (!appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize())) {
      FrameUtils.flushFrame(writeBuffer, writer);
      appender.reset(writeBuffer, true);
      if (!appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize())) {
        throw new IllegalStateException();
      }
    }
  }
  /** write the left result */
  private void writeLeftResults(IFrameTupleAccessor leftAccessor, int tIndex) throws Exception {
    tb.reset();
    for (int i = 0; i < inputRecDesc.getFields().length; i++) {
      int tupleStart = leftAccessor.getTupleStartOffset(tIndex);
      int fieldStart = leftAccessor.getFieldStartOffset(tIndex, i);
      int offset = leftAccessor.getFieldSlotsLength() + tupleStart + fieldStart;
      int len = leftAccessor.getFieldEndOffset(tIndex, i) - fieldStart;
      dos.write(leftAccessor.getBuffer().array(), offset, len);
      tb.addFieldEndOffset();
    }

    if (!appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize())) {
      FrameUtils.flushFrame(writeBuffer, writer);
      appender.reset(writeBuffer, true);
      if (!appender.append(tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize())) {
        throw new IllegalStateException();
      }
    }
  }