private void processDeletes(
      SearchIndexBuilderWorker worker, Connection connection, List<SearchBuilderItem> runtimeToDo)
      throws SQLException, IOException {

    if (indexStorage.indexExists()) {
      IndexReader indexReader = null;
      try {
        indexReader = indexStorage.getIndexReader();

        // Open the index
        for (Iterator<SearchBuilderItem> tditer = runtimeToDo.iterator();
            worker.isRunning() && tditer.hasNext(); ) {
          SearchBuilderItem sbi = (SearchBuilderItem) tditer.next();
          if (!SearchBuilderItem.STATE_LOCKED.equals(sbi.getSearchstate())) {
            // should only be getting pending
            // items
            log.warn(
                " Found Item that was not pending " //$NON-NLS-1$
                    + sbi.getName());
            continue;
          }
          if (SearchBuilderItem.ACTION_UNKNOWN.equals(sbi.getSearchaction())) {
            sbi.setSearchstate(SearchBuilderItem.STATE_COMPLETED);
            updateOrSave(connection, sbi);

            continue;
          }
          // remove document
          // if this is mult segment it might not work.
          try {
            indexReader.deleteDocuments(new Term(SearchService.FIELD_REFERENCE, sbi.getName()));
            if (SearchBuilderItem.ACTION_DELETE.equals(sbi.getSearchaction())) {
              sbi.setSearchstate(SearchBuilderItem.STATE_COMPLETED);
              updateOrSave(connection, sbi);

            } else {
              sbi.setSearchstate(SearchBuilderItem.STATE_PENDING_2);
            }

          } catch (IOException ex) {
            log.warn("Failed to delete Page ", ex); // $NON-NLS-1$
          }
        }
      } finally {
        if (indexReader != null) {
          indexStorage.closeIndexReader(indexReader);
          indexReader = null;
        }
      }
    }
  }
  private List findPending(int batchSize, Connection connection, SearchIndexBuilderWorker worker)
      throws SQLException {
    // Pending is the first 100 items
    // State == PENDING
    // Action != Unknown
    long start = System.currentTimeMillis();
    try {
      log.debug("TXFind pending with " + connection); // $NON-NLS-1$

      SearchBuilderItem masterItem = getMasterItem(connection);
      Integer masterAction = getMasterAction(masterItem);
      log.debug(
          " Master Item is "
              + masterItem.getName()
              + ":" //$NON-NLS-1$ //$NON-NLS-2$
              + masterItem.getSearchaction()
              + ":" //$NON-NLS-1$
              + masterItem.getSearchstate()
              + "::" //$NON-NLS-1$
              + masterItem.getVersion());
      if (SearchBuilderItem.ACTION_REFRESH.equals(masterAction)) {
        log.debug(" Master Action is " + masterAction); // $NON-NLS-1$
        log.debug("  REFRESH = " + SearchBuilderItem.ACTION_REFRESH); // $NON-NLS-1$
        log.debug("  RELOAD = " + SearchBuilderItem.ACTION_REBUILD); // $NON-NLS-1$
        // get a complete list of all items, before the master
        // action version
        // if there are none, update the master action action to
        // completed
        // and return a blank list

        refreshIndex(connection, masterItem);

      } else if (SearchBuilderItem.ACTION_REBUILD.equals(masterAction)) {
        rebuildIndex(connection, masterItem, worker);
      } else {
        // get all site masters and perform the required action.
        List siteMasters = getSiteMasterItems(connection);
        for (Iterator i = siteMasters.iterator(); i.hasNext(); ) {
          SearchBuilderItem siteMaster = (SearchBuilderItem) i.next();
          Integer action = getSiteMasterAction(siteMaster);
          if (SearchBuilderItem.ACTION_REBUILD.equals(action)) {
            rebuildIndex(connection, siteMaster, worker);
          } else if (SearchBuilderItem.ACTION_REFRESH.equals(action)) {
            refreshIndex(connection, siteMaster);
          }
        }
      }
      PreparedStatement pst = null;
      PreparedStatement lockedPst = null;
      ResultSet rst = null;
      try {
        pst =
            connection.prepareStatement(
                "select " //$NON-NLS-1$
                    + SEARCH_BUILDER_ITEM_FIELDS
                    + " from " //$NON-NLS-1$
                    + SEARCH_BUILDER_ITEM_T
                    + " where searchstate = ? and     " //$NON-NLS-1$
                    + "        itemscope = ?  order by version "); //$NON-NLS-1$
        lockedPst =
            connection.prepareStatement(
                "update " //$NON-NLS-1$
                    + SEARCH_BUILDER_ITEM_T
                    + " set searchstate = ? " //$NON-NLS-1$
                    + " where id = ?  and  searchstate = ? "); //$NON-NLS-1$
        pst.clearParameters();
        pst.setInt(1, SearchBuilderItem.STATE_PENDING.intValue());
        pst.setInt(2, SearchBuilderItem.ITEM.intValue());
        rst = pst.executeQuery();
        ArrayList<SearchBuilderItemImpl> a = new ArrayList<SearchBuilderItemImpl>();
        while (rst.next() && a.size() < batchSize) {

          SearchBuilderItemImpl sbi = new SearchBuilderItemImpl();
          populateSearchBuilderItem(rst, sbi);
          if (!SearchBuilderItem.ACTION_UNKNOWN.equals(sbi.getSearchaction())) {
            lockedPst.clearParameters();
            lockedPst.setInt(1, SearchBuilderItem.STATE_LOCKED.intValue());
            lockedPst.setString(2, sbi.getId());
            lockedPst.setInt(3, SearchBuilderItem.STATE_PENDING.intValue());
            if (lockedPst.executeUpdate() == 1) {
              sbi.setSearchstate(SearchBuilderItem.STATE_LOCKED);
              a.add(sbi);
            }
            connection.commit();
          }
        }
        return a;
      } finally {
        try {
          rst.close();
        } catch (Exception ex) {
          log.debug(ex);
        }
        try {
          pst.close();
        } catch (Exception ex) {
          log.debug(ex);
        }
      }

    } finally {
      long finish = System.currentTimeMillis();
      log.debug(" findPending took " + (finish - start) + " ms"); // $NON-NLS-1$ //$NON-NLS-2$
    }
  }