cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r618098 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/graph/ main/java/org/apache/cayenne/remote/ test/java/org/apache/cayenne/
Date Sun, 03 Feb 2008 21:42:16 GMT
Author: aadamchik
Date: Sun Feb  3 13:42:14 2008
New Revision: 618098

URL: http://svn.apache.org/viewvc?rev=618098&view=rev
Log:
CAY-971 ROP GraphDiff "compressor"
(the simplest compression step - cancel repeating simple property changes)

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/GraphDiffCompressor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneContextGraphDiffCompressorTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/NodePropertyChangeOperation.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/remote/ClientChannel.java

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/GraphDiffCompressor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/GraphDiffCompressor.java?rev=618098&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/GraphDiffCompressor.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/GraphDiffCompressor.java
Sun Feb  3 13:42:14 2008
@@ -0,0 +1,113 @@
+/*****************************************************************
+ *   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.cayenne.graph;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A utility class that removes redundant graph changes from the graph diff.
+ * 
+ * @author Andrus Adamchik
+ * @since 3.0
+ */
+public class GraphDiffCompressor {
+
+    public GraphDiff compress(GraphDiff diff) {
+
+        if (diff.isNoop()) {
+            return diff;
+        }
+
+        CompressAction action = new CompressAction();
+        diff.apply(action);
+        return action.getCompressedDiff();
+    }
+
+    final class CompressAction implements GraphChangeHandler {
+
+        private List<GraphDiff> compressed = new ArrayList<GraphDiff>();
+        private Map<Object, List<NodeDiff>> diffsByNode = new HashMap<Object,
List<NodeDiff>>();
+
+        GraphDiff getCompressedDiff() {
+            return new CompoundDiff(compressed);
+        }
+
+        public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
+            registerDiff(new ArcCreateOperation(nodeId, targetNodeId, arcId));
+        }
+
+        public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
+            registerDiff(new ArcDeleteOperation(nodeId, targetNodeId, arcId));
+        }
+
+        public void nodeCreated(Object nodeId) {
+            registerDiff(new NodeCreateOperation(nodeId));
+        }
+
+        public void nodeIdChanged(Object nodeId, Object newId) {
+            registerDiff(new NodeIdChangeOperation(nodeId, newId));
+        }
+
+        public void nodeRemoved(Object nodeId) {
+            registerDiff(new NodeDeleteOperation(nodeId));
+        }
+
+        public void nodePropertyChanged(
+                Object nodeId,
+                String property,
+                Object oldValue,
+                Object newValue) {
+
+            List<NodeDiff> diffs = diffsByNode.get(nodeId);
+            if (diffs != null) {
+
+                for (int i = diffs.size() - 1; i >= 0; i--) {
+                    NodeDiff diff = diffs.get(i);
+                    if (diff instanceof NodePropertyChangeOperation) {
+                        NodePropertyChangeOperation propertyDiff = (NodePropertyChangeOperation)
diff;
+                        if (property.equals(propertyDiff.getProperty())) {
+                            propertyDiff.setNewValue(newValue);
+                            return;
+                        }
+                    }
+                }
+            }
+
+            registerDiff(new NodePropertyChangeOperation(
+                    nodeId,
+                    property,
+                    oldValue,
+                    newValue));
+        }
+
+        private void registerDiff(NodeDiff diff) {
+            compressed.add(diff);
+            List<NodeDiff> diffs = diffsByNode.get(diff.getNodeId());
+            if (diffs == null) {
+                diffs = new ArrayList<NodeDiff>();
+                diffsByNode.put(diff.getNodeId(), diffs);
+            }
+
+            diffs.add(diff);
+        }
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/NodePropertyChangeOperation.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/NodePropertyChangeOperation.java?rev=618098&r1=618097&r2=618098&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/NodePropertyChangeOperation.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/graph/NodePropertyChangeOperation.java
Sun Feb  3 13:42:14 2008
@@ -43,9 +43,25 @@
     public NodePropertyChangeOperation(Object nodeId, String property, Object oldValue,
             Object newValue, int diffId) {
         super(nodeId, diffId);
-        
+
         this.property = property;
         this.oldValue = oldValue;
+        this.newValue = newValue;
+    }
+
+    /**
+     * @since 3.0
+     */
+    public String getProperty() {
+        return property;
+    }
+
+    /**
+     * Chnages the 'newValue'.
+     * 
+     * @since 3.0
+     */
+    void setNewValue(Object newValue) {
         this.newValue = newValue;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/remote/ClientChannel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/remote/ClientChannel.java?rev=618098&r1=618097&r2=618098&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/remote/ClientChannel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/remote/ClientChannel.java
Sun Feb  3 13:42:14 2008
@@ -34,6 +34,7 @@
 import org.apache.cayenne.event.EventSubject;
 import org.apache.cayenne.graph.CompoundDiff;
 import org.apache.cayenne.graph.GraphDiff;
+import org.apache.cayenne.graph.GraphDiffCompressor;
 import org.apache.cayenne.graph.GraphEvent;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.query.Query;
@@ -54,6 +55,7 @@
     protected EventManager eventManager;
     protected EntityResolver entityResolver;
     protected boolean channelEventsEnabled;
+    protected GraphDiffCompressor diffCompressor;
 
     EventBridge remoteChannelListener;
 
@@ -85,6 +87,7 @@
             throws CayenneRuntimeException {
 
         this.connection = connection;
+        this.diffCompressor = new GraphDiffCompressor();
         this.eventManager = eventManager;
         this.channelEventsEnabled = eventManager != null && channelEventsEnabled;
 
@@ -167,6 +170,8 @@
             ObjectContext originatingContext,
             GraphDiff changes,
             int syncType) {
+        
+        changes = diffCompressor.compress(changes);
 
         GraphDiff replyDiff = (GraphDiff) send(new SyncMessage(
                 originatingContext,
@@ -270,9 +275,9 @@
      * Sends a message via connector, getting a result as an instance of a specific class.
      * 
      * @throws org.apache.cayenne.CayenneRuntimeException if an underlying connector
-     *             exception occured, or a result is not of expected type.
+     *             exception occurred, or a result is not of expected type.
      */
-    protected Object send(ClientMessage message, Class resultClass) {
+    protected Object send(ClientMessage message, Class<?> resultClass) {
         Object result = connection.sendMessage(message);
 
         if (result != null && !resultClass.isInstance(result)) {

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneContextGraphDiffCompressorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneContextGraphDiffCompressorTest.java?rev=618098&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneContextGraphDiffCompressorTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneContextGraphDiffCompressorTest.java
Sun Feb  3 13:42:14 2008
@@ -0,0 +1,109 @@
+/*****************************************************************
+ *   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.cayenne;
+
+import org.apache.cayenne.access.ClientServerChannel;
+import org.apache.cayenne.access.DataDomain;
+import org.apache.cayenne.graph.GraphChangeHandler;
+import org.apache.cayenne.graph.GraphDiff;
+import org.apache.cayenne.remote.ClientChannel;
+import org.apache.cayenne.remote.service.LocalConnection;
+import org.apache.cayenne.testdo.mt.ClientMtTable1;
+import org.apache.cayenne.unit.AccessStack;
+import org.apache.cayenne.unit.CayenneCase;
+import org.apache.cayenne.unit.CayenneResources;
+
+public class CayenneContextGraphDiffCompressorTest extends CayenneCase {
+
+    @Override
+    protected AccessStack buildAccessStack() {
+        return CayenneResources.getResources().getAccessStack(MULTI_TIER_ACCESS_STACK);
+    }
+
+    public void testMultipleSimpleProperties() {
+        DiffCounter serverChannel = new DiffCounter(getDomain());
+        LocalConnection connection = new LocalConnection(
+                serverChannel,
+                LocalConnection.HESSIAN_SERIALIZATION);
+        ClientChannel channel = new ClientChannel(connection);
+        CayenneContext context = new CayenneContext(channel);
+        
+        
+        ClientMtTable1 o1 = context.newObject(ClientMtTable1.class);
+        o1.setGlobalAttribute1("v1");
+        o1.setGlobalAttribute1("v2");
+        
+        context.commitChanges();
+        assertEquals(1, serverChannel.nodePropertiesChanged);
+        assertEquals(1, serverChannel.nodesCreated);
+    }
+
+    final class DiffCounter extends ClientServerChannel implements GraphChangeHandler {
+        
+        int arcsCreated;
+        int arcsDeleted;
+        int nodesCreated;
+        int nodeIdsChanged;
+        int nodePropertiesChanged;
+        int nodesRemoved;
+
+        public DiffCounter(DataDomain domain) {
+            super(domain);
+        }
+        
+        @Override
+        public GraphDiff onSync(
+                ObjectContext originatingContext,
+                GraphDiff changes,
+                int syncType) {
+            
+            changes.apply(this);
+            
+            return super.onSync(originatingContext, changes, syncType);
+        }
+
+        public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
+            arcsCreated++;
+        }
+
+        public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
+            arcsDeleted++;
+        }
+
+        public void nodeCreated(Object nodeId) {
+            nodesCreated++;
+        }
+
+        public void nodeIdChanged(Object nodeId, Object newId) {
+            nodeIdsChanged++;
+        }
+
+        public void nodePropertyChanged(
+                Object nodeId,
+                String property,
+                Object oldValue,
+                Object newValue) {
+            nodePropertiesChanged++;
+        }
+
+        public void nodeRemoved(Object nodeId) {
+            nodesRemoved++;
+        }
+    }
+}



Mime
View raw message