Author: ppoddar
Date: Thu Feb 14 14:16:45 2008
New Revision: 627888

URL: http://svn.apache.org/viewvc?rev=627888&view=rev
Log:
OPENJPA-515 Add support for targeted query via query hints

Modified:
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/Slice.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreManager.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreQuery.java
    
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
    
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
 Thu Feb 14 14:16:45 2008
@@ -50,6 +50,7 @@
         * operating set. The slice is assigned only if a StateManager has never
         * been assigned before.
         */
+       @Override
        public OpenJPAStateManager persist(Object pc, Object id, boolean 
explicit,
                        OpCallbacks call) {
                OpenJPAStateManager sm = getStateManager(pc);

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java
 Thu Feb 14 14:16:45 2008
@@ -38,11 +38,11 @@
        /**
         * Gets the active slice identifiers. This list is determined by the
         * configuration properties either by explicit listing in 
-        * <code>slice.Names</code> property or by scanning 
<code>slice.*.*</code>
-        * properties.
+        * <code>openjpa.slice.Names</code> property or by scanning 
+        * <code>openjpa.slice.*.*</code> properties.
         * <br> 
         * The ordering of the slice identifiers is determined when they are
-        * specified explicitly in <code>slice.Names</code> property or 
+        * specified explicitly in <code>openjpa.slice.Names</code> property or 
         * ordered alphabetically when found by scanning the properties.
         * <br>
         * This list always returns the identifiers that are <em>active</em>, 
slices
@@ -60,7 +60,7 @@
        /**
         * Gets the slices of given status.
         * @param statuses list of status flags. If null, returns all slices 
-        * irrespective of status;
+        * irrespective of status.
         */
        List<Slice> getSlices(Slice.Status...statuses);
        

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
 Thu Feb 14 14:16:45 2008
@@ -33,13 +33,14 @@
         * Gets the name of the slice where a given instance will be stored.
         *  
         * @param pc The newly persistent or to-be-merged object. 
-        * @param slices list of names of the configured slices. The ordering 
of 
-        * the list is either explicit (by the <code>slice.Names</code> 
property)
-        * or implicit i.e. alphabetic if <code>slice.Names</code> is 
unspecified.  
+        * @param slices list of names of the active slices. The ordering of 
+        * the list is either explicit <code>openjpa.slice.Names</code> property
+        * or implicit i.e. alphabetic order of available identifiers if 
+        * <code>openjpa.slice.Names</code> is unspecified.  
         * @param context generic persistence context managing the given 
instance.
         * 
         * @return identifier of the slice. This name must match one of the
-        * configured slice names. 
+        * given slice names. 
         * @see DistributedConfiguration#getActiveSliceNames()
         */
        String distribute(Object pc, List<String> slices, Object context);

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java
 Thu Feb 14 14:16:45 2008
@@ -27,21 +27,34 @@
 /**
  * Derives configuration for Slice.
  * Introduces a specialized BrokerFactory aliased as <code>slice</code>.
- * All Slice specific configuration is prefixed as <code>slice.XXX</code>
+ * All Slice specific configuration is prefixed as 
+ * <code>openjpa.slice.*.*</code>
  * 
  * @author Pinaki Poddar 
  *
  */
 public class ProductDerivation extends AbstractProductDerivation implements
                OpenJPAProductDerivation {
-
+    /**
+     * Prefix for all Slice-specific configuration properties. 
+     */
+    public static final String PREFIX_SLICE = "openjpa.slice";
+    
+    /**
+     * Hint key <code>openjpa.hint.slice.Target </code> to specify a subset of 
+     * slices for query. The value corresponding to the key is comma-separated
+     * list of slice identifiers.
+     *  
+     */
+    public static final String HINT_TARGET  = "openjpa.hint.slice.Target";
+    
        @SuppressWarnings("unchecked")
        public void putBrokerFactoryAliases(Map m) {
                m.put("slice", DistributedJDBCBrokerFactory.class.getName());
        }
 
        public String getConfigurationPrefix() {
-               return "openjpa.slice";
+               return PREFIX_SLICE;
        }
 
        public int getType() {

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/Slice.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/Slice.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/Slice.java 
(original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/Slice.java 
Thu Feb 14 14:16:45 2008
@@ -21,7 +21,8 @@
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 
 /**
- * Represents a database slice of immutable logical name.
+ * Represents a database slice of immutable logical name, a configuration and
+ * status.
  * 
  * @author Pinaki Poddar 
  *

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
 Thu Feb 14 14:16:45 2008
@@ -49,6 +49,7 @@
 import org.apache.openjpa.slice.DistributedBrokerImpl;
 import org.apache.openjpa.slice.DistributionPolicy;
 import org.apache.openjpa.slice.ExecutorServiceValue;
+import org.apache.openjpa.slice.ProductDerivation;
 import org.apache.openjpa.slice.Slice;
 import org.apache.openjpa.util.UserException;
 
@@ -76,10 +77,10 @@
     protected ExecutorServiceValue executorServicePlugin;
     protected PluginValue distributionPolicyPlugin;
 
-    public static final String PREFIX_SLICE = "openjpa.slice.";
-    public static final String PREFIX_OPENJPA = "openjpa.";
-    public static final String REGEX_DOT = "\\.";
     public static final String DOT = ".";
+    public static final String REGEX_DOT = "\\.";
+    public static final String PREFIX_SLICE = ProductDerivation.PREFIX_SLICE + 
DOT;
+    public static final String PREFIX_OPENJPA = "openjpa.";
     private static Localizer _loc =
             Localizer.forPackage(DistributedJDBCConfigurationImpl.class);
 
@@ -322,10 +323,11 @@
         if (sliceNames.isEmpty()) {
             throw new UserException(_loc.get("slice-none-configured"));
         } 
+        String unit = getPersistenceUnitName(original);
         for (String key : sliceNames) {
             JDBCConfiguration child = new JDBCConfigurationImpl();
             child.fromProperties(createSliceProperties(original, key));
-            child.setId(key);
+            child.setId(unit+DOT+key);
             Slice slice = new Slice(key, child);
             _slices.add(slice);
             if (log.isTraceEnabled())

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreManager.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreManager.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreManager.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreManager.java
 Thu Feb 14 14:16:45 2008
@@ -21,6 +21,7 @@
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Collection;
 import java.util.HashMap;
@@ -61,6 +62,7 @@
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.FieldMetaData;
 import org.apache.openjpa.slice.DistributionPolicy;
+import org.apache.openjpa.slice.ProductDerivation;
 import org.apache.openjpa.slice.transaction.DistributedNaiveTransaction;
 import org.apache.openjpa.slice.transaction.DistributedTransactionManager;
 import org.apache.openjpa.slice.transaction.NaiveTransactionManager;
@@ -275,7 +277,8 @@
             boolean subclasses, FetchConfiguration fetch) {
         ResultObjectProvider[] tmp = new ResultObjectProvider[_slices.size()];
         int i = 0;
-        for (SliceStoreManager slice : _slices) {
+        List<SliceStoreManager> targets = getTargets(fetch);
+        for (SliceStoreManager slice : targets) {
             tmp[i++] = slice.executeExtent(meta, subclasses, fetch);
         }
         return new MergedResultObjectProvider(tmp);
@@ -371,7 +374,8 @@
                 return lookup(slice).initialize(sm, state, fetch, edata);
         }
         // not a part of Query result load. Look into the slices till found
-        for (SliceStoreManager slice : _slices) {
+        List<SliceStoreManager> targets = getTargets(fetch);
+        for (SliceStoreManager slice : targets) {
             if (slice.initialize(sm, state, fetch, edata)) {
                 sm.setImplData(slice.getName(), true);
                 return true;
@@ -500,6 +504,31 @@
             list.add(slice.getConnection());
         DistributedConnection con = new DistributedConnection(list);
         return new RefCountConnection(con);
+    }
+    
+    /**
+     * Gets the list of slices mentioned as  
+     * [EMAIL PROTECTED] ProductDerivation#HINT_TARGET hint} of the given
+     * [EMAIL PROTECTED] FetchConfiguration#getHint(String) fetch 
configuration}. 
+     * 
+     * @return all active slices if a) the hint is not specified or b) a null 
+     * value or c) a non-String or d) matches no active slice.
+     */
+    List<SliceStoreManager> getTargets(FetchConfiguration fetch) {
+        if (fetch == null)
+            return _slices;
+        Object hint = fetch.getHint(ProductDerivation.HINT_TARGET);
+        if (hint == null || !(hint instanceof String)) 
+            return _slices;
+        List<String> targetNames = Arrays.asList(hint.toString().split("\\,"));
+        List<SliceStoreManager> targets = new ArrayList<SliceStoreManager>();
+        for (SliceStoreManager slice : _slices) {
+           if (targetNames.contains(slice.getName()))
+              targets.add(slice);
+           }
+          if (targets.isEmpty())
+            return _slices;
+        return targets;
     }
 
     private static class Flusher implements Callable<Collection> {

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreQuery.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreQuery.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreQuery.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedStoreQuery.java
 Thu Feb 14 14:16:45 2008
@@ -29,6 +29,7 @@
 import org.apache.openjpa.jdbc.kernel.JDBCStore;
 import org.apache.openjpa.jdbc.kernel.JDBCStoreQuery;
 import org.apache.openjpa.kernel.ExpressionStoreQuery;
+import org.apache.openjpa.kernel.FetchConfiguration;
 import org.apache.openjpa.kernel.OrderingMergedResultObjectProvider;
 import org.apache.openjpa.kernel.QueryContext;
 import org.apache.openjpa.kernel.StoreQuery;
@@ -37,6 +38,7 @@
 import org.apache.openjpa.lib.rop.RangeResultObjectProvider;
 import org.apache.openjpa.lib.rop.ResultObjectProvider;
 import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.slice.ProductDerivation;
 import org.apache.openjpa.util.StoreException;
 
 /**
@@ -63,8 +65,14 @@
     public Executor newDataStoreExecutor(ClassMetaData meta, boolean subs) {
        ParallelExecutor ex = new ParallelExecutor(this, meta, subs, _parser, 
                        ctx.getCompilation());
+       
+       FetchConfiguration fetch = getContext().getFetchConfiguration();
+       DistributedStoreManager store = (DistributedStoreManager)getContext()
+           .getStoreContext().getStoreManager().getInnermostDelegate();
+        List<SliceStoreManager> targets = store.getTargets(fetch);
         for (StoreQuery q:_queries) {
-               ex.addExecutor(q.newDataStoreExecutor(meta, subs));
+            if (targets.contains(((JDBCStoreQuery)q).getStore()))
+                ex.addExecutor(q.newDataStoreExecutor(meta, subs));
         }
         return ex;
     }

Modified: 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
 Thu Feb 14 14:16:45 2008
@@ -215,10 +215,8 @@
             if (!meta.isMapped() || meta.isEmbeddedOnly() 
                 || Modifier.isAbstract(meta.getDescribedType().getModifiers()))
                 continue;
-            List all = em.createQuery("SELECT o FROM " + meta.getTypeAlias() + 
" o").
-                getResultList();
-            for (Object pc:all)
-                em.remove(pc);
+            em.createQuery("DELETE FROM " + meta.getTypeAlias() + " o").
+                executeUpdate();
         }
         em.getTransaction().commit();
         em.close();

Modified: 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java?rev=627888&r1=627887&r2=627888&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
 Thu Feb 14 14:16:45 2008
@@ -18,11 +18,14 @@
  */
 package org.apache.openjpa.slice;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
 import org.apache.openjpa.slice.SlicePersistence;
 
 public class TestQuery extends SliceTestCase {
@@ -81,6 +84,21 @@
         }
         em.getTransaction().rollback();
         assertEquals(limit, result.size());
+    }
+    
+    public void testHint() {
+        List<String> targets = new ArrayList<String>();
+        targets.add("Even");
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        Query query = em.createQuery("SELECT p FROM PObject p");
+        query.setHint(ProductDerivation.HINT_TARGET, "Even");
+        List result = query.getResultList();
+        for (Object pc : result) {
+            String slice = SlicePersistence.getSlice(pc);
+            assertTrue(targets.contains(slice));
+        }
+        em.getTransaction().rollback();
     }
     
     protected String getPersistenceUnitName() {


Reply via email to