MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClusterjLoad.java
1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
3  *
4  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
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 License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 package com.mysql.cluster.crund;
21 
22 import com.mysql.clusterj.ClusterJHelper;
23 import com.mysql.clusterj.Constants;
24 import com.mysql.clusterj.Query;
25 import com.mysql.clusterj.Session;
26 import com.mysql.clusterj.SessionFactory;
27 import com.mysql.clusterj.query.QueryDomainType;
28 import com.mysql.clusterj.query.QueryBuilder;
29 
30 import java.util.Collection;
31 import java.util.Iterator;
32 import java.util.Arrays;
33 import java.util.Map;
34 
38 public class ClusterjLoad extends CrundDriver {
39 
40  // ClusterJ settings
41  protected String mgmdConnect;
42 
43  // ClusterJ resources
44  protected SessionFactory sessionFactory;
45  protected Session session;
46 
47  // ----------------------------------------------------------------------
48  // ClusterJ intializers/finalizers
49  // ----------------------------------------------------------------------
50 
51  @Override
52  protected void initProperties() {
53  super.initProperties();
54 
55  out.print("setting clusterj properties ...");
56 
57  final StringBuilder msg = new StringBuilder();
58 
59  // check required properties
60  mgmdConnect
61  = props.getProperty(Constants.PROPERTY_CLUSTER_CONNECTSTRING);
62 
63  if (msg.length() == 0) {
64  out.println(" [ok]");
65  } else {
66  out.println();
67  out.print(msg.toString());
68  }
69 
70  // have mgmdConnect initialized first
71  descr = "->clusterj(" + mgmdConnect + ")";
72  }
73 
74  @Override
75  protected void printProperties() {
76  super.printProperties();
77 
78  out.println();
79  out.println("clusterj settings ...");
80  out.println("ndb.mgmdConnect " + mgmdConnect);
81  for (Iterator<Map.Entry<Object,Object>> i
82  = props.entrySet().iterator(); i.hasNext();) {
83  Map.Entry<Object,Object> e = i.next();
84  final String k = (String)e.getKey();
85  if (k.startsWith("com.mysql.clusterj")) {
86  final StringBuilder s = new StringBuilder("..");
87  s.append(k, 18, k.length());
88  while (s.length() < 31) s.append(' ');
89  out.println(s + " " + e.getValue());
90  }
91  }
92  }
93 
94  @Override
95  protected void initLoad() throws Exception {
96  // XXX support generic load class
97  //super.init();
98 
99  out.println();
100  out.print("creating SessionFactory ...");
101  out.flush();
102  sessionFactory = ClusterJHelper.getSessionFactory(props);
103  out.println(" [SessionFactory: 1]");
104  }
105 
106  @Override
107  protected void closeLoad() throws Exception {
108  out.println();
109  out.print("closing SessionFactory ...");
110  out.flush();
111  if (sessionFactory != null)
112  sessionFactory.close();
113  sessionFactory = null;
114  out.println(" [ok]");
115 
116  // XXX support generic load class
117  //super.close();
118  }
119 
120  // ----------------------------------------------------------------------
121  // ClusterJ operations
122  // ----------------------------------------------------------------------
123 
124  protected abstract class ClusterjOp extends Op {
125  public ClusterjOp(String name) {
126  super(name);
127  }
128 
129  public void init() {}
130 
131  public void close() {}
132  };
133 
134  protected int checkFields(IA o) {
135  final int cint = o.getCint();
136  final long clong = o.getClong();
137  verify(clong == cint);
138  final float cfloat = o.getCfloat();
139  verify(cfloat == cint);
140  final double cdouble = o.getCdouble();
141  verify(cdouble == cint);
142  return cint;
143  }
144 
145  protected int checkFields(IB0 o) {
146  final int cint = o.getCint();
147  final long clong = o.getClong();
148  verify(clong == cint);
149  final float cfloat = o.getCfloat();
150  verify(cfloat == cint);
151  final double cdouble = o.getCdouble();
152  verify(cdouble == cint);
153  return cint;
154  }
155 
156  protected void initOperations() {
157  out.print("initializing operations ...");
158  out.flush();
159 
160  for (CrundDriver.XMode m : xMode) {
161  // inner classes can only refer to a constant
162  final CrundDriver.XMode mode = m;
163 
164  ops.add(
165  new ClusterjOp("insA_" + mode.toString().toLowerCase()) {
166  public void run(int nOps) {
167  if (mode != CrundDriver.XMode.INDY) {
168  beginTransaction();
169  }
170  for (int i = 0; i < nOps; i++) {
171  final IA o = session.newInstance(IA.class);
172  assert o != null;
173  o.setId(i);
174  session.persist(o);
175  if (mode == CrundDriver.XMode.EACH) {
176  session.flush();
177  }
178  }
179  if (mode != CrundDriver.XMode.INDY) {
180  commitTransaction();
181  }
182  }
183  });
184 
185  ops.add(
186  new ClusterjOp("insB0_" + mode.toString().toLowerCase()) {
187  public void run(int nOps) {
188  if (mode != CrundDriver.XMode.INDY) {
189  beginTransaction();
190  }
191  for (int i = 0; i < nOps; i++) {
192  final IB0 o = session.newInstance(IB0.class);
193  assert o != null;
194  o.setId(i);
195  o.setCvarbinary_def(null);
196  session.persist(o);
197  if (mode == CrundDriver.XMode.EACH) {
198  session.flush();
199  }
200  }
201  if (mode != CrundDriver.XMode.INDY) {
202  commitTransaction();
203  }
204  }
205  });
206 
207  ops.add(
208  new ClusterjOp("setAByPK_" + mode.toString().toLowerCase()) {
209  public void run(int nOps) {
210  if (mode != CrundDriver.XMode.INDY) {
211  beginTransaction();
212  }
213  for (int i = 0; i < nOps; i++) {
214  // blind update
215  final IA o = session.newInstance(IA.class);
216  o.setId(i);
217  assert o != null;
218  o.setCint(i);
219  o.setClong((long)i);
220  o.setCfloat((float)i);
221  o.setCdouble((double)i);
222  session.updatePersistent(o);
223  if (mode == CrundDriver.XMode.EACH) {
224  session.flush();
225  }
226  }
227  if (mode != CrundDriver.XMode.INDY) {
228  commitTransaction();
229  }
230  }
231  });
232 
233  ops.add(
234  new ClusterjOp("setB0ByPK_" + mode.toString().toLowerCase()) {
235  public void run(int nOps) {
236  if (mode != CrundDriver.XMode.INDY) {
237  beginTransaction();
238  }
239  for (int i = 0; i < nOps; i++) {
240  // blind update
241  final IB0 o = session.newInstance(IB0.class);
242  o.setId(i);
243  assert o != null;
244  o.setCint(i);
245  o.setClong((long)i);
246  o.setCfloat((float)i);
247  o.setCdouble((double)i);
248  session.updatePersistent(o);
249  if (mode == CrundDriver.XMode.EACH) {
250  session.flush();
251  }
252  }
253  if (mode != CrundDriver.XMode.INDY) {
254  commitTransaction();
255  }
256  }
257  });
258 
259  ops.add(
260  new ClusterjOp("getAByPK_" + mode.toString().toLowerCase()) {
261  public void run(int nOps) {
262  if (mode != CrundDriver.XMode.INDY) {
263  beginTransaction();
264  }
265  if (mode != CrundDriver.XMode.BULK) {
266  for (int i = 0; i < nOps; i++) {
267  final IA o = session.find(IA.class, i);
268  assert o != null;
269  final int id = o.getId();
270  verify(id == i);
271  final int j = checkFields(o);
272  verify(j == id);
273  }
274  } else {
275  IA[] objs = new IA[nOps];
276  for (int i = 0; i < nOps; ++i) {
277  final IA o = session.newInstance(IA.class, i);
278  objs[i] =o;
279  }
280  session.load(objs);
281  session.flush();
282  for (int i = 0; i < nOps; ++i) {
283  IA o = objs[i];
284  final int id = o.getId();
285  verify(id == i);
286  final int j = checkFields(o);
287  verify (j == id);
288  }
289  }
290  if (mode != CrundDriver.XMode.INDY) {
291  commitTransaction();
292  }
293  }
294  });
295 
296  ops.add(
297  new ClusterjOp("getB0ByPK_" + mode.toString().toLowerCase()) {
298  public void run(int nOps) {
299  if (mode != CrundDriver.XMode.INDY) {
300  beginTransaction();
301  }
302  if (mode != CrundDriver.XMode.BULK) {
303  for (int i = 0; i < nOps; i++) {
304  final IB0 o = session.find(IB0.class, i);
305  assert o != null;
306  final int id = o.getId();
307  verify(id == i);
308  final int j = checkFields(o);
309  verify(j == id);
310  }
311  } else {
312  IB0[] objs = new IB0[nOps];
313  for (int i = 0; i < nOps; ++i) {
314  final IB0 o = session.newInstance(IB0.class, i);
315  objs[i] =o;
316  }
317  session.load(objs);
318  session.flush();
319  for (int i = 0; i < nOps; ++i) {
320  IB0 o = objs[i];
321  final int id = o.getId();
322  verify(id == i);
323  final int j = checkFields(o);
324  verify (j == id);
325  }
326  }
327  if (mode != CrundDriver.XMode.INDY) {
328  commitTransaction();
329  }
330  }
331  });
332 
333  for (int i = 0, l = 1; l <= maxVarbinaryBytes; l *= 10, i++) {
334  final byte[] b = bytes[i];
335  assert l == b.length;
336 
337  ops.add(
338  new ClusterjOp("setVarbinary" + l + "_" + mode.toString().toLowerCase()) {
339  public void run(int nOps) {
340  if (mode != CrundDriver.XMode.INDY) {
341  beginTransaction();
342  }
343  for (int i = 0; i < nOps; i++) {
344  // blind update
345  final IB0 o = session.newInstance(IB0.class);
346  o.setId(i);
347  assert o != null;
348  o.setCvarbinary_def(b);
349  session.updatePersistent(o);
350  if (mode == CrundDriver.XMode.EACH) {
351  session.flush();
352  }
353  }
354  if (mode != CrundDriver.XMode.INDY) {
355  commitTransaction();
356  }
357  }
358  });
359 
360  ops.add(
361  new ClusterjOp("getVarbinary" + l + "_" + mode.toString().toLowerCase()) {
362 
363  // TODO implement BULK using session.load
364  public void run(int nOps) {
365  if (mode != CrundDriver.XMode.INDY) {
366  beginTransaction();
367  }
368  for (int i = 0; i < nOps; i++) {
369  final IB0 o = session.find(IB0.class, i);
370  assert o != null;
371  verify(Arrays.equals(b, o.getCvarbinary_def()));
372  }
373  if (mode != CrundDriver.XMode.INDY) {
374  commitTransaction();
375  }
376  }
377  });
378 
379  ops.add(
380  new ClusterjOp("clearVarbinary" + l + "_" + mode.toString().toLowerCase()) {
381  public void run(int nOps) {
382  if (mode != CrundDriver.XMode.INDY) {
383  beginTransaction();
384  }
385  for (int i = 0; i < nOps; i++) {
386  // blind update
387  final IB0 o = session.newInstance(IB0.class);
388  o.setId(i);
389  assert o != null;
390  o.setCvarbinary_def(null);
391  session.updatePersistent(o);
392  if (mode == CrundDriver.XMode.EACH) {
393  session.flush();
394  }
395  }
396  if (mode != CrundDriver.XMode.INDY) {
397  commitTransaction();
398  }
399  }
400  });
401  }
402 
403  for (int i = 0, l = 1; l <= maxVarcharChars; l *= 10, i++) {
404  final String s = strings[i];
405  assert l == s.length();
406 
407  ops.add(
408  new ClusterjOp("setVarchar" + l + "_" + mode.toString().toLowerCase()) {
409  public void run(int nOps) {
410  if (mode != CrundDriver.XMode.INDY) {
411  beginTransaction();
412  }
413  for (int i = 0; i < nOps; i++) {
414  // blind update
415  final IB0 o = session.newInstance(IB0.class);
416  o.setId(i);
417  assert o != null;
418  o.setCvarchar_def(s);
419  session.updatePersistent(o);
420  if (mode == CrundDriver.XMode.EACH) {
421  session.flush();
422  }
423  }
424  if (mode != CrundDriver.XMode.INDY) {
425  commitTransaction();
426  }
427  }
428  });
429 
430  ops.add(
431  new ClusterjOp("getVarchar" + l + "_" + mode.toString().toLowerCase()) {
432 
433  // TODO implement BULK using session.load
434  public void run(int nOps) {
435  if (mode != CrundDriver.XMode.INDY) {
436  beginTransaction();
437  }
438  for (int i = 0; i < nOps; i++) {
439  final IB0 o = session.find(IB0.class, i);
440  assert o != null;
441  verify(s.equals(o.getCvarchar_def()));
442  }
443  if (mode != CrundDriver.XMode.INDY) {
444  commitTransaction();
445  }
446  }
447  });
448 
449  ops.add(
450  new ClusterjOp("clearVarchar" + l + "_" + mode.toString().toLowerCase()) {
451  public void run(int nOps) {
452  if (mode != CrundDriver.XMode.INDY) {
453  beginTransaction();
454  }
455  for (int i = 0; i < nOps; i++) {
456  // blind update
457  final IB0 o = session.newInstance(IB0.class);
458  o.setId(i);
459  assert o != null;
460  o.setCvarchar_def(null);
461  session.updatePersistent(o);
462  if (mode == CrundDriver.XMode.EACH) {
463  session.flush();
464  }
465  }
466  if (mode != CrundDriver.XMode.INDY) {
467  commitTransaction();
468  }
469  }
470  });
471  }
472 
473  ops.add(
474  new ClusterjOp("setB0->A_" + mode.toString().toLowerCase()) {
475  public void run(int nOps) {
476  if (mode != CrundDriver.XMode.INDY) {
477  beginTransaction();
478  }
479  for (int i = 0; i < nOps; i++) {
480  // blind update
481  final IB0 b0 = session.newInstance(IB0.class);
482  b0.setId(i);
483  assert b0 != null;
484  final int aId = i % nOps;
485  b0.setAid(aId);
486  session.updatePersistent(b0);
487  if (mode == CrundDriver.XMode.EACH) {
488  session.flush();
489  }
490  }
491  if (mode != CrundDriver.XMode.INDY) {
492  commitTransaction();
493  }
494  }
495  });
496 
497  ops.add(
498  new ClusterjOp("navB0->A") {
499  public void run(int nOps) {
500  if (mode != CrundDriver.XMode.INDY) {
501  beginTransaction();
502  }
503  for (int i = 0; i < nOps; i++) {
504  final IB0 b0 = session.find(IB0.class, i);
505  assert b0 != null;
506  int aid = b0.getAid();
507  final IA a = session.find(IA.class, aid);
508  assert a != null;
509  final int id = a.getId();
510  verify(id == i % nOps);
511  final int j = checkFields(a);
512  verify(j == id);
513  }
514  if (mode != CrundDriver.XMode.INDY) {
515  commitTransaction();
516  }
517  }
518  });
519 
520  ops.add(
521  new ClusterjOp("navA->B0") {
522  protected Query<IB0> query;
523 
524  public void init() {
525  super.init();
526  }
527 
528  public void close() {
529  // XXX there's no close() on query, dobj, builder?
530  super.close();
531  }
532 
533  public void run(int nOps) {
534  if (mode != CrundDriver.XMode.INDY) {
535  beginTransaction();
536  }
537  // QueryBuilder is the sessionFactory for queries
538  final QueryBuilder builder
539  = session.getQueryBuilder();
540  // QueryDomainType is the main interface
541  final QueryDomainType<IB0> dobj
542  = builder.createQueryDefinition(IB0.class);
543  // filter by aid
544  dobj.where(dobj.get("aid").equal(dobj.param("aid")));
545  query = session.createQuery(dobj);
546  for (int i = 0; i < nOps; i++) {
547  // find B0s by query
548  query.setParameter("aid", i);
549  // fetch results
550  Collection<IB0> b0s = query.getResultList();
551  assert b0s != null;
552 
553  // check query results
554  verify(b0s.size() > 0);
555  for (IB0 b0 : b0s) {
556  assert b0 != null;
557  final int id = b0.getId();
558  verify(id % nOps == i);
559  final int j = checkFields(b0);
560  verify(j == id);
561  }
562  }
563  if (mode != CrundDriver.XMode.INDY) {
564  commitTransaction();
565  }
566  }
567  });
568 
569  ops.add(
570  new ClusterjOp("nullB0->A") {
571  public void run(int nOps) {
572  if (mode != CrundDriver.XMode.INDY) {
573  beginTransaction();
574  }
575  for (int i = 0; i < nOps; i++) {
576  // blind update
577  final IB0 b0 = session.newInstance(IB0.class);
578  b0.setId(i);
579  assert b0 != null;
580  b0.setAid(0);
581  session.updatePersistent(b0);
582  if (mode == CrundDriver.XMode.EACH) {
583  session.flush();
584  }
585  }
586  if (mode != CrundDriver.XMode.INDY) {
587  commitTransaction();
588  }
589  }
590  });
591 
592  ops.add(
593  new ClusterjOp("delB0ByPK_" + mode.toString().toLowerCase()) {
594  public void run(int nOps) {
595  if (mode != CrundDriver.XMode.INDY) {
596  beginTransaction();
597  }
598  for (int i = 0; i < nOps; i++) {
599  // blind delete
600  final IB0 o = session.newInstance(IB0.class);
601  assert o != null;
602  o.setId(i);
603  session.remove(o);
604  if (mode == CrundDriver.XMode.EACH) {
605  session.flush();
606  }
607  }
608  if (mode != CrundDriver.XMode.INDY) {
609  commitTransaction();
610  }
611  }
612  });
613 
614  ops.add(
615  new ClusterjOp("delAByPK_" + mode.toString().toLowerCase()) {
616  public void run(int nOps) {
617  if (mode != CrundDriver.XMode.INDY) {
618  beginTransaction();
619  }
620  for (int i = 0; i < nOps; i++) {
621  // blind delete
622  final IA o = session.newInstance(IA.class);
623  assert o != null;
624  o.setId(i);
625  session.remove(o);
626  if (mode == CrundDriver.XMode.EACH) {
627  session.flush();
628  }
629  }
630  if (mode != CrundDriver.XMode.INDY) {
631  commitTransaction();
632  }
633  }
634  });
635 
636  ops.add(
637  new ClusterjOp("insAattr_" + mode.toString().toLowerCase()) {
638  public void run(int nOps) {
639  if (mode != CrundDriver.XMode.INDY) {
640  beginTransaction();
641  }
642  for (int i = 0; i < nOps; i++) {
643  final IA o = session.newInstance(IA.class);
644  assert o != null;
645  o.setId(i);
646  o.setCint(-i);
647  o.setClong((long)-i);
648  o.setCfloat((float)-i);
649  o.setCdouble((double)-i);
650  session.persist(o);
651  if (mode == CrundDriver.XMode.EACH) {
652  session.flush();
653  }
654  }
655  if (mode != CrundDriver.XMode.INDY) {
656  commitTransaction();
657  }
658  }
659  });
660 
661  ops.add(
662  new ClusterjOp("insB0attr_" + mode.toString().toLowerCase()) {
663  public void run(int nOps) {
664  if (mode != CrundDriver.XMode.INDY) {
665  beginTransaction();
666  }
667  for (int i = 0; i < nOps; i++) {
668  final IB0 o = session.newInstance(IB0.class);
669  assert o != null;
670  o.setId(i);
671  o.setCint(-i);
672  o.setClong((long)-i);
673  o.setCfloat((float)-i);
674  o.setCdouble((double)-i);
675  o.setCvarbinary_def(null);
676  session.persist(o);
677  if (mode == CrundDriver.XMode.EACH) {
678  session.flush();
679  }
680  }
681  if (mode != CrundDriver.XMode.INDY) {
682  commitTransaction();
683  }
684  }
685  });
686 
687  ops.add(
688  new ClusterjOp("delAllB0_" + mode.toString().toLowerCase()) {
689  public void run(int nOps) {
690  if (mode != CrundDriver.XMode.INDY) {
691  beginTransaction();
692  }
693  int del = session.deletePersistentAll(IB0.class);
694  assert del == nOps;
695  if (mode != CrundDriver.XMode.INDY) {
696  commitTransaction();
697  }
698  }
699  });
700 
701  ops.add(
702  new ClusterjOp("delAllA_" + mode.toString().toLowerCase()) {
703  public void run(int nOps) {
704  if (mode != CrundDriver.XMode.INDY) {
705  beginTransaction();
706  }
707  int del = session.deletePersistentAll(IA.class);
708  assert del == nOps;
709  if (mode != CrundDriver.XMode.INDY) {
710  commitTransaction();
711  }
712  }
713  });
714 
715  }
716  // prepare queries
717  for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
718  ((ClusterjOp)i.next()).init();
719  }
720 
721  out.println(" [ClusterjOp: " + ops.size() + "]");
722  }
723 
724  protected void closeOperations() {
725  out.print("closing operations ...");
726  out.flush();
727 
728  // close all queries
729  for (Iterator<CrundDriver.Op> i = ops.iterator(); i.hasNext();) {
730  ((ClusterjOp)i.next()).close();
731  }
732  ops.clear();
733 
734  out.println(" [ok]");
735  }
736 
737  protected void beginTransaction() {
738  session.currentTransaction().begin();
739  }
740 
741  protected void commitTransaction() {
742  session.currentTransaction().commit();
743  }
744 
745  // ----------------------------------------------------------------------
746  // ClusterJ datastore operations
747  // ----------------------------------------------------------------------
748 
749  protected void initConnection() {
750  out.println();
751  out.print("creating ClusterJ Session ...");
752  out.flush();
753  session = sessionFactory.getSession();
754  out.println(" [Session: 1]");
755  }
756 
757  protected void closeConnection() {
758  out.println();
759  out.print("closing ClusterJ Session ...");
760  out.flush();
761  if (session != null)
762  session.close();
763  session = null;
764  out.println(" [ok]");
765  }
766 
767  protected void clearPersistenceContext() {
768  // nothing to do as long as we're not caching beyond Tx scope
769  }
770 
771  protected void clearData() {
772  out.print("deleting all objects ...");
773  out.flush();
774 
775  session.currentTransaction().begin();
776  int delB0 = session.deletePersistentAll(IB0.class);
777  out.print(" [B0: " + delB0);
778  out.flush();
779  int delA = session.deletePersistentAll(IA.class);
780  out.print(", A: " + delA);
781  out.flush();
782  session.currentTransaction().commit();
783 
784  out.println("]");
785  }
786 
787  // ----------------------------------------------------------------------
788 
789  static public void main(String[] args) {
790  System.out.println("ClusterjLoad.main()");
791  parseArguments(args);
792  new ClusterjLoad().run();
793  System.out.println();
794  System.out.println("ClusterjLoad.main(): done.");
795  }
796 }