<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>DDeeffeerrrreedd Nnooddee--ccooppyyiinngg sScchheemmee ffoorr XXQQuueerryy Pprroocceessssoorrss</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Jan Kurˇs</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Jan Vrany´ Jan Kurs</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Jan Vrany</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Software Engineering Group</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>FIT CˇVUT</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>KoSleojfntw</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>nPgrGagrouue</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>CFzeITchCRVepUuTb</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>lic Kolejn kurs.ja</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>jaPnr.avgruaen</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>yC@zfeicth.cRveuptu.bclzic kurs.jan@post.cz</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>jan.vrany@fit.cvut.cz</string-name>
        </contrib>
      </contrib-group>
      <pub-date>
        <year>2010</year>
      </pub-date>
      <fpage>131</fpage>
      <lpage>138</lpage>
      <abstract>
        <p>XQuery is generic, widely adopted language for querying and manipulating XML data. Many of currently available native XML databases are using XQuery as its primary query language. The XQuery specification requires each XML node to belong to exactly one XML tree. In case of the XML subtree is appended into a new XML structure, the whole subtree has to be copied. This may lead into excessive and unnecessary data copying and duplication. In this paper, we present a new XML node copying scheme that defers the node data copy operation unless necessary. We will show that this schemes significantly reduces the XML node copy operations required during the query processing.</p>
      </abstract>
      <kwd-group>
        <kwd>XML</kwd>
        <kwd>XQuery</kwd>
        <kwd>XQuery Processor</kwd>
        <kwd>Smalltalk</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>1 Introduction
. . .</p>
      <p>Every node belongs to exactly one tree, and every tree has exactly one
root node.</p>
      <p>. . .</p>
      <p>If a XML node is added into a new XML tree, the naive realization of this
requirement would create a new node (by copying the original one) and the copy
would be placed into the new XML tree. Consider the query at figure 1 is to be
evaluated and its output is to be serialized to an output file.</p>
      <p>A whole XML subtree that matches fn:doc("doc.xml")//authors is never
used. This may lead into excessive node copying and higher memory
consumptions depending on the subtree size.</p>
      <p>In this paper we will describe an efficient node-copying scheme that avoids
unnecessary copying while preserving XQuery semantics. We will also discuss its
correctness and benchmark results.
1 let $authors = element authors { fn:doc("doc.xml")//authors }
2 let $titles = element titles { fn:doc("doc.xml")//titles }
3 return element result { $titles }</p>
      <p>The paper is organized as follows: section 2 give an overall description of the
node-copying scheme mentioned above. Section 3 discusses experimental results
based on running XMark benchmarks. Section 4 provides a brief overview of
related work. Section 5 concludes by summarizing presented work.
2</p>
      <p>Deferred Node-copying
The basic idea is simple: share existing XML nodes between node hierarchies and
defer node-copy operation unless absolutely inevitable. In our implementation
the XML node can belong into multiple node hierarchies, although the XQuery
specification requirement mentioned in section 1 is preserved.</p>
      <p>The deferred node copying scheme has been developed to meet two main
goals:
– separate query processing logic from underlying physical data model and
– reduce memory consumption by preventing unnecessary data copying</p>
      <p>The first requirement has software engineering origin. XQuery processors
should be able to operate over various data models, not necessarily XML-based.
Moreover, good separation of query processor from physical data model provides
possibility to use one XQuery implementation in multiple environments – as a
standalone XQuery tools or within a database management machine.</p>
      <p>The latter goal came from practical needs. In case of large documents and
complex queries, naive implementation of an XQuery may consume – in edge
cases – twice more memory than actually needed.
2.1</p>
      <p>XDM Adaptor
XDM specification defines a sequence to be an instance of data model. Each
sequence consists of zero or more items. An item is either a node or atomic value.
The specification also defines a bunch of node properties such as dm:node-name
or dm:parent.</p>
      <p>To meet our first goal we separates node from its physical data storage though
an XDM adaptor which operates on so called node ids. Node id is an unique
identifier of an XML node within particular physical storage. The structure of
the node id is not defined – in fact node id could be anything: reference to a
DOM node in memory, pointer to a database file or simple integer.</p>
      <p>Usage of XDM adaptor give us easy and straightforward way how to access
different physical data models. XDM adaptor abstracts any kind of data source
and may use any kind of optimization (such as extensive caching) to access data
effectively. However the physical data storage and access strategies are hidden
to the rest of the XQuery processor.
2.2</p>
      <p>Node States
In order to defer copy operation, a new node property called node state is
introduced. Each node is in exactly one state from following three states:
Accessed State. Nodes that come from external data source are in accessed
state.</p>
      <p>Constructed State. Nodes that are constructed during query processing are
in accessed state.</p>
      <p>Hybrid State. Nodes which belongs to multiple node hierarchies are in a hybrid
state.
2.3</p>
      <p>Actions
During the query processing, the state of the node may change. The state
diagram of the node is shown at figure 2. There are three kinds of actions:
Copy Action. The copy action is performed whenever the XML subtree is
appended into a new XML hierarchy. The original subtree should be duplicated
in order to meet the requirement XML node to belong into just one node
hierarchy.</p>
      <p>Change Action. The change action models any change in a data model such
as setting a new parent.
“Child Read” Action. The “child read” action represents the situation when
the XQuery processor accesses child nodes of given node.</p>
      <p>Copy</p>
      <p>Copy</p>
      <p>Child read</p>
      <p>Change/child read
Hybrid</p>
      <p>Accessed
error</p>
      <p>Constructed</p>
      <p>Copy</p>
      <p>Change</p>
      <p>Change/child read</p>
      <p>Consider a document doc.xml (it’s content is shown at figure 3) and query 1
(figure 4). During execution of the query, following actions are performed:
1 &lt;?xml version="1.0"?&gt;
2 &lt;root&gt;
3 &lt;elem&gt;elem1&lt;/elem&gt;
4 &lt;elem&gt;elem2&lt;/elem&gt;
5 &lt;/root&gt;
1 element myroot {
2 attribute attr { ’value’ },
3 fn:doc("doc.xml")/elem[0]
4 }
1. The myroot element is created in a constructed state. Then change actions
are issued on that node: setting the node name “myroot”, adding attribute
“attr” and appending a text node.
2. Afterwards, the doc.xml is read and two child read actions are performed in
order to evaluate XPath expression.
3. Finally, the first elem (accessed) node from doc.xml is to be added into the
myroot (constructed) node – the elem node and all its descendants should
be copied.
2.4</p>
      <p>Transitions
Accessed Node Transitions. When a copy action of accessed node is
triggered, the node state is changed from accessed to hybrid and no physical data
copy is made. Changes to accessed nodes are not permitted – any change will
immediately lead into an error.</p>
      <p>Constructed Node Transitions. Copy operations on constructed nodes
behaves exactly as on accessed nodes. Changes to constructed nodes are permitted.
Hybrid Node Transitions. Transitions based on actions on hybrid nodes are
bit more interesting:
Copy Action. Copy action on hybrid nodes is a no-op. As a result, the same
node is returned with its state unchanged.</p>
      <p>Change Action. Whenever any of node properties (dm:parent, dm:name etc.)
is to be changed the node state is changed to constructed and all node
properties are copied. See the query at figure 6. When processing expression
at line 5, two things happen (in that order):
1. The text node “elem1” (a result of $doc/elem[0]/text() expression)
is added to the myroot element. States of nodes after this addition are
depicted at left side of figure 7.
2. Afterwards, the text node value “elem1“ has to be changed to the “elem1
is the first” because of the specification requirements. Obviously, the
hybrid text node must be copied. The XML data accessible though $doc
must remain unchanged.</p>
      <p>Child Read Action. While appending a XML tree into a new structure, the
state of a root node of the appended tree is changed to hybrid, the reference
from the new structure is added to the hybrid. The rest of the appended
tree (children of the root node) are unchanged – they don’t know, that their
parent has changed its state to hybrid. This cause serious problems while
executing XPath commands. To overcome this issue, we convert hybrid node
to a constructed one during child read action. Such a behavior is illustrated
at figure 8.</p>
      <p>Data are physically copied only when hybrid node is either being changed or
its children are being read.</p>
      <p>}
1 let $doc:= doc("doc.xml")
2 return
3 element myroot {
4 element myelem {
5 { $doc/elem[0]/text() } is the first }
6 }
7
Serialization of Result Set. Once the query is processed, serialization of result
set may not lead into XML node copying. Because query is already processed, no
node kind transitions must be performed during serialization and thus no node
copies must be created. Obviously, if the application wants work with the result
set as with nodes in memory and wants to perform some modification on it, the
result set must be copied.</p>
      <p>Discussion</p>
    </sec>
    <sec id="sec-2">
      <title>Specification Conformance</title>
      <p>
        Although deferred node-copying scheme does not require the XML nodes to
belong to exactly one node hierarchy it preserves original XQuery semantics.
Our claim is based on the results from the XQuery Test Suite [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
      </p>
      <p>The axes tests and element constructors tests from Minimal Conformance
Expressions section of XQTS Catalogue cover the node identity semantics and
were used to test the correctness of deferred node-copying scheme. Our
proofof-concept implementation successfully passes all the mentioned test cases.
3.2</p>
    </sec>
    <sec id="sec-3">
      <title>Benchmarks</title>
      <p>
        Presented deferred node-copying scheme has been developed in order to increase
XQuery processor performance by reducing number of copy operations. A natural
question is whether this scheme has substantial effect in real-world applications.
The table 3 shows number of copy operations for selected XMark [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] queries1 on
a file created with the XMark data generator.
      </p>
      <p>Number of saved copies is dependent on a query characteristics. There are
no new nodes created in a Q1 command and that is why there is no difference in
results. There are text nodes appended to elements in a Q2 command. The text
nodes does not need to be copied at all, only transformed to the hybrid state.</p>
      <p>There is a subtree appended to each result item during the Q13 execution.
Without the optimization, each element of a tree has to be copied, but with the
optimization turned on, only a few of nodes are copied.
1 Plus one nonstandard query marked INC. Its code is element a
{doc("file:///auctions.xml") }. We include it as an illustration of extreme case.</p>
      <p>DNC</p>
      <p>IC</p>
      <p>Q. #</p>
      <p>DNC</p>
      <p>IC
Nh Nc Nh</p>
      <p>Nh Nc Nh</p>
      <p>Nc
Q1
Q3
Q5
Q7
Q9
Q11
Q13
Q15
Q17
Q19
Nh – number of hybrid nodes created
Nc – number of physically copied nodes
DNC – evaluated using deferred node-copying scheme
IC – evaluated using immediate copy as specified by the XQuery specification
eXist XQuery Processor. eXist2 is an open-source XML-native database with
XQuery as its primary query language. As far as we know, eXist XQuery
implementation unconditionally copies nodes whenever the node is to be added into
a different node hierarchy. Our approach is different since we avoid unnecessary
copy operations.</p>
      <p>Saxon XQuery Processor. Saxon3 is well-known, widely adopted XML tool
set including XSLT 2.0, XPath 2.0 and XQuery 1.0 processor. Saxon’s XQuery
processor introduces concept of virtual nodes – a light-weigh node shallow copies
that shares as many properties as possible with their origin.</p>
      <p>Similarly to our approach, for a given virtual node some of standard XDM
properties may be overridden – namely the parent property. When the Saxon
2 http://exist.sourceforge.net/
3 http://saxon.sourceforge.net/
XQuery processor iterates over virtual node’s children, those are converted to
virtual nodes.</p>
      <p>However, presented deferred node copying scheme differs from virtual nodes
approach in several aspects:
1. Creating virtual copies requires a new object to be allocated in the memory.</p>
      <p>Deferred node copying scheme shares the same object.
2. Creation of virtual copies is a part of XQuery processing logic and must
be explicitly expressed, whereas our approach separates copying logic of an
XDM model from the query evaluation logic.
5</p>
      <p>Conclusion and Future Work
This paper presents a deferred XML node-copying scheme for XQuery processors
that significantly reduces number of source nodes copy operations required
during query processing. This scheme defers the copy operation unless absolutely
inevitable. Whether the node is actually copied depends on a node state, a new
property which is maintained for each node in addition to standard XDM
properties. Correctness of this approach has been successfully tested by XQuery Test
Suite.</p>
      <p>The main benefits of deferred node-copying scheme are: (i) efficiency, (ii) easy
to implement, (iii) independent on physical data model and (iv) independent on
XQuery processing logic.</p>
      <p>
        As a future plan, we plan to extend this scheme for use with various XML
indexing approaches, Ctree [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] and [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] in particular.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>M. N.</given-names>
            <surname>Mary</surname>
          </string-name>
          <article-title>Ferna´ndez, Ashok Malhotra</article-title>
          .
          <source>Jonathan Marsh and N. Walsh. XQuery 1.0 and XPath 2</source>
          .
          <article-title>0 Data Model (XDM)</article-title>
          .
          <source>W3C, 1st edition</source>
          ,
          <year>2006</year>
          . http://www.w3.org/TR/xpath-datamodel.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>A.</given-names>
            <surname>Schmidt</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Waas</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Kersten</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M. J.</given-names>
            <surname>Carey</surname>
          </string-name>
          ,
          <string-name>
            <surname>I. Manolescu</surname>
          </string-name>
          , and
          <string-name>
            <given-names>R.</given-names>
            <surname>Busse</surname>
          </string-name>
          .
          <article-title>Xmark: A benchmark for xml data management</article-title>
          . In In VLDB, pages
          <fpage>974</fpage>
          -
          <lpage>985</lpage>
          ,
          <year>2002</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>W3C XML Query Working Group. XML Query</surname>
          </string-name>
          <article-title>Test Suite</article-title>
          .
          <source>W3C, 1st edition</source>
          ,
          <year>2006</year>
          . http://www.w3.org/XML/Query/test-suite/.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>Q.</given-names>
            <surname>Zou</surname>
          </string-name>
          , S. Liu, and
          <string-name>
            <given-names>W. W.</given-names>
            <surname>Chu</surname>
          </string-name>
          .
          <article-title>Ctree: a compact tree for indexing xml data</article-title>
          .
          <source>In WIDM '04: Proceedings of the 6th annual ACM international workshop on Web information and data management</source>
          , pages
          <fpage>39</fpage>
          -
          <lpage>46</lpage>
          , New York, NY, USA,
          <year>2004</year>
          . ACM.
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>Q.</given-names>
            <surname>Zou</surname>
          </string-name>
          , S. Liu, and
          <string-name>
            <given-names>W. W.</given-names>
            <surname>Chu</surname>
          </string-name>
          .
          <article-title>Using a compact tree to index and query xml data</article-title>
          .
          <source>In CIKM '04: Proceedings of the thirteenth ACM international conference on Information and knowledge management</source>
          , pages
          <fpage>234</fpage>
          -
          <lpage>235</lpage>
          , New York, NY, USA,
          <year>2004</year>
          . ACM.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>