Repository: vxquery
Updated Branches:
  refs/heads/master d9e8dc254 -> 2e3b83371


Changes to classes to use UTF8StringPointable length and to use the 
FuncitonHelper.arrraysEqual method


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

Branch: refs/heads/master
Commit: 6527a2861d88b530a9c4220ef077a84012ae4985
Parents: 7d7ab00
Author: riyafa <[email protected]>
Authored: Tue May 17 12:32:14 2016 +0530
Committer: riyafa <[email protected]>
Committed: Tue May 17 12:32:14 2016 +0530

----------------------------------------------------------------------
 .../accessors/jsonItem/ObjectPointable.java     | 121 +++++++++++++
 .../builders/jsonItem/ObjectBuilder.java        |  58 ++++++
 .../vxquery/datamodel/values/ValueTag.java      |   1 +
 .../datamodel/AbstractPointableTest.java        |   9 +-
 .../vxquery/datamodel/ObjectByteTest.java       | 181 +++++++++++++++++++
 5 files changed, 369 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/vxquery/blob/6527a286/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
new file mode 100644
index 0000000..27c2d20
--- /dev/null
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/jsonItem/ObjectPointable.java
@@ -0,0 +1,121 @@
+/*
+ * 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.vxquery.datamodel.accessors.jsonItem;
+
+import java.io.IOException;
+
+import org.apache.hyracks.api.dataflow.value.ITypeTraits;
+import org.apache.hyracks.data.std.api.AbstractPointable;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.api.IPointableFactory;
+import org.apache.hyracks.data.std.primitive.IntegerPointable;
+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.exceptions.ErrorCode;
+import org.apache.vxquery.exceptions.SystemException;
+import org.apache.vxquery.runtime.functions.util.FunctionHelper;
+
+public class ObjectPointable extends AbstractPointable {
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new ObjectPointable();
+        }
+    };
+    private static final int ENTRY_COUNT_SIZE = 4;
+    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 static int getSlotValue(byte[] bytes, int start, int idx) {
+        return IntegerPointable.getInteger(bytes, getSlotArrayOffset(start) + 
idx * SLOT_SIZE);
+    }
+
+    private static int getEntryCount(byte[] bytes, int start) {
+        return IntegerPointable.getInteger(bytes, start);
+    }
+
+    private static int getKeyLength(byte[] b, int s) {
+        return UTF8StringPointable.getUTFLength(b,s+1) + 3;
+    }
+
+    private static int getSlotArrayOffset(int start) {
+        return start + ENTRY_COUNT_SIZE;
+    }
+
+    private static int getDataAreaOffset(byte[] bytes, int start) {
+        return getSlotArrayOffset(start) + getEntryCount(bytes, start) * 
SLOT_SIZE;
+    }
+
+    public int getEntryCount() {
+        return getEntryCount(bytes, start);
+    }
+
+    public void getKeys(IPointable result) throws SystemException {
+        try {
+            abvs.reset();
+            sb.reset(abvs);
+            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);
+            }
+            sb.finish();
+            result.set(abvs);
+        } catch (IOException e) {
+            throw new SystemException(ErrorCode.SYSE0001);
+        }
+    }
+
+    public void getValue(TaggedValuePointable key, IPointable pointer) {
+        int dataAreaOffset = getDataAreaOffset(bytes, start);
+        int entryCount = getEntryCount();
+        int s, l;
+        for (int i = 0; i < entryCount; i++) {
+            s = dataAreaOffset + getRelativeEntryStartOffset(i);
+            l = getKeyLength(bytes, s);
+            key1.set(bytes, s, l);
+            if (FunctionHelper.arraysEqual(key1, key)) {
+                pointer.set(bytes, s + l, getEntryLength(i) - l);
+                break;
+            }
+        }
+    }
+
+    private int getRelativeEntryStartOffset(int idx) {
+        return idx == 0 ? 0 : getSlotValue(bytes, start, idx - 1);
+    }
+
+    private int getEntryLength(int idx) {
+        return getSlotValue(bytes, start, idx) - 
getRelativeEntryStartOffset(idx);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/vxquery/blob/6527a286/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
new file mode 100644
index 0000000..9432ea2
--- /dev/null
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/jsonItem/ObjectBuilder.java
@@ -0,0 +1,58 @@
+/*
+ * 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.vxquery.datamodel.builders.jsonItem;
+
+import java.io.DataOutput;
+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.values.ValueTag;
+import org.apache.vxquery.util.GrowableIntArray;
+
+public class ObjectBuilder {
+    private final GrowableIntArray slots = new GrowableIntArray();
+    private final ArrayBackedValueStorage dataArea = new 
ArrayBackedValueStorage();
+    private IMutableValueStorage mvs;
+
+    public void reset(IMutableValueStorage mvs) {
+        this.mvs = mvs;
+        slots.clear();
+        dataArea.reset();
+    }
+
+    public void addItem(IValueReference key, IValueReference value) throws 
IOException {
+        dataArea.getDataOutput().write(key.getByteArray(), 
key.getStartOffset(), key.getLength());
+        dataArea.getDataOutput().write(value.getByteArray(), 
value.getStartOffset(), value.getLength());
+        slots.append(dataArea.getLength());
+    }
+
+    public void finish() throws IOException {
+        DataOutput out = mvs.getDataOutput();
+        out.write(ValueTag.OBJECT_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());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/vxquery/blob/6527a286/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 e2c89e9..7827fe1 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
@@ -82,6 +82,7 @@ public class ValueTag {
     public static final int COMMENT_NODE_TAG = 105;
     public static final int PI_NODE_TAG = 106;
     public static final int NODE_TREE_TAG = 107;
+    public static final int OBJECT_TAG=109;
 
     public static boolean isAtomic(int tag) {
         return tag < 100;

http://git-wip-us.apache.org/repos/asf/vxquery/blob/6527a286/vxquery-core/src/test/java/org/apache/vxquery/datamodel/AbstractPointableTest.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/test/java/org/apache/vxquery/datamodel/AbstractPointableTest.java
 
b/vxquery-core/src/test/java/org/apache/vxquery/datamodel/AbstractPointableTest.java
index 9343465..9c2a68b 100644
--- 
a/vxquery-core/src/test/java/org/apache/vxquery/datamodel/AbstractPointableTest.java
+++ 
b/vxquery-core/src/test/java/org/apache/vxquery/datamodel/AbstractPointableTest.java
@@ -28,7 +28,9 @@ public class AbstractPointableTest {
 
     protected void getTaggedValuePointable(Object value, IPointable result) 
throws IOException {
         int start = abvsInput.getLength();
-        if (value instanceof java.lang.Long) {
+        if (value instanceof java.lang.Integer) {
+            writeInteger((Integer) value, abvsInput.getDataOutput());
+        }else if (value instanceof java.lang.Long) {
             writeLong((Long) value, abvsInput.getDataOutput());
         } else if (value instanceof java.lang.Double) {
             writeDouble((Double) value, abvsInput.getDataOutput());
@@ -38,6 +40,11 @@ public class AbstractPointableTest {
         result.set(abvsInput.getByteArray(), start, abvsInput.getLength() - 
start);
     }
 
+    protected void writeInteger(Integer value, DataOutput dOut) throws 
IOException {
+        dOut.write(ValueTag.XS_LONG_TAG);
+        dOut.writeLong(value);
+    }
+
     protected void writeLong(Long value, DataOutput dOut) throws IOException {
         dOut.write(ValueTag.XS_LONG_TAG);
         dOut.writeLong(value);

http://git-wip-us.apache.org/repos/asf/vxquery/blob/6527a286/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
new file mode 100644
index 0000000..197cd82
--- /dev/null
+++ 
b/vxquery-core/src/test/java/org/apache/vxquery/datamodel/ObjectByteTest.java
@@ -0,0 +1,181 @@
+/*
+ * 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.vxquery.datamodel;
+
+import java.io.IOException;
+
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+import org.apache.vxquery.datamodel.accessors.SequencePointable;
+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;
+
+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();
+
+    @Test
+    public void testEmptyObject() {
+        // Build test Object
+        abvsResult.reset();
+        try {
+            ob.reset(abvsResult);
+            ob.finish();
+        } catch (IOException e) {
+            Assert.fail("Test failed to write the sequence pointable.");
+        }
+        tvp.set(abvsResult);
+
+        // Check results.
+        if (tvp.getTag() != ValueTag.OBJECT_TAG) {
+            Assert.fail("Type tag is incorrect. Expected: " + 
ValueTag.OBJECT_TAG + " Got: " + tvp.getTag());
+        }
+        tvp.getValue(op);
+        if (op.getEntryCount() != 0) {
+            Assert.fail("Sequence size is incorrect. Expected: 0 Got: " + 
op.getEntryCount());
+        }
+    }
+
+    @Test
+    public void testSingleItemObject() {
+        // Build test Object
+        abvsResult.reset();
+        try {
+            ob.reset(abvsResult);
+            getTaggedValuePointable("id", tvp1);
+            getTaggedValuePointable(1, tvp2);
+            ob.addItem(tvp1, tvp2);
+            ob.finish();
+        } catch (IOException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        tvp.set(abvsResult);
+
+        // Check results.
+        if (tvp.getTag() != ValueTag.OBJECT_TAG) {
+            Assert.fail("Type tag is incorrect. Expected: " + 
ValueTag.OBJECT_TAG + " Got: " + tvp.getTag());
+        }
+
+        tvp.getValue(op);
+        try {
+            op.getKeys(tvp);
+        } catch (SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+
+        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)) {
+            Assert.fail("Key is incorrect.");
+        }
+
+        op.getValue(tvp1, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp2)) {
+            Assert.fail("Value is incorrect for the given key.");
+        }
+    }
+
+    @Test
+    public void testManyItemObject() {
+        // Build test object
+        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);
+            ob.finish();
+        } catch (IOException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+        tvp.set(abvsResult);
+
+        // Check results.
+        if (tvp.getTag() != ValueTag.OBJECT_TAG) {
+            Assert.fail("Type tag is incorrect. Expected: " + 
ValueTag.SEQUENCE_TAG + " Got: " + tvp.getTag());
+        }
+        tvp.getValue(op);
+        if (op.getEntryCount() != 3) {
+            Assert.fail("Object size is incorrect. Expected: 3 Got: " + 
op.getEntryCount());
+        }
+
+        //Test keys
+        try {
+            op.getKeys(tvp);
+        } catch (SystemException e) {
+            Assert.fail("Test failed to write the object pointable.");
+        }
+
+        if (tvp.getTag() != ValueTag.SEQUENCE_TAG) {
+            Assert.fail("Sequence tag is incorrect. Expected: " + 
ValueTag.SEQUENCE_TAG + " Got: " + tvp.getTag());
+        }
+        tvp.getValue(sp);
+        if (sp.getEntryCount() != 3) {
+            Assert.fail("Sequence size is incorrect. Expected: 3 Got: " + 
sp.getEntryCount());
+        }
+        sp.getEntry(0, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp1)) {
+            Assert.fail("Sequence item one is incorrect. Expected: " + 
ValueTag.XS_LONG_TAG + " Got: " + tvp.getTag());
+        }
+        sp.getEntry(1, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp3)) {
+            Assert.fail(
+                    "Sequence item two is incorrect. Expected: " + 
ValueTag.XS_DOUBLE_TAG + " Got: " + tvp.getTag());
+        }
+        sp.getEntry(2, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp5)) {
+            Assert.fail(
+                    "Sequence item three is incorrect. Expected: " + 
ValueTag.XS_STRING_TAG + " Got: " + tvp.getTag());
+        }
+
+        //Test values
+        op.getValue(tvp1, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp2)) {
+            Assert.fail("Value is incorrect for the given key.");
+        }
+        op.getValue(tvp3, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp4)) {
+            Assert.fail("Value is incorrect for the given key.");
+        }
+        op.getValue(tvp5, tvp);
+        if (!FunctionHelper.arraysEqual(tvp, tvp6)) {
+            Assert.fail("Value is incorrect for the given key.");
+        }
+    }
+
+}

Reply via email to