Refactor SimpleObjectUnionScalarEvaluator
Project: http://git-wip-us.apache.org/repos/asf/vxquery/repo Commit: http://git-wip-us.apache.org/repos/asf/vxquery/commit/f9272583 Tree: http://git-wip-us.apache.org/repos/asf/vxquery/tree/f9272583 Diff: http://git-wip-us.apache.org/repos/asf/vxquery/diff/f9272583 Branch: refs/heads/master Commit: f9272583a5e149095557764435b28d0476af9e72 Parents: 85cb281 Author: riyafa <[email protected]> Authored: Thu Jul 14 21:15:38 2016 +0530 Committer: Preston Carman <[email protected]> Committed: Tue Jul 19 08:40:22 2016 -0700 ---------------------------------------------------------------------- ...bstractObjectConstructorScalarEvaluator.java | 53 +++++++++ .../ObjectConstructorScalarEvaluator.java | 36 ++----- .../SimpleObjectUnionScalarEvaluator.java | 107 ++++++++++--------- .../Json/Object/q15_object.txt | 1 + .../Json/Object/q16_object.txt | 1 + .../Queries/XQuery/Json/Object/q15_object.xq | 20 ++++ .../Queries/XQuery/Json/Object/q16_object.xq | 20 ++++ .../test/resources/cat/JsonObjectQueries.xml | 10 ++ 8 files changed, 170 insertions(+), 78 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/AbstractObjectConstructorScalarEvaluator.java ---------------------------------------------------------------------- diff --git a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/AbstractObjectConstructorScalarEvaluator.java b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/AbstractObjectConstructorScalarEvaluator.java new file mode 100644 index 0000000..810de40 --- /dev/null +++ b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/AbstractObjectConstructorScalarEvaluator.java @@ -0,0 +1,53 @@ +/* + * 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.runtime.functions.jsonitem; + +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.api.context.IHyracksTaskContext; +import org.apache.hyracks.data.std.api.IPointable; +import org.apache.hyracks.data.std.util.ArrayBackedValueStorage; +import org.apache.vxquery.datamodel.accessors.TaggedValuePointable; +import org.apache.vxquery.datamodel.builders.jsonitem.ObjectBuilder; +import org.apache.vxquery.runtime.functions.base.AbstractTaggedValueArgumentScalarEvaluator; +import org.apache.vxquery.runtime.functions.util.FunctionHelper; + +import java.util.ArrayList; +import java.util.List; + +public abstract class AbstractObjectConstructorScalarEvaluator extends AbstractTaggedValueArgumentScalarEvaluator { + protected final IHyracksTaskContext ctx; + protected final ObjectBuilder ob; + protected final ArrayBackedValueStorage abvs; + protected final List<TaggedValuePointable> tvps; + + public AbstractObjectConstructorScalarEvaluator(IHyracksTaskContext ctx, IScalarEvaluator[] args) { + super(args); + this.ctx = ctx; + ob = new ObjectBuilder(); + abvs = new ArrayBackedValueStorage(); + tvps = new ArrayList<>(); + } + + protected boolean isDuplicateKeys(IPointable key, List<TaggedValuePointable> pointables) { + for (TaggedValuePointable tvp : pointables) { + if (tvp != null && FunctionHelper.arraysEqual(tvp, key)) { + return true; + } + } + return false; + } +} http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java ---------------------------------------------------------------------- diff --git a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java index c002ad7..0d718a4 100644 --- a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java +++ b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java @@ -26,34 +26,24 @@ 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.builders.jsonitem.ArrayBuilder; -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.ErrorCode; import org.apache.vxquery.exceptions.SystemException; -import org.apache.vxquery.runtime.functions.base.AbstractTaggedValueArgumentScalarEvaluator; -import org.apache.vxquery.runtime.functions.util.FunctionHelper; import java.io.IOException; -public class ObjectConstructorScalarEvaluator extends AbstractTaggedValueArgumentScalarEvaluator { - private ObjectBuilder ob; - private TaggedValuePointable[] pointables; +public class ObjectConstructorScalarEvaluator extends AbstractObjectConstructorScalarEvaluator { private IPointable vp; private UTF8StringPointable sp; private SequencePointable seqp; - protected final IHyracksTaskContext ctx; - private final ArrayBackedValueStorage abvs; private final ArrayBackedValueStorage abvs1; private final BooleanPointable bp; private final ArrayBuilder ab; public ObjectConstructorScalarEvaluator(IHyracksTaskContext ctx, IScalarEvaluator[] args) { - super(args); - this.ctx = ctx; - abvs = new ArrayBackedValueStorage(); + super(ctx, args); abvs1 = new ArrayBackedValueStorage(); - ob = new ObjectBuilder(); vp = VoidPointable.FACTORY.createPointable(); sp = (UTF8StringPointable) UTF8StringPointable.FACTORY.createPointable(); seqp = (SequencePointable) SequencePointable.FACTORY.createPointable(); @@ -61,33 +51,21 @@ public class ObjectConstructorScalarEvaluator extends AbstractTaggedValueArgumen ab = new ArrayBuilder(); } - private boolean isDuplicate(TaggedValuePointable tempKey) { - for (TaggedValuePointable tvp : pointables) { - tempKey.getValue(vp); - if (tvp != null && FunctionHelper.arraysEqual(tvp, vp)) { - return true; - } - } - return false; - } - @Override protected void evaluate(TaggedValuePointable[] args, IPointable result) throws SystemException { - TaggedValuePointable key, value, qmc; + TaggedValuePointable key, value, qmc, tvp; try { abvs.reset(); ob.reset(abvs); - + tvps.clear(); int len = args.length; - pointables = new TaggedValuePointable[len / 3]; for (int i = 0; i < len; i += 3) { key = args[i]; value = args[i + 1]; qmc = args[i + 2]; - if (!isDuplicate(key)) { - pointables[i / 3] = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable(); - key.getValue(pointables[i / 3]); - sp.set(vp); + if (!isDuplicateKeys(key, tvps)) { + tvps.add(key); + key.getValue(sp); if (value.getTag() == ValueTag.SEQUENCE_TAG) { qmc.getValue(bp); value.getValue(seqp); http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java ---------------------------------------------------------------------- diff --git a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java index 60347b1..960cea4 100644 --- a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java +++ b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java @@ -24,79 +24,88 @@ 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.values.ValueTag; -import org.apache.vxquery.datamodel.values.XDMConstants; import org.apache.vxquery.exceptions.ErrorCode; import org.apache.vxquery.exceptions.SystemException; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -public class SimpleObjectUnionScalarEvaluator extends ObjectConstructorScalarEvaluator { +public class SimpleObjectUnionScalarEvaluator extends AbstractObjectConstructorScalarEvaluator { private final SequencePointable sp, sp1; + private ObjectPointable op; + private TaggedValuePointable key; + private final UTF8StringPointable stringKey; public SimpleObjectUnionScalarEvaluator(IHyracksTaskContext ctx, IScalarEvaluator[] args) { super(ctx, args); sp = (SequencePointable) SequencePointable.FACTORY.createPointable(); sp1 = (SequencePointable) SequencePointable.FACTORY.createPointable(); + stringKey = (UTF8StringPointable) UTF8StringPointable.FACTORY.createPointable(); } @Override protected void evaluate(TaggedValuePointable[] args, IPointable result) throws SystemException { - List<TaggedValuePointable> tvps = new ArrayList<>(); - - ObjectPointable op; - TaggedValuePointable key, value; TaggedValuePointable arg = args[0]; - if (arg.getTag() == ValueTag.SEQUENCE_TAG) { - arg.getValue(sp); - TaggedValuePointable tempTvp = ppool.takeOne(TaggedValuePointable.class); - TaggedValuePointable boolTvp = ppool.takeOne(TaggedValuePointable.class); - UTF8StringPointable tempKey = ppool.takeOne(UTF8StringPointable.class); - XDMConstants.setFalse(boolTvp); - try { + if (!(arg.getTag() == ValueTag.SEQUENCE_TAG || arg.getTag() == ValueTag.OBJECT_TAG)) { + throw new SystemException(ErrorCode.FORG0006); + } + TaggedValuePointable tempTvp = ppool.takeOne(TaggedValuePointable.class); + TaggedValuePointable tempValue = ppool.takeOne(TaggedValuePointable.class); + try { + abvs.reset(); + ob.reset(abvs); + tvps.clear(); + if (arg.getTag() == ValueTag.SEQUENCE_TAG) { + arg.getValue(sp); for (int i = 0; i < sp.getEntryCount(); ++i) { op = (ObjectPointable) ObjectPointable.FACTORY.createPointable(); sp.getEntry(i, tempTvp); tempTvp.getValue(op); op.getKeys(tempTvp); - if (tempTvp.getTag() == ValueTag.XS_STRING_TAG) { - key = ppool.takeOne(TaggedValuePointable.class); - value = ppool.takeOne(TaggedValuePointable.class); - tempTvp.getValue(tempKey); - op.getValue(tempKey, value); - key.set(tempTvp); - tvps.add(key); - tvps.add(value); - tvps.add(boolTvp); + addPairs(tempTvp, tempValue); + } + } else { + op = (ObjectPointable) ObjectPointable.FACTORY.createPointable(); + arg.getValue(op); + addPairs(tempTvp, tempValue); + } + ob.finish(); + result.set(abvs); + } catch (IOException e) { + throw new SystemException(ErrorCode.SYSE0001, e); + } finally { + ppool.giveBack(tempTvp); + for (TaggedValuePointable pointable : tvps) { + ppool.giveBack(pointable); + } + } + } - } else if (tempTvp.getTag() == ValueTag.SEQUENCE_TAG) { - tempTvp.getValue(sp1); - for (int j = 0; j < sp1.getEntryCount(); ++j) { - key = ppool.takeOne(TaggedValuePointable.class); - value = ppool.takeOne(TaggedValuePointable.class); - sp1.getEntry(j, tempTvp); - tempTvp.getValue(tempKey); - op.getValue(tempKey, value); - key.set(tempTvp); - tvps.add(key); - tvps.add(value); - tvps.add(boolTvp); - } + private void addPair(TaggedValuePointable tempTvp, TaggedValuePointable tempValue) + throws IOException, SystemException { + if (!isDuplicateKeys(tempTvp, tvps)) { + key = ppool.takeOne(TaggedValuePointable.class); + key.set(tempTvp); + tvps.add(key); + tempTvp.getValue(stringKey); + op.getValue(stringKey, tempValue); + ob.addItem(stringKey, tempValue); + } else { + throw new SystemException(ErrorCode.JNDY0003); + } + } - } - } - super.evaluate(tvps.toArray(new TaggedValuePointable[tvps.size()]), result); - } catch (IOException e) { - throw new SystemException(ErrorCode.SYSE0001, e); - } finally { - ppool.giveBack(tempKey); - ppool.giveBack(tempTvp); - ppool.giveBack(boolTvp); - for (TaggedValuePointable pointable : tvps) { - ppool.giveBack(pointable); - } + private void addPairs(TaggedValuePointable tempTvp, TaggedValuePointable tempValue) + throws IOException, SystemException { + op.getKeys(tempTvp); + if (tempTvp.getTag() == ValueTag.XS_STRING_TAG) { + addPair(tempTvp, tempValue); + } else if (tempTvp.getTag() == ValueTag.SEQUENCE_TAG) { + tempTvp.getValue(sp1); + for (int j = 0; j < sp1.getEntryCount(); ++j) { + key = ppool.takeOne(TaggedValuePointable.class); + sp1.getEntry(j, tempTvp); + addPair(tempTvp, tempValue); } } } http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q15_object.txt ---------------------------------------------------------------------- diff --git a/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q15_object.txt b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q15_object.txt new file mode 100644 index 0000000..37514da --- /dev/null +++ b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q15_object.txt @@ -0,0 +1 @@ +{"Captain":"Kirk"} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q16_object.txt ---------------------------------------------------------------------- diff --git a/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q16_object.txt b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q16_object.txt new file mode 100644 index 0000000..e0e0cb4 --- /dev/null +++ b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q16_object.txt @@ -0,0 +1 @@ +{"Captain":"Kirk","123456":"NUM"} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q15_object.xq ---------------------------------------------------------------------- diff --git a/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q15_object.xq b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q15_object.xq new file mode 100644 index 0000000..18d0d9d --- /dev/null +++ b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q15_object.xq @@ -0,0 +1,20 @@ +(: 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. :) + +(: Json Object Query :) +(: Issue VXQUERY-212 :) +{| { "Captain" : "Kirk" } |} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q16_object.xq ---------------------------------------------------------------------- diff --git a/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q16_object.xq b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q16_object.xq new file mode 100644 index 0000000..36b6e60 --- /dev/null +++ b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q16_object.xq @@ -0,0 +1,20 @@ +(: 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. :) + +(: Json Object Query :) +(: Issue VXQUERY-212 :) +{| { "Captain" : "Kirk" , 123456 : "NUM"} |} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/vxquery/blob/f9272583/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml ---------------------------------------------------------------------- diff --git a/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml b/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml index 2ee68c4..1968853 100644 --- a/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml +++ b/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml @@ -90,4 +90,14 @@ <query name="q14_object" date="2016-07-09"/> <output-file compare="Text">q14_object.txt</output-file> </test-case> + <test-case name="json-object-q15" FilePath="Json/Object/" Creator="Riyafa Abdul Hameed"> + <description>Object.</description> + <query name="q15_object" date="2016-07-16"/> + <output-file compare="Text">q15_object.txt</output-file> + </test-case> + <test-case name="json-object-q16" FilePath="Json/Object/" Creator="Riyafa Abdul Hameed"> + <description>Object.</description> + <query name="q16_object" date="2016-07-16"/> + <output-file compare="Text">q16_object.txt</output-file> + </test-case> </test-group> \ No newline at end of file
