void setTransactionRowLookups(DoubleIntIndex pointerLookup) {

    for (int i = 0, size = transactionRowLookup.size(); i < size; i++) {
      int key = transactionRowLookup.getKey(i);
      int lookupIndex = pointerLookup.findFirstEqualKeyIndex(key);

      if (lookupIndex != -1) {
        transactionRowLookup.setValue(i, pointerLookup.getValue(lookupIndex));
      }
    }
  }
  /**
   * Return a lookup of all row ids for cached tables in transactions. For auto-defrag, as currently
   * there will be no RowAction entries at the time of defrag.
   */
  public DoubleIntIndex getTransactionIDList() {

    writeLock.lock();

    try {
      int size = rowActionMap.size();
      DoubleIntIndex lookup = new DoubleIntIndex(size, false);

      lookup.setKeysSearchTarget();

      Iterator it = this.rowActionMap.keySet().iterator();

      for (; it.hasNext(); ) {
        lookup.addUnique(it.nextInt(), 0);
      }

      return lookup;
    } finally {
      writeLock.unlock();
    }
  }
  void writeTransactionRows() {

    for (int i = 0, size = transactionRowLookup.size(); i < size; i++) {
      if (transactionRowLookup.getValue(i) != 0) {
        continue;
      }

      int key = transactionRowLookup.getKey(i);

      try {
        transactionRowLookup.setValue(i, (int) (fileOffset / scale));

        RowInputInterface rowIn = cache.readObject(key);

        fileStreamOut.write(rowIn.getBuffer(), 0, rowIn.getSize());

        fileOffset += rowIn.getSize();
      } catch (HsqlException e) {
      } catch (IOException e) {
      }
    }
  }
  /** Convert row ID's for cached table rows in transactions */
  public void convertTransactionIDs(DoubleIntIndex lookup) {

    writeLock.lock();

    try {
      RowAction[] list = new RowAction[rowActionMap.size()];
      Iterator it = this.rowActionMap.values().iterator();

      for (int i = 0; it.hasNext(); i++) {
        list[i] = (RowAction) it.next();
      }

      rowActionMap.clear();

      for (int i = 0; i < list.length; i++) {
        int pos = lookup.lookupFirstEqual(list[i].getPos());

        list[i].setPos(pos);
        rowActionMap.put(pos, list[i]);
      }
    } finally {
      writeLock.unlock();
    }
  }
  int[] writeTableToDataFile(Table table) throws IOException {

    Session session = database.getSessionManager().getSysSession();
    PersistentStore store = session.sessionData.getRowStore(table);
    RowOutputInterface rowOut = new RowOutputBinary(1024, scale);
    DoubleIntIndex pointerLookup =
        new DoubleIntIndex(table.getPrimaryIndex().sizeEstimate(store), false);
    int[] rootsArray = table.getIndexRootsArray();
    long pos = fileOffset;
    int count = 0;

    pointerLookup.setKeysSearchTarget();
    Error.printSystemOut("lookup begins: " + stopw.elapsedTime());

    RowIterator it = table.rowIterator(session);

    for (; it.hasNext(); count++) {
      CachedObject row = it.getNextRow();

      pointerLookup.addUnsorted(row.getPos(), (int) (pos / scale));

      if (count % 50000 == 0) {
        Error.printSystemOut("pointer pair for row " + count + " " + row.getPos() + " " + pos);
      }

      pos += row.getStorageSize();
    }

    Error.printSystemOut(table.getName().name + " list done: " + stopw.elapsedTime());

    count = 0;
    it = table.rowIterator(session);

    for (; it.hasNext(); count++) {
      CachedObject row = it.getNextRow();

      rowOut.reset();
      row.write(rowOut, pointerLookup);
      fileStreamOut.write(rowOut.getOutputStream().getBuffer(), 0, rowOut.size());

      fileOffset += row.getStorageSize();

      if ((count) % 50000 == 0) {
        Error.printSystemOut(count + " rows " + stopw.elapsedTime());
      }
    }

    for (int i = 0; i < rootsArray.length; i++) {
      if (rootsArray[i] == -1) {
        continue;
      }

      int lookupIndex = pointerLookup.findFirstEqualKeyIndex(rootsArray[i]);

      if (lookupIndex == -1) {
        throw Error.error(ErrorCode.DATA_FILE_ERROR);
      }

      rootsArray[i] = pointerLookup.getValue(lookupIndex);
    }

    setTransactionRowLookups(pointerLookup);
    Error.printSystemOut(table.getName().name + " : table converted");

    return rootsArray;
  }