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, 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.benchmark.tws;
21 
22 import com.mysql.clusterj.ClusterJHelper;
23 import com.mysql.clusterj.SessionFactory;
24 import com.mysql.clusterj.Session;
25 import com.mysql.clusterj.LockMode;
26 import com.mysql.clusterj.Constants;
27 import com.mysql.clusterj.annotation.Index;
28 import com.mysql.clusterj.annotation.PersistenceCapable;
29 import com.mysql.clusterj.annotation.PrimaryKey;
30 
31 import java.util.Map;
32 import java.util.Iterator;
33 
34 
35 class ClusterjLoad extends TwsLoad {
36 
37  // ClusterJ resources
38  protected SessionFactory sessionFactory;
39  protected Session session;
40 
41 
42  public ClusterjLoad(TwsDriver driver, MetaData md) {
43  super(driver, md);
44  }
45 
46  // ----------------------------------------------------------------------
47  // ClusterJ intializers/finalizers
48  // ----------------------------------------------------------------------
49 
50  protected void initProperties() {
51  out.println();
52  out.print("setting clusterj properties ...");
53 
54  final StringBuilder msg = new StringBuilder();
55  final String eol = System.getProperty("line.separator");
56 
57  // check required properties
58  String mgmdConnect
59  = driver.props.getProperty(Constants.PROPERTY_CLUSTER_CONNECTSTRING);
60 
61  if (msg.length() == 0) {
62  out.println(" [ok]");
63  } else {
64  out.println();
65  out.print(msg.toString());
66  }
67 
68  // have mgmdConnect initialized first
69  descr = "clusterj(" + mgmdConnect + ")";
70  }
71 
72  protected void printProperties() {
73  for (Iterator<Map.Entry<Object,Object>> i
74  = driver.props.entrySet().iterator(); i.hasNext();) {
75  Map.Entry<Object,Object> e = i.next();
76  final String k = (String)e.getKey();
77  if (k.startsWith("com.mysql.clusterj")) {
78  final StringBuilder s = new StringBuilder("..");
79  s.append(k, 18, k.length());
80  while (s.length() < 31) s.append(' ');
81  out.println(s + " " + e.getValue());
82  }
83  }
84  }
85 
86  public void init() throws Exception {
87  super.init();
88  assert (sessionFactory == null);
89 
90  // load native library (better diagnostics doing it explicitely)
91  //loadSystemLibrary("ndbclient");
92 
93  out.print("creating SessionFactory ...");
94  out.flush();
95  sessionFactory = ClusterJHelper.getSessionFactory(driver.props);
96  out.println(" [ok]");
97  }
98 
99  public void close() throws Exception {
100  assert (sessionFactory != null);
101 
102  out.println();
103  out.print("closing SessionFactory ...");
104  out.flush();
105  sessionFactory.close();
106  sessionFactory = null;
107  out.println(" [ok]");
108 
109  super.close();
110  }
111 
112  // ----------------------------------------------------------------------
113  // ClusterJ datastore operations
114  // ----------------------------------------------------------------------
115 
116  public void initConnection() {
117  assert (sessionFactory != null);
118  assert (session == null);
119 
120  out.println();
121  out.println("initializing clusterj resources ...");
122 
123  out.print("starting clusterj session ...");
124  out.flush();
125  session = sessionFactory.getSession();
126  out.println(" [ok]");
127 
128  out.print("setting session lock mode ...");
129  session.setLockMode(driver.lockMode);
130  out.println(" [ok: " + driver.lockMode + "]");
131  }
132 
133  public void closeConnection() {
134  assert (session != null);
135 
136  out.println();
137  out.println("releasing clusterj resources ...");
138 
139  out.print("closing clusterj session ...");
140  out.flush();
141  session.close();
142  session = null;
143  out.println(" [ok]");
144  }
145 
146  // ----------------------------------------------------------------------
147 
148  public void runOperations() {
149  out.println();
150  out.println("running ClusterJ operations ..."
151  + " [nRows=" + driver.nRows + "]");
152 
153  if (driver.doSingle) {
154  if (driver.doInsert) runClusterjInsert(TwsDriver.XMode.SINGLE);
155  if (driver.doLookup) runClusterjLookup(TwsDriver.XMode.SINGLE);
156  if (driver.doUpdate) runClusterjUpdate(TwsDriver.XMode.SINGLE);
157  if (driver.doDelete) runClusterjDelete(TwsDriver.XMode.SINGLE);
158  }
159  if (driver.doBulk) {
160  if (driver.doInsert) runClusterjInsert(TwsDriver.XMode.BULK);
161  if (driver.doLookup) runClusterjLookup(TwsDriver.XMode.BULK);
162  if (driver.doUpdate) runClusterjUpdate(TwsDriver.XMode.BULK);
163  if (driver.doDelete) runClusterjDelete(TwsDriver.XMode.BULK);
164  }
165  if (driver.doBatch) {
166  if (driver.doInsert) runClusterjInsert(TwsDriver.XMode.BATCH);
167  //if (driver.doLookup) runClusterjLookup(TwsDriver.XMode.BATCH);
168  if (driver.doUpdate) runClusterjUpdate(TwsDriver.XMode.BATCH);
169  if (driver.doDelete) runClusterjDelete(TwsDriver.XMode.BATCH);
170  }
171  }
172 
173  // ----------------------------------------------------------------------
174 
175  protected void runClusterjInsert(TwsDriver.XMode mode) {
176  final String name = "insert_" + mode.toString().toLowerCase();
177  driver.begin(name);
178 
179  if (mode != TwsDriver.XMode.SINGLE)
180  session.currentTransaction().begin();
181  for(int i = 0; i < driver.nRows; i++) {
182  clusterjInsert(i);
183  if (mode == TwsDriver.XMode.BULK)
184  session.flush();
185  }
186  if (mode != TwsDriver.XMode.SINGLE)
187  session.currentTransaction().commit();
188 
189  driver.finish(name);
190  }
191 
192  protected void clusterjInsert(int c0) {
193 
194  assert(metaData != null);
195 
196  final CJSubscriber o = session.newInstance(CJSubscriber.class);
197  final int i = c0;
198  final String str = Integer.toString(i);
199  //final String oneChar = Integer.toString(1);
200  o.setC0(str);
201  int width = metaData.getColumnWidth(1);
202  o.setC1(fixedStr.substring(0, width));
203  o.setC2(i);
204  o.setC3(i);
205  //o.setC4(i);
206  width = metaData.getColumnWidth(5);
207  o.setC5(fixedStr.substring(0, width));
208  width = metaData.getColumnWidth(6);
209  o.setC6(fixedStr.substring(0, width));
210  width = metaData.getColumnWidth(7);
211  o.setC7(fixedStr.substring(0, width));
212  width = metaData.getColumnWidth(8);
213  o.setC8(fixedStr.substring(0, width));
214  width = metaData.getColumnWidth(9);
215  o.setC9(fixedStr.substring(0, width));
216  width = metaData.getColumnWidth(10);
217  o.setC10(fixedStr.substring(0, width));
218  width = metaData.getColumnWidth(11);
219  o.setC11(fixedStr.substring(0, width));
220  width = metaData.getColumnWidth(12);
221  o.setC12(fixedStr.substring(0, width));
222  width = metaData.getColumnWidth(13);
223  o.setC13(fixedStr.substring(0, width));
224  width = metaData.getColumnWidth(14);
225  o.setC14(fixedStr.substring(0, width));
226  session.persist(o);
227  }
228 
229  // ----------------------------------------------------------------------
230 
231  protected void runClusterjLookup(TwsDriver.XMode mode) {
232  assert(mode != TwsDriver.XMode.BATCH);
233 
234  final String name = "lookup_" + mode.toString().toLowerCase();
235  driver.begin(name);
236 
237  if (mode != TwsDriver.XMode.SINGLE)
238  session.currentTransaction().begin();
239  for(int i = 0; i < driver.nRows; i++) {
240  clusterjLookup(i);
241  }
242  if (mode != TwsDriver.XMode.SINGLE)
243  session.currentTransaction().commit();
244 
245  driver.finish(name);
246  }
247 
248  protected void clusterjLookup(int c0) {
249  final CJSubscriber o
250  = session.find(CJSubscriber.class, Integer.toString(c0));
251  if (o != null) {
252  // not verifying at this time
253  String ac0 = o.getC0();
254  String c1 = o.getC1();
255  int c2 = o.getC2();
256  int c3 = o.getC3();
257  int c4 = o.getC4();
258  String c5 = o.getC5();
259  String c6 = o.getC6();
260  String c7 = o.getC7();
261  String c8 = o.getC8();
262  String c9 = o.getC9();
263  String c10 = o.getC10();
264  String c11 = o.getC11();
265  String c12 = o.getC12();
266  String c13 = o.getC13();
267  String c14 = o.getC14();
268  }
269  }
270 
271  // ----------------------------------------------------------------------
272 
273  protected void runClusterjUpdate(TwsDriver.XMode mode) {
274  final String name = "update_" + mode.toString().toLowerCase();
275  driver.begin(name);
276 
277  if (mode != TwsDriver.XMode.SINGLE)
278  session.currentTransaction().begin();
279  for(int i = 0; i < driver.nRows; i++) {
280  clusterjUpdate(i);
281  if (mode == TwsDriver.XMode.BULK)
282  session.flush();
283  }
284  if (mode != TwsDriver.XMode.SINGLE)
285  session.currentTransaction().commit();
286 
287  driver.finish(name);
288  }
289 
290  protected void clusterjUpdate(int c0) {
291  final String str0 = Integer.toString(c0);
292  final int r = -c0;
293  final String str1 = Integer.toString(r);
294 
295  // blind update
296  final CJSubscriber o = session.newInstance(CJSubscriber.class);
297  o.setC0(str0);
298  //final CJSubscriber o = session.find(CJSubscriber.class, str0);
299  //String oneChar = Integer.toString(2);
300  o.setC1(str1);
301  o.setC2(r);
302  o.setC3(r);
303  //o.setC4(r);
304  o.setC5(str1);
305  o.setC6(str1);
306  o.setC7(str1);
307  o.setC8(str1);
308  //o.setC9(oneChar);
309  //o.setC10(oneChar);
310  //o.setC11(str);
311  //o.setC12(str);
312  //o.setC13(oneChar);
313  //o.setC14(str);
314  session.updatePersistent(o);
315  }
316 
317  // ----------------------------------------------------------------------
318 
319  protected void runClusterjDelete(TwsDriver.XMode mode) {
320  final String name = "delete_" + mode.toString().toLowerCase();
321  driver.begin(name);
322 
323  if (mode != TwsDriver.XMode.SINGLE)
324  session.currentTransaction().begin();
325  for(int i = 0; i < driver.nRows; i++) {
326  clusterjDelete(i);
327  if (mode == TwsDriver.XMode.BULK)
328  session.flush();
329  }
330  if (mode != TwsDriver.XMode.SINGLE)
331  session.currentTransaction().commit();
332 
333  driver.finish(name);
334  }
335 
336  protected void clusterjDelete(int c0) {
337  // XXX use new API for blind delete
338  final CJSubscriber o = session.newInstance(CJSubscriber.class);
339  o.setC0(Integer.toString(c0));
340  assert o != null;
341  session.remove(o);
342  }
343 
344  // ----------------------------------------------------------------------
345 
346  @PersistenceCapable(table="mytable")
347  //@Index(name="c0_UNIQUE")
348  static public interface CJSubscriber {
349  @PrimaryKey
350  String getC0();
351  void setC0(String c0);
352 
353  @Index(name="c1_UNIQUE")
354  String getC1();
355  void setC1(String c1);
356 
357  @Index(name="c2_UNIQUE")
358  int getC2();
359  void setC2(int c2);
360 
361  int getC3();
362  void setC3(int c3);
363 
364  int getC4();
365  void setC4(int c4);
366 
367  String getC5();
368  void setC5(String c5);
369 
370  String getC6();
371  void setC6(String c6);
372 
373  @Index(name="c7_UNIQUE")
374  String getC7();
375  void setC7(String c7);
376 
377  @Index(name="c8_UNIQUE")
378  String getC8();
379  void setC8(String c8);
380 
381  String getC9();
382  void setC9(String c9);
383 
384  String getC10();
385  void setC10(String c10);
386 
387  String getC11();
388  void setC11(String c11);
389 
390  String getC12();
391  void setC12(String c12);
392 
393  String getC13();
394  void setC13(String c13);
395 
396  String getC14();
397  void setC14(String c14);
398  }
399 
400 }