/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.stress;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.objectweb.speedo.pobjects.basic.BasicA;
import org.objectweb.speedo.pobjects.collection.AMMB;
import org.objectweb.speedo.pobjects.collection.BMMB;
import org.objectweb.speedo.pobjects.collection.Ref2Ref2AMMB;
import org.objectweb.speedo.pobjects.ref.Department;
import org.objectweb.speedo.pobjects.ref.Employee;
import org.objectweb.speedo.pobjects.userid.AutoIncFieldId;
import org.objectweb.speedo.pobjects.userid.Ref2AutoIncFieldId;
import org.objectweb.speedo.runtime.query.POBuilder;
import org.objectweb.speedo.runtime.query.PORemover;
import org.objectweb.util.monolog.api.BasicLevel;
/**
*
* @author M. Guillemin
*/
public abstract class QueryHelper extends StressHelper {
/**
* is the lists of object identifier prepared before the transaction
* execution.
* if (oids == null) {
* db is not initialised
* } else if (oids != null && oids.length==0) {
* db initialised and keepOid = false
* } else {
* db initialised and keepOid == true
* }
*/
protected static Object[] oids = null;
protected String DBSIZE = getLoggerName() + ".dbsize";
protected String NO_DB_INIT = getLoggerName() + ".nodbinit";
public QueryHelper(String s) {
super(s);
}
protected String[] getClassNamesToInit() {
return new String[]{Employee.class.getName(),
Department.class.getName(),
AMMB.class.getName(),
BMMB.class.getName(),
Ref2Ref2AMMB.class.getName(),
Ref2AutoIncFieldId.class.getName(),
AutoIncFieldId.class.getName(),
BasicA.class.getName()};
};
protected boolean keepOid() {
return false;
}
/*
public void setUp() throws Exception {
logger.log(BasicLevel.DEBUG, "setUp.");
cleanup();
initDataStructure(true);
debug = logger.isLoggable(BasicLevel.DEBUG);
}
*/
protected void prepareTest(TaskManager tm, Object ctx) {
super.prepareTest(tm, ctx);
if (oids!=null) {
oids=null; // force initialization of the database in PrepareTask
// delete previous data
new PORemover(getName()).testRemovingOfPersistentObject();
}
}
/**
* Creates the persistent object if it is not already done.
* @param task the task to prepare
* @param _ctx the context of the test.
*/
protected void prepareTask(Task task, Object _ctx) {
super.prepareTask(task, _ctx);
QueryCtx ctx = (QueryCtx) _ctx;
if (oids==null) {
synchronized (getClass()) {
if (oids == null && !Boolean.getBoolean(NO_DB_INIT)) {
//Initialisation the database
logger.log(BasicLevel.INFO, "\tPreparing test...");
new POBuilder(getName()).testCreationOfPersistentObject();
//db initialized without oids
oids = new Object[0];
logger.log(BasicLevel.INFO, "\tTest Prepared.");
}
}
}
}
/**
* The context to use for the object
*/
public class QueryCtx {
public int queries[];
public QueryCtx(String v) {
logger.log(BasicLevel.DEBUG, "querysubset="+v);
ArrayList al = new ArrayList(35);
if (v != null) { // get the subset of the queries
v = v.trim();
if (!v.startsWith("$")) {
StringTokenizer st = new StringTokenizer(v, ", ", false);
while(st.hasMoreTokens()) {
try {
al.add(new Integer(st.nextToken()));
} catch (NumberFormatException e) {
}
}
}
}
if (al.size() == 0) { // All the queries could be executed
for(int i=0;i<35; i++) {
al.add(new Integer(i));
}
}
queries = new int[al.size()];
for(int i=(al.size()-1); i>=0; i--) {
queries[i] = ((Integer) al.get(i)).intValue();
//logger.log(BasicLevel.DEBUG, "queries["+i+"]="+queries[i]);
}
}
public String toString() {
return "queries = " + queries;
}
}
}