KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > jdbc > drivers > melbourne > BulkWriteMultiValue


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.drivers.melbourne;
21
22 import org.polepos.data.*;
23 import org.polepos.teams.jdbc.*;
24
25
26
27 /**
28  * Performs a bulkwrite by employing multiple VALUES part in the SQL statement.
29  * @author Herkules
30  *
31  * Typical:
32  * BUNCH=10: 50000 objects: write 1062ms (0.02124ms/object), read 172ms (0.00344ms/object), delete 0ms (0.0ms/object)
33  * BUNCH=100: 50000 objects: write 500ms (0.01ms/object), read 156ms (0.00312ms/object), delete 0ms (0.0ms/object)
34  * BUNCH=1000: 50000 objects: write 360ms (0.0072ms/object), read 218ms (0.00436ms/object), delete 0ms (0.0ms/object)
35  */

36 public class BulkWriteMultiValue implements BulkWriteStrategy
37 {
38     private final JdbcCar mCar;
39
40     /**
41      * Creates a new instance of BulkWriteMultiValue.
42      */

43     public BulkWriteMultiValue( JdbcCar car )
44     {
45         mCar = car;
46     }
47
48     /**
49      * Dump an array of pilots to the DB by creating a loooong SQL statement.
50      */

51     public void savePilots( String JavaDoc tablename, Pilot[] p, int count, int index)
52     {
53         if ( count == 0 ) return;
54         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc( "insert into ");
55         stmt.append(tablename);
56         stmt.append(" (id,Name,FrontName,Points,LicenseID) values " );
57         for ( int i = 0; i < count-1; i++ )
58         {
59             values( stmt, p[i], index++ );
60             stmt.append(",");
61         }
62         values( stmt, p[count-1], index++ );
63         
64         mCar.executeSQL( stmt.toString() );
65     }
66
67     
68     /**
69      * Helper.
70      */

71     private void values( StringBuffer JavaDoc to, Pilot p, int idx )
72     {
73         to.append( "(" ).append( Integer.toString( idx ) ).append( ", '" );
74         to.append( p.getName() ).append( "', '" );
75         to.append( p.getFirstName() ).append( "', " );
76         to.append( Integer.toString( p.getPoints() ) ).append( ", " );
77         to.append( Integer.toString( p.getLicenseID() ) ).append( ")" );
78     }
79     
80 }
81
Popular Tags