1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
4 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
20 package com.mysql.cluster.benchmark.tws;
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;
32 import java.util.Iterator;
35 class ClusterjLoad extends TwsLoad {
38 protected SessionFactory sessionFactory;
39 protected Session session;
42 public ClusterjLoad(TwsDriver driver, MetaData md) {
46 // ----------------------------------------------------------------------
47 // ClusterJ intializers/finalizers
48 // ----------------------------------------------------------------------
50 protected void initProperties() {
52 out.print("setting clusterj properties ...");
54 final StringBuilder msg = new StringBuilder();
55 final String eol = System.getProperty("line.separator");
57 // check required properties
59 = driver.props.getProperty(Constants.PROPERTY_CLUSTER_CONNECTSTRING);
61 if (msg.length() == 0) {
65 out.print(msg.toString());
68 // have mgmdConnect initialized first
69 descr = "clusterj(" + mgmdConnect + ")";
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());
86 public void init() throws Exception {
88 assert (sessionFactory == null);
90 // load native library (better diagnostics doing it explicitely)
91 //loadSystemLibrary("ndbclient");
93 out.print("creating SessionFactory ...");
95 sessionFactory = ClusterJHelper.getSessionFactory(driver.props);
99 public void close() throws Exception {
100 assert (sessionFactory != null);
103 out.print("closing SessionFactory ...");
105 sessionFactory.close();
106 sessionFactory = null;
107 out.println(" [ok]");
112 // ----------------------------------------------------------------------
113 // ClusterJ datastore operations
114 // ----------------------------------------------------------------------
116 public void initConnection() {
117 assert (sessionFactory != null);
118 assert (session == null);
121 out.println("initializing clusterj resources ...");
123 out.print("starting clusterj session ...");
125 session = sessionFactory.getSession();
126 out.println(" [ok]");
128 out.print("setting session lock mode ...");
129 session.setLockMode(driver.lockMode);
130 out.println(" [ok: " + driver.lockMode + "]");
133 public void closeConnection() {
134 assert (session != null);
137 out.println("releasing clusterj resources ...");
139 out.print("closing clusterj session ...");
143 out.println(" [ok]");
146 // ----------------------------------------------------------------------
148 public void runOperations() {
150 out.println("running ClusterJ operations ..."
151 + " [nRows=" + driver.nRows + "]");
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);
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);
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);
173 // ----------------------------------------------------------------------
175 protected void runClusterjInsert(TwsDriver.XMode mode) {
176 final String name = "insert_" + mode.toString().toLowerCase();
179 if (mode != TwsDriver.XMode.SINGLE)
180 session.currentTransaction().begin();
181 for(int i = 0; i < driver.nRows; i++) {
183 if (mode == TwsDriver.XMode.BULK)
186 if (mode != TwsDriver.XMode.SINGLE)
187 session.currentTransaction().commit();
192 protected void clusterjInsert(int c0) {
194 assert(metaData != null);
196 final CJSubscriber o = session.newInstance(CJSubscriber.class);
198 final String str = Integer.toString(i);
199 //final String oneChar = Integer.toString(1);
201 int width = metaData.getColumnWidth(1);
202 o.setC1(fixedStr.substring(0, width));
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));
229 // ----------------------------------------------------------------------
231 protected void runClusterjLookup(TwsDriver.XMode mode) {
232 assert(mode != TwsDriver.XMode.BATCH);
234 final String name = "lookup_" + mode.toString().toLowerCase();
237 if (mode != TwsDriver.XMode.SINGLE)
238 session.currentTransaction().begin();
239 for(int i = 0; i < driver.nRows; i++) {
242 if (mode != TwsDriver.XMode.SINGLE)
243 session.currentTransaction().commit();
248 protected void clusterjLookup(int c0) {
250 = session.find(CJSubscriber.class, Integer.toString(c0));
252 // not verifying at this time
253 String ac0 = o.getC0();
254 String c1 = o.getC1();
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();
271 // ----------------------------------------------------------------------
273 protected void runClusterjUpdate(TwsDriver.XMode mode) {
274 final String name = "update_" + mode.toString().toLowerCase();
277 if (mode != TwsDriver.XMode.SINGLE)
278 session.currentTransaction().begin();
279 for(int i = 0; i < driver.nRows; i++) {
281 if (mode == TwsDriver.XMode.BULK)
284 if (mode != TwsDriver.XMode.SINGLE)
285 session.currentTransaction().commit();
290 protected void clusterjUpdate(int c0) {
291 final String str0 = Integer.toString(c0);
293 final String str1 = Integer.toString(r);
296 final CJSubscriber o = session.newInstance(CJSubscriber.class);
298 //final CJSubscriber o = session.find(CJSubscriber.class, str0);
299 //String oneChar = Integer.toString(2);
314 session.updatePersistent(o);
317 // ----------------------------------------------------------------------
319 protected void runClusterjDelete(TwsDriver.XMode mode) {
320 final String name = "delete_" + mode.toString().toLowerCase();
323 if (mode != TwsDriver.XMode.SINGLE)
324 session.currentTransaction().begin();
325 for(int i = 0; i < driver.nRows; i++) {
327 if (mode == TwsDriver.XMode.BULK)
330 if (mode != TwsDriver.XMode.SINGLE)
331 session.currentTransaction().commit();
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));
344 // ----------------------------------------------------------------------
346 @PersistenceCapable(table="mytable")
347 //@Index(name="c0_UNIQUE")
348 static public interface CJSubscriber {
351 void setC0(String c0);
353 @Index(name="c1_UNIQUE")
355 void setC1(String c1);
357 @Index(name="c2_UNIQUE")
368 void setC5(String c5);
371 void setC6(String c6);
373 @Index(name="c7_UNIQUE")
375 void setC7(String c7);
377 @Index(name="c8_UNIQUE")
379 void setC8(String c8);
382 void setC9(String c9);
385 void setC10(String c10);
388 void setC11(String c11);
391 void setC12(String c12);
394 void setC13(String c13);
397 void setC14(String c14);