openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ss...@apache.org
Subject svn commit: r680579 - in /openjpa/branches/0.9.7-r547073: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/
Date Tue, 29 Jul 2008 00:20:04 GMT
Author: ssegu
Date: Mon Jul 28 17:19:51 2008
New Revision: 680579

URL: http://svn.apache.org/viewvc?rev=680579&view=rev
Log:
OPENJPA-521. Committing on behalf of Qin Feng. 

Added:
    openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
    openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
    openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
Modified:
    openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
    openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java

Modified: openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java?rev=680579&r1=680578&r2=680579&view=diff
==============================================================================
--- openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
(original)
+++ openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
Mon Jul 28 17:19:51 2008
@@ -20,10 +20,11 @@
 
 import java.util.BitSet;
 
-import org.apache.openjpa.kernel.OpenJPAStateManager;
 import org.apache.openjpa.kernel.AbstractPCData;
+import org.apache.openjpa.kernel.OpenJPAStateManager;
 import org.apache.openjpa.kernel.PCData;
 import org.apache.openjpa.kernel.PCDataImpl;
+import org.apache.openjpa.kernel.PCState;
 import org.apache.openjpa.kernel.StoreContext;
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.FieldMetaData;
@@ -119,6 +120,65 @@
         super.store(sm, fields);
     }
 
+    /**
+     * Store field-level information from the given state manager.
+     * Special process of checking if the cached collection data is out of order.
+     */
+    protected void storeField(OpenJPAStateManager sm, FieldMetaData fmd) {
+        if (fmd.getManagement() != fmd.MANAGE_PERSISTENT)
+            return;
+        int index = fmd.getIndex();
+
+        // if the field is a collection and has "order by" set, don't cache
+        // it if this store is coming from a create or update (i.e., only
+        // enlist in cache if this is coming from a database read).
+        if (fmd.getOrders().length > 0) {
+            if (sm.getPCState() == PCState.PNEW)
+                return;
+            if (sm.getPCState() == PCState.PDIRTY) {
+                clearData(index);
+                return;
+            }
+        }
+
+        super.storeField(sm, fmd);
+
+        // If this field is used in "order by", we need to invalidate cache
+        // for the collection that refer to this field.
+        if ((sm.getPCState() == PCState.PDIRTY) && fmd.isUsedInOrderBy()) {
+            clearInverseRelationCache(sm, fmd);
+        }
+    }
+
+    /**
+     * Check if this field is in use of "order by" by other field collections
+     * in inverse relation. If it is, clear the other field cache because it
+     * could be out of order.
+     */
+    protected void clearInverseRelationCache(OpenJPAStateManager sm,
+        FieldMetaData fmd) {
+        ClassMetaData cmd = sm.getMetaData();
+        FieldMetaData[] fields = cmd.getFields();
+        for (int i = 0; i < fields.length; i++) {
+            FieldMetaData[] inverses = fields[i].getInverseMetaDatas();
+            if (inverses.length == 0)
+                continue;
+            for (int j = 0; j < inverses.length; j++) {
+                if (inverses[j].getOrderDeclaration()
+                    .indexOf(fmd.getName()) != -1) {
+                    DataCache cache = sm.getMetaData().getDataCache();
+                    Object oid = sm.getContext().getObjectId(sm.fetch(i));
+                    DataCachePCData data = cache.get(oid);
+                    if ((data != null) &&
+                        (data instanceof DataCachePCDataImpl)) {
+                        ((DataCachePCDataImpl) data)
+                            .clearData(inverses[j].getIndex());
+                    }
+                }
+            }
+        }
+    }
+
     protected Object toData(FieldMetaData fmd, Object val, StoreContext ctx) {
         // avoid caching large result set fields
         if (fmd.isLRS())

Modified: openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
URL: http://svn.apache.org/viewvc/openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java?rev=680579&r1=680578&r2=680579&view=diff
==============================================================================
--- openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
(original)
+++ openjpa/branches/0.9.7-r547073/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
Mon Jul 28 17:19:51 2008
@@ -196,6 +196,8 @@
     // ordering on load
     private Order[] _orders = null;
     private String _orderDec = null;
+    // indicate if this field is used by other field as "order by" value 
+    private boolean _usedInOrderBy = false;
 
     /**
      * Constructor.
@@ -1047,6 +1049,12 @@
                         decs[i] = decs[i].substring(0, spc);
                     }
                     orders[i] = getRepository().newOrder(this, decs[i], asc);
+                    //set "isUsedInOrderBy" to the field
+                    ClassMetaData elemCls = getElement()
+                        .getDeclaredTypeMetaData();
+                    FieldMetaData fmd = elemCls.getDeclaredField(decs[i]);
+                    if (fmd != null)
+                        fmd.setUsedInOrderBy(true);
                 }
                 _orders = orders;
             }
@@ -1930,6 +1938,22 @@
 	}
 
     /**
+     * Check if this field is used by other field as "order by" value.
+     *
+     */
+    public boolean isUsedInOrderBy() {
+        return _usedInOrderBy;
+    }
+    
+    /**
+     * Whether this field is used by other field as "order by" value .
+     *
+     */
+    public void setUsedInOrderBy(boolean isUsed) {
+        _usedInOrderBy = isUsed;
+    }
+    
+    /**
      * Serializable wrapper around a {@link Method} or {@link Field}. For 
      * space considerations, this does not support {@link Constructor}s.
      */

Added: openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
URL: http://svn.apache.org/viewvc/openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java?rev=680579&view=auto
==============================================================================
--- openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
(added)
+++ openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
Mon Jul 28 17:19:51 2008
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.datacache;
+
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.Entity;
+
+@Entity
+public class OrderedOneManyChild {
+    @Id
+    private long id;
+
+    private String name;
+
+    @ManyToOne
+    private OrderedOneManyParent parent;
+    
+    public long getId() { 
+        return id; 
+    }
+
+    public void setId(long id) { 
+        this.id = id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public OrderedOneManyParent getParent() { 
+        return parent; 
+    }
+
+    public void setParent(OrderedOneManyParent parent) { 
+        this.parent = parent; 
+    }
+    
+}

Added: openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
URL: http://svn.apache.org/viewvc/openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java?rev=680579&view=auto
==============================================================================
--- openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
(added)
+++ openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
Mon Jul 28 17:19:51 2008
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.datacache;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OrderBy;
+
+@Entity
+public class OrderedOneManyParent {
+	@Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @OneToMany(mappedBy="parent")
+    @OrderBy("name ASC")
+    private List<OrderedOneManyChild> children = 
+        new ArrayList<OrderedOneManyChild>();
+
+    public long getId() { 
+        return id; 
+    }
+
+    public List<OrderedOneManyChild> getChildren() { 
+        return children; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+}
+

Added: openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java?rev=680579&view=auto
==============================================================================
--- openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
(added)
+++ openjpa/branches/0.9.7-r547073/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
Mon Jul 28 17:19:51 2008
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.datacache;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Defined ordering is only applied when the collection is loaded from the datastore.
+ * It is not maintained by Openjpa as you modify the collection in memory. 
+ * Openjpa invalid data cache in case the persistence operation may result cache in wrong
order.
+ * This test suite tests various cases for the above problem.
+ */
+public class TestOrderbyInDataCache extends SingleEMFTestCase {
+	private long pid;
+    public void setUp() {
+        setUp("openjpa.DataCache", "true", 
+            "openjpa.RemoteCommitProvider", "sjvm", 
+            OrderedOneManyParent.class,
+            OrderedOneManyChild.class, CLEAR_TABLES);
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        OrderedOneManyParent prt = new OrderedOneManyParent();
+        prt.setName("Maria");
+        em.persist(prt);
+        //insert child in reverse order
+        for (int i = 4; i > 1; i--) {
+        	OrderedOneManyChild child = new OrderedOneManyChild();
+            child.setId(i);
+            child.setName("child" + i);
+            child.setParent(prt);
+            prt.getChildren().add(child);
+            em.persist(child);
+        }        
+        em.getTransaction().commit();
+        pid = prt.getId();
+        em.close();
+    }
+
+    /**
+     * Test if child list is in order after new child list is added in setup().
+     *
+     */
+    public void testGetChildList(){
+    	EntityManager em = emf.createEntityManager();
+    	OrderedOneManyParent prt = em.find(OrderedOneManyParent.class, pid);
+        assertEquals(3, prt.getChildren().size());
+        //the order should be "child2", "child3", "child4"
+        for (int i = 1; i < 4; i++) {
+            assertEquals("child" + (i + 1), prt.getChildren().
+                get(i-1).getName());
+        }   
+        em.close();
+    }
+    
+    public void testInsertChild() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        OrderedOneManyParent prt = em.find(OrderedOneManyParent.class, pid);
+        OrderedOneManyChild child = new OrderedOneManyChild();
+        child.setId(1);
+        child.setName("child1");
+        child.setParent(prt);
+        prt.getChildren().add(child);
+        em.persist(child);
+        em.getTransaction().commit();
+        em.close();
+        
+        //obtain object in new persistence context
+        em = emf.createEntityManager();
+        prt = em.find(OrderedOneManyParent.class, pid);
+        assertEquals(4, prt.getChildren().size());
+        //the order should be "child1", "child2", "child3", "child4"
+        for (int i = 1; i < 5; i++) {
+            assertEquals("child" + i, prt.getChildren().
+                get(i-1).getName());
+        }          
+        em.close();
+    }
+    
+    public void testUpdateChildName() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        OrderedOneManyChild child = em.find(OrderedOneManyChild.class, 4);
+        child.setName("child1");
+        em.persist(child);
+        em.getTransaction().commit();
+        OrderedOneManyParent prt = em.find(OrderedOneManyParent.class, pid);
+        assertEquals(3, prt.getChildren().size());
+        //the order should be "child1", "child2", "child3"
+        for (int i = 1; i < 4; i++) {
+            assertEquals("child" + i, prt.getChildren().
+                get(i-1).getName());
+        }          
+        em.close();    	
+    }
+}



Mime
View raw message