com.groiss.store
Class BulkQuery

java.lang.Object
  extended by com.groiss.store.BulkQuery

public class BulkQuery
extends java.lang.Object

Allows efficient execution of queries for a set of items.

Suppose one has a Collection of items and wants to pose a query to the database for each of those items. This can be quite a performance and throughput hog, since the overhead of a query execution is payed for each single item:

 select 1 from atable where y='red' and x=1;
 select 1 from atable where y='red' and x=2;
 select 1 from atable where y='red' and x=3;
 ...
 

Often, the situation can be dealt with quite efficiently by using an IN-query.

 select x from atable where y='red' and x IN (1,2,3,...);
 

The BulkQuery class provides a convenient abstraction to use this metaphor. The typical use would be as follows:

Example:

 BulkQuery bQ = new BulkQuery();
 for (Enumeration elems = v.elements(); elems.hasMoreElements();) {
   MyElement e = (MyElement) elems.nextElement();
   bQ.add(Long.toString(MyElement.getOid()));
 }
 String query = "select x from atable where y='red' and x in (?)";
 Map result = bQ.execute(query,1);
 for (Enumeration elems = v.elements(); elems.hasMoreElements();) {
   MyElement e = (MyElement) elems.nextElement();
   if (result.containsKey(Long.toString(MyElement.getOid()))) {
     ...
   } else {
     ...
 }
 

But the BulkQuery can also be used if a collection of persistent objects should be retrieved using an IN-query for better performance. The usage is quite similar to the example above, only an other 'execute' method (execute(Class,String)) must be used to achive this goal.

Example:

 BulkQuery bQ = new BulkQuery();
 for (Enumeration elems = v.elements(); elems.hasMoreElements();) {
   MyElement e = (MyElement) elems.nextElement();
   MyReferencedElement ref = e.getReferencedElement();
   bQ.add(Long.toString(ref.getOid()));
 }
 String condition = "oid in (?)";
 Collection result = bQ.execute(MyReferencedElement.class, condition);
 ...
 

If you happen to have a conveniently structured pre-splitted list of values already at hand (like the splitResult when Worklist.getAdditionalData(List,Vector) is called), you can construct a BulkQuery with the alternative Constructor. The elements of the Collection are Strings which contain comma separated values.

Example:

 BulkQuery bQ = new BulkQuery(splitResult,true);
 String aquery = "select oid from a_table where process in (?)";
 Map result1 = bQ.execute(bquery,1);
 ...
 String bquery = "select oid from b_table where process in (?)";
 Map result1 = bQ.execute(bquery,1);
 ...
 


Constructor Summary
BulkQuery()
          Construct a fresh BulkQuery.
BulkQuery(java.util.Collection c)
          Construct a fresh BulkQuery and add each of the elements of the Collection.
BulkQuery(java.util.Collection c, boolean isSplitted)
          Construct a fresh BulkQuery and add the elements of the Collection.
 
Method Summary
 void add(java.lang.String s)
          Add a String to the query.
 java.util.Collection execute(java.lang.Class c, java.lang.String condition)
          Execute the condition based on the current elements.
 java.util.Map execute(java.lang.String query, int pos)
          Execute the query based on the current elements.
 java.util.Collection execute2(java.lang.Class c, java.lang.String q)
          Execute the query based on the current elements.
static int getSplitSize()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BulkQuery

public BulkQuery()
Construct a fresh BulkQuery.


BulkQuery

public BulkQuery(java.util.Collection c)
Construct a fresh BulkQuery and add each of the elements of the Collection. The elements must either be String objects or implement the Persistent interface.

Parameters:
c - the Collection whose elements are added to the BulkQuery.

BulkQuery

public BulkQuery(java.util.Collection c,
                 boolean isSplitted)
Construct a fresh BulkQuery and add the elements of the Collection.

Parameters:
c - the Collection whose elements are added to the BulkQuery.
isSplitted - if false, each of the (String or Persistent) elements of the Collection c are added; behaves like BulkQuery(Collection). If isSplitted is true, the Collection is assumed to be already splitted to a convenient size. Each of the elements of the Collection must be a String in the form of a comma separated list of values. No single String should have more than getSplitSize() values, else the query might fail.
Method Detail

add

public void add(java.lang.String s)
Add a String to the query.

Parameters:
s - the String to be added.

getSplitSize

public static int getSplitSize()

execute

public java.util.Map execute(java.lang.String query,
                             int pos)
Execute the query based on the current elements.

The query text must be a valid SQL statement. A question mark ? value in this string indicates the position of the varying parameters (the IN-List). The pos argument indicates which column of the SQL ResultSet should be used to construct the returned Map object.

Parameters:
query - The query text.
pos - The position in the queries ResultSet which should be used for construction of the result.
Returns:
A Map of the results of the query..

execute

public java.util.Collection execute(java.lang.Class c,
                                    java.lang.String condition)
Execute the condition based on the current elements.

The condition text must be a valid SQL condition statement (whithout keyword 'where'). A question mark ? value in this string indicates the position of the varying parameters (the IN-List). The c argument indicates on which persistent object the condition should be applied

Parameters:
c - the class which instances are wanted as the result (c must implement PersistentObject)
condition - the condition for filtering whithin the instances.
Returns:
A collection of instances of the passed class which match the specified condition

execute2

public java.util.Collection execute2(java.lang.Class c,
                                     java.lang.String q)
Execute the query based on the current elements.

The query text must be a valid SQL query. A question mark ? value in this string indicates the position of the varying parameters (the IN-List). The c argument indicates on which persistent object the query should be applied

Parameters:
c - the class which instances are wanted as the result (c must implement PersistentObject)
q - a SQL query like in Store.list2(Class, String, Object[])
Returns:
A collection of instances of the passed class which match the specified condition


Copyright © 2001-2006 Groiss Informatics GmbH. All Rights Reserved.