Matthias Brantner has proposed merging 
lp:~zorba-coders/zorba/feature-reference_module into lp:zorba.

Commit message:
Added new module (http://www.zorba-xquery.com/modules/references) for 
(de)referencing nodes and json objects.


Requested reviews:
  Matthias Brantner (matthias-brantner)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/feature-reference_module/+merge/138345

Added new module (http://www.zorba-xquery.com/modules/references) for 
(de)referencing nodes and json objects.

References for JSON objects or arrays can only be retrieved if the item is 
stored in a collection.
-- 
https://code.launchpad.net/~zorba-coders/zorba/feature-reference_module/+merge/138345
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'ChangeLog'
--- ChangeLog	2012-11-20 01:01:49 +0000
+++ ChangeLog	2012-12-06 00:07:26 +0000
@@ -7,6 +7,8 @@
   * (bug #1039284) Implemented jn:json-doc().
   * (bug #867363) Added http-uri-resolution feature to disable HTTP
 	when resolving URIs.
+  * http://www.zorba-xquery.com/modules/references to (de)reference
+    XML nodes or JSON objects and arrays that are stored in a collection
 
 Optimizations:
   * Various optimizations in the implementation of the optimizer rules.

=== modified file 'include/zorba/pregenerated/diagnostic_list.h'
--- include/zorba/pregenerated/diagnostic_list.h	2012-11-12 16:17:02 +0000
+++ include/zorba/pregenerated/diagnostic_list.h	2012-12-06 00:07:26 +0000
@@ -530,7 +530,7 @@
 
 extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0070_INVALID_SERIALIZATION_METHOD_FOR_SAX;
 
-extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0080_CANNOT_RETRIEVE_NODE_REFERENCE;
+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0080_CANNOT_RETRIEVE_REFERENCE;
 
 extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0090_CANNOT_VALIDATE_NON_ROOT;
 

=== modified file 'modules/com/zorba-xquery/www/modules/CMakeLists.txt'
--- modules/com/zorba-xquery/www/modules/CMakeLists.txt	2012-10-08 12:09:36 +0000
+++ modules/com/zorba-xquery/www/modules/CMakeLists.txt	2012-12-06 00:07:26 +0000
@@ -60,6 +60,8 @@
   URI "http://www.zorba-xquery.com/modules/math";)
 DECLARE_ZORBA_MODULE(FILE node-reference.xq VERSION 2.0
   URI "http://www.zorba-xquery.com/modules/node-reference";)
+DECLARE_ZORBA_MODULE(FILE reference.xq VERSION 1.0
+  URI "http://www.zorba-xquery.com/modules/reference";)
 DECLARE_ZORBA_MODULE(FILE node-position.xq VERSION 2.0
   URI "http://www.zorba-xquery.com/modules/node-position";)
 DECLARE_ZORBA_MODULE(FILE node.xq VERSION 2.0

=== added file 'modules/com/zorba-xquery/www/modules/reference.xq'
--- modules/com/zorba-xquery/www/modules/reference.xq	1970-01-01 00:00:00 +0000
+++ modules/com/zorba-xquery/www/modules/reference.xq	2012-12-06 00:07:26 +0000
@@ -0,0 +1,90 @@
+xquery version "1.0";
+
+(:
+ : Copyright 2006-2012 The FLWOR Foundation.
+ :
+ : Licensed 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.
+:)
+
+(:~
+ : The module provides functions to compute an immutable and opaque reference 
+ : for nodes, objects, or arrays and to retrieve such items given their
+ : identifier, respectively.
+ : The identifiers are immutable, i.e. a identifier does not change
+ : during the items lifetime and cannot be reused for another item after the
+ : original item gets deleted.
+ : Identifiers are unique, in that, two different items will never have the same 
+ : identifier. A item, at any time during its lifetime, can be retrieved by its 
+ : identifier.
+ :
+ : <p>Please see the <a href="../../html/data_lifecycle.html">data lifecycle
+ : documentation</a> about details on storing items.</p>
+ : 
+ : @see <a href="../../html/data_lifecycle.html">Data Lifecycle</a>
+ :
+ : @author Matthias Brantner
+ :
+ :)
+module namespace ref = "http://www.zorba-xquery.com/modules/reference";;
+
+declare namespace zerr = "http://www.zorba-xquery.com/errors";;
+
+declare namespace ver = "http://www.zorba-xquery.com/options/versioning";;
+
+declare option ver:module-version "1.0";
+
+(:~
+ : Returns an immutable and opaque reference (with type xs:anyURI) for
+ : a given node, object, or array.
+ : 
+ : <p>The generated identifier is immutable, i.e. a identifier does not
+ : change during the item's lifetime and cannot be reused for another node after
+ : the original item gets deleted.</p>
+ :
+ : <p>Identifiers are also unique, in that, two different items will never 
+ : have the same identifier.</p>
+ :
+ : A item, at any time during its lifetime, can be retrieved by its 
+ : identifier, using the <tt>ref:dereference</tt> function.
+ :
+ : Please note that a reference can only be retrieved for a JSON object or JSON
+ : reference if the item is a member of a collection.
+ :
+ : @param $arg the node, object, or array for which the URI should be computed
+ :
+ : @return the opaque URI of the item.
+ :
+ : @error zerr::ZAPI0080 is raised if the object or array passed as argument
+ :   is not a member of a collection.
+ :)
+declare function ref:reference(
+  $arg as item()
+) as xs:anyURI external;
+
+(:~
+ : Returns the node, object, or array identified by the given reference.
+ :
+ : <p>The function returns the empty sequence if the item
+ : that is referenced does not exist.</p>
+ :
+ : @param $arg the URI of the item to retrieve.
+ :
+ : @return the item identified by the URI passed as parameter
+ :         or the empty-sequence if no item with that URI is found.
+ :
+ : @error zerr:ZAPI0028 if the given URI is not a valid reference
+ :        computed by the <tt>ref:reference</tt> function.
+ :)
+declare function ref:dereference(
+  $arg as xs:anyURI
+) as item()? external;

=== modified file 'src/context/static_context.cpp'
--- src/context/static_context.cpp	2012-12-02 07:25:04 +0000
+++ src/context/static_context.cpp	2012-12-06 00:07:26 +0000
@@ -351,6 +351,10 @@
 "http://www.zorba-xquery.com/modules/node-reference";;
 
 const char*
+static_context::ZORBA_REFERENCE_FN_NS =
+"http://www.zorba-xquery.com/modules/reference";;
+
+const char*
 static_context::ZORBA_NODEPOS_FN_NS =
 "http://www.zorba-xquery.com/modules/node-position";;
 
@@ -510,6 +514,7 @@
     return (ns == ZORBA_MATH_FN_NS ||
             ns == ZORBA_BASE64_FN_NS ||
             ns == ZORBA_NODEREF_FN_NS ||
+            ns == ZORBA_REFERENCE_FN_NS ||
             ns == ZORBA_NODEPOS_FN_NS ||
 
             ns == ZORBA_STORE_DYNAMIC_DOCUMENTS_FN_NS ||

=== modified file 'src/context/static_context.h'
--- src/context/static_context.h	2012-11-12 17:12:41 +0000
+++ src/context/static_context.h	2012-12-06 00:07:26 +0000
@@ -514,6 +514,7 @@
   static const char* ZORBA_JSON_FN_NS;
 
   static const char* ZORBA_NODEREF_FN_NS;
+  static const char* ZORBA_REFERENCE_FN_NS;
   static const char* ZORBA_NODEPOS_FN_NS;
   static const char* ZORBA_STORE_DYNAMIC_COLLECTIONS_DDL_FN_NS;
   static const char* ZORBA_STORE_DYNAMIC_COLLECTIONS_DML_FN_NS;

=== modified file 'src/diagnostics/diagnostic_en.xml'
--- src/diagnostics/diagnostic_en.xml	2012-12-01 01:04:39 +0000
+++ src/diagnostics/diagnostic_en.xml	2012-12-06 00:07:26 +0000
@@ -2030,8 +2030,8 @@
       <value>"$1": invalid serialization method for SAX</value>
     </diagnostic>
 
-    <diagnostic code="ZAPI0080" name="CANNOT_RETRIEVE_NODE_REFERENCE">
-      <value>can not retrieve node-reference for a node that is not in a collection</value>
+    <diagnostic code="ZAPI0080" name="CANNOT_RETRIEVE_REFERENCE">
+      <value>can not retrieve reference for a JSON object that is not in a collection</value>
     </diagnostic>
      
     <diagnostic code="ZAPI0090" name="CANNOT_VALIDATE_NON_ROOT">

=== modified file 'src/diagnostics/pregenerated/diagnostic_list.cpp'
--- src/diagnostics/pregenerated/diagnostic_list.cpp	2012-11-12 16:17:02 +0000
+++ src/diagnostics/pregenerated/diagnostic_list.cpp	2012-12-06 00:07:26 +0000
@@ -774,7 +774,7 @@
 ZorbaErrorCode ZAPI0070_INVALID_SERIALIZATION_METHOD_FOR_SAX( "ZAPI0070" );
 
 
-ZorbaErrorCode ZAPI0080_CANNOT_RETRIEVE_NODE_REFERENCE( "ZAPI0080" );
+ZorbaErrorCode ZAPI0080_CANNOT_RETRIEVE_REFERENCE( "ZAPI0080" );
 
 
 ZorbaErrorCode ZAPI0090_CANNOT_VALIDATE_NON_ROOT( "ZAPI0090" );

=== modified file 'src/diagnostics/pregenerated/dict_en.cpp'
--- src/diagnostics/pregenerated/dict_en.cpp	2012-12-01 01:04:39 +0000
+++ src/diagnostics/pregenerated/dict_en.cpp	2012-12-06 00:07:26 +0000
@@ -323,7 +323,7 @@
   { "ZAPI0043", "cannot serialize JSON item using current output method" },
   { "ZAPI0045", "cannot serialize sequence containing both JSON and XML items" },
   { "ZAPI0070", "\"$1\": invalid serialization method for SAX" },
-  { "ZAPI0080", "can not retrieve node-reference for a node that is not in a collection" },
+  { "ZAPI0080", "can not retrieve reference for a JSON object that is not in a collection" },
   { "ZAPI0090", "non root element cannot be validated in place" },
   { "ZCSE0001", "\"$1\": nonexistent input field" },
   { "ZCSE0002", "\"$1\": incompatible input field${: type=2}${, class=3}" },

=== modified file 'src/functions/library.cpp'
--- src/functions/library.cpp	2012-09-19 21:16:15 +0000
+++ src/functions/library.cpp	2012-12-06 00:07:26 +0000
@@ -45,6 +45,7 @@
 #include "functions/func_ic_ddl.h"
 #include "functions/func_maths.h"
 #include "functions/func_nodes.h"
+#include "functions/func_reference.h"
 #include "functions/func_node_position.h"
 #include "functions/func_node_sort_distinct.h"
 #include "functions/func_numerics.h"
@@ -128,6 +129,7 @@
   populate_context_json(sctx);
   populate_context_maths(sctx);
   populate_context_nodes(sctx);
+  populate_context_reference(sctx);
   populate_context_node_position(sctx);
   populate_context_numerics(sctx);
   populate_context_other_diagnostics(sctx);

=== added file 'src/functions/pregenerated/func_reference.cpp'
--- src/functions/pregenerated/func_reference.cpp	1970-01-01 00:00:00 +0000
+++ src/functions/pregenerated/func_reference.cpp	2012-12-06 00:07:26 +0000
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2006-2012 The FLWOR Foundation.
+ *
+ * Licensed 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.
+ */
+ 
+// ******************************************
+// *                                        *
+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
+// * SEE .xml FILE WITH SAME NAME           *
+// *                                        *
+// ******************************************
+
+
+#include "stdafx.h"
+#include "runtime/reference/reference.h"
+#include "functions/func_reference.h"
+
+
+namespace zorba{
+
+
+
+PlanIter_t fn_reference_reference::codegen(
+  CompilerCB*,
+  static_context* sctx,
+  const QueryLoc& loc,
+  std::vector<PlanIter_t>& argv,
+  expr& ann) const
+{
+  return new ReferenceIterator(sctx, loc, argv);
+}
+
+PlanIter_t fn_reference_has_reference::codegen(
+  CompilerCB*,
+  static_context* sctx,
+  const QueryLoc& loc,
+  std::vector<PlanIter_t>& argv,
+  expr& ann) const
+{
+  return new HasReferenceIterator(sctx, loc, argv);
+}
+
+PlanIter_t fn_reference_assign_reference::codegen(
+  CompilerCB*,
+  static_context* sctx,
+  const QueryLoc& loc,
+  std::vector<PlanIter_t>& argv,
+  expr& ann) const
+{
+  return new AssignReferenceIterator(sctx, loc, argv);
+}
+
+PlanIter_t fn_reference_dereference::codegen(
+  CompilerCB*,
+  static_context* sctx,
+  const QueryLoc& loc,
+  std::vector<PlanIter_t>& argv,
+  expr& ann) const
+{
+  return new DereferenceIterator(sctx, loc, argv);
+}
+
+void populate_context_reference(static_context* sctx)
+{
+
+
+      {
+    DECL_WITH_KIND(sctx, fn_reference_reference,
+        (createQName("http://www.zorba-xquery.com/modules/reference","","reference";), 
+        GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_ONE, 
+        GENV_TYPESYSTEM.ANY_URI_TYPE_ONE),
+        FunctionConsts::FN_REFERENCE_REFERENCE_1);
+
+  }
+
+
+
+
+      {
+    DECL_WITH_KIND(sctx, fn_reference_has_reference,
+        (createQName("http://www.zorba-xquery.com/modules/reference","","has-reference";), 
+        GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_ONE, 
+        GENV_TYPESYSTEM.BOOLEAN_TYPE_ONE),
+        FunctionConsts::FN_REFERENCE_HAS_REFERENCE_1);
+
+  }
+
+
+
+
+      {
+    DECL_WITH_KIND(sctx, fn_reference_assign_reference,
+        (createQName("http://www.zorba-xquery.com/modules/reference","","assign-reference";), 
+        GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_ONE, 
+        GENV_TYPESYSTEM.ANY_URI_TYPE_ONE, 
+        GENV_TYPESYSTEM.BOOLEAN_TYPE_ONE),
+        FunctionConsts::FN_REFERENCE_ASSIGN_REFERENCE_2);
+
+  }
+
+
+
+
+      {
+    DECL_WITH_KIND(sctx, fn_reference_dereference,
+        (createQName("http://www.zorba-xquery.com/modules/reference","","dereference";), 
+        GENV_TYPESYSTEM.ANY_URI_TYPE_ONE, 
+        GENV_TYPESYSTEM.STRUCTURED_ITEM_TYPE_QUESTION),
+        FunctionConsts::FN_REFERENCE_DEREFERENCE_1);
+
+  }
+
+}
+
+
+}
+
+
+

=== added file 'src/functions/pregenerated/func_reference.h'
--- src/functions/pregenerated/func_reference.h	1970-01-01 00:00:00 +0000
+++ src/functions/pregenerated/func_reference.h	2012-12-06 00:07:26 +0000
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2006-2012 The FLWOR Foundation.
+ *
+ * Licensed 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.
+ */
+ 
+// ******************************************
+// *                                        *
+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
+// * SEE .xml FILE WITH SAME NAME           *
+// *                                        *
+// ******************************************
+
+
+#ifndef ZORBA_FUNCTIONS_REFERENCE_H
+#define ZORBA_FUNCTIONS_REFERENCE_H
+
+
+#include "common/shared_types.h"
+#include "functions/function_impl.h"
+
+
+namespace zorba {
+
+
+void populate_context_reference(static_context* sctx);
+
+
+
+
+//fn-reference:reference
+class fn_reference_reference : public function
+{
+public:
+  fn_reference_reference(const signature& sig, FunctionConsts::FunctionKind kind)
+    : 
+    function(sig, kind)
+  {
+
+  }
+
+  bool mustCopyInputNodes(expr* fo, csize producer) const { return true; }
+
+  CODEGEN_DECL();
+};
+
+
+//fn-reference:has-reference
+class fn_reference_has_reference : public function
+{
+public:
+  fn_reference_has_reference(const signature& sig, FunctionConsts::FunctionKind kind)
+    : 
+    function(sig, kind)
+  {
+
+  }
+
+  CODEGEN_DECL();
+};
+
+
+//fn-reference:assign-reference
+class fn_reference_assign_reference : public function
+{
+public:
+  fn_reference_assign_reference(const signature& sig, FunctionConsts::FunctionKind kind)
+    : 
+    function(sig, kind)
+  {
+
+  }
+
+  unsigned short getScriptingKind() const { return SEQUENTIAL_FUNC_EXPR; }
+
+  CODEGEN_DECL();
+};
+
+
+//fn-reference:dereference
+class fn_reference_dereference : public function
+{
+public:
+  fn_reference_dereference(const signature& sig, FunctionConsts::FunctionKind kind)
+    : 
+    function(sig, kind)
+  {
+
+  }
+
+  CODEGEN_DECL();
+};
+
+
+} //namespace zorba
+
+
+#endif
+/*
+ * Local variables:
+ * mode: c++
+ * End:
+ */ 

=== modified file 'src/functions/pregenerated/function_enum.h'
--- src/functions/pregenerated/function_enum.h	2012-10-22 20:06:08 +0000
+++ src/functions/pregenerated/function_enum.h	2012-12-06 00:07:26 +0000
@@ -360,6 +360,10 @@
   FN_ZORBA_RANDOM_RANDOM_1,
   FN_ZORBA_UTIL_UUID_0,
   FN_ZORBA_RANDOM_UUID_0,
+  FN_REFERENCE_REFERENCE_1,
+  FN_REFERENCE_HAS_REFERENCE_1,
+  FN_REFERENCE_ASSIGN_REFERENCE_2,
+  FN_REFERENCE_DEREFERENCE_1,
   FN_ZORBA_SCHEMA_VALIDATE_IN_PLACE_1,
   FN_ZORBA_SCHEMA_SCHEMA_TYPE_1,
   FN_ZORBA_SCHEMA_IS_VALIDATED_1,

=== modified file 'src/runtime/pregenerated/iterator_enum.h'
--- src/runtime/pregenerated/iterator_enum.h	2012-10-15 13:39:36 +0000
+++ src/runtime/pregenerated/iterator_enum.h	2012-12-06 00:07:26 +0000
@@ -267,6 +267,10 @@
   TYPE_SeededRandomIterator,
   TYPE_RandomIterator,
   TYPE_UuidIterator,
+  TYPE_ReferenceIterator,
+  TYPE_HasReferenceIterator,
+  TYPE_AssignReferenceIterator,
+  TYPE_DereferenceIterator,
   TYPE_ValidateIterator,
   TYPE_ZorbaValidateInPlaceIterator,
   TYPE_ZorbaSchemaTypeIterator,

=== added directory 'src/runtime/reference'
=== added directory 'src/runtime/reference/pregenerated'
=== added file 'src/runtime/reference/pregenerated/reference.cpp'
--- src/runtime/reference/pregenerated/reference.cpp	1970-01-01 00:00:00 +0000
+++ src/runtime/reference/pregenerated/reference.cpp	2012-12-06 00:07:26 +0000
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2006-2012 The FLWOR Foundation.
+ *
+ * Licensed 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.
+ */
+ 
+// ******************************************
+// *                                        *
+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
+// * SEE .xml FILE WITH SAME NAME           *
+// *                                        *
+// ******************************************
+
+#include "stdafx.h"
+#include "zorbatypes/rchandle.h"
+#include "zorbatypes/zstring.h"
+#include "runtime/visitors/planiter_visitor.h"
+#include "runtime/reference/reference.h"
+#include "system/globalenv.h"
+
+
+
+namespace zorba {
+
+// <ReferenceIterator>
+SERIALIZABLE_CLASS_VERSIONS(ReferenceIterator)
+
+void ReferenceIterator::serialize(::zorba::serialization::Archiver& ar)
+{
+  serialize_baseclass(ar,
+  (NaryBaseIterator<ReferenceIterator, PlanIteratorState>*)this);
+}
+
+
+void ReferenceIterator::accept(PlanIterVisitor& v) const
+{
+  v.beginVisit(*this);
+
+  std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
+  std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
+  for ( ; lIter != lEnd; ++lIter ){
+    (*lIter)->accept(v);
+  }
+
+  v.endVisit(*this);
+}
+
+ReferenceIterator::~ReferenceIterator() {}
+
+// </ReferenceIterator>
+
+
+// <HasReferenceIterator>
+SERIALIZABLE_CLASS_VERSIONS(HasReferenceIterator)
+
+void HasReferenceIterator::serialize(::zorba::serialization::Archiver& ar)
+{
+  serialize_baseclass(ar,
+  (NaryBaseIterator<HasReferenceIterator, PlanIteratorState>*)this);
+}
+
+
+void HasReferenceIterator::accept(PlanIterVisitor& v) const
+{
+  v.beginVisit(*this);
+
+  std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
+  std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
+  for ( ; lIter != lEnd; ++lIter ){
+    (*lIter)->accept(v);
+  }
+
+  v.endVisit(*this);
+}
+
+HasReferenceIterator::~HasReferenceIterator() {}
+
+// </HasReferenceIterator>
+
+
+// <AssignReferenceIterator>
+SERIALIZABLE_CLASS_VERSIONS(AssignReferenceIterator)
+
+void AssignReferenceIterator::serialize(::zorba::serialization::Archiver& ar)
+{
+  serialize_baseclass(ar,
+  (NaryBaseIterator<AssignReferenceIterator, PlanIteratorState>*)this);
+}
+
+
+void AssignReferenceIterator::accept(PlanIterVisitor& v) const
+{
+  v.beginVisit(*this);
+
+  std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
+  std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
+  for ( ; lIter != lEnd; ++lIter ){
+    (*lIter)->accept(v);
+  }
+
+  v.endVisit(*this);
+}
+
+AssignReferenceIterator::~AssignReferenceIterator() {}
+
+// </AssignReferenceIterator>
+
+
+// <DereferenceIterator>
+SERIALIZABLE_CLASS_VERSIONS(DereferenceIterator)
+
+void DereferenceIterator::serialize(::zorba::serialization::Archiver& ar)
+{
+  serialize_baseclass(ar,
+  (NaryBaseIterator<DereferenceIterator, PlanIteratorState>*)this);
+}
+
+
+void DereferenceIterator::accept(PlanIterVisitor& v) const
+{
+  v.beginVisit(*this);
+
+  std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
+  std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
+  for ( ; lIter != lEnd; ++lIter ){
+    (*lIter)->accept(v);
+  }
+
+  v.endVisit(*this);
+}
+
+DereferenceIterator::~DereferenceIterator() {}
+
+// </DereferenceIterator>
+
+
+
+}
+
+

=== added file 'src/runtime/reference/pregenerated/reference.h'
--- src/runtime/reference/pregenerated/reference.h	1970-01-01 00:00:00 +0000
+++ src/runtime/reference/pregenerated/reference.h	2012-12-06 00:07:26 +0000
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2006-2012 The FLWOR Foundation.
+ *
+ * Licensed 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.
+ */
+ 
+// ******************************************
+// *                                        *
+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
+// * SEE .xml FILE WITH SAME NAME           *
+// *                                        *
+// ******************************************
+#ifndef ZORBA_RUNTIME_REFERENCE_REFERENCE_H
+#define ZORBA_RUNTIME_REFERENCE_REFERENCE_H
+
+
+#include "common/shared_types.h"
+
+
+
+#include "runtime/base/narybase.h"
+
+
+namespace zorba {
+
+/**
+ *        
+ *      declare function ref:reference($item as item()) as xs:anyURI
+ *    
+ * Author: Federico Cavalieri
+ */
+class ReferenceIterator : public NaryBaseIterator<ReferenceIterator, PlanIteratorState>
+{ 
+public:
+  SERIALIZABLE_CLASS(ReferenceIterator);
+
+  SERIALIZABLE_CLASS_CONSTRUCTOR2T(ReferenceIterator,
+    NaryBaseIterator<ReferenceIterator, PlanIteratorState>);
+
+  void serialize( ::zorba::serialization::Archiver& ar);
+
+  ReferenceIterator(
+    static_context* sctx,
+    const QueryLoc& loc,
+    std::vector<PlanIter_t>& children)
+    : 
+    NaryBaseIterator<ReferenceIterator, PlanIteratorState>(sctx, loc, children)
+  {}
+
+  virtual ~ReferenceIterator();
+
+  void accept(PlanIterVisitor& v) const;
+
+  bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+};
+
+
+/**
+ * 
+ *      declare function ref:has-reference($item as item()) as xs:boolean
+ *    
+ * Author: Till Westmann
+ */
+class HasReferenceIterator : public NaryBaseIterator<HasReferenceIterator, PlanIteratorState>
+{ 
+public:
+  SERIALIZABLE_CLASS(HasReferenceIterator);
+
+  SERIALIZABLE_CLASS_CONSTRUCTOR2T(HasReferenceIterator,
+    NaryBaseIterator<HasReferenceIterator, PlanIteratorState>);
+
+  void serialize( ::zorba::serialization::Archiver& ar);
+
+  HasReferenceIterator(
+    static_context* sctx,
+    const QueryLoc& loc,
+    std::vector<PlanIter_t>& children)
+    : 
+    NaryBaseIterator<HasReferenceIterator, PlanIteratorState>(sctx, loc, children)
+  {}
+
+  virtual ~HasReferenceIterator();
+
+  void accept(PlanIterVisitor& v) const;
+
+  bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+};
+
+
+/**
+ * 
+ *      declare function ref:assign-reference($item as item()) as xs:boolean
+ *    
+ * Author: Till Westmann
+ */
+class AssignReferenceIterator : public NaryBaseIterator<AssignReferenceIterator, PlanIteratorState>
+{ 
+public:
+  SERIALIZABLE_CLASS(AssignReferenceIterator);
+
+  SERIALIZABLE_CLASS_CONSTRUCTOR2T(AssignReferenceIterator,
+    NaryBaseIterator<AssignReferenceIterator, PlanIteratorState>);
+
+  void serialize( ::zorba::serialization::Archiver& ar);
+
+  AssignReferenceIterator(
+    static_context* sctx,
+    const QueryLoc& loc,
+    std::vector<PlanIter_t>& children)
+    : 
+    NaryBaseIterator<AssignReferenceIterator, PlanIteratorState>(sctx, loc, children)
+  {}
+
+  virtual ~AssignReferenceIterator();
+
+  void accept(PlanIterVisitor& v) const;
+
+  bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+};
+
+
+/**
+ * 
+ *      declare function ref:dereference($reference as xs:anyURI) as item()?
+ *    
+ * Author: Federico Cavalieri
+ */
+class DereferenceIterator : public NaryBaseIterator<DereferenceIterator, PlanIteratorState>
+{ 
+public:
+  SERIALIZABLE_CLASS(DereferenceIterator);
+
+  SERIALIZABLE_CLASS_CONSTRUCTOR2T(DereferenceIterator,
+    NaryBaseIterator<DereferenceIterator, PlanIteratorState>);
+
+  void serialize( ::zorba::serialization::Archiver& ar);
+
+  DereferenceIterator(
+    static_context* sctx,
+    const QueryLoc& loc,
+    std::vector<PlanIter_t>& children)
+    : 
+    NaryBaseIterator<DereferenceIterator, PlanIteratorState>(sctx, loc, children)
+  {}
+
+  virtual ~DereferenceIterator();
+
+  void accept(PlanIterVisitor& v) const;
+
+  bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+};
+
+
+}
+#endif
+/*
+ * Local variables:
+ * mode: c++
+ * End:
+ */ 

=== added file 'src/runtime/reference/reference_impl.cpp'
--- src/runtime/reference/reference_impl.cpp	1970-01-01 00:00:00 +0000
+++ src/runtime/reference/reference_impl.cpp	2012-12-06 00:07:26 +0000
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2006-2012 The FLWOR Foundation.
+ * 
+ * Licensed 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.
+ */
+#include "stdafx.h"
+
+#include "runtime/reference/reference.h"
+//#include "zorbamisc/ns_consts.h"
+
+#include "system/globalenv.h"
+#include "context/static_context.h"
+
+#include "store/api/item.h"
+//#include "store/api/iterator.h"
+#include "store/api/item_factory.h"
+#include "store/api/store.h"
+
+//#include "util/string_util.h"
+//#include "util/uri_util.h"
+//#include "zorbautils/string_util.h"
+
+using namespace std;
+
+namespace zorba {
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+ReferenceIterator::nextImpl(store::Item_t& aResult, PlanState& aPlanState) const
+{
+  store::Item_t lItem;
+  bool lHasRef;
+
+  PlanIteratorState* state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, aPlanState);
+
+  consumeNext(lItem, theChildren[0].getp(), aPlanState);
+
+  try
+  {
+    lHasRef = GENV_STORE.getNodeReference(aResult, lItem);
+  }
+  catch (ZorbaException& e)
+  {
+    set_source( e, loc );
+    throw;
+  }
+  STACK_PUSH(lHasRef, state);
+
+  STACK_END (state);
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+HasReferenceIterator::nextImpl(store::Item_t& aResult, PlanState& aPlanState) const
+{
+  store::Item_t lItem;
+  xs_boolean lHasReference;
+
+  PlanIteratorState* state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, aPlanState);
+
+  consumeNext(lItem, theChildren[0].getp(), aPlanState);
+
+  lHasReference = GENV_STORE.hasReference(lItem);
+
+  STACK_PUSH(GENV_ITEMFACTORY->createBoolean(aResult, lHasReference), state);
+
+  STACK_END (state);
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+AssignReferenceIterator::nextImpl(store::Item_t& aResult, PlanState& aPlanState) const
+{
+  store::Item_t lItem;
+  store::Item_t lUUID;
+  xs_boolean lHaveResult;
+
+  PlanIteratorState* state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, aPlanState);
+
+  consumeNext(lItem, theChildren[0].getp(), aPlanState);
+  consumeNext(lUUID, theChildren[1].getp(), aPlanState);
+  try
+  {
+    lHaveResult = GENV_STORE.assignReference(lItem, lUUID->getStringValue());
+  }
+  catch (ZorbaException& e)
+  {
+    set_source( e, loc );
+    throw;
+  }
+  STACK_PUSH(GENV_ITEMFACTORY->createBoolean(aResult, lHaveResult), state);
+
+  STACK_END (state);
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+DereferenceIterator::nextImpl(store::Item_t& result, PlanState& planState) const
+{
+  store::Item_t lUUID;
+  bool haveResult;
+
+  PlanIteratorState* state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
+
+  consumeNext(lUUID, theChildren[0].getp(), planState);
+  try
+  {
+    haveResult = GENV_STORE.getNodeByReference(result, lUUID->getStringValue());
+  }
+  catch (ZorbaException& e)
+  {
+    set_source( e, loc );
+    throw;
+  }
+  STACK_PUSH(haveResult, state);
+
+  STACK_END (state);
+}
+
+} // namespace zorba
+/* vim:set et sw=2 ts=2: */

=== modified file 'src/runtime/spec/mappings.xml'
--- src/runtime/spec/mappings.xml	2012-10-08 12:09:36 +0000
+++ src/runtime/spec/mappings.xml	2012-12-06 00:07:26 +0000
@@ -23,6 +23,10 @@
     <zorba:namespace uri="http://www.zorba-xquery.com/modules/node-reference";
                      define="ZORBA_NODEREF_FN_NS"
                      prefix="fn-zorba-ref"/>
+
+    <zorba:namespace uri="http://www.zorba-xquery.com/modules/reference";
+                     define="ZORBA_REF_FN_NS"
+                     prefix="fn-reference"/>
     
     <zorba:namespace uri="http://www.zorba-xquery.com/modules/node-position";
                      define="ZORBA_NODEPOS_FN_NS"

=== added directory 'src/runtime/spec/reference'
=== added file 'src/runtime/spec/reference/reference.xml'
--- src/runtime/spec/reference/reference.xml	1970-01-01 00:00:00 +0000
+++ src/runtime/spec/reference/reference.xml	2012-12-06 00:07:26 +0000
@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+/////////////////////////////////////////////////////////////////////////////////
+//                                                                             //
+/////////////////////////////////////////////////////////////////////////////////
+-->
+<zorba:iterators
+  xmlns:zorba="http://www.zorba-xquery.com";
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://www.zorba-xquery.com ../runtime.xsd">
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="ReferenceIterator">
+
+    <zorba:description author="Federico Cavalieri">       
+      declare function ref:reference($item as item()) as xs:anyURI
+    </zorba:description>
+    
+    <zorba:function>
+    
+      <zorba:signature localname="reference" prefix="fn-reference">
+        <zorba:param>structured-item()</zorba:param>   
+        <zorba:output>xs:anyURI</zorba:output>
+      </zorba:signature>
+      
+      <zorba:methods>
+        <zorba:mustCopyInputNodes value="true"/>
+      </zorba:methods>
+
+    </zorba:function>
+
+  </zorba:iterator>
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="HasReferenceIterator">
+
+    <zorba:description author="Till Westmann">
+      declare function ref:has-reference($item as item()) as xs:boolean
+    </zorba:description>
+
+    <zorba:function>
+
+      <zorba:signature localname="has-reference" prefix="fn-reference">
+        <zorba:param>structured-item()</zorba:param>
+        <zorba:output>xs:boolean</zorba:output>
+      </zorba:signature>
+
+    </zorba:function>
+
+  </zorba:iterator>
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="AssignReferenceIterator">
+
+    <zorba:description author="Till Westmann">
+      declare function ref:assign-reference($item as item()) as xs:boolean
+    </zorba:description>
+
+    <zorba:function>
+
+      <zorba:signature localname="assign-reference" prefix="fn-reference">
+        <zorba:param>structured-item()</zorba:param>
+        <zorba:param>xs:anyURI</zorba:param>
+        <zorba:output>xs:boolean</zorba:output>
+      </zorba:signature>
+
+      <zorba:methods>
+        <zorba:getScriptingKind returnValue="SEQUENTIAL_FUNC_EXPR"/>
+      </zorba:methods>
+
+    </zorba:function>
+
+  </zorba:iterator>
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="DereferenceIterator">
+
+    <zorba:description author="Federico Cavalieri">
+      declare function ref:dereference($reference as xs:anyURI) as item()?
+    </zorba:description>
+
+    <zorba:function>
+    
+      <zorba:signature localname="dereference" prefix="fn-reference">
+        <zorba:param>xs:anyURI</zorba:param>
+        <zorba:output>structured-item()?</zorba:output>
+      </zorba:signature>
+      
+    </zorba:function>
+        
+  </zorba:iterator>
+
+</zorba:iterators>
+

=== modified file 'src/runtime/visitors/pregenerated/planiter_visitor.h'
--- src/runtime/visitors/pregenerated/planiter_visitor.h	2012-10-15 13:39:36 +0000
+++ src/runtime/visitors/pregenerated/planiter_visitor.h	2012-12-06 00:07:26 +0000
@@ -560,6 +560,14 @@
 
     class UuidIterator;
 
+    class ReferenceIterator;
+
+    class HasReferenceIterator;
+
+    class AssignReferenceIterator;
+
+    class DereferenceIterator;
+
 #ifndef ZORBA_NO_XMLSCHEMA
     class ValidateIterator;
 #endif
@@ -1518,6 +1526,18 @@
     virtual void beginVisit ( const UuidIterator& ) = 0;
     virtual void endVisit   ( const UuidIterator& ) = 0;
 
+    virtual void beginVisit ( const ReferenceIterator& ) = 0;
+    virtual void endVisit   ( const ReferenceIterator& ) = 0;
+
+    virtual void beginVisit ( const HasReferenceIterator& ) = 0;
+    virtual void endVisit   ( const HasReferenceIterator& ) = 0;
+
+    virtual void beginVisit ( const AssignReferenceIterator& ) = 0;
+    virtual void endVisit   ( const AssignReferenceIterator& ) = 0;
+
+    virtual void beginVisit ( const DereferenceIterator& ) = 0;
+    virtual void endVisit   ( const DereferenceIterator& ) = 0;
+
 #ifndef ZORBA_NO_XMLSCHEMA
     virtual void beginVisit ( const ValidateIterator& ) = 0;
     virtual void endVisit   ( const ValidateIterator& ) = 0;

=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.cpp'
--- src/runtime/visitors/pregenerated/printer_visitor.cpp	2012-10-15 13:39:36 +0000
+++ src/runtime/visitors/pregenerated/printer_visitor.cpp	2012-12-06 00:07:26 +0000
@@ -62,6 +62,7 @@
 #include "runtime/parsing_and_serializing/parsing_and_serializing.h"
 #include "runtime/qnames/qnames.h"
 #include "runtime/random/random.h"
+#include "runtime/reference/reference.h"
 #include "runtime/schema/schema.h"
 #include "runtime/sequences/sequences.h"
 #include "runtime/store/documents.h"
@@ -3467,6 +3468,62 @@
 }
 // </UuidIterator>
 
+
+// <ReferenceIterator>
+void PrinterVisitor::beginVisit ( const ReferenceIterator& a) {
+  thePrinter.startBeginVisit("ReferenceIterator", ++theId);
+  printCommons( &a, theId );
+  thePrinter.endBeginVisit( theId );
+}
+
+void PrinterVisitor::endVisit ( const ReferenceIterator& ) {
+  thePrinter.startEndVisit();
+  thePrinter.endEndVisit();
+}
+// </ReferenceIterator>
+
+
+// <HasReferenceIterator>
+void PrinterVisitor::beginVisit ( const HasReferenceIterator& a) {
+  thePrinter.startBeginVisit("HasReferenceIterator", ++theId);
+  printCommons( &a, theId );
+  thePrinter.endBeginVisit( theId );
+}
+
+void PrinterVisitor::endVisit ( const HasReferenceIterator& ) {
+  thePrinter.startEndVisit();
+  thePrinter.endEndVisit();
+}
+// </HasReferenceIterator>
+
+
+// <AssignReferenceIterator>
+void PrinterVisitor::beginVisit ( const AssignReferenceIterator& a) {
+  thePrinter.startBeginVisit("AssignReferenceIterator", ++theId);
+  printCommons( &a, theId );
+  thePrinter.endBeginVisit( theId );
+}
+
+void PrinterVisitor::endVisit ( const AssignReferenceIterator& ) {
+  thePrinter.startEndVisit();
+  thePrinter.endEndVisit();
+}
+// </AssignReferenceIterator>
+
+
+// <DereferenceIterator>
+void PrinterVisitor::beginVisit ( const DereferenceIterator& a) {
+  thePrinter.startBeginVisit("DereferenceIterator", ++theId);
+  printCommons( &a, theId );
+  thePrinter.endBeginVisit( theId );
+}
+
+void PrinterVisitor::endVisit ( const DereferenceIterator& ) {
+  thePrinter.startEndVisit();
+  thePrinter.endEndVisit();
+}
+// </DereferenceIterator>
+
 #ifndef ZORBA_NO_XMLSCHEMA
 // <ValidateIterator>
 void PrinterVisitor::beginVisit ( const ValidateIterator& a) {

=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.h'
--- src/runtime/visitors/pregenerated/printer_visitor.h	2012-10-15 13:39:36 +0000
+++ src/runtime/visitors/pregenerated/printer_visitor.h	2012-12-06 00:07:26 +0000
@@ -860,6 +860,18 @@
     void beginVisit( const UuidIterator& );
     void endVisit  ( const UuidIterator& );
 
+    void beginVisit( const ReferenceIterator& );
+    void endVisit  ( const ReferenceIterator& );
+
+    void beginVisit( const HasReferenceIterator& );
+    void endVisit  ( const HasReferenceIterator& );
+
+    void beginVisit( const AssignReferenceIterator& );
+    void endVisit  ( const AssignReferenceIterator& );
+
+    void beginVisit( const DereferenceIterator& );
+    void endVisit  ( const DereferenceIterator& );
+
 #ifndef ZORBA_NO_XMLSCHEMA
     void beginVisit( const ValidateIterator& );
     void endVisit  ( const ValidateIterator& );

=== modified file 'src/store/naive/hashmap_nodep.h'
--- src/store/naive/hashmap_nodep.h	2012-09-19 21:16:15 +0000
+++ src/store/naive/hashmap_nodep.h	2012-12-06 00:07:26 +0000
@@ -65,6 +65,43 @@
 };
 
 
+/***************************************************************************//**
+  Class to privide the equality and hash functions for the NodePointerHashMap
+  class defined below.
+*******************************************************************************/
+class ItemPointerHashMapCmp
+{
+public:
+  static bool equal(const store::Item* n1, const store::Item* n2)
+  {
+    return n1 == n2;
+  }
+
+  static uint32_t hash(const store::Item* n)
+  {
+    return hashfun::h32((void*)(&n), sizeof(n));
+  }
+};
+
+
+/*******************************************************************************
+  A hash-based map container mapping item pointers to values of type V. 
+  Equality is based on the Item::equals() method.
+*******************************************************************************/
+template <class V>
+class ItemPointerHashMap : public HashMap<const store::Item*,
+                                          V,
+                                          ItemPointerHashMapCmp>
+{
+public:
+  ItemPointerHashMap(ulong size, bool sync) 
+    :
+    HashMap<const store::Item*, V, ItemPointerHashMapCmp>(size, sync)
+  {
+  }
+};
+
+
 }
 }
 

=== modified file 'src/store/naive/json_items.cpp'
--- src/store/naive/json_items.cpp	2012-10-08 12:09:36 +0000
+++ src/store/naive/json_items.cpp	2012-12-06 00:07:26 +0000
@@ -36,6 +36,14 @@
 /******************************************************************************
 
 *******************************************************************************/
+JSONTree::~JSONTree()
+{
+  GET_STORE().unregisterReferenceToDeletedNode(theRoot);
+}
+
+/******************************************************************************
+
+*******************************************************************************/
 store::Item* JSONNull::getType() const
 {
   return GET_STORE().JS_NULL_QNAME;

=== modified file 'src/store/naive/json_items.h'
--- src/store/naive/json_items.h	2012-10-08 12:09:36 +0000
+++ src/store/naive/json_items.h	2012-12-06 00:07:26 +0000
@@ -97,6 +97,8 @@
   JSONTree() : theCollection(NULL), theId(), theRoot(NULL)
   {}
 
+  ~JSONTree();
+
   simplestore::Collection* getCollection() const
   {
     return theCollection;

=== modified file 'src/store/naive/simple_store.cpp'
--- src/store/naive/simple_store.cpp	2012-10-08 12:09:36 +0000
+++ src/store/naive/simple_store.cpp	2012-12-06 00:07:26 +0000
@@ -27,6 +27,7 @@
 #include "node_factory.h"
 #include "pul_primitive_factory.h"
 #include "node_items.h"
+#include "json_items.h"
 
 #include "diagnostics/zorba_exception.h"
 #include "diagnostics/diagnostic.h"
@@ -88,8 +89,8 @@
   {
     if (theNodeToReferencesMap.size() > 0)
     {
-      NodeRefMap::iterator iter = theNodeToReferencesMap.begin();
-      NodeRefMap::iterator end = theNodeToReferencesMap.end();
+      ItemRefMap::iterator iter = theNodeToReferencesMap.begin();
+      ItemRefMap::iterator end = theNodeToReferencesMap.end();
       for (; iter != end; ++iter)
       {
         std::cerr << "Reference: " << (*iter).second
@@ -278,27 +279,28 @@
 ********************************************************************************/
 bool SimpleStore::getNodeReference(store::Item_t& result, const store::Item* node)
 {
-  const XmlNode* xmlNode = static_cast<const XmlNode*>(node);
+  bool lHasReference = hasReference(node);
 
-  if (xmlNode->haveReference())
+  if (lHasReference)
   {
-    NodeRefMap::iterator resIt = theNodeToReferencesMap.find(xmlNode);
+    ItemRefMap::iterator resIt = theNodeToReferencesMap.find(node);
 
     ZORBA_FATAL(resIt != theNodeToReferencesMap.end(),"Node reference cannot be found");
 
     zstring id = (*resIt).second;
     return theItemFactory->createAnyURI(result, id);
   }
-
-  uuid u;
-  uuid::create(&u);
-  std::ostringstream oss;
-  oss << "urn:uuid:" << u;
-  zstring uuidStr = oss.str();
-
-  assignReference(xmlNode, uuidStr);
-
-  return theItemFactory->createAnyURI(result, uuidStr);
+  else
+  {
+    uuid u;
+    uuid::create(&u);
+    std::ostringstream oss;
+    oss << "urn:uuid:" << u;
+    zstring uuidStr = oss.str();
+    assignReference(node, uuidStr);
+
+    return theItemFactory->createAnyURI(result, uuidStr);
+  }
 }
 
 
@@ -310,22 +312,58 @@
 ********************************************************************************/
 bool SimpleStore::hasReference(const store::Item* node)
 {
-  return static_cast<const XmlNode*>(node)->haveReference();
+  using namespace zorba::simplestore::json;
+
+  bool lHasReference = false;
+  if (node->isNode())
+  {
+    const XmlNode* x = static_cast<const XmlNode*>(node);
+    lHasReference = x->haveReference();
+  }
+  else
+  {
+    assert(node->isJSONItem());
+    JSONItem* j = const_cast<JSONItem*>(static_cast<const JSONItem*>(node));
+
+    // only root nodes in a collection can have a reference
+    if (j->getTree() && j->getTree()->getRoot() == j)
+    {
+      ItemRefMap::iterator lIter = theNodeToReferencesMap.find(node);
+
+      lHasReference = (lIter != theNodeToReferencesMap.end());
+    }
+  }
+  return lHasReference;
 }
 
 
 bool SimpleStore::assignReference(const store::Item* node, const zstring& reference)
 {
-  const XmlNode* xmlNode = static_cast<const XmlNode*>(node);
+  using namespace zorba::simplestore::json;
+
+  bool lHasReference = hasReference(node);
+
+  if (lHasReference)
+  {
+    return false;
+  }
+
+  if (node->isNode())
+  {
+    const XmlNode* x = static_cast<const XmlNode*>(node);
+    const_cast<XmlNode*>(x)->setHaveReference();
+  }
+  else
+  {
+    assert(node->isJSONItem());
+    const JSONItem* j = static_cast<const JSONItem*>(node);
+
+    if (!j->getTree() || j != j->getTree()->getRoot())
+      throw ZORBA_EXCEPTION(zerr::ZAPI0080_CANNOT_RETRIEVE_REFERENCE);
+  }
+
   zstring uuidStr = reference;
-
-  if (xmlNode->haveReference())
-  {
-    return false;
-  }
-  const_cast<XmlNode*>(xmlNode)->setHaveReference();
-
-  theNodeToReferencesMap.insert(xmlNode, uuidStr);
+  theNodeToReferencesMap.insert(node, uuidStr);
   theReferencesToNodeMap[uuidStr] = node;
 
   return true;
@@ -367,55 +405,59 @@
   @param node XDM node
   @return whether the node was registered or not.
 ********************************************************************************/
-bool SimpleStore::unregisterReferenceToUnusedNode(XmlNode* node)
+bool SimpleStore::unregisterReferenceToUnusedNode(store::Item* node)
 {
-  if (!node->haveReference())
+  bool lHasReference = hasReference(node);
+
+  if (!lHasReference)
+  {
     return false;
+  }
 
-  NodeRefMap::iterator resIt;
+  ItemRefMap::iterator resIt;
 
   if ((resIt = theNodeToReferencesMap.find(node)) != theNodeToReferencesMap.end())
   {
     zstring value = (*resIt).second;
     theNodeToReferencesMap.erase(resIt);
-    node->resetHaveReference();
+
+    if (node->isNode())
+    {
+      static_cast<XmlNode*>(node)->resetHaveReference();
+    }
 
     theReferencesToNodeMap.erase(value);
 
     return true;
   }
-  else
-  {
-    return false;
-  }
+  assert(false);
 }
 
 /*******************************************************************************
+ * Remove a reference from the cache
   Does nothing in the simple store.
 
   @param node XDM node
   @return whether the node was registered or not.
 ********************************************************************************/
-bool SimpleStore::unregisterReferenceToDeletedNode(XmlNode* node)
+bool SimpleStore::unregisterReferenceToDeletedNode(store::Item* node)
 {
-  // Does nothing, since there is no persistency layer. A deleted node can still
-  // be retrieved with a reference, so its reference may not be removed from the
-  // cache.
-  // Merely returns true if entry found, false otherwise.
+  using namespace zorba::simplestore::json;
   
-  if (!node->haveReference())
-    return false;
-
-  NodeRefMap::iterator resIt;
-
-  if ((resIt = theNodeToReferencesMap.find(node)) != theNodeToReferencesMap.end())
-  {
-    return true;
-  }
-  else
-  {
-    return false;
-  }
+  bool lHasReference = hasReference(node);
+
+  if (!lHasReference)
+  {
+    return false;
+  }
+
+  ItemRefMap::iterator resIt = theNodeToReferencesMap.find(node);
+  zstring id = (*resIt).second;
+
+  theNodeToReferencesMap.erase(node);
+  theReferencesToNodeMap.erase(id);
+
+  return true;
 }
 
 

=== modified file 'src/store/naive/simple_store.h'
--- src/store/naive/simple_store.h	2012-09-19 21:16:15 +0000
+++ src/store/naive/simple_store.h	2012-12-06 00:07:26 +0000
@@ -21,6 +21,7 @@
 #include "node_factory.h"
 #include "pul_primitive_factory.h"
 #include "tree_id_generator.h"
+#include "zorbautils/hashmap_itemp.h"
 
 namespace zorba {
 namespace simplestore {
@@ -47,14 +48,14 @@
   friend class zorba::StoreManager;
 
   typedef std::map<const zstring, const store::Item*> RefNodeMap;
-  typedef NodePointerHashMap<zstring> NodeRefMap;
+  typedef zorba::simplestore::ItemPointerHashMap<zorba::zstring> ItemRefMap;
 
 private:
   ulong                         theCollectionCounter;
   SYNC_CODE(Mutex               theCollectionCounterMutex;)
 
   RefNodeMap                    theReferencesToNodeMap;
-  NodeRefMap                    theNodeToReferencesMap;
+  ItemRefMap                    theNodeToReferencesMap;
 
 public:
   ulong createCollectionId();
@@ -98,9 +99,9 @@
 
   void destroyTreeIdGeneratorFactory(TreeIdGeneratorFactory* g) const;
 
-  bool unregisterReferenceToUnusedNode(XmlNode* node);
+  bool unregisterReferenceToUnusedNode(store::Item* node);
 
-  bool unregisterReferenceToDeletedNode(XmlNode* node);
+  bool unregisterReferenceToDeletedNode(store::Item* node);
 
   //
   // Store api methods

=== modified file 'src/store/naive/store.h'
--- src/store/naive/store.h	2012-09-19 21:16:15 +0000
+++ src/store/naive/store.h	2012-12-06 00:07:26 +0000
@@ -420,10 +420,10 @@
 
   // Unregisters a reference to an unused node (upon its destruction in
   // the memory).
-  virtual bool unregisterReferenceToUnusedNode(XmlNode* node) = 0;
+  virtual bool unregisterReferenceToUnusedNode(store::Item* node) = 0;
 
   // Unregisters a reference to a node that was deleted (by XQUF).
-  virtual bool unregisterReferenceToDeletedNode(XmlNode* node) = 0;
+  virtual bool unregisterReferenceToDeletedNode(store::Item* node) = 0;
 
 /*----------------------- Temp Sequence Management ---------------------------*/
 public:

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/json-reference_3.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/json-reference_3.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/json-reference_3.xml.res	2012-12-06 00:07:26 +0000
@@ -0,0 +1,1 @@
+{ "foo" : "bar" }

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/json-reference_4.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/json-reference_4.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/json-reference_4.xml.res	2012-12-06 00:07:26 +0000
@@ -0,0 +1,1 @@
+false true

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/json-reference_5.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/json-reference_5.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/json-reference_5.xml.res	2012-12-06 00:07:26 +0000
@@ -0,0 +1,1 @@
+[ 1 ]

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_1.spec'
--- test/rbkt/Queries/zorba/reference/json-reference_1.spec	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_1.spec	2012-12-06 00:07:26 +0000
@@ -0,0 +1,1 @@
+Error: http://www.zorba-xquery.com/errors:ZAPI0080

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_1.xq'
--- test/rbkt/Queries/zorba/reference/json-reference_1.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_1.xq	2012-12-06 00:07:26 +0000
@@ -0,0 +1,14 @@
+import module namespace id = "http://www.zorba-xquery.com/modules/reference";;
+
+variable $node := { "foo" : "bar" };
+
+variable $before-currentid:=id:reference($node);
+variable $got:=id:reference($node);
+variable $after:=id:reference($node);
+
+<result>
+<before>{fn:string-length($before-currentid)}</before>
+<after>{fn:string-length($got)}</after>
+<stable>{$got eq $after}</stable>
+</result>
+

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_2.spec'
--- test/rbkt/Queries/zorba/reference/json-reference_2.spec	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_2.spec	2012-12-06 00:07:26 +0000
@@ -0,0 +1,1 @@
+Error: http://www.zorba-xquery.com/errors:ZAPI0028

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_2.xq'
--- test/rbkt/Queries/zorba/reference/json-reference_2.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_2.xq	2012-12-06 00:07:26 +0000
@@ -0,0 +1,3 @@
+import module namespace id = "http://www.zorba-xquery.com/modules/reference";;
+
+id:dereference(xs:anyURI("this:is-not-a-reference"))

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_3.xq'
--- test/rbkt/Queries/zorba/reference/json-reference_3.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_3.xq	2012-12-06 00:07:26 +0000
@@ -0,0 +1,15 @@
+import module namespace ddl = "http://www.zorba-xquery.com/modules/store/dynamic/collections/ddl";;
+import module namespace dml = "http://www.zorba-xquery.com/modules/store/dynamic/collections/dml";;
+
+import module namespace ref = "http://www.zorba-xquery.com/modules/reference";;
+
+declare namespace ann = "http://www.zorba-xquery.com/annotations";;
+
+declare %ann:sequential function local:test()
+{
+  ddl:create(xs:QName("local:coll"));
+  dml:insert-last(xs:QName("local:coll"), { "foo" : "bar" });
+  ref:dereference(ref:reference(dml:collection(xs:QName("local:coll"))))
+};
+
+local:test()

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_4.xq'
--- test/rbkt/Queries/zorba/reference/json-reference_4.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_4.xq	2012-12-06 00:07:26 +0000
@@ -0,0 +1,21 @@
+import module namespace ddl = "http://www.zorba-xquery.com/modules/store/dynamic/collections/ddl";;
+import module namespace dml = "http://www.zorba-xquery.com/modules/store/dynamic/collections/dml";;
+import module namespace ns = "http://example.org/datamodule/"; at "collections.xqdata";
+
+import module namespace ref = "http://www.zorba-xquery.com/modules/reference";;
+
+declare namespace ann = "http://www.zorba-xquery.com/annotations";;
+
+declare %ann:sequential function local:test()
+{
+  ddl:create(xs:QName("ns:coll"));
+  dml:insert-last(xs:QName("ns:coll"), { "foo" : "bar" });
+  variable $node := dml:collection(xs:QName("ns:coll"));
+  variable $has1 := ref:has-reference($node);
+  variable $ref  := ref:reference($node);
+  variable $has2 := ref:has-reference($node);
+  ($has1, $has2)
+};
+
+local:test()
+

=== added file 'test/rbkt/Queries/zorba/reference/json-reference_5.xq'
--- test/rbkt/Queries/zorba/reference/json-reference_5.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/json-reference_5.xq	2012-12-06 00:07:26 +0000
@@ -0,0 +1,16 @@
+import module namespace ddl = "http://www.zorba-xquery.com/modules/store/dynamic/collections/ddl";;
+import module namespace dml = "http://www.zorba-xquery.com/modules/store/dynamic/collections/dml";;
+
+import module namespace ref = "http://www.zorba-xquery.com/modules/reference";;
+
+declare namespace ann = "http://www.zorba-xquery.com/annotations";;
+
+declare %ann:sequential function local:test()
+{
+  ddl:create(xs:QName("local:coll"));
+  dml:insert-last(xs:QName("local:coll"), [ 1 ]);
+  ref:dereference(ref:reference(dml:collection(xs:QName("local:coll"))))
+};
+
+local:test()
+

-- 
Mailing list: https://launchpad.net/~zorba-coders
Post to     : zorba-coders@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zorba-coders
More help   : https://help.launchpad.net/ListHelp

Reply via email to