i improved the speed of file parsing, be making less database hits
This commit is contained in:
@@ -37,13 +37,14 @@ import com.TwentyCodes.java.OrderProcessor.ProgressListener;
|
|||||||
*/
|
*/
|
||||||
public class OrderDB {
|
public class OrderDB {
|
||||||
|
|
||||||
// private final String CREATE_TABLE = "CREATE TABLE Orders (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 'item' blob);";
|
private final String CREATE_TABLE = "CREATE TABLE Orders (id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, item blob);";
|
||||||
|
|
||||||
private final String INSERT = "insert into Orders (item) values (?);";
|
private final String INSERT = "insert into Orders (item) values (?);";
|
||||||
private ProgressListener mListener;
|
private ProgressListener mListener;
|
||||||
private PreparedStatement prep;
|
private PreparedStatement prep;
|
||||||
// private String dbLocation;
|
// private String dbLocation;
|
||||||
private boolean isLoadingFile = false;
|
private boolean isLoadingFile = false;
|
||||||
|
private ArrayList<Order> list;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new OrderDB
|
* Creates a new OrderDB
|
||||||
@@ -57,15 +58,15 @@ public class OrderDB {
|
|||||||
// Class.forName("org.sqlite.JDBC");
|
// Class.forName("org.sqlite.JDBC");
|
||||||
Class.forName("com.mysql.jdbc.Driver");
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
Connection conn = getConnection();
|
Connection conn = getConnection();
|
||||||
// Statement stat = conn.createStatement();
|
Statement stat = conn.createStatement();
|
||||||
|
|
||||||
// stat.executeUpdate("drop table if exists Orders;");
|
// stat.executeUpdate("drop table if exists Orders;");
|
||||||
// try {
|
try {
|
||||||
// stat.executeUpdate(CREATE_TABLE);
|
stat.executeUpdate(CREATE_TABLE);
|
||||||
// } catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
// //most likely the table already exist
|
//most likely the table already exist
|
||||||
// e.printStackTrace();
|
e.printStackTrace();
|
||||||
// }
|
}
|
||||||
conn.close();
|
conn.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,10 +102,12 @@ public class OrderDB {
|
|||||||
/*
|
/*
|
||||||
* check the current database for an orders with a matching order number
|
* check the current database for an orders with a matching order number
|
||||||
*/
|
*/
|
||||||
for(Order item : getAllOrders())
|
for(Order item : list)
|
||||||
if(item.getGoogleOrderNumber() == order.getGoogleOrderNumber())
|
if(item.getGoogleOrderNumber() == order.getGoogleOrderNumber())
|
||||||
throw new OrderExistsException(order.toString());
|
throw new OrderExistsException(order.toString());
|
||||||
|
|
||||||
|
list.add(order);
|
||||||
|
|
||||||
saveOrder(order);
|
saveOrder(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +132,7 @@ public class OrderDB {
|
|||||||
ObjectInputStream ins;
|
ObjectInputStream ins;
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
if(mListener != null && ! isLoadingFile)
|
if(mListener != null && ! isLoadingFile)
|
||||||
mListener.onProgressUpdate(row++, count);
|
mListener.onProgressUpdate("Getting All Orders", false, row++, count);
|
||||||
bais = new ByteArrayInputStream(rs.getBytes("item"));
|
bais = new ByteArrayInputStream(rs.getBytes("item"));
|
||||||
ins = new ObjectInputStream(bais);
|
ins = new ObjectInputStream(bais);
|
||||||
list.add((Order) ins.readObject());
|
list.add((Order) ins.readObject());
|
||||||
@@ -181,8 +184,11 @@ public class OrderDB {
|
|||||||
public void load(File file) throws SQLException, IOException, ClassNotFoundException{
|
public void load(File file) throws SQLException, IOException, ClassNotFoundException{
|
||||||
|
|
||||||
// Scanner scan = new Scanner(file);
|
// Scanner scan = new Scanner(file);
|
||||||
|
prep = null;
|
||||||
|
list = null;
|
||||||
|
|
||||||
CSVParser csvParser = new CSVParser(new FileInputStream(file));
|
CSVParser csvParser = new CSVParser(new FileInputStream(file));
|
||||||
|
list = new ArrayList<Order>(getAllOrders());
|
||||||
|
|
||||||
int line = 1;
|
int line = 1;
|
||||||
int lineCount = countLines(file);
|
int lineCount = countLines(file);
|
||||||
@@ -191,25 +197,32 @@ public class OrderDB {
|
|||||||
Connection conn = getConnection();
|
Connection conn = getConnection();
|
||||||
prep = conn.prepareStatement(INSERT);
|
prep = conn.prepareStatement(INSERT);
|
||||||
conn.setAutoCommit(true);
|
conn.setAutoCommit(true);
|
||||||
|
try {
|
||||||
|
while (csvParser.lastLineNumber() < lineCount) {
|
||||||
|
|
||||||
while(csvParser.lastLineNumber() < lineCount){
|
if (mListener != null)
|
||||||
|
mListener.onProgressUpdate(file.toString() + " ~ Parsing Order: "+ line +" of "+ lineCount, false, line++, lineCount);
|
||||||
|
|
||||||
if(mListener != null)
|
if (Main.DEBUG)
|
||||||
mListener.onProgressUpdate(line++, lineCount);
|
System.out.println("\non line: " + line);
|
||||||
|
|
||||||
if(Main.DEBUG)
|
|
||||||
System.out.println("\non line: "+ line);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
addOrder(new Order(csvParser.getLine()));
|
addOrder(new Order(csvParser.getLine()));
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (InvalidDateFormatException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (OrderExistsException e) {
|
} catch (OrderExistsException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} catch (InvalidDateFormatException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
prep.close();
|
||||||
conn.close();
|
conn.close();
|
||||||
isLoadingFile = false;
|
isLoadingFile = false;
|
||||||
}
|
}
|
||||||
@@ -233,6 +246,7 @@ public class OrderDB {
|
|||||||
|
|
||||||
prep.setBytes(1, bos.toByteArray());
|
prep.setBytes(1, bos.toByteArray());
|
||||||
prep.executeUpdate();
|
prep.executeUpdate();
|
||||||
|
// prep.addBatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -265,7 +279,7 @@ public class OrderDB {
|
|||||||
ObjectInputStream ins;
|
ObjectInputStream ins;
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
if(mListener != null && ! isLoadingFile)
|
if(mListener != null && ! isLoadingFile)
|
||||||
mListener.onProgressUpdate(row++, count);
|
mListener.onProgressUpdate("Searching for: "+text, false, row++, count);
|
||||||
bais = new ByteArrayInputStream(rs.getBytes("item"));
|
bais = new ByteArrayInputStream(rs.getBytes("item"));
|
||||||
ins = new ObjectInputStream(bais);
|
ins = new ObjectInputStream(bais);
|
||||||
Order order = (Order) ins.readObject();
|
Order order = (Order) ins.readObject();
|
||||||
|
|||||||
@@ -17,6 +17,6 @@ public interface ProgressListener {
|
|||||||
* @param progress
|
* @param progress
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public void onProgressUpdate(int progress, int max);
|
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -158,12 +158,12 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
|
|||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onProgressUpdate(int progress, int max) {
|
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
|
||||||
mProgressBar.setMaximum(max);
|
mProgressBar.setMaximum(max);
|
||||||
mProgressBar.setValue(progress);
|
mProgressBar.setValue(progress);
|
||||||
mProgressBar.setString(mCurrentFile + " ~ Parsing Order: "+ progress +" of "+mProgressBar.getMaximum());
|
mProgressBar.setString(string);
|
||||||
if(mProgressBar.isIndeterminate()) {
|
if(mProgressBar.isIndeterminate()) {
|
||||||
mProgressBar.setIndeterminate(false);
|
mProgressBar.setIndeterminate(isIndeterminate);
|
||||||
pack();
|
pack();
|
||||||
}
|
}
|
||||||
if(progress >= mProgressBar.getMaximum()){
|
if(progress >= mProgressBar.getMaximum()){
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class OrderPane extends JScrollPane {
|
|||||||
int total = 0;
|
int total = 0;
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
if(mListener != null)
|
if(mListener != null)
|
||||||
mListener.onProgressUpdate(i, list.size());
|
mListener.onProgressUpdate("Processing Order: "+ i +" of "+ list.size(), false, i, list.size());
|
||||||
switch (list.get(i).getFinancialStatus()) {
|
switch (list.get(i).getFinancialStatus()) {
|
||||||
case CANCELLED:
|
case CANCELLED:
|
||||||
updateTextPane(list.get(i).toString(), TextStyle.RED);
|
updateTextPane(list.get(i).toString(), TextStyle.RED);
|
||||||
|
|||||||
@@ -257,17 +257,18 @@ public class SearchPanel extends JPanel implements ActionListener, ProgressListe
|
|||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onProgressUpdate(int progress, int max) {
|
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
|
||||||
|
mProgressBar.setMaximum(max);
|
||||||
|
mProgressBar.setValue(progress);
|
||||||
|
mProgressBar.setIndeterminate(isIndeterminate);
|
||||||
|
mProgressBar.setString(string);
|
||||||
|
|
||||||
if(Main.DEBUG)
|
if(Main.DEBUG)
|
||||||
System.out.println("search progress = "+ progress);
|
System.out.println("search progress = "+ progress);
|
||||||
|
|
||||||
progress++;
|
progress++;
|
||||||
if(mProgressBar.isIndeterminate()) {
|
|
||||||
mProgressBar.setIndeterminate(false);
|
|
||||||
mFrame.pack();
|
|
||||||
}
|
|
||||||
if(isProcessing){
|
if(isProcessing){
|
||||||
mProgressBar.setString("Processing Order: "+ mListSize +" of "+ progress);
|
|
||||||
try {
|
try {
|
||||||
mProgressBar.setValue(progress + MainWindow.db.getCount());
|
mProgressBar.setValue(progress + MainWindow.db.getCount());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
|||||||
@@ -79,9 +79,11 @@ public class ShowAllPanel extends JPanel implements ActionListener, ProgressList
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onProgressUpdate(int progress, int max) {
|
public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
|
||||||
mProgressBar.setMaximum(max);
|
mProgressBar.setMaximum(max);
|
||||||
mProgressBar.setValue(progress);
|
mProgressBar.setValue(progress);
|
||||||
|
mProgressBar.setIndeterminate(isIndeterminate);
|
||||||
|
mProgressBar.setString(string);
|
||||||
if(progress >= mProgressBar.getMaximum()){
|
if(progress >= mProgressBar.getMaximum()){
|
||||||
mFrame.pack();
|
mFrame.pack();
|
||||||
mProgressBar.setString("Done");
|
mProgressBar.setString("Done");
|
||||||
|
|||||||
Reference in New Issue
Block a user