Author: chetanm
Date: Fri Nov 25 14:20:14 2016
New Revision: 1771322

URL: http://svn.apache.org/viewvc?rev=1771322&view=rev
Log:
OAK-5126 - Support ChangeSet merging and serialization

Add support for serializing and deserializing as json

Added:
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSet.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetBuilderTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSet.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSet.java?rev=1771322&r1=1771321&r2=1771322&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSet.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSet.java
 Fri Nov 25 14:20:14 2016
@@ -23,6 +23,11 @@ import java.util.Set;
 import javax.annotation.CheckForNull;
 
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.commons.json.JsopReader;
+import org.apache.jackrabbit.oak.commons.json.JsopTokenizer;
+import org.apache.jackrabbit.oak.commons.json.JsopWriter;
 
 /**
  * A ChangeSet is a collection of items that have been changed as part of a
@@ -116,6 +121,8 @@ public final class ChangeSet {
                 getPropertyNames() == null;
     }
 
+    //~---------------------------------------------------< equals/hashcode >
+
     @Override
     public boolean equals(Object o) {
         if (this == o) return true;
@@ -139,4 +146,79 @@ public final class ChangeSet {
     public int hashCode() {
         return 0;
     }
+
+    //~----------------------------------------------------< json support >
+
+    public String asString(){
+        JsopWriter json = new JsopBuilder();
+        json.object();
+        json.key("maxPathDepth").value(maxPathDepth);
+        addToJson(json, "parentPaths", parentPaths);
+        addToJson(json, "parentNodeNames", parentNodeNames);
+        addToJson(json, "parentNodeTypes", parentNodeTypes);
+        addToJson(json, "propertyNames", propertyNames);
+        addToJson(json, "allNodeTypes", allNodeTypes);
+        json.endObject();
+        return json.toString();
+    }
+
+    public static ChangeSet fromString(String json) {
+        JsopReader reader = new JsopTokenizer(json);
+        int maxPathDepth = 0;
+        Set<String> parentPaths = null;
+        Set<String> parentNodeNames = null;
+        Set<String> parentNodeTypes = null;
+        Set<String> propertyNames = null;
+        Set<String> allNodeTypes = null;
+
+        reader.read('{');
+        if (!reader.matches('}')) {
+            do {
+                String name = reader.readString();
+                reader.read(':');
+                if ("maxPathDepth".equals(name)){
+                    maxPathDepth = 
Integer.parseInt(reader.read(JsopReader.NUMBER));
+                } else {
+                    Set<String> data = readArrayAsSet(reader);
+                    if ("parentPaths".equals(name)){
+                        parentPaths = data;
+                    } else if ("parentNodeNames".equals(name)){
+                        parentNodeNames = data;
+                    } else if ("parentNodeTypes".equals(name)){
+                        parentNodeTypes = data;
+                    } else if ("propertyNames".equals(name)){
+                        propertyNames = data;
+                    } else if ("allNodeTypes".equals(name)){
+                        allNodeTypes = data;
+                    }
+                }
+            } while (reader.matches(','));
+            reader.read('}');
+        }
+        reader.read(JsopReader.END);
+        return new ChangeSet(maxPathDepth, parentPaths, parentNodeNames, 
parentNodeTypes, propertyNames, allNodeTypes);
+    }
+
+    private static Set<String> readArrayAsSet(JsopReader reader) {
+        Set<String> values = Sets.newHashSet();
+        reader.read('[');
+        for (boolean first = true; !reader.matches(']'); first = false) {
+            if (!first) {
+                reader.read(',');
+            }
+            values.add(reader.readString());
+        }
+        return values;
+    }
+
+    private static void addToJson(JsopWriter json, String name, Set<String> 
values){
+        if (values == null){
+            return;
+        }
+        json.key(name).array();
+        for (String v : values){
+            json.value(v);
+        }
+        json.endArray();
+    }
 }
\ No newline at end of file

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetBuilderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetBuilderTest.java?rev=1771322&r1=1771321&r2=1771322&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetBuilderTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetBuilderTest.java
 Fri Nov 25 14:20:14 2016
@@ -50,7 +50,6 @@ public class ChangeSetBuilderTest {
         ChangeSetBuilder cb1 = new ChangeSetBuilder(5, 2);
         add(cb1, "1");
 
-        ChangeSet csAll = new ChangeSet(2, of("p-2"), of("nn-2"), of("pnt-2"), 
of("pn-2"), of("nt-2"));
         ChangeSet cs1 = new ChangeSet(2, null, of("nn-2"), of("nt-2"), 
of("pn-2"), of("nt-2"));
         ChangeSet mcs1 = cb1.add(cs1).build();
         assertNull(mcs1.getParentPaths());

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetTest.java?rev=1771322&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetTest.java
 Fri Nov 25 14:20:14 2016
@@ -0,0 +1,52 @@
+/*
+ * 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.jackrabbit.oak.plugins.observation;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ChangeSetTest {
+
+    @Test
+    public void asJson() throws Exception{
+        ChangeSet cs1 = new ChangeSet(2, of("p-2", "p-3"), null,
+                ImmutableSet.<String>of(), of("pn-2"), of("nt-2"));
+        String json = cs1.asString();
+
+        ChangeSet cs2 = ChangeSet.fromString(json);
+        assertEquals(cs1, cs2);
+        assertNull(cs2.getParentNodeNames());
+        assertTrue(cs2.getParentNodeTypes().isEmpty());
+    }
+
+    @Test
+    public void asJsonAll() throws Exception{
+        ChangeSet cs1 = new ChangeSet(2, of("p-2"), of("nn-2"), of("pnt-2"), 
of("pn-2"), of("nt-2"));
+        String json = cs1.asString();
+        ChangeSet cs2 = ChangeSet.fromString(json);
+        assertEquals(cs1, cs2);
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/observation/ChangeSetTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to