Changes in this commit:
ENTRY_COUNT_SIZE takes its size from IntegerPointable
Removed string tag for key and custom built it
Return false for the getValue if value for the given key don't exist
Changed the pointable names in the tests
Added js:null


Project: http://git-wip-us.apache.org/repos/asf/vxquery/repo
Commit: http://git-wip-us.apache.org/repos/asf/vxquery/commit/a0f01bd0
Tree: http://git-wip-us.apache.org/repos/asf/vxquery/tree/a0f01bd0
Diff: http://git-wip-us.apache.org/repos/asf/vxquery/diff/a0f01bd0

Branch: refs/heads/master
Commit: a0f01bd0c4e60c73ebab1720bafb17f79aee8231
Parents: 131295d
Author: riyafa <[email protected]>
Authored: Wed May 18 12:15:21 2016 +0530
Committer: riyafa <[email protected]>
Committed: Wed May 18 12:15:21 2016 +0530

----------------------------------------------------------------------
 .../accessors/jsonItem/ObjectPointable.java     |  57 +++++---
 .../builders/jsonItem/ObjectBuilder.java        |  11 +-
 .../vxquery/datamodel/values/ValueTag.java      |   2 +
 .../vxquery/types/BuiltinTypeConstants.java     |   4 +-
 .../vxquery/datamodel/ObjectByteTest.java       | 141 ++++++++++++++-----
 5 files changed, 160 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/vxquery/blob/a0f01bd0/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/jsonItem/ObjectPointable.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/jsonItem/ObjectPointable.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/jsonItem/ObjectPointable.java
index 039d8cd..5b2e264 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/jsonItem/ObjectPointable.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/jsonItem/ObjectPointable.java
@@ -16,6 +16,7 @@
  */
 package org.apache.vxquery.datamodel.accessors.jsonItem;
 
+import java.io.DataOutput;
 import java.io.IOException;
 
 import org.apache.hyracks.api.dataflow.value.ITypeTraits;
@@ -27,10 +28,12 @@ import 
org.apache.hyracks.data.std.primitive.UTF8StringPointable;
 import org.apache.hyracks.data.std.primitive.VoidPointable;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
 import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
-import org.apache.vxquery.datamodel.builders.sequence.SequenceBuilder;
+import org.apache.vxquery.datamodel.values.ValueTag;
+import org.apache.vxquery.datamodel.values.XDMConstants;
 import org.apache.vxquery.exceptions.ErrorCode;
 import org.apache.vxquery.exceptions.SystemException;
 import org.apache.vxquery.runtime.functions.util.FunctionHelper;
+import org.apache.vxquery.util.GrowableIntArray;
 
 /**
  * The datamodel of the JSON object is represented in this class:
@@ -54,11 +57,11 @@ public class ObjectPointable extends AbstractPointable {
             return new ObjectPointable();
         }
     };
-    private static final int ENTRY_COUNT_SIZE = 4;
+    private static final int ENTRY_COUNT_SIZE = 
IntegerPointable.TYPE_TRAITS.getFixedLength();
     private static final int SLOT_SIZE = 4;
     private final ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
-    private final UTF8StringPointable key1 = (UTF8StringPointable) 
UTF8StringPointable.FACTORY.createPointable();
-    private final SequenceBuilder sb = new SequenceBuilder();
+    private final GrowableIntArray slots = new GrowableIntArray();
+    private final ArrayBackedValueStorage dataArea = new 
ArrayBackedValueStorage();
 
     private static int getSlotValue(byte[] bytes, int start, int idx) {
         return IntegerPointable.getInteger(bytes, getSlotArrayOffset(start) + 
idx * SLOT_SIZE);
@@ -69,7 +72,7 @@ public class ObjectPointable extends AbstractPointable {
     }
 
     private static int getKeyLength(byte[] b, int s) {
-        return UTF8StringPointable.getUTFLength(b, s + 1) + 3;
+        return UTF8StringPointable.getUTFLength(b, s) + 2;
     }
 
     private static int getSlotArrayOffset(int start) {
@@ -83,35 +86,57 @@ public class ObjectPointable extends AbstractPointable {
     public void getKeys(IPointable result) throws SystemException {
         try {
             abvs.reset();
-            sb.reset(abvs);
+            slots.clear();
+            dataArea.reset();
             int dataAreaOffset = getDataAreaOffset(bytes, start);
             int entryCount = getEntryCount();
             int s;
             for (int i = 0; i < entryCount; i++) {
                 s = dataAreaOffset + getRelativeEntryStartOffset(i);
-                key1.set(bytes, s, getKeyLength(bytes, s));
-                sb.addItem(key1);
+                dataArea.getDataOutput().write(ValueTag.XS_STRING_TAG);
+                dataArea.getDataOutput().write(bytes, s, getKeyLength(bytes, 
s));
+                slots.append(dataArea.getLength());
             }
-            sb.finish();
+            finishSequenceBuild();
             result.set(abvs);
         } catch (IOException e) {
             throw new SystemException(ErrorCode.SYSE0001);
         }
     }
 
-    public void getValue(TaggedValuePointable key, TaggedValuePointable 
result) {
+    private void finishSequenceBuild() throws IOException {
+        DataOutput out = abvs.getDataOutput();
+        if (slots.getSize() != 1) {
+            out.write(ValueTag.SEQUENCE_TAG);
+            int size = slots.getSize();
+            out.writeInt(size);
+            if (size > 0) {
+                int[] slotArray = slots.getArray();
+                for (int i = 0; i < size; ++i) {
+                    out.writeInt(slotArray[i]);
+                }
+                out.write(dataArea.getByteArray(), dataArea.getStartOffset(), 
dataArea.getLength());
+            }
+        } else {
+            out.write(dataArea.getByteArray(), dataArea.getStartOffset(), 
dataArea.getLength());
+        }
+    }
+
+    public void getValue(TaggedValuePointable key, TaggedValuePointable 
result) throws SystemException {
         int dataAreaOffset = getDataAreaOffset(bytes, start);
         int entryCount = getEntryCount();
-        int s, l;
-        for (int i = 0; i < entryCount; i++) {
+        int s, l, i;
+        for (i = 0; i < entryCount; i++) {
             s = dataAreaOffset + getRelativeEntryStartOffset(i);
             l = getKeyLength(bytes, s);
-            key1.set(bytes, s, l);
-            if (FunctionHelper.arraysEqual(key1, key)) {
+            if (FunctionHelper.arraysEqual(bytes, s, l, key.getByteArray(), 
key.getStartOffset() + 1,
+                    key.getLength() - 1)) {
                 result.set(bytes, s + l, getEntryLength(i) - l);
-                break;
+                return;
             }
-            //Todo: if no key found return null
+        }
+        if (entryCount == 0 || i == entryCount) {
+            XDMConstants.setFalse(result);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/vxquery/blob/a0f01bd0/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/jsonItem/ObjectBuilder.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/jsonItem/ObjectBuilder.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/jsonItem/ObjectBuilder.java
index 9432ea2..8f49198 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/jsonItem/ObjectBuilder.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/jsonItem/ObjectBuilder.java
@@ -22,10 +22,12 @@ import java.io.IOException;
 import org.apache.hyracks.data.std.api.IMutableValueStorage;
 import org.apache.hyracks.data.std.api.IValueReference;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+import org.apache.vxquery.datamodel.builders.base.AbstractBuilder;
+import org.apache.vxquery.datamodel.builders.base.IBuilder;
 import org.apache.vxquery.datamodel.values.ValueTag;
 import org.apache.vxquery.util.GrowableIntArray;
 
-public class ObjectBuilder {
+public class ObjectBuilder extends AbstractBuilder implements IBuilder {
     private final GrowableIntArray slots = new GrowableIntArray();
     private final ArrayBackedValueStorage dataArea = new 
ArrayBackedValueStorage();
     private IMutableValueStorage mvs;
@@ -36,8 +38,13 @@ public class ObjectBuilder {
         dataArea.reset();
     }
 
+    @Override
+    public int getValueTag() {
+        return ValueTag.OBJECT_TAG;
+    }
+
     public void addItem(IValueReference key, IValueReference value) throws 
IOException {
-        dataArea.getDataOutput().write(key.getByteArray(), 
key.getStartOffset(), key.getLength());
+        dataArea.getDataOutput().write(key.getByteArray(), 
key.getStartOffset() + 1, key.getLength() - 1);
         dataArea.getDataOutput().write(value.getByteArray(), 
value.getStartOffset(), value.getLength());
         slots.append(dataArea.getLength());
     }

http://git-wip-us.apache.org/repos/asf/vxquery/blob/a0f01bd0/vxquery-core/src/main/java/org/apache/vxquery/datamodel/values/ValueTag.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/values/ValueTag.java 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/values/ValueTag.java
index c464bc5..1f6ddc7 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/values/ValueTag.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/values/ValueTag.java
@@ -74,6 +74,8 @@ public class ValueTag {
     public static final int XS_NMTOKENS_TAG = 
BuiltinTypeConstants.XS_NMTOKENS_TYPE_ID;
     public static final int XS_ENTITIES_TAG = 
BuiltinTypeConstants.XS_ENTITIES_TYPE_ID;
 
+    public static final int JS_NULL_TAG = BuiltinTypeConstants.JS_NULL_ID;
+
     public static final int SEQUENCE_TAG = 100;
     public static final int DOCUMENT_NODE_TAG = 101;
     public static final int ELEMENT_NODE_TAG = 102;

http://git-wip-us.apache.org/repos/asf/vxquery/blob/a0f01bd0/vxquery-core/src/main/java/org/apache/vxquery/types/BuiltinTypeConstants.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/types/BuiltinTypeConstants.java 
b/vxquery-core/src/main/java/org/apache/vxquery/types/BuiltinTypeConstants.java
index eefebcc..79894a9 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/types/BuiltinTypeConstants.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/types/BuiltinTypeConstants.java
@@ -72,5 +72,7 @@ public class BuiltinTypeConstants {
     public static final int XS_NMTOKENS_TYPE_ID = 50;
     public static final int XS_ENTITIES_TYPE_ID = 51;
 
-    public static final int BUILTIN_TYPE_COUNT = 52;
+    public static final int JS_NULL_ID = 52;
+
+    public static final int BUILTIN_TYPE_COUNT = 53;
 }

http://git-wip-us.apache.org/repos/asf/vxquery/blob/a0f01bd0/vxquery-core/src/test/java/org/apache/vxquery/datamodel/ObjectByteTest.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/test/java/org/apache/vxquery/datamodel/ObjectByteTest.java 
b/vxquery-core/src/test/java/org/apache/vxquery/datamodel/ObjectByteTest.java
index eb29134..fd9310f 100644
--- 
a/vxquery-core/src/test/java/org/apache/vxquery/datamodel/ObjectByteTest.java
+++ 
b/vxquery-core/src/test/java/org/apache/vxquery/datamodel/ObjectByteTest.java
@@ -22,6 +22,7 @@ import 
org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
 import org.apache.vxquery.datamodel.accessors.jsonItem.ObjectPointable;
 import org.apache.vxquery.datamodel.builders.jsonItem.ObjectBuilder;
 import org.apache.vxquery.datamodel.values.ValueTag;
+import org.apache.vxquery.datamodel.values.XDMConstants;
 import org.apache.vxquery.exceptions.SystemException;
 import org.apache.vxquery.runtime.functions.util.FunctionHelper;
 import org.junit.Test;
@@ -29,17 +30,17 @@ import org.junit.Test;
 import junit.framework.Assert;
 
 public class ObjectByteTest extends AbstractPointableTest {
-    ArrayBackedValueStorage abvsResult = new ArrayBackedValueStorage();
-    ObjectBuilder ob = new ObjectBuilder();
-    TaggedValuePointable tvp = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    TaggedValuePointable tvp1 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    TaggedValuePointable tvp2 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    TaggedValuePointable tvp3 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    TaggedValuePointable tvp4 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    TaggedValuePointable tvp5 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    TaggedValuePointable tvp6 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-    SequencePointable sp = (SequencePointable) 
SequencePointable.FACTORY.createPointable();
-    ObjectPointable op = (ObjectPointable) 
ObjectPointable.FACTORY.createPointable();
+    private ArrayBackedValueStorage abvsResult = new ArrayBackedValueStorage();
+    private ObjectBuilder ob = new ObjectBuilder();
+    private TaggedValuePointable tvp = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private TaggedValuePointable tvpKey1 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private TaggedValuePointable tvpValue1 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private TaggedValuePointable tvpKey2 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private TaggedValuePointable tvpValue2 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private TaggedValuePointable tvpKey3 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private TaggedValuePointable tvpValue3 = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+    private SequencePointable sp = (SequencePointable) 
SequencePointable.FACTORY.createPointable();
+    private ObjectPointable op = (ObjectPointable) 
ObjectPointable.FACTORY.createPointable();
 
     @Test
     public void testEmptyObject() {
@@ -69,9 +70,9 @@ public class ObjectByteTest extends AbstractPointableTest {
         abvsResult.reset();
         try {
             ob.reset(abvsResult);
-            getTaggedValuePointable("id", tvp1);
-            getTaggedValuePointable(1, tvp2);
-            ob.addItem(tvp1, tvp2);
+            getTaggedValuePointable("id", tvpKey1);
+            getTaggedValuePointable(1, tvpValue1);
+            ob.addItem(tvpKey1, tvpValue1);
             ob.finish();
         } catch (IOException e) {
             Assert.fail("Test failed to write the object pointable.");
@@ -96,12 +97,15 @@ public class ObjectByteTest extends AbstractPointableTest {
         if (tvp.getTag() != ValueTag.XS_STRING_TAG) {
             Assert.fail("Type tag is incorrect. Expected: " + 
ValueTag.XS_STRING_TAG + " Got: " + tvp.getTag());
         }
-        if (!FunctionHelper.arraysEqual(tvp, tvp1)) {
+        if (!FunctionHelper.arraysEqual(tvp, tvpKey1)) {
             Assert.fail("Key is incorrect. Expected: id");
         }
-
-        op.getValue(tvp1, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp2)) {
+        try {
+            op.getValue(tvpKey1, tvp);
+        } catch (SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        if (!FunctionHelper.arraysEqual(tvp, tvpValue1)) {
             Assert.fail("Value is incorrect for the given key.");
         }
     }
@@ -112,15 +116,15 @@ public class ObjectByteTest extends AbstractPointableTest 
{
         try {
             // Add three items
             ob.reset(abvsResult);
-            getTaggedValuePointable("name", tvp1);
-            getTaggedValuePointable("A green door", tvp2);
-            ob.addItem(tvp1, tvp2);
-            getTaggedValuePointable("price", tvp3);
-            getTaggedValuePointable(12.5, tvp4);
-            ob.addItem(tvp3, tvp4);
-            getTaggedValuePointable("properties", tvp5);
-            getTaggedValuePointable(100l, tvp6);
-            ob.addItem(tvp5, tvp6);
+            getTaggedValuePointable("name", tvpKey1);
+            getTaggedValuePointable("A green door", tvpValue1);
+            ob.addItem(tvpKey1, tvpValue1);
+            getTaggedValuePointable("price", tvpKey2);
+            getTaggedValuePointable(12.5, tvpValue2);
+            ob.addItem(tvpKey2, tvpValue2);
+            getTaggedValuePointable("properties", tvpKey3);
+            getTaggedValuePointable(100L, tvpValue3);
+            ob.addItem(tvpKey3, tvpValue3);
             ob.finish();
         } catch (IOException e) {
             Assert.fail("Test failed to write the object pointable.");
@@ -151,34 +155,99 @@ public class ObjectByteTest extends AbstractPointableTest 
{
             Assert.fail("Object size is incorrect. Expected: 3 Got: " + 
sp.getEntryCount());
         }
         sp.getEntry(0, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp1)) {
+        if (!FunctionHelper.arraysEqual(tvp, tvpKey1)) {
             Assert.fail("Object key one is incorrect. Expected: name");
         }
         sp.getEntry(1, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp3)) {
+        if (!FunctionHelper.arraysEqual(tvp, tvpKey2)) {
             Assert.fail("Object key two is incorrect. Expected: price");
         }
         sp.getEntry(2, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp5)) {
+        if (!FunctionHelper.arraysEqual(tvp, tvpKey3)) {
             Assert.fail("Object key three is incorrect. Expected: properties");
         }
 
         //Test values
-        op.getValue(tvp1, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp2)) {
+        try {
+            op.getValue(tvpKey1, tvp);
+        } catch (SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        if (!FunctionHelper.arraysEqual(tvp, tvpValue1)) {
             Assert.fail("Value is incorrect for the given key. Expected: A 
green door with valuetag: "
                     + ValueTag.XS_STRING_TAG + " Got valuetag: " + 
tvp.getTag());
         }
-        op.getValue(tvp3, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp4)) {
+        try {
+            op.getValue(tvpKey2, tvp);
+        } catch (SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        if (!FunctionHelper.arraysEqual(tvp, tvpValue2)) {
             Assert.fail("Value is incorrect for the given key. Expected: 12.5 
with valuetag: " + ValueTag.XS_DOUBLE_TAG
                     + " Got valuetag: " + tvp.getTag());
         }
-        op.getValue(tvp5, tvp);
-        if (!FunctionHelper.arraysEqual(tvp, tvp6)) {
+        try {
+            op.getValue(tvpKey3, tvp);
+        } catch (SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        if (!FunctionHelper.arraysEqual(tvp, tvpValue3)) {
             Assert.fail("Value is incorrect for the given key. Expected: 100 
with valuetag: " + ValueTag.XS_LONG_TAG
                     + " Got valuetag: " + tvp.getTag());
         }
     }
 
+    @Test
+    public void testItemNotExistObject() {
+        // Build empty test Object
+        abvsResult.reset();
+        try {
+            ob.reset(abvsResult);
+            ob.finish();
+        } catch (IOException e) {
+            Assert.fail("Test failed to write the Object pointable.");
+        }
+        tvp.set(abvsResult);
+        tvp.getValue(op);
+        try {
+            getTaggedValuePointable("key", tvpKey1);
+            op.getValue(tvpKey1, tvpValue1);
+        } catch (IOException | SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        XDMConstants.setFalse(tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvpValue1)) {
+            Assert.fail("Value is incorrect for the given key. Expected: false 
with valuetag: "
+                    + ValueTag.XS_BOOLEAN_TAG + " Got valuetag: " + 
tvp.getTag());
+        }
+
+        // Build test object
+        try {
+            // Add two items
+            ob.reset(abvsResult);
+            getTaggedValuePointable("name", tvpKey1);
+            getTaggedValuePointable("A green door", tvpValue1);
+            ob.addItem(tvpKey1, tvpValue1);
+            getTaggedValuePointable("price", tvpKey2);
+            getTaggedValuePointable(12.5, tvpValue2);
+            ob.addItem(tvpKey2, tvpValue2);
+            ob.finish();
+        } catch (IOException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+
+        tvp.set(abvsResult);
+        tvp.getValue(op);
+        try {
+            getTaggedValuePointable("key", tvpKey3);
+            op.getValue(tvpKey3, tvpValue3);
+        } catch (IOException | SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        XDMConstants.setFalse(tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvpValue3)) {
+            Assert.fail("Value is incorrect for the given key. Expected: false 
with valuetag: "
+                    + ValueTag.XS_BOOLEAN_TAG + " Got valuetag: " + 
tvp.getTag());
+        }
+    }
 }

Reply via email to