Exemplo n.º 1
0
  private Collection<Object> processBatch(Mode mode, Collection<Object> batch) throws Exception {
    switch (mode) {
      case SAVE:
        batchProcessor.save(batch);
        break;
      case DELETE:
        batchProcessor.delete(batch);
        break;
    }

    return new ArrayList<>(batchSize);
  }
  /**
   * Issues a query with a potentially large number of keys in batches. For example, you might have
   * 10,000 ids that you wish to use in an "in" clause, but don't trust the database to be smart
   * about optimizing that many keys, so instead you use batchQuery like so:
   *
   * <pre>
   *    Collection<Integer> keys = ...;
   *    String query = "select NAME from USERS where USER_ID in (#KEYS)";
   *    JDBCUtil.BatchProcessor proc = new JDBCUtil.BatchProcessor() {
   *        public void process (ResultSet row) {
   *            String name = rs.getString(1);
   *            // do whatever with name
   *        }
   *    };
   *    JDBCUtil.batchQuery(conn, query, keys, false, 500, proc);
   * </pre>
   *
   * @param query the SQL query to run for each batch with the string <code>#KEYS#</code> in the
   *     place where the batch of keys should be substituted.
   * @param escapeKeys if true, {@link #escape} will be called on each key to escape any dangerous
   *     characters and wrap the key in quotes.
   * @param batchSize the number of keys at a time to substitute in for <code>#KEYS#</code>.
   */
  public static void batchQuery(
      Connection conn,
      String query,
      Collection<?> keys,
      boolean escapeKeys,
      int batchSize,
      BatchProcessor processor)
      throws SQLException {
    Statement stmt = conn.createStatement();
    try {
      Iterator<?> itr = keys.iterator();
      while (itr.hasNext()) {
        // group one batch of keys together
        StringBuilder buf = new StringBuilder();
        for (int ii = 0; ii < batchSize && itr.hasNext(); ii++) {
          if (ii > 0) {
            buf.append(",");
          }
          String key = String.valueOf(itr.next());
          buf.append(escapeKeys ? escape(key) : key);
        }

        // issue the query with that batch
        String squery = query.replace("#KEYS#", buf.toString());
        ResultSet rs = stmt.executeQuery(squery);
        while (rs.next()) {
          processor.process(rs);
        }
      }

    } finally {
      close(stmt);
    }
  }
Exemplo n.º 3
0
 public <T extends PoiBaseBo> void processRecords(
     List<ValidateResult<T>> validateResults, BatchProcessor<T> processor) {
   List<T> successRecords = new ArrayList<T>();
   List<T> errorList = new ArrayList<T>();
   for (ValidateResult<T> validateResult : validateResults) {
     if (validateResult.isSuccess()) {
       if (!errorList.contains(validateResult.getObject())
           && !successRecords.contains(validateResult.getObject())) {
         successRecords.add(validateResult.getObject());
       }
     } else {
       int index = successRecords.indexOf(validateResult.getObject());
       if (index >= 0) {
         successRecords.remove(index);
       }
       if (!errorList.contains(validateResult.getObject())) {
         errorList.add(validateResult.getObject());
       }
     }
   }
   Collections.removeDuplicate(successRecords);
   if (successRecords.size() > 0) {
     try {
       processor.processor(successRecords);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
Exemplo n.º 4
0
  private void process() {
    Object o = queue.poll();

    try {
      if (o != null) {
        if (o instanceof CObj) {
          processCObj((CObj) o);
        } else if (o instanceof CObjList) {
          CObjList cl = (CObjList) o;

          for (int c = 0; c < cl.size(); c++) {
            processCObj(cl.get(c));
          }

          cl.close();
        } else {
          processor.processObj(o);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 5
0
 private void processCObj(CObj o) {
   processor.processCObj(o);
 }
Exemplo n.º 6
0
 public void addProcessor(CObjProcessor p) {
   processor.addProcessor(p);
 }