
de.uni_leipzig.asv.utils.Producer Maven / Gradle / Ivy
/*******************************************************************************
* The MIT License (MIT)
* Copyright (c) 2007, University of Leipzig, Institut für Informatik,
* Abteilung Autmatische Sprachverarbeitung
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
/*
*
$Header: /usr/cvs/toolbox/src/de/uni_leipzig/asv/utils/Producer.java,v 1.5 2007/05/04 11:04:11 lsteiner Exp $
*
* Created on Jun 9, 2005 by knorke
*
* package de.uni_leipzig.asv.utils for knorke project
*
*/
package de.uni_leipzig.asv.utils;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* @author knorke
* @date Jun 9, 2005 2:32:15 PM
*/
public class Producer extends Thread {
private int interval = 50000;
private final Consumer consumer;
private final DBConnection connection;
private final String tablename;
private String[] cols;
private final String primkey;
private int lastKey = -1;
/**
* @param connection
* @param tablename
* @param cols
* can be null or empty (to fetch ALL columns)
* @param primkey
*/
public Producer(DBConnection connection, String tablename, String[] cols,
String primkey) {
this.consumer = new Consumer();
this.connection = connection;
this.tablename = tablename;
if (cols == null) {
this.cols = new String[0];
} else {
this.cols = cols;
}
this.primkey = primkey;
}
public Consumer getConsumer() {
return this.consumer;
}
public int getInterval() {
return this.interval;
}
public void setInterval(int i) {
if (i > 0) {
this.interval = i;
} else {
System.err
.println("ProducerThread: cannot set the fetching interval below zero");
}
}
private ResultSet fetch(int startkey) {
StringBuffer statement = new StringBuffer();
if (this.cols.length == 0) {
statement
.append("SELECT * FROM " + this.tablename + " WHERE "
+ this.primkey + ">" + startkey + " LIMIT "
+ this.interval);
} else {
statement.append("SELECT " + this.primkey);
for (String element : this.cols) {
statement.append("," + element);
}
statement
.append(" FROM " + this.tablename + " WHERE "
+ this.primkey + ">" + startkey + " LIMIT "
+ this.interval);
}
System.err.println("ProducerThread: fetching with statement "
+ statement.toString());
return this.connection.executeQuery(statement.toString());
}
@Override
public void run() {
ResultSet resset = null;
int linenrs = 0;
ResultSetMetaData metadata = null;
String data[] = null;
// main loop
while (true) {
synchronized (this.consumer) {
// fuellstand ist unter threshold gefallen
// ein nachfuellen mit daten ist noetig
if (this.consumer.size() <= this.consumer.getFillThreshold()) {
System.err
.println("ProducerThread: good morning (bufsize: "
+ this.consumer.size() + ")");
try {
// if this is the first fetch()-call, the first key is
// -1
resset = fetch(this.lastKey);
metadata = resset.getMetaData();
while (resset.next()) {
if (this.cols.length > 0) {
if (metadata.getColumnCount() - 1 != this.cols.length) {
System.err.println("ProducerThread: got "
+ metadata.getColumnCount()
+ " cols, but i've requested "
+ this.cols.length + " cols!");
throw new RuntimeException(
"critical error! please submit a full bug report");
}
data = new String[this.cols.length];
// fetch the requested cols by names
for (int i = 0; i < this.cols.length; i++) {
data[i] = resset.getString(this.cols[i]);
}
} // end if(this.cols != null)
else {
// build a stringarray
data = new String[metadata.getColumnCount()];
for (int i = 0; i < data.length; i++) {
data[i] = resset.getString(i + 1);
}
}
// get the primary key, to know the next interval
// (from lastkey to lastkey+intervall)
this.lastKey = resset.getInt(this.primkey);
// count the linenr fetched
linenrs++;
// some debug output
// System.err.println( "ProducerThread: (" +
// this.lastKey + ") adding " + data + " " +
// data.length );
// adding the coldatas to the consumer
this.consumer.add(data);
}
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
// System.err.println("<< "+linenrs+" != "+this.interval);
if (linenrs < this.interval) {
System.err.println("ProducerThread: all data fetched");
this.consumer.setEOF(true);
break;
} else {
linenrs = 0;
}
} else {
try {
this.consumer.notify();
// this.consumer.wait(5000);
System.err
.println("ProducerThread: good n8 (bufsize: "
+ this.consumer.size() + ")");
this.consumer.wait();
} catch (InterruptedException e) {
System.err.println("IOIterator: Exception cought!");
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
} // end else
} // end main loop
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy