jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1304307 - in /jackrabbit/oak/trunk/oak-jcr/src: main/java/org/apache/jackrabbit/oak/jcr/ main/java/org/apache/jackrabbit/oak/jcr/state/ main/java/org/apache/jackrabbit/oak/jcr/util/ test/java/org/apache/jackrabbit/oak/jcr/
Date Fri, 23 Mar 2012 12:18:59 GMT
Author: mduerig
Date: Fri Mar 23 12:18:59 2012
New Revision: 1304307

URL: http://svn.apache.org/viewvc?rev=1304307&view=rev
Log:
OAK-15: Clean up oak-jcr
Use Scalar instead of JsonValue (WIP)

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/ChangeTree.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/TransientNodeState.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/ValueConverter.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1304307&r1=1304306&r2=1304307&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
Fri Mar 23 12:18:59 2012
@@ -20,11 +20,8 @@ import org.apache.jackrabbit.JcrConstant
 import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
 import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
 import org.apache.jackrabbit.mk.model.PropertyState;
-import org.apache.jackrabbit.oak.jcr.json.FullJsonParser;
 import org.apache.jackrabbit.oak.jcr.json.JsonValue;
 import org.apache.jackrabbit.oak.jcr.json.JsonValue.JsonAtom;
-import org.apache.jackrabbit.oak.jcr.json.JsonValue.Type;
-import org.apache.jackrabbit.oak.jcr.json.UnescapingJsonTokenizer;
 import org.apache.jackrabbit.oak.jcr.state.TransientNodeState;
 import org.apache.jackrabbit.oak.jcr.util.Function1;
 import org.apache.jackrabbit.oak.jcr.util.ItemNameMatcher;
@@ -852,27 +849,8 @@ public class NodeImpl extends ItemImpl i
     private Iterator<Property> propertyIterator(Iterator<PropertyState> properties)
{
         return Iterators.map(properties, new Function1<PropertyState, Property>() {
             @Override
-            public Property apply(PropertyState state) {
-                // fixme: use Scalar class
-                JsonValue value;
-                String v = state.getEncodedValue();
-                if (v.startsWith("[")) {
-                    value = FullJsonParser.parseArray(new UnescapingJsonTokenizer(v));
-                }
-                else if (v.startsWith("\"")) {
-                    value =  new JsonAtom(v.substring(1, v.length() - 1), Type.STRING);
-                }
-                else if ("true".equalsIgnoreCase(v)) {
-                    value = JsonAtom.TRUE;
-                }
-                else if ("false".equalsIgnoreCase(v)) {
-                    value = JsonAtom.FALSE;
-                }
-                else {
-                    value = new JsonAtom(v, Type.NUMBER);
-                }
-
-                return PropertyImpl.create(sessionContext, NodeImpl.this.state, state.getName(),
value);
+            public Property apply(PropertyState propertyState) {
+                return PropertyImpl.create(sessionContext, state, propertyState);
             }
         });
     }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java?rev=1304307&r1=1304306&r2=1304307&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
Fri Mar 23 12:18:59 2012
@@ -16,11 +16,12 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
-import org.apache.jackrabbit.oak.jcr.json.JsonValue;
+import org.apache.jackrabbit.mk.model.PropertyState;
 import org.apache.jackrabbit.oak.jcr.state.TransientNodeState;
 import org.apache.jackrabbit.oak.jcr.util.LogUtil;
 import org.apache.jackrabbit.oak.jcr.util.Path;
 import org.apache.jackrabbit.oak.jcr.util.ValueConverter;
+import org.apache.jackrabbit.oak.kernel.KernelPropertyState;
 import org.apache.jackrabbit.value.ValueHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,8 +53,7 @@ public class PropertyImpl extends ItemIm
     private static final Logger log = LoggerFactory.getLogger(PropertyImpl.class);
 
     private final TransientNodeState parentState;
-    private final String name;
-    private final JsonValue value;
+    private final PropertyState state;
 
     static Property create(SessionContext<SessionImpl> sessionContext, Path path) throws
PathNotFoundException,
             ItemNotFoundException {
@@ -64,12 +64,12 @@ public class PropertyImpl extends ItemIm
         }
 
         String name = path.getName();
-        JsonValue value = parentState.getPropertyValue(name);
-        return new PropertyImpl(sessionContext, parentState, name, value);
+        PropertyState state = parentState.getPropertyState(name);
+        return new PropertyImpl(sessionContext, parentState, state);
     }
 
-    static Property create(SessionContext<SessionImpl> sessionContext, TransientNodeState
parentState, String name, JsonValue value) {
-        return new PropertyImpl(sessionContext, parentState, name, value);
+    static Property create(SessionContext<SessionImpl> sessionContext, TransientNodeState
parentState, PropertyState state) {
+        return new PropertyImpl(sessionContext, parentState, state);
     }
 
     public static boolean exist(SessionContext<SessionImpl> sessionContext, Path path)
{
@@ -77,11 +77,10 @@ public class PropertyImpl extends ItemIm
         return parentState != null && parentState.hasProperty(path.getName());
     }
 
-    private PropertyImpl(SessionContext<SessionImpl> sessionContext, TransientNodeState
parentState, String name, JsonValue value) {
+    private PropertyImpl(SessionContext<SessionImpl> sessionContext, TransientNodeState
parentState, PropertyState state) {
         super(sessionContext);
         this.parentState = parentState;
-        this.name = name;
-        this.value = value;
+        this.state = state;
     }
 
     //---------------------------------------------------------------< Item >---
@@ -98,7 +97,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public String getName() throws RepositoryException {
-        return name;
+        return state.getName();
     }
 
     /**
@@ -106,7 +105,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public String getPath() throws RepositoryException {
-        return parentState.getPath().concat(name).toJcrPath();
+        return parentState.getPath().concat(state.getName()).toJcrPath();
     }
 
     /**
@@ -142,7 +141,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public boolean isNew() {
-        return parentState.isPropertyNew(name);
+        return parentState.isPropertyNew(state.getName());
     }
 
     /**
@@ -150,7 +149,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public boolean isModified() {
-        return parentState.isPropertyModified(name);
+        return parentState.isPropertyModified(state.getName());
     }
 
     /**
@@ -158,7 +157,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public void remove() throws RepositoryException {
-        parentState.removeProperty(name);
+        parentState.removeProperty(state.getName());
     }
 
     /**
@@ -359,7 +358,7 @@ public class PropertyImpl extends ItemIm
             throw new ValueFormatException(LogUtil.safeGetJCRPath(this) + " is multi-valued.");
         }
 
-        return ValueConverter.toValue(getValueFactory(), value.asAtom());
+        return ValueConverter.toValue(getValueFactory(), ((KernelPropertyState) state).getValue());
 // fixme don't cast
     }
 
     @Override
@@ -369,7 +368,7 @@ public class PropertyImpl extends ItemIm
             throw new ValueFormatException(LogUtil.safeGetJCRPath(this) + " is not multi-valued.");
         }
 
-        return ValueConverter.toValue(getValueFactory(), value.asArray());
+        return ValueConverter.toValues(getValueFactory(), ((KernelPropertyState) state).getValues());
// fixme don't cast
     }
 
     /**
@@ -544,7 +543,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public boolean isMultiple() throws RepositoryException {
-        return value.isArray();
+        return ((KernelPropertyState) state).isMultiValues();  // fixme don't cast
     }
 
     //------------------------------------------------------------< private >---
@@ -584,7 +583,7 @@ public class PropertyImpl extends ItemIm
             remove();
         }
         else {
-            parentState.setProperty(name, ValueConverter.toJsonValue(value));
+            parentState.setProperty(state.getName(), ValueConverter.toJsonValue(value));
         }
     }
 
@@ -600,11 +599,11 @@ public class PropertyImpl extends ItemIm
             throw new IllegalArgumentException("Property type of a value cannot be undefined
(" + LogUtil.safeGetJCRPath(this) + ").");
         }
 
-        if (value == null) {
+        if (values == null) {
             remove();
         }
         else {
-            parentState.setProperty(name, ValueConverter.toJsonValue(values));
+            parentState.setProperty(state.getName(), ValueConverter.toJsonValue(values));
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/ChangeTree.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/ChangeTree.java?rev=1304307&r1=1304306&r2=1304307&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/ChangeTree.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/ChangeTree.java
Fri Mar 23 12:18:59 2012
@@ -272,17 +272,7 @@ public class ChangeTree {
                     new Function1<Entry<String, JsonValue>, PropertyState>()
{
                         @Override
                         public PropertyState apply(final Entry<String, JsonValue> entry)
{
-                            return new PropertyState() {
-                                @Override
-                                public String getName() {
-                                    return entry.getKey();
-                                }
-
-                                @Override
-                                public String getEncodedValue() {
-                                    return entry.getValue().toJson();
-                                }
-                            };
+                            return TransientNodeState.createPropertyState(entry.getKey(),
entry.getValue());
                         }
                     });
         }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/TransientNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/TransientNodeState.java?rev=1304307&r1=1304306&r2=1304307&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/TransientNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/state/TransientNodeState.java
Fri Mar 23 12:18:59 2012
@@ -19,9 +19,11 @@
 
 package org.apache.jackrabbit.oak.jcr.state;
 
+import org.apache.jackrabbit.ScalarImpl;
 import org.apache.jackrabbit.mk.model.ChildNodeEntry;
 import org.apache.jackrabbit.mk.model.NodeState;
 import org.apache.jackrabbit.mk.model.PropertyState;
+import org.apache.jackrabbit.oak.api.Scalar;
 import org.apache.jackrabbit.oak.jcr.SessionContext;
 import org.apache.jackrabbit.oak.jcr.SessionImpl;
 import org.apache.jackrabbit.oak.jcr.json.FullJsonParser;
@@ -36,11 +38,14 @@ import org.apache.jackrabbit.oak.jcr.uti
 import org.apache.jackrabbit.oak.jcr.util.Path;
 import org.apache.jackrabbit.oak.jcr.util.Predicate;
 import org.apache.jackrabbit.oak.kernel.KernelNodeState;
+import org.apache.jackrabbit.oak.kernel.KernelPropertyState;
 
 import javax.jcr.ItemExistsException;
 import javax.jcr.ItemNotFoundException;
 import javax.jcr.PathNotFoundException;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 
 import static org.apache.jackrabbit.oak.jcr.util.Iterators.toIterable;
 
@@ -235,6 +240,45 @@ public class TransientNodeState {
         return value;
     }
 
+    public PropertyState getPropertyState(String name) throws ItemNotFoundException {
+        JsonValue value = getPropertyValueOrNull(name);
+        if (value == null) {
+            throw new ItemNotFoundException(name);
+        }
+
+        return createPropertyState(name, value);
+    }
+
+    public static PropertyState createPropertyState(String name, JsonValue value) {
+        switch (value.type()) {
+            case STRING:
+            case NUMBER:
+            case BOOLEAN:
+                return new KernelPropertyState(name, toScalar(value));
+            case ARRAY:
+                List<Scalar> values = new ArrayList<Scalar>();
+                for (JsonValue v : value.asArray().value()) {
+                    values.add(toScalar(v));
+                }
+                return new KernelPropertyState(name, values);
+            default:
+                throw new IllegalStateException("Invalid value");
+        }
+    }
+
+    private static Scalar toScalar(JsonValue value) {
+        switch (value.type()) {
+            case STRING:
+                return ScalarImpl.createString(value.asAtom().value());
+            case NUMBER:
+                return ScalarImpl.createNumber(value.asAtom().value());
+            case BOOLEAN:
+                return ScalarImpl.createBoolean(value.asAtom().isTrue());
+            default:
+                throw new IllegalStateException("Invalid value");
+        }
+    }
+
     /**
      * @param name name of the property
      * @return {@code true} iff this instance has a property name {@code name}.

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/ValueConverter.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/ValueConverter.java?rev=1304307&r1=1304306&r2=1304307&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/ValueConverter.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/util/ValueConverter.java
Fri Mar 23 12:18:59 2012
@@ -19,6 +19,8 @@
 
 package org.apache.jackrabbit.oak.jcr.util;
 
+import org.apache.jackrabbit.ScalarImpl;
+import org.apache.jackrabbit.oak.api.Scalar;
 import org.apache.jackrabbit.oak.jcr.json.JsonValue;
 import org.apache.jackrabbit.oak.jcr.json.JsonValue.JsonArray;
 import org.apache.jackrabbit.oak.jcr.json.JsonValue.JsonAtom;
@@ -40,6 +42,34 @@ import java.util.List;
 public final class ValueConverter {
     private ValueConverter() {}
 
+    public static Scalar toScalar(Value value) throws RepositoryException {
+        switch (value.getType()) {
+            case PropertyType.STRING: {
+                return ScalarImpl.createString(value.getString());
+            }
+            case PropertyType.DOUBLE: {
+                return ScalarImpl.createDouble(value.getDouble());
+            }
+            case PropertyType.LONG: {
+                return ScalarImpl.createLong(value.getLong());
+            }
+            case PropertyType.BOOLEAN: {
+                return ScalarImpl.createBoolean(value.getBoolean());
+            }
+            case PropertyType.DECIMAL:
+            case PropertyType.BINARY:
+            case PropertyType.DATE:
+            case PropertyType.NAME:
+            case PropertyType.PATH:
+            case PropertyType.REFERENCE:
+            case PropertyType.WEAKREFERENCE:
+            case PropertyType.URI:
+            default: {
+                throw new UnsupportedRepositoryOperationException("toScalar"); // todo implement
toScalar
+            }
+        }
+    }
+    
     public static JsonValue toJsonValue(Value value) throws RepositoryException {
         switch (value.getType()) {
             case PropertyType.STRING: {
@@ -80,6 +110,34 @@ public final class ValueConverter {
         return new JsonArray(jsonValues);
     }
 
+    public static Value toValue(ValueFactory valueFactory, Scalar scalar)
+            throws UnsupportedRepositoryOperationException {
+
+        switch (scalar.getType()) {
+            case Scalar.BOOLEAN:
+                return valueFactory.createValue(scalar.getBoolean());
+            case Scalar.LONG:
+                return valueFactory.createValue(scalar.getLong());
+            case Scalar.DOUBLE:
+                return valueFactory.createValue(scalar.getDouble());
+            case Scalar.STRING:
+                return valueFactory.createValue(scalar.getString());
+            default:
+                throw new UnsupportedRepositoryOperationException("toValue"); // todo implement
toValue
+        }
+    }
+
+    public static Value[] toValues(ValueFactory valueFactory, List<Scalar> scalars)
+            throws UnsupportedRepositoryOperationException {
+
+        Value[] values = new Value[scalars.size()];
+        int k = 0;
+        for (Scalar scalar : scalars) {
+            values[k++] = toValue(valueFactory, scalar);
+        }
+        return values;
+    }
+
     public static Value toValue(ValueFactory valueFactory, JsonAtom jsonAtom) {
         switch (jsonAtom.type()) {
             case STRING: {
@@ -116,4 +174,5 @@ public final class ValueConverter {
         }
         return values;
     }
+
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java?rev=1304307&r1=1304306&r2=1304307&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
Fri Mar 23 12:18:59 2012
@@ -877,7 +877,7 @@ public class RepositoryTest extends Abst
     @Test
     public void setStringProperty() throws RepositoryException, IOException {
         Node parentNode = getNode(TEST_PATH);
-        addProperty(parentNode, "string", getSession().getValueFactory().createValue("string
value"));
+        addProperty(parentNode, "string", getSession().getValueFactory().createValue("string
\" value"));
 
         Property property = parentNode.getProperty("string");
         property.setValue("new value");



Mime
View raw message