20 package com.mysql.cluster.crund;
22 import java.util.Iterator;
23 import java.util.Arrays;
25 import java.sql.SQLException;
26 import java.sql.DriverManager;
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
44 protected Connection
conn;
45 protected PreparedStatement delAllA;
46 protected PreparedStatement delAllB0;
52 protected void initProperties() {
53 super.initProperties();
55 out.print(
"setting jdbc properties ...");
57 final StringBuilder
msg =
new StringBuilder();
58 final String eol = System.getProperty(
"line.separator");
61 driver = props.getProperty(
"jdbc.driver");
63 throw new RuntimeException(
"Missing property: jdbc.driver");
66 Class.forName(driver);
67 }
catch (ClassNotFoundException e) {
68 out.println(
"Cannot load JDBC driver '" + driver
69 +
"' from classpath '"
70 + System.getProperty(
"java.class.path") +
"'");
71 throw new RuntimeException(e);
74 url = props.getProperty(
"jdbc.url");
76 throw new RuntimeException(
"Missing property: jdbc.url");
79 user = props.getProperty(
"jdbc.user");
80 password = props.getProperty(
"jdbc.password");
82 if (msg.length() == 0) {
86 out.print(msg.toString());
93 protected void printProperties() {
94 super.printProperties();
97 out.println(
"jdbc settings ...");
98 out.println(
"jdbc.driver: " + driver);
99 out.println(
"jdbc.url: " + url);
100 out.println(
"jdbc.user: \"" + user +
"\"");
101 out.println(
"jdbc.password: \"" + password +
"\"");
104 protected void initLoad()
throws Exception {
109 protected void closeLoad()
throws Exception {
119 final protected String sql;
120 protected PreparedStatement stmt;
127 public void init()
throws SQLException {
129 stmt =
conn.prepareStatement(sql);
132 public void close()
throws SQLException {
139 static protected void setCommonAttributes(PreparedStatement stmt,
int i)
140 throws SQLException {
142 stmt.setLong(3, (
long)
i);
143 stmt.setFloat(4, (
float)i);
144 stmt.setDouble(5, (
double)i);
147 static protected int getCommonAttributes(ResultSet rs)
148 throws SQLException {
149 final int cint = rs.getInt(2);
150 final long clong = rs.getLong(3);
151 verify(clong == cint);
152 final float cfloat = rs.getFloat(4);
153 verify(cfloat == cint);
154 final double cdouble = rs.getDouble(5);
155 verify(cdouble == cint);
159 protected void initOperations() throws SQLException {
160 out.print(
"initializing statements ...");
168 new JdbcOp(
"insA_" +
mode.toString().toLowerCase(),
169 "INSERT INTO a (id) VALUES (?)") {
170 public void run(
int nOps)
throws SQLException {
172 for (
int i = 1; i <= nOps; i++) {
177 int cnt = stmt.executeUpdate();
182 int[] cnts = stmt.executeBatch();
183 for (
int i = 0; i < cnts.length; i++) {
184 verify(cnts[i] == 1);
193 new JdbcOp(
"insB0_" +
mode.toString().toLowerCase(),
194 "INSERT INTO b0 (id) VALUES (?)") {
195 public void run(
int nOps)
throws SQLException {
197 for (
int i = 1; i <= nOps; i++) {
202 int cnt = stmt.executeUpdate();
207 int[] cnts = stmt.executeBatch();
208 for (
int i = 0; i < cnts.length; i++) {
209 verify(cnts[i] == 1);
218 new JdbcOp(
"setAByPK_" +
mode.toString().toLowerCase(),
219 "UPDATE a a SET a.cint = ?, a.clong = ?, a.cfloat = ?, a.cdouble = ? WHERE (a.id = ?)") {
220 public void run(
int nOps)
throws SQLException {
222 for (
int i = 1; i <= nOps; i++) {
232 int cnt = stmt.executeUpdate();
237 int[] cnts = stmt.executeBatch();
238 for (
int i = 0; i < cnts.length; i++) {
239 verify(
name +
" " + i, 1, cnts[i]);
248 new JdbcOp(
"setB0ByPK_" +
mode.toString().toLowerCase(),
249 "UPDATE b0 b0 SET b0.cint = ?, b0.clong = ?, b0.cfloat = ?, b0.cdouble = ? WHERE (b0.id = ?)") {
250 public void run(
int nOps)
throws SQLException {
252 for (
int i = 1; i <= nOps; i++) {
262 int cnt = stmt.executeUpdate();
267 int[] cnts = stmt.executeBatch();
268 for (
int i = 0; i < cnts.length; i++) {
269 verify(cnts[i] == 1);
278 new JdbcOp(
"getAByPK_" +
mode.toString().toLowerCase(),
279 "SELECT id, cint, clong, cfloat, cdouble FROM a WHERE (id = ?)") {
280 public void run(
int nOps)
throws SQLException {
282 for (
int i = 1; i <= nOps; i++) {
284 ResultSet rs = stmt.executeQuery();
286 final int id = rs.getInt(1);
288 final int j = getCommonAttributes(rs);
299 new JdbcOp(
"getB0ByPK_" +
mode.toString().toLowerCase(),
300 "SELECT id, cint, clong, cfloat, cdouble FROM b0 WHERE (id = ?)") {
301 public void run(
int nOps)
throws SQLException {
303 for (
int i = 1; i <= nOps; i++) {
305 ResultSet rs = stmt.executeQuery();
307 final int id = rs.getInt(1);
309 final int j = getCommonAttributes(rs);
319 for (
int i = 0, l = 1; l <= maxVarbinaryBytes; l *= 10, i++) {
320 final byte[] b = bytes[
i];
321 assert l == b.length;
324 new JdbcOp(
"setVarbinary" + l +
"_" +
mode.toString().toLowerCase(),
325 "UPDATE b0 b0 SET b0.cvarbinary_def = ? WHERE (b0.id = ?)") {
326 public void run(
int nOps)
throws SQLException {
328 for (
int i = 1; i <= nOps; i++) {
334 int cnt = stmt.executeUpdate();
339 int[] cnts = stmt.executeBatch();
340 for (
int i = 0; i < cnts.length; i++) {
341 verify(cnts[i] == 1);
350 new JdbcOp(
"getVarbinary" + l +
"_" +
mode.toString().toLowerCase(),
351 "SELECT cvarbinary_def FROM b0 WHERE (id = ?)") {
352 public void run(
int nOps)
throws SQLException {
354 for (
int i = 1; i <= nOps; i++) {
356 ResultSet rs = stmt.executeQuery();
358 final byte[] r = rs.getBytes(1);
359 verify(Arrays.equals(b, r));
369 new JdbcOp(
"clearVarbinary" + l +
"_" +
mode.toString().toLowerCase(),
370 "UPDATE b0 b0 SET b0.cvarbinary_def = NULL WHERE (b0.id = ?)") {
371 public void run(
int nOps)
throws SQLException {
373 for (
int i = 1; i <= nOps; i++) {
378 int cnt = stmt.executeUpdate();
383 int[] cnts = stmt.executeBatch();
384 for (
int i = 0; i < cnts.length; i++) {
385 verify(cnts[i] == 1);
394 for (
int i = 0, l = 1; l <= maxVarcharChars; l *= 10, i++) {
396 assert l == s.length();
399 new JdbcOp(
"setVarchar" + l +
"_" +
mode.toString().toLowerCase(),
400 "UPDATE b0 b0 SET b0.cvarchar_def = ? WHERE (b0.id = ?)") {
401 public void run(
int nOps)
throws SQLException {
403 for (
int i = 1; i <= nOps; i++) {
404 stmt.setString(1, s);
409 int cnt = stmt.executeUpdate();
414 int[] cnts = stmt.executeBatch();
415 for (
int i = 0; i < cnts.length; i++) {
416 verify(cnts[i] == 1);
425 new JdbcOp(
"getVarchar" + l +
"_" +
mode.toString().toLowerCase(),
426 "SELECT cvarchar_def FROM b0 WHERE (id = ?)") {
427 public void run(
int nOps)
throws SQLException {
429 for (
int i = 1; i <= nOps; i++) {
431 ResultSet rs = stmt.executeQuery();
433 final String r = rs.getString(1);
444 new JdbcOp(
"clearVarchar" + l +
"_" +
mode.toString().toLowerCase(),
445 "UPDATE b0 b0 SET b0.cvarchar_def = NULL WHERE (b0.id = ?)") {
446 public void run(
int nOps)
throws SQLException {
448 for (
int i = 1; i <= nOps; i++) {
453 int cnt = stmt.executeUpdate();
458 int[] cnts = stmt.executeBatch();
459 for (
int i = 0; i < cnts.length; i++) {
460 verify(cnts[i] == 1);
470 for (
int i = 3, l = 1000; l <= maxBlobBytes; l *= 10, i++) {
471 final byte[] b = bytes[
i];
472 assert l == b.length;
475 new JdbcOp(
"setBlob" + l +
"_" +
mode.toString().toLowerCase(),
476 "UPDATE b0 b0 SET b0.cblob_def = ? WHERE (b0.id = ?)") {
477 public void run(
int nOps)
throws SQLException {
479 for (
int i = 1; i <= nOps; i++) {
485 int cnt = stmt.executeUpdate();
490 int[] cnts = stmt.executeBatch();
491 for (
int i = 0; i < cnts.length; i++) {
492 verify(cnts[i] == 1);
501 new JdbcOp(
"getBlob" + l +
"_" +
mode.toString().toLowerCase(),
502 "SELECT cblob_def FROM b0 WHERE (id = ?)") {
503 public void run(
int nOps)
throws SQLException {
505 for (
int i = 1; i <= nOps; i++) {
507 ResultSet rs = stmt.executeQuery();
509 final byte[] r = rs.getBytes(1);
510 verify(Arrays.equals(b, r));
522 for (
int i = 3, l = 1000; l <= maxTextChars; l *= 10, i++) {
524 assert l == s.length();
527 new JdbcOp(
"setText" + l +
"_" +
mode.toString().toLowerCase(),
528 "UPDATE b0 b0 SET b0.ctext_def = ? WHERE (b0.id = ?)") {
529 public void run(
int nOps)
throws SQLException {
531 for (
int i = 1; i <= nOps; i++) {
532 stmt.setString(1, s);
537 int cnt = stmt.executeUpdate();
542 int[] cnts = stmt.executeBatch();
543 for (
int i = 0; i < cnts.length; i++) {
544 verify(cnts[i] == 1);
553 new JdbcOp(
"getText" + l +
"_" +
mode.toString().toLowerCase(),
554 "SELECT ctext_def FROM b0 WHERE (id = ?)") {
555 public void run(
int nOps)
throws SQLException {
557 for (
int i = 1; i <= nOps; i++) {
559 ResultSet rs = stmt.executeQuery();
561 final String r = rs.getString(1);
574 new JdbcOp(
"setB0->A_" +
mode.toString().toLowerCase(),
575 "UPDATE b0 b0 SET b0.a_id = ? WHERE (b0.id = ?)") {
576 public void run(
int nOps)
throws SQLException {
578 for (
int i = 1; i <= nOps; i++) {
579 int aId = ((i - 1) % nOps) + 1;
585 int cnt = stmt.executeUpdate();
590 int[] cnts = stmt.executeBatch();
591 for (
int i = 0; i < cnts.length; i++) {
592 verify(cnts[i] == 1);
601 new JdbcOp(
"navB0->A_subsel_" +
mode.toString().toLowerCase(),
602 "SELECT id, cint, clong, cfloat, cdouble FROM a WHERE id = (SELECT b0.a_id FROM b0 b0 WHERE b0.id = ?)") {
603 public void run(
int nOps)
throws SQLException {
605 for (
int i = 1; i <= nOps; i++) {
607 ResultSet rs = stmt.executeQuery();
609 final int id = rs.getInt(1);
610 verify(
id == ((i - 1) % nOps) + 1);
611 final int j = getCommonAttributes(rs);
622 new JdbcOp(
"navB0->A_joinproj_" +
mode.toString().toLowerCase(),
623 "SELECT a.id, a.cint, a.clong, a.cfloat, a.cdouble FROM a a, b0 b0 WHERE (a.id = b0.a_id AND b0.id = ?)") {
624 public void run(
int nOps)
throws SQLException {
626 for (
int i = 1; i <= nOps; i++) {
628 ResultSet rs = stmt.executeQuery();
630 final int id = rs.getInt(1);
631 verify(
id == ((i - 1) % nOps) + 1);
632 final int j = getCommonAttributes(rs);
643 new JdbcOp(
"navB0->A_2stmts_" +
mode.toString().toLowerCase(),
644 "SELECT id, cint, clong, cfloat, cdouble FROM a WHERE id = ?") {
646 protected PreparedStatement stmt0;
648 public void init() throws SQLException {
650 stmt0 =
conn.prepareStatement(
"SELECT a_id FROM b0 WHERE id = ?");
653 public void close() throws SQLException {
659 public void run(
int nOps)
throws SQLException {
661 for (
int i = 1; i <= nOps; i++) {
664 ResultSet rs0 = stmt0.executeQuery();
666 int aId = rs0.getInt(1);
667 verify(aId == ((i - 1) % nOps) + 1);
672 ResultSet rs = stmt.executeQuery();
674 final int id = rs.getInt(1);
676 final int j = getCommonAttributes(rs);
687 new JdbcOp(
"navA->B0_" +
mode.toString().toLowerCase(),
688 "SELECT id, cint, clong, cfloat, cdouble FROM b0 WHERE (a_id = ?)") {
689 public void run(
int nOps)
throws SQLException {
692 for (
int i = 1; i <= nOps; i++) {
694 ResultSet rs = stmt.executeQuery();
696 final int id = rs.getInt(1);
697 verify(((
id - 1) % nOps) + 1 == i);
698 final int j = getCommonAttributes(rs);
711 new JdbcOp(
"nullB0->A_" +
mode.toString().toLowerCase(),
712 "UPDATE b0 b0 SET b0.a_id = NULL WHERE (b0.id = ?)") {
713 public void run(
int nOps)
throws SQLException {
715 for (
int i = 1; i <= nOps; i++) {
720 int cnt = stmt.executeUpdate();
725 int[] cnts = stmt.executeBatch();
726 for (
int i = 0; i < cnts.length; i++) {
727 verify(cnts[i] == 1);
737 new JdbcOp(
"delB0ByPK_" +
mode.toString().toLowerCase(),
738 "DELETE FROM b0 WHERE id = ?") {
739 public void run(
int nOps)
throws SQLException {
741 for (
int i = 1; i <= nOps; i++) {
746 int cnt = stmt.executeUpdate();
751 int[] cnts = stmt.executeBatch();
752 for (
int i = 0; i < cnts.length; i++) {
753 verify(cnts[i] == 1);
763 new JdbcOp(
"delAByPK_" +
mode.toString().toLowerCase(),
764 "DELETE FROM a WHERE id = ?") {
765 public void run(
int nOps)
throws SQLException {
767 for (
int i = 1; i <= nOps; i++) {
772 int cnt = stmt.executeUpdate();
777 int[] cnts = stmt.executeBatch();
778 for (
int i = 0; i < cnts.length; i++) {
779 verify(cnts[i] == 1);
788 new JdbcOp(
"insAattr_" +
mode.toString().toLowerCase(),
789 "INSERT INTO a (id, cint, clong, cfloat, cdouble) VALUES (?, ?, ?, ?, ?)") {
790 public void run(
int nOps)
throws SQLException {
792 for (
int i = 1; i <= nOps; i++) {
794 setCommonAttributes(stmt, -i);
798 int cnt = stmt.executeUpdate();
803 int[] cnts = stmt.executeBatch();
804 for (
int i = 0; i < cnts.length; i++) {
805 verify(cnts[i] == 1);
814 new JdbcOp(
"insB0attr_" +
mode.toString().toLowerCase(),
815 "INSERT INTO b0 (id, cint, clong, cfloat, cdouble) VALUES (?, ?, ?, ?, ?)") {
816 public void run(
int nOps)
throws SQLException {
818 for (
int i = 1; i <= nOps; i++) {
820 setCommonAttributes(stmt, -i);
824 int cnt = stmt.executeUpdate();
829 int[] cnts = stmt.executeBatch();
830 for (
int i = 0; i < cnts.length; i++) {
831 verify(cnts[i] == 1);
840 new JdbcOp(
"delAllB0_" +
mode.toString().toLowerCase(),
842 public void run(
int nOps)
throws SQLException {
844 int cnt = stmt.executeUpdate();
852 new JdbcOp(
"delAllA_" +
mode.toString().toLowerCase(),
854 public void run(
int nOps)
throws SQLException {
856 int cnt = stmt.executeUpdate();
865 for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
866 ((JdbcOp)i.next()).init();
868 out.println(
" [JdbcOp: " + ops.size() +
"]");
871 protected void closeOperations() throws SQLException {
872 out.print(
"closing statements ...");
875 for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
876 ((JdbcOp)i.next()).close();
879 out.println(
" [ok]");
886 protected void initConnection() throws SQLException {
887 assert (
conn == null);
890 out.println(
"initializing jdbc resources ...");
893 out.print(
"creating jdbc connection ...");
895 conn = DriverManager.getConnection(url, user, password);
897 conn.setAutoCommit(
false);
898 delAllA =
conn.prepareStatement(
"DELETE FROM a");
899 delAllB0 =
conn.prepareStatement(
"DELETE FROM b0");
900 out.println(
" [ok: " + url +
"]");
902 out.print(
"setting isolation level ...");
905 final int il =
Connection.TRANSACTION_READ_COMMITTED;
906 conn.setTransactionIsolation(il);
908 switch (
conn.getTransactionIsolation()) {
910 out.print(
"READ_UNCOMMITTED");
913 out.print(
"READ_COMMITTED");
916 out.print(
"REPEATABLE_READ");
919 out.print(
"SERIALIZABLE");
927 protected void closeConnection() throws SQLException {
928 assert (
conn != null);
931 out.println(
"releasing jdbc resources ...");
934 out.print(
"closing jdbc connection ...");
936 if (delAllB0 != null)
945 out.println(
" [ok]");
948 protected void clearPersistenceContext() {
952 protected void clearData() throws SQLException {
953 conn.setAutoCommit(
false);
954 out.print(
"deleting all rows ...");
956 int delB0 = delAllB0.executeUpdate();
957 out.print(
" [B0: " + delB0);
959 int delA = delAllA.executeUpdate();
960 out.print(
", A: " + delA);
968 static public void main(
String[] args) {
969 System.out.println(
"JdbcLoad.main()");
971 new JdbcLoad().run();
972 System.out.println();
973 System.out.println(
"JdbcLoad.main(): done.");