Nux 1.0a5

nux.xom.pool
Class XQueryPool

java.lang.Object
  extended bynux.xom.pool.XQueryPool

public class XQueryPool
extends Object

Efficient thread-safe pool/cache of XQuery objects, creating and holding at most maxPoolEntries XQuery objects (each representing a compiled query). On cache miss, a new object is created via a factory, cached for future reuse, and then returned. Pool eviction is based on a LRU (least recently used) policy.

This class helps to avoid the large overhead involved in constructing (i.e. compiling) an XQuery instance, in particul for complex queries over small input XML documents. Most useful in high throughput server container environments (e.g. large-scale Peer-to-Peer messaging network infrastructures over high-bandwidth networks, scalable MOMs, etc).

Example usage (in any arbitrary thread and any arbitrary object):

     XQuery xquery = XQueryPool.GLOBAL_POOL.getXQuery(new File("/tmp/test.xq"));
     //XQuery xquery = XQueryPool.GLOBAL_POOL.getXQuery("for $i in /* return $i/headline_text", null);
     Document doc = BuilderPool.GLOBAL_POOL.getBuilder(false).build(new File("/tmp/test.xml"));
     Nodes results = xquery.execute(doc).toNodes();
     for (int i=0; i < results.size(); i++) {
         System.out.println("node "+i+": "+results.get(i).toXML());
     }
 

Note: Internally uses extremely short-lived locks; the resulting potential lock contention is completely negligible.

Author:
whoschek.AT.lbl.DOT.gov, $Author: hoschek3 $

Field Summary
static XQueryPool GLOBAL_POOL
          A default pool (can be shared freely across threads without harm); global per class loader.
 
Constructor Summary
XQueryPool()
          Creates a new pool with default parameters.
XQueryPool(int maxPoolEntries, XQueryFactory factory)
          Creates a new pool that uses the given factory on cache misses, and that can hold at most maxPoolEntries queries.
 
Method Summary
 XQuery getXQuery(File query)
          Returns an XQuery for the given input query.
 XQuery getXQuery(ResourceResolver resolver, String resourceName, URI baseURI)
          Returns an XQuery for the input stream obtained from resolving the given resourceName against the given resolver.
 XQuery getXQuery(String query, URI baseURI)
          Returns an XQuery for the given input query, using the given base URI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GLOBAL_POOL

public static final XQueryPool GLOBAL_POOL
A default pool (can be shared freely across threads without harm); global per class loader.

Constructor Detail

XQueryPool

public XQueryPool()
Creates a new pool with default parameters.


XQueryPool

public XQueryPool(int maxPoolEntries,
                  XQueryFactory factory)
Creates a new pool that uses the given factory on cache misses, and that can hold at most maxPoolEntries queries.

Parameters:
maxPoolEntries - the maximum number of queries this pool can hold before starting to evict old queries. A value of zero effectively disables pooling.
factory - the factory creating new XQuery instances on cache misses
Method Detail

getXQuery

public XQuery getXQuery(File query)
                 throws XQueryException,
                        IOException
Returns an XQuery for the given input query.

Parameters:
query - the query to compile
Throws:
IOException - if an I/O error occured while reading the query.
XQueryException - if the query cannot be compiled, e.g. due too a syntax error.

getXQuery

public XQuery getXQuery(String query,
                        URI baseURI)
                 throws XQueryException
Returns an XQuery for the given input query, using the given base URI.

Parameters:
query - the query to compile
baseURI - the base URI of the query (may be null)
Throws:
XQueryException - if the query cannot be compiled, e.g. due too a syntax error.

getXQuery

public XQuery getXQuery(ResourceResolver resolver,
                        String resourceName,
                        URI baseURI)
                 throws XQueryException,
                        IOException,
                        MissingResourceException
Returns an XQuery for the input stream obtained from resolving the given resourceName against the given resolver.

Parameters:
resolver - an object that can produce an input stream for a given resource name.
resourceName - the resource name (e.g. a path or URL)
baseURI - the base URI of the query (may be null)
Throws:
MissingResourceException - if the resolver could not find the resource (unchecked exception)
IOException - if an I/O error occured while reading the query from the stream
XQueryException - if the query cannot be compiled, e.g. due too a syntax error

Nux 1.0a5