Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.intermine.sql.writebatch;
/*
* Copyright (C) 2002-2022 FlyMine
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. See the LICENSE file for more
* information or http://www.gnu.org/copyleft/lesser.html.
*
*/
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.intermine.sql.DatabaseUtil;
/**
* An implementation of the BatchWriter interface that uses simple JDBC addBatch() and
* executeBatch() methods.
*
* @author Matthew Wakeling
*/
public class BatchWriterSimpleImpl implements BatchWriter
{
private static final Logger LOG = Logger.getLogger(BatchWriterSimpleImpl.class);
protected int deleteTempTableSize = 200;
protected Connection con;
protected Statement preDeleteBatch;
protected List deleteBatches;
protected Statement postDeleteBatch;
protected List addBatches;
protected Statement lastBatch;
/**
* This sets the threshold above which a temp table will be used for deletes.
*
* @param deleteTempTableSize the threshold
*/
public void setThreshold(int deleteTempTableSize) {
this.deleteTempTableSize = deleteTempTableSize;
}
/**
* {@inheritDoc}
*/
public List write(Connection con, Map tables,
Set filter) throws SQLException {
this.con = con;
// Initialise object for action. Note this code is NOT re-entrant.
preDeleteBatch = null;
deleteBatches = new ArrayList();
postDeleteBatch = null;
addBatches = new ArrayList();
lastBatch = null;
Map activityMap = new HashMap();
for (Map.Entry tableEntry : tables.entrySet()) {
String name = tableEntry.getKey();
if ((filter == null) || filter.contains(name)) {
int activity = 0;
Table table = tableEntry.getValue();
if (table instanceof TableBatch) {
activity += 2 * doDeletes(name, (TableBatch) table);
activity += doInserts(name, (TableBatch) table, addBatches);
} else {
activity += 2 * doIndirectionDeletes(name, (IndirectionTableBatch) table);
activity += doIndirectionInserts(name, (IndirectionTableBatch) table,
addBatches);
}
table.clear();
if (activity > 0) {
activityMap.put(name, new Integer(activity));
}
}
}
List retval = new ArrayList();
if (preDeleteBatch != null) {
retval.add(new FlushJobStatementBatchImpl(preDeleteBatch));
}
retval.addAll(deleteBatches);
if (postDeleteBatch != null) {
retval.add(new FlushJobStatementBatchImpl(postDeleteBatch));
}
retval.addAll(addBatches);
if (lastBatch != null) {
retval.add(new FlushJobStatementBatchImpl(lastBatch));
}
if (!activityMap.isEmpty()) {
retval.add(new FlushJobUpdateStatistics(activityMap, this, con));
}
// Help the garbage collector
preDeleteBatch = null;
deleteBatches = null;
postDeleteBatch = null;
addBatches = null;
lastBatch = null;
return retval;
}
/**
* Performs all the inserts for the given table name and table batch.
*
* @param name the name of the table
* @param table the table batch
* @param batches the List of batches into which new flushjobs should be placed
* @return the number of rows inserted
* @throws SQLException if an error occurs
*/
@SuppressWarnings("unchecked")
protected int doInserts(String name, TableBatch table,
@SuppressWarnings("unused") List batches) throws SQLException {
String[] colNames = table.getColNames();
if ((colNames != null) && (!table.getIdsToInsert().isEmpty())) {
StringBuffer preambleBuffer = new StringBuffer("INSERT INTO ").append(name)
.append(" (");
for (int i = 0; i < colNames.length; i++) {
if (i > 0) {
preambleBuffer.append(", ");
}
preambleBuffer.append(colNames[i]);
}
preambleBuffer.append(") VALUES (");
String preamble = preambleBuffer.toString();
for (Map.Entry