01: /*
02: This file is part of the PolePosition database benchmark
03: http://www.polepos.org
04:
05: This program is free software; you can redistribute it and/or
06: modify it under the terms of the GNU General Public License
07: as published by the Free Software Foundation; either version 2
08: of the License, or (at your option) any later version.
09:
10: This program is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public
16: License along with this program; if not, write to the Free
17: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18: MA 02111-1307, USA. */
19:
20: package org.polepos.teams.jdbc;
21:
22: import java.util.*;
23:
24: import org.polepos.circuits.imola.*;
25: import org.polepos.data.*;
26: import org.polepos.framework.*;
27: import org.polepos.teams.jdbc.drivers.melbourne.*;
28:
29: public class ImolaJdbc extends JdbcDriver implements ImolaDriver {
30: private final static int BULKSIZE = 1000;
31: private static final String TABLE = "sanmarino";
32:
33: public void takeSeatIn(Car car, TurnSetup setup)
34: throws CarMotorFailureException {
35: super .takeSeatIn(car, setup);
36:
37: jdbcCar().openConnection();
38: jdbcCar().dropTable(TABLE);
39: jdbcCar().createTable(
40: TABLE,
41: new String[] { "id", "Name", "FirstName", "Points",
42: "LicenseID" },
43: new Class[] { Integer.TYPE, String.class, String.class,
44: Integer.TYPE, Integer.TYPE });
45: jdbcCar().closeConnection();
46: }
47:
48: public void store() {
49: Pilot[] pilots = new Pilot[BULKSIZE];
50: BulkWriteStrategy writer = new BulkWritePreparedStatement(
51: jdbcCar(), TABLE);
52: for (int i = 0; i < setup().getObjectCount(); i++) {
53: storePilot(pilots, writer, i + 1);
54: }
55: jdbcCar().commit();
56: }
57:
58: public void retrieve() {
59: List<Integer> ids = new ArrayList<Integer>(setup()
60: .getSelectCount());
61: for (int id = 1; id <= setup().getSelectCount(); id++) {
62: ids.add(id);
63: }
64: performSingleResultQuery("select * from " + TABLE
65: + " where id=?", ids);
66: }
67:
68: private void storePilot(Pilot[] pilots, BulkWriteStrategy writer,
69: int idx) {
70: int bulkidx = (idx - 1) % BULKSIZE;
71: pilots[bulkidx] = new Pilot("Pilot_" + idx, "Jonny_" + idx,
72: idx, idx);
73: if (isBulkWritePoint(idx, bulkidx)) {
74: writer
75: .savePilots(TABLE, pilots, bulkidx + 1, idx
76: - bulkidx);
77: Log.logger.fine("bulk write after writing at " + idx); //NOI18N
78: }
79: if (isCommitPoint(idx)) {
80: jdbcCar().commit();
81: Log.logger.fine("commit after writing at " + idx); //NOI18N
82: }
83: }
84:
85: private boolean isBulkWritePoint(int idx, int bulkidx) {
86: return (idx > 1 && bulkidx == BULKSIZE - 1)
87: || idx == setup().getObjectCount();
88: }
89:
90: private boolean isCommitPoint(int idx) {
91: return setup().getCommitInterval() > 0
92: && idx % setup().getCommitInterval() == 0
93: && idx < setup().getObjectCount();
94: }
95: }
|