1 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 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 []{ "id", "Name", "FirstName", "Points", "LicenseID" }, 41 new Class []{Integer.TYPE, String .class, String .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 > ids=new ArrayList<Integer >(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 ); } 69 if ( isCommitPoint(idx)){ 70 jdbcCar().commit(); 71 Log.logger.fine( "commit after writing at " + idx ); } 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 |