KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > jdbc > ImolaJdbc


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
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
30 public class ImolaJdbc extends JdbcDriver implements ImolaDriver {
31     private final static int BULKSIZE = 1000;
32     private static final String JavaDoc TABLE = "sanmarino";
33
34     public void takeSeatIn(Car car, TurnSetup setup) throws CarMotorFailureException
35     {
36         super.takeSeatIn(car, setup);
37
38         jdbcCar().openConnection();
39         jdbcCar().dropTable( TABLE);
40         jdbcCar().createTable( TABLE, new String JavaDoc[]{ "id", "Name", "FirstName", "Points", "LicenseID" },
41                     new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE} );
42         jdbcCar().closeConnection();
43     }
44
45     public void store() {
46         Pilot[] pilots = new Pilot[ BULKSIZE ];
47         BulkWriteStrategy writer = new BulkWritePreparedStatement(jdbcCar(), TABLE);
48         for ( int i = 0; i < setup().getObjectCount(); i++ ){
49             storePilot(pilots, writer, i+1);
50         }
51         jdbcCar().commit();
52     }
53     
54     public void retrieve() {
55         List<Integer JavaDoc> ids=new ArrayList<Integer JavaDoc>(setup().getSelectCount());
56         for(int id=1;id<=setup().getSelectCount();id++) {
57             ids.add(id);
58         }
59         performSingleResultQuery( "select * from "+TABLE+" where id=?",ids );
60     }
61
62     private void storePilot(Pilot[] pilots, BulkWriteStrategy writer, int idx) {
63         int bulkidx=(idx-1)%BULKSIZE;
64         pilots[ bulkidx ] = new Pilot( "Pilot_" + idx, "Jonny_" + idx, idx , idx );
65         if ( isBulkWritePoint(idx, bulkidx)){
66             writer.savePilots(TABLE, pilots, bulkidx+1, idx - bulkidx );
67             Log.logger.fine( "bulk write after writing at " + idx ); //NOI18N
68
}
69         if ( isCommitPoint(idx)){
70             jdbcCar().commit();
71             Log.logger.fine( "commit after writing at " + idx ); //NOI18N
72
}
73     }
74
75     private boolean isBulkWritePoint(int idx, int bulkidx) {
76         return (idx>1 && bulkidx == BULKSIZE-1) || idx == setup().getObjectCount();
77     }
78
79     private boolean isCommitPoint(int idx) {
80         return setup().getCommitInterval() > 0 && idx%setup().getCommitInterval()==0 && idx<setup().getObjectCount();
81     }
82 }
83
Popular Tags