001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.teams.jdbc;
021:
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024:
025: import org.polepos.circuits.melbourne.*;
026: import org.polepos.data.*;
027: import org.polepos.framework.*;
028: import org.polepos.teams.jdbc.drivers.melbourne.*;
029:
030: /**
031: * @author Herkules
032: */
033: public class MelbourneJdbc extends JdbcDriver implements
034: MelbourneDriver {
035: /**
036: * Number of pilot to be written at once.
037: */
038: private final static int BULKSIZE = 1000;
039:
040: public void takeSeatIn(Car car, TurnSetup setup)
041: throws CarMotorFailureException {
042: super .takeSeatIn(car, setup);
043:
044: jdbcCar().openConnection();
045:
046: //
047: // Create database structure
048: //
049: jdbcCar().dropTable("australia");
050: jdbcCar().createTable(
051: "australia",
052: new String[] { "ID", "Name", "FirstName", "Points",
053: "LicenseID" },
054: new Class[] { Integer.TYPE, String.class, String.class,
055: Integer.TYPE, Integer.TYPE });
056:
057: jdbcCar().closeConnection();
058: }
059:
060: public void write() {
061:
062: int numobjects = setup().getObjectCount();
063: int commitintervall = setup().getCommitInterval();
064: int commitctr = 0;
065:
066: Pilot[] pilots = new Pilot[BULKSIZE];
067: int idx = 0;
068:
069: //BulkWriteStrategy writer = new BulkWriteSingle();
070: BulkWriteStrategy writer = new BulkWritePreparedStatement(
071: jdbcCar(), "australia");
072: //BulkWriteStrategy writer = new BulkWriteMultiValue();
073:
074: for (int i = 1; i <= numobjects; i++) {
075: Pilot p = new Pilot("Pilot_" + i, "Herkules", i, i);
076: pilots[idx++] = p;
077: if (idx == BULKSIZE) {
078: writer
079: .savePilots("australia", pilots, idx, i - idx
080: + 1);
081: idx = 0;
082: }
083:
084: if (commitintervall > 0 && ++commitctr >= commitintervall) {
085: commitctr = 0;
086: jdbcCar().commit();
087: Log.logger.fine("commit while writing at " + i + 1); //NOI18N
088: }
089:
090: addToCheckSum(i);
091: }
092:
093: // Write the rest
094: writer.savePilots("australia", pilots, idx, numobjects - idx);
095:
096: jdbcCar().commit();
097: }
098:
099: public void read() {
100:
101: int numobjects = setup().getObjectCount();
102:
103: try {
104: ResultSet rs = jdbcCar().executeQuery(
105: "select * from australia");
106:
107: for (int i = 0; i < numobjects; i++) {
108: rs.next();
109: Pilot p = new Pilot(rs.getString(2), rs.getString(3),
110: rs.getInt(4), rs.getInt(5));
111: addToCheckSum(p.getPoints());
112: }
113: } catch (SQLException sqlex) {
114: sqlex.printStackTrace();
115: }
116: }
117:
118: public void read_hot() {
119: read();
120: }
121:
122: public void delete() {
123: jdbcCar().executeSQL("delete from australia");
124: jdbcCar().commit();
125: }
126:
127: }
|