jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1135558 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/ main/java/org/apache/jackrabbit/mk/json/ main/java/org/apache/jackrabbit/mk/mem/ test/java/org/apache/jackrabbit/mk/json/
Date Tue, 14 Jun 2011 13:20:20 GMT
Author: thomasm
Date: Tue Jun 14 13:20:20 2011
New Revision: 1135558

URL: http://svn.apache.org/viewvc?rev=1135558&view=rev
Log:
Faster Json value parsing.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java?rev=1135558&r1=1135557&r2=1135558&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Tue Jun 14 13:20:20 2011
@@ -307,9 +307,7 @@ public class MicroKernelImpl implements 
                     if (t.matches(JsopTokenizer.NULL)) {
                         value = null;
                     } else {
-                        JsopBuilder buff = new JsopBuilder();
-                        parseRawValue(t, buff);
-                        value = buff.toString();
+                        value = t.readRawValue().trim();
                     }
                     String nodePath = PathUtil.concat(path, relPath);
                     String parentPath = PathUtil.getParentPath(nodePath);
@@ -336,30 +334,33 @@ public class MicroKernelImpl implements 
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
-
-        // todo implement
-
-        return 0;
+        try {
+            return rep.getStore().getBlobLength(blobId);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     public int read(String blobId, long pos, byte[] buff, int off, int length) throws MicroKernelException
{
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
-
-        // todo implement
-
-        return 0;
+        try {
+            return rep.getStore().readBlob(blobId, pos, buff, off, length);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     public String write(InputStream in) throws MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
-
-        // todo implement
-
-        return null;
+        try {
+            return rep.getStore().writeBlob(in);
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
     }
 
     //-------------------------------------------------------< implementation >
@@ -389,41 +390,6 @@ public class MicroKernelImpl implements 
         }
     }
 
-    private static void parseRawValue(JsopTokenizer t, JsopBuilder buff) {
-        if (t.matches('[')) {
-            buff.array();
-            if (!t.matches(']')) {
-                do {
-                    parseRawValue(t, buff);
-                } while (t.matches(','));
-                t.read(']');
-                buff.endArray();
-            }
-        } else if (t.matches('{')) {
-            buff.object();
-            if (!t.matches('}')) {
-                do {
-                    buff.key(t.readString());
-                    t.read(':');
-                    parseRawValue(t, buff);
-                } while (t.matches(','));
-                t.read('}');
-                buff.endObject();
-            }
-        } else if (t.matches(JsopTokenizer.NUMBER)) {
-            buff.encodedValue(t.getToken());
-        } else if (t.matches(JsopTokenizer.TRUE)) {
-            buff.value(true);
-        } else if (t.matches(JsopTokenizer.FALSE)) {
-            buff.value(false);
-        } else if (t.matches(JsopTokenizer.COMMENT)) {
-            buff.append(t.getToken());
-            buff.value(t.readString());
-        } else if (t.matches(JsopTokenizer.STRING)) {
-            buff.encodedValue("\"" + t.lastEncodedString() + "\"");
-        }
-    }
-
     static void addNode(LinkedList<AddNodeOperation> list, String path, String name,
JsopTokenizer t) throws Exception {
         AddNodeOperation op = new AddNodeOperation();
         op.path = path;
@@ -435,9 +401,7 @@ public class MicroKernelImpl implements 
                 if (t.matches('{')) {
                     addNode(list, PathUtil.concat(path, name), key, t);
                 } else {
-                    JsopBuilder buff = new JsopBuilder();
-                    parseRawValue(t, buff);
-                    op.props.put(key, buff.toString());
+                    op.props.put(key, t.readRawValue().trim());
                 }
             } while (t.matches(','));
             t.read('}');

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java?rev=1135558&r1=1135557&r2=1135558&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
Tue Jun 14 13:20:20 2011
@@ -21,9 +21,6 @@ package org.apache.jackrabbit.mk.json;
  */
 public class JsopTokenizer {
 
-    // "- " is "patch remove", without space is a negative number
-    // please note .0 and -.1 are not valid numbers
-
     public final static int STRING = 0, NUMBER = 1, TRUE = 2, FALSE = 3, NULL = 4;
     public final static int COMMENT = 5, ERROR = 6, END = 7;
 
@@ -33,12 +30,14 @@ public class JsopTokenizer {
 
     private final String jsop;
     private final int length;
+    private int lastPos;
     private int pos;
     private int currentType;
+    private boolean currentEscaped;
     private String currentToken;
     private int lastType;
     private String lastToken;
-    private String encodedString;
+    private boolean lastEscaped;
 
     public JsopTokenizer(String json) {
         this.jsop = json;
@@ -47,7 +46,6 @@ public class JsopTokenizer {
     }
 
     public String toString() {
-        // TODO newlines in strings should probably throw an exception
         return jsop;
     }
 
@@ -68,6 +66,15 @@ public class JsopTokenizer {
      * @return the token
      */
     public String getToken() {
+        return lastEscaped ? decode(lastToken) : lastToken;
+    }
+
+    /**
+     * Get the last encoded (raw) string, including escape sequences.
+     *
+     * @return the encoded string
+     */
+    public String getEscapedToken() {
         return lastToken;
     }
 
@@ -78,9 +85,8 @@ public class JsopTokenizer {
      * @throws IllegalStateException if the token type doesn't match
      */
     public String read(int type) {
-        String token = currentToken;
         if (matches(type)) {
-            return token;
+            return getToken();
         }
         throw getFormatException(jsop, pos, getTokenType(type));
     }
@@ -114,8 +120,10 @@ public class JsopTokenizer {
      * @return the token type
      */
     public int read() {
+        lastPos = pos;
         lastType = currentType;
         lastToken = currentToken;
+        lastEscaped = currentEscaped;
         try {
             currentType = readToken();
         } catch (IllegalArgumentException e) {
@@ -128,16 +136,8 @@ public class JsopTokenizer {
         return lastType;
     }
 
-    /**
-     * Get the last encoded (raw) string, including escape sequences.
-     *
-     * @return the encoded string
-     */
-    public String lastEncodedString() {
-        return encodedString;
-    }
-
     private int readToken() {
+        currentEscaped = false;
         char ch;
         while (true) {
             if (pos >= length) {
@@ -152,22 +152,16 @@ public class JsopTokenizer {
         int start = pos++;
         switch (ch) {
         case '\"': {
-            boolean escaped = false;
             while (true) {
                 ch = jsop.charAt(pos++);
                 if (ch == '\"') {
                     break;
                 } else if (ch == '\\') {
-                    escaped = true;
+                    currentEscaped = true;
                     pos++;
                 }
             }
-            String s = encodedString = jsop.substring(start + 1, pos - 1);
-            if (escaped) {
-                currentToken = decode(s);
-            } else {
-                currentToken = s;
-            }
+            currentToken = jsop.substring(start + 1, pos - 1);
             return STRING;
         }
         case '{':
@@ -351,4 +345,50 @@ public class JsopTokenizer {
         return s;
     }
 
+    /**
+     * Read a value and return the raw Json representation.
+     *
+     * @return the Json representation of the value
+     */
+    public String readRawValue() {
+        int start = lastPos;
+        skipRawValue();
+        return jsop.substring(start, lastPos);
+    }
+
+    private void skipRawValue() {
+        switch (currentType) {
+        case '[':
+            read();
+            if (!matches(']')) {
+                do {
+                    skipRawValue();
+                } while (matches(','));
+                read(']');
+            }
+            break;
+        case '{':
+            read();
+            if (!matches('}')) {
+                do {
+                    readString();
+                    read(':');
+                    skipRawValue();
+                } while (matches(','));
+                read('}');
+            }
+            break;
+        case NULL:
+        case NUMBER:
+        case TRUE:
+        case FALSE:
+        case COMMENT:
+        case STRING:
+            read();
+            break;
+        default:
+            throw getFormatException(jsop, pos, "value");
+        }
+    }
+
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java?rev=1135558&r1=1135557&r2=1135558&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
Tue Jun 14 13:20:20 2011
@@ -259,11 +259,11 @@ public class NodeImpl {
             return Val.get(type, t.readString());
         }
         String s = t.readString();
-        String encoded = t.lastEncodedString();
-        if (!encoded.startsWith("\\/")) {
+        String escaped = t.getEscapedToken();
+        if (!escaped.startsWith("\\/")) {
             return Val.get(s);
         }
-        s = encoded.substring(2);
+        s = escaped.substring(2);
         if (s.startsWith("NaN")) {
             return Val.get(Double.NaN);
         }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java?rev=1135558&r1=1135557&r2=1135558&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Tue Jun 14 13:20:20 2011
@@ -37,12 +37,32 @@ public class JsopTest extends TestCase {
         assertEquals("string", t.readString());
         t.read(':');
         assertEquals("/Date(0)/", t.readString());
-        assertEquals("/Date(0)/", t.lastEncodedString());
+        assertEquals("/Date(0)/", t.getEscapedToken());
         t.read(',');
         assertEquals("date", t.readString());
         t.read(':');
         assertEquals("/Date(0)/", t.readString());
-        assertEquals("\\/Date(0)\\/", t.lastEncodedString());
+        assertEquals("\\/Date(0)\\/", t.getEscapedToken());
+    }
+
+    public void testRawValue() {
+        JsopTokenizer t = new JsopTokenizer("{\"x\": [1], null, true, {\"y\": 1}, error}");
+        t.read('{');
+        assertEquals("x", t.readString());
+        t.read(':');
+        assertEquals(" [1]", t.readRawValue());
+        t.read(',');
+        assertEquals(" null", t.readRawValue());
+        t.read(',');
+        assertEquals(" true", t.readRawValue());
+        t.read(',');
+        assertEquals(" {\"y\": 1}", t.readRawValue());
+        try {
+            t.readRawValue();
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
     }
 
     public void testTokenizer() {



Mime
View raw message