jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1299359 - in /jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state: PersistentNodeState.java PropertyStateImpl.java
Date Sun, 11 Mar 2012 13:54:56 GMT
Author: mduerig
Date: Sun Mar 11 13:54:55 2012
New Revision: 1299359

URL: http://svn.apache.org/viewvc?rev=1299359&view=rev
Log:
Microkernel based prototype of JCR implementation (WIP)
- improve parsing of properties 

Modified:
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PersistentNodeState.java
    jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PropertyStateImpl.java

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PersistentNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PersistentNodeState.java?rev=1299359&r1=1299358&r2=1299359&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PersistentNodeState.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PersistentNodeState.java
Sun Mar 11 13:54:55 2012
@@ -1,9 +1,14 @@
 package org.apache.jackrabbit.state;
 
 import org.apache.jackrabbit.Path;
-import org.apache.jackrabbit.json.FullJsonParser;
+import org.apache.jackrabbit.json.JsonHandler;
+import org.apache.jackrabbit.json.JsonParser;
+import org.apache.jackrabbit.json.JsonTokenizer;
 import org.apache.jackrabbit.json.JsonValue;
+import org.apache.jackrabbit.json.JsonValue.JsonArray;
+import org.apache.jackrabbit.json.JsonValue.JsonAtom;
 import org.apache.jackrabbit.json.JsonValue.JsonObject;
+import org.apache.jackrabbit.json.Token;
 import org.apache.jackrabbit.json.UnescapingJsonTokenizer;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.oak.model.AbstractChildNodeEntry;
@@ -16,28 +21,31 @@ import org.apache.jackrabbit.spi.commons
 import org.apache.jackrabbit.spi.commons.iterator.Transformer;
 import org.apache.jackrabbit.utils.Function0;
 
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Set;
+
+import static org.apache.jackrabbit.json.FullJsonParser.parseObject;
 
 public class PersistentNodeState extends AbstractNodeState {
     private final MicroKernel microkernel;
     private final String revision;
     private final Path path;
     
-    private final Function0<JsonObject> properties = new Function0<JsonObject>()
{
-        private JsonObject properties;
-        
-        @Override
-        public JsonObject apply() {
-            if (properties == null) {
-                String json = microkernel.getNodes(path.toMkPath(), revision, 0, 0, -1, null);
-                properties = FullJsonParser.parseObject(new UnescapingJsonTokenizer(json));
+    private final Function0<Map<String, PropertyStateImpl>> properties =
+        new Function0<Map<String, PropertyStateImpl>>() {
+            private Map<String, PropertyStateImpl> properties;
+
+            @Override
+            public Map<String, PropertyStateImpl> apply() {
+                if (properties == null) {
+                    properties = readProperties();
+                }
+                return properties;
             }
-            return properties;
-        }
     };
-
+    
     public PersistentNodeState(MicroKernel microkernel, String revision, Path path) {
         this.microkernel = microkernel;
         this.revision = revision;
@@ -46,25 +54,13 @@ public class PersistentNodeState extends
     
     @Override
     public PropertyState getProperty(String name) {
-        return new PropertyStateImpl(name, properties.apply().get(name));
+        return properties.apply().get(name);
     }
 
     @Override
     public Iterable<PropertyState> getProperties() {
-        return new Iterable<PropertyState>() {
-            private final Set<Entry<String, JsonValue>> props = properties.apply().value().entrySet();
-
-            @Override
-            public Iterator<PropertyState> iterator() {
-                return Iterators.transformIterator(props.iterator(),
-                    new Transformer<Entry<String, JsonValue>, PropertyState>()
{
-                        @Override
-                        public PropertyState transform(Entry<String, JsonValue> entry)
{
-                            return new PropertyStateImpl(entry.getKey(), entry.getValue());
-                        }
-                    });
-            }
-        };
+        // fixme: make return type of getProperties covariant
+        return (Iterable<PropertyState>) (Iterable<?>) properties.apply().values();
     }
 
     @Override
@@ -79,7 +75,7 @@ public class PersistentNodeState extends
 
     @Override
     public long getChildNodeCount() {
-        JsonValue count = properties.apply().get(":childNodeCount");
+        JsonValue count = properties.apply().get(":childNodeCount").getValue();
         long c = toLong(count);
         if (c < 0) {
             return super.getChildNodeCount();
@@ -91,8 +87,10 @@ public class PersistentNodeState extends
 
     @Override
     public Iterable<ChildNodeEntry> getChildNodeEntries(final long offset, final long
length) {
+        // fixme: microkernel should also use long for length
+        final String json = microkernel.getNodes(path.toMkPath(), revision, 1, offset, (int)
length, null);
         return new Iterable<ChildNodeEntry>() {
-            private final JsonObject jsonObject = getNodesAsJson(offset, length);
+            private final JsonObject jsonObject = parseObject(new UnescapingJsonTokenizer(json));
 
             private final Iterator<Entry<String, JsonValue>> childObjects =
                 Iterators.filterIterator(jsonObject.value().entrySet().iterator(),
@@ -121,12 +119,6 @@ public class PersistentNodeState extends
 
     //------------------------------------------< private >---
 
-    private JsonObject getNodesAsJson(long offset, long length) {
-        // fixme: microkernel should also use long for length
-        String json = microkernel.getNodes(path.toMkPath(), revision, 1, offset, (int) length,
null);
-        return FullJsonParser.parseObject(new UnescapingJsonTokenizer(json));
-    }
-
     private ChildNodeEntry createChildNodeEntry(final String name) {
         return new AbstractChildNodeEntry() {
             @Override
@@ -155,4 +147,33 @@ public class PersistentNodeState extends
         }
     }
 
+    private Map<String, PropertyStateImpl> readProperties() {
+        String json = microkernel.getNodes(path.toMkPath(), revision, 0, 0, -1, null);
+        final Map<String, PropertyStateImpl> properties = new HashMap<String, PropertyStateImpl>();
+
+        new JsonParser(new JsonHandler(){
+            JsonArray multiValue;
+
+            @Override
+            public void atom(Token key, Token value) {
+                if (multiValue == null) {
+                    properties.put(key.text(), new PropertyStateImpl(key.text(), new JsonAtom(value)));
+                }
+                else {
+                    multiValue.add(new JsonAtom(value));
+                }
+            }
+
+            @Override
+            public void array(JsonParser parser, Token key, JsonTokenizer tokenizer) {
+                multiValue = new JsonArray();
+                super.array(parser, key, tokenizer);
+                properties.put(key.text(), new PropertyStateImpl(key.text(), multiValue));
+                multiValue = null;
+            }
+        }).parseObject(new UnescapingJsonTokenizer(json));
+
+        return properties;
+    }
+
 }

Modified: jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PropertyStateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PropertyStateImpl.java?rev=1299359&r1=1299358&r2=1299359&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PropertyStateImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-microkernel/src/main/java/org/apache/jackrabbit/state/PropertyStateImpl.java
Sun Mar 11 13:54:55 2012
@@ -25,4 +25,9 @@ public class PropertyStateImpl extends A
     public String getEncodedValue() {
         return value.toJson();
     }
+
+    @Override
+    public String toString() {
+        return name + ':' + getEncodedValue();
+    }
 }



Mime
View raw message