← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/treeidapi into lp:zorba

 

Ghislain Fourny has proposed merging lp:~zorba-coders/zorba/treeidapi into lp:zorba.

Requested reviews:
  Markos Zaharioudakis (markos-za)
  Matthias Brantner (matthias-brantner)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/treeidapi/+merge/93591

Introducing tree ID API.
-- 
https://code.launchpad.net/~zorba-coders/zorba/treeidapi/+merge/93591
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/store/naive/CMakeLists.txt'
--- src/store/naive/CMakeLists.txt	2011-06-22 17:30:15 +0000
+++ src/store/naive/CMakeLists.txt	2012-02-17 15:26:34 +0000
@@ -47,6 +47,7 @@
     inmemorystore.cpp
     inmemorystorec.cpp
     dataguide.cpp
+    tree_id.cpp
 )
 
 IF (NOT ZORBA_NO_FULL_TEXT)

=== modified file 'src/store/naive/atomic_items.cpp'
--- src/store/naive/atomic_items.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/atomic_items.cpp	2012-02-17 15:26:34 +0000
@@ -40,6 +40,7 @@
 #include "node_items.h"
 #include "atomic_items.h"
 #include "ordpath.h"
+#include "tree_id.h"
 
 #include "util/ascii_util.h"
 #include "util/string_util.h"
@@ -1043,12 +1044,12 @@
 StructuralAnyUriItem::StructuralAnyUriItem(
     zstring& encoded,
     ulong collectionId,
-    ulong treeId, 
+    const TreeId_t& treeId, 
     store::StoreConsts::NodeKind nodeKind,
     const OrdPath& ordPath)
   :
   theCollectionId(collectionId),
-  theTreeId(treeId),
+  theTreeId(treeId->copy()),
   theNodeKind(nodeKind),
   theOrdPath(ordPath)
 {
@@ -1091,8 +1092,10 @@
   //
   // Decode tree id
   //
-  theTreeId = strtoul(start, &next, 10);
-
+  ulong ulid = strtoul(start, &next, 10);
+  ostringstream oss;
+  oss << std::dec << ulid;
+  theTreeId = GET_DEFAULT_TREE_ID_GENERATOR().fromString(oss.str());
   if (errno != 0 || start == next)
     throw ZORBA_EXCEPTION(zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS(theValue));
 
@@ -1144,7 +1147,7 @@
 
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      theOrdPath.getRelativePosition(other->theOrdPath) == OrdPath::ANCESTOR);
   }
 }
@@ -1169,7 +1172,7 @@
 
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      other->theNodeKind != store::StoreConsts::attributeNode &&
      theNodeKind != store::StoreConsts::attributeNode &&
      theOrdPath.getRelativePosition2(other->theOrdPath) == OrdPath::FOLLOWING_SIBLING);
@@ -1196,7 +1199,7 @@
 
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      theOrdPath.getRelativePosition(other->theOrdPath) == OrdPath::FOLLOWING);
   }
 }
@@ -1220,7 +1223,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      other->theNodeKind != store::StoreConsts::attributeNode &&
      theOrdPath.getRelativePosition(other->theOrdPath) == OrdPath::DESCENDANT);
   }
@@ -1245,7 +1248,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      theOrdPath.getRelativePosition(other->theOrdPath) == OrdPath::DESCENDANT);
   }
 }
@@ -1269,7 +1272,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      other->theNodeKind != store::StoreConsts::attributeNode &&
      theNodeKind != store::StoreConsts::attributeNode &&
      theOrdPath.getRelativePosition2(other->theOrdPath) == OrdPath::PRECEDING_SIBLING);
@@ -1295,7 +1298,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      theOrdPath.getRelativePosition(other->theOrdPath) == OrdPath::PRECEDING);
   }
 }
@@ -1319,7 +1322,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      other->theNodeKind != store::StoreConsts::attributeNode &&
      theOrdPath.getRelativePosition2(other->theOrdPath) == OrdPath::CHILD);
   }
@@ -1344,7 +1347,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      other->theNodeKind == store::StoreConsts::attributeNode &&
      theOrdPath.getRelativePosition2(other->theOrdPath) == OrdPath::CHILD);
   }
@@ -1369,7 +1372,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return 
     (other->theCollectionId == theCollectionId &&
-     other->theTreeId == theTreeId &&
+     GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
      theOrdPath.getRelativePosition2(other->theOrdPath) == OrdPath::PARENT);
   }
 }
@@ -1391,8 +1394,11 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return
     (theCollectionId > other->theCollectionId ||
-    (theCollectionId == other->theCollectionId && theTreeId > other->theTreeId) ||
-    (theCollectionId == other->theCollectionId && other->theTreeId == theTreeId && theOrdPath > other->theOrdPath));
+    (theCollectionId == other->theCollectionId &&
+        GET_DEFAULT_TREE_ID_GENERATOR().isBefore(other->theTreeId, theTreeId)) ||
+    (theCollectionId == other->theCollectionId &&
+        GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
+        theOrdPath > other->theOrdPath));
   }
 }
 
@@ -1413,8 +1419,11 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return
     (theCollectionId < other->theCollectionId ||
-    (theCollectionId == other->theCollectionId && theTreeId < other->theTreeId) ||
-    (theCollectionId == other->theCollectionId && other->theTreeId == theTreeId && theOrdPath < other->theOrdPath));
+    (theCollectionId == other->theCollectionId &&
+        GET_DEFAULT_TREE_ID_GENERATOR().isBefore(theTreeId, other->theTreeId)) ||
+    (theCollectionId == other->theCollectionId &&
+        GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
+        theOrdPath < other->theOrdPath));
   }
 }
 
@@ -1479,7 +1488,7 @@
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
 
     if (other->theCollectionId == theCollectionId &&
-        other->theTreeId == theTreeId &&
+        GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId) &&
         other->theNodeKind != store::StoreConsts::attributeNode &&
         theNodeKind != store::StoreConsts::attributeNode)
     {
@@ -1512,7 +1521,7 @@
   {
     StructuralAnyUriItem* other = static_cast<StructuralAnyUriItem*>(aOther.getp());
     return (theCollectionId == other->theCollectionId &&
-            theTreeId == other->theTreeId);
+            GET_DEFAULT_TREE_ID_GENERATOR().equals(other->theTreeId, theTreeId));
   }
 }
 

=== modified file 'src/store/naive/atomic_items.h'
--- src/store/naive/atomic_items.h	2012-01-26 19:56:14 +0000
+++ src/store/naive/atomic_items.h	2012-02-17 15:26:34 +0000
@@ -31,6 +31,7 @@
 #include "store/api/xs_type_codes.h"
 #include "store/naive/store_defs.h"
 #include "store/naive/shared_types.h"
+#include "tree_id.h"
 
 #ifndef ZORBA_NO_FULL_TEXT
 #include "store/naive/naive_ft_token_iterator.h"
@@ -615,7 +616,7 @@
 
 protected:
   ulong                        theCollectionId;
-  ulong                        theTreeId;
+  TreeId_t                     theTreeId;
   store::StoreConsts::NodeKind theNodeKind;
   OrdPath                      theOrdPath;
 
@@ -630,7 +631,7 @@
   StructuralAnyUriItem(
       zstring& value,
       ulong collectionId,
-      ulong treeId,
+      const TreeId_t& treeId,
       store::StoreConsts::NodeKind nodeKind,
       const OrdPath& ordPath);
 

=== modified file 'src/store/naive/loader_dtd.cpp'
--- src/store/naive/loader_dtd.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/loader_dtd.cpp	2012-02-17 15:26:34 +0000
@@ -124,7 +124,7 @@
   if (docUri.empty())
   {
     std::ostringstream uristream;
-    uristream << "zorba://internalDocumentURI-" << theTree->getId();
+    uristream << "zorba://internalDocumentURI-" << theTree->getId()->toString();
     theDocUri = uristream.str();
   }
   else
@@ -566,7 +566,7 @@
   if (docUri.empty())
   {
     std::ostringstream uristream;
-    uristream << "zorba://internalDocumentURI-" << theTree->getId();
+    uristream << "zorba://internalDocumentURI-" << theTree->getId()->toString();
 
     theDocUri = uristream.str();
   }

=== modified file 'src/store/naive/loader_fast.cpp'
--- src/store/naive/loader_fast.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/loader_fast.cpp	2012-02-17 15:26:34 +0000
@@ -260,7 +260,7 @@
   if (docUri.empty())
   {
     std::ostringstream uristream;
-    uristream << "zorba://internalDocumentURI-" << theTree->getId();
+    uristream << "zorba://internalDocumentURI-" << theTree->getId()->toString();
 
     theDocUri = uristream.str();
   }

=== modified file 'src/store/naive/node_items.cpp'
--- src/store/naive/node_items.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/node_items.cpp	2012-02-17 15:26:34 +0000
@@ -81,7 +81,7 @@
 }
 
 
-XmlTree::XmlTree(XmlNode* root, ulong id)
+XmlTree::XmlTree(XmlNode* root, TreeId_t id)
   :
   theRefCount(0),
   theId(id),
@@ -1548,7 +1548,7 @@
   theDocUri(docUri)
 {
   NODE_TRACE1("{\nConstructing doc node " << this << " tree = "
-              << getTree()->getId() << ":" << getTree()
+              << getTree()->getId()->toString() << ":" << getTree()
               << " doc uri = " << docUri);
 }
 
@@ -1900,7 +1900,7 @@
 
   NODE_TRACE1("Constructed element node " << this << " parent = "
               << std::hex << (parent ? (ulong)parent : 0) << " pos = " << pos
-              << " tree = " << getTree()->getId() << ":" << getTree()
+              << " tree = " << getTree()->getId()->toString() << ":" << getTree()
               << " ordpath = " << theOrdPath.show()
               << " name = " << theName->getStringValue()
               << " type = " << getType()->getStringValue());
@@ -3332,7 +3332,7 @@
 
   NODE_TRACE1("Constructed attribute node " << this << " parent = "
               << std::hex << (parent ? (ulong)parent : 0) << " pos = " << pos
-              << " tree = " << getTree()->getId() << ":" << getTree()
+              << " tree = " << getTree()->getId()->toString() << ":" << getTree()
               << " ordpath = " << theOrdPath.show()
               << " name = " << theName->getStringValue()
               << " value = " << getStringValue());
@@ -3714,7 +3714,7 @@
 #else
   NODE_TRACE1("Constructed text node " << this << " parent = "
               << std::hex << (parent ? (ulong)parent : 0) << " pos = " << pos
-              << " tree = " << getTree()->getId() << ":" << getTree()
+              << " tree = " << getTree()->getId()->toString() << ":" << getTree()
               << " content = " << getText());
 #endif
 }
@@ -4396,7 +4396,7 @@
 
   NODE_TRACE1("Constructed pi node " << this << " parent = "
               << std::hex << (parent ? (ulong)parent : 0) << " pos = " << pos
-              << " tree = " << getTree()->getId() << ":" << getTree()
+              << " tree = " << getTree()->getId()->toString() << ":" << getTree()
               << " ordpath = " << theOrdPath.show() << " target = " << theTarget);
 }
 
@@ -4521,7 +4521,7 @@
 
   NODE_TRACE1("Constructed comment node " << this << " parent = "
               << std::hex << (parent ? (ulong)parent : 0) << " pos = " << pos
-              << " tree = " << getTree()->getId() << ":" << getTree()
+              << " tree = " << getTree()->getId()->toString() << ":" << getTree()
               << " ordpath = " << theOrdPath.show() << " content = "
               << theContent);
 }

=== modified file 'src/store/naive/node_items.h'
--- src/store/naive/node_items.h	2012-01-26 19:56:14 +0000
+++ src/store/naive/node_items.h	2012-02-17 15:26:34 +0000
@@ -29,6 +29,8 @@
 #include "store/naive/item_vector.h"
 #include "store/naive/ordpath.h"
 #include "store/naive/nsbindings.h" // TODO remove by introducing explicit destructors
+#include "tree_id.h"
+#include "simple_store.h"
 
 // Note: whether the EMBEDED_TYPE is defined or not is done in store_defs.h
 #ifndef EMBEDED_TYPE
@@ -177,7 +179,7 @@
   mutable long              theRefCount;
   SYNC_CODE(mutable RCLock  theRCLock;)
 
-  ulong                     theId;
+  TreeId_t                  theId;
   ulong                     thePos;
 
   SimpleCollection        * theCollection;
@@ -200,7 +202,7 @@
 #endif
 
 protected:
-  XmlTree(XmlNode* root, ulong id);
+  XmlTree(XmlNode* root, TreeId_t id);
 
 public:
   XmlTree();
@@ -215,9 +217,9 @@
 
   SYNC_CODE(RCLock* getRCLock() const { return &theRCLock; })
 
-  void setId(ulong id) { theId = id; }
+  void setId(TreeId_t id) { theId = id; }
 
-  ulong getId() const { return theId; }
+  const TreeId_t& getId() const { return theId; }
 
   ulong getCollectionId() const;
 
@@ -512,7 +514,7 @@
 
   XmlTree* getTree() const { return (XmlTree*)theUnion.treeRCPtr; }
 
-  ulong getTreeId() const { return getTree()->getId(); }
+  const TreeId_t& getTreeId() const { return getTree()->getId(); }
 
   XmlNode* getRoot() const { return getTree()->getRoot(); }
 
@@ -1618,13 +1620,13 @@
   {
     if (col1 == 0)
     {
-      ulong tree1 = this->getTreeId();
-      ulong tree2 = other->getTreeId();
+      const TreeId_t& tree1 = this->getTreeId();
+      const TreeId_t& tree2 = other->getTreeId();
 
-      if (tree1 < tree2)
+      if (GET_DEFAULT_TREE_ID_GENERATOR().isBefore(tree1, tree2))
         return -1;
 
-      if (tree1 == tree2)
+      if (GET_DEFAULT_TREE_ID_GENERATOR().equals(tree1, tree2))
         return compareInSameTree(this, other);
     }
     else

=== modified file 'src/store/naive/simple_collection.cpp'
--- src/store/naive/simple_collection.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/simple_collection.cpp	2012-02-17 15:26:34 +0000
@@ -41,11 +41,11 @@
   : 
   theName(aName),
   theIsDynamic(aDynamicCollection),
-  theTreeCounter(1),
   theAnnotations(aAnnotations),
   theNodeType(aNodeType)
 {
   theId = GET_STORE().createCollectionId();
+  theTreeIdGenerator = GET_STORE().getTreeIdGeneratorFactory().createTreeGenerator();
 }
 
 /*******************************************************************************
@@ -54,9 +54,9 @@
 SimpleCollection::SimpleCollection()
   : 
   theIsDynamic(false),
-  theTreeCounter(1),
   theNodeType(NULL)
 {
+  theTreeIdGenerator = GET_STORE().getTreeIdGeneratorFactory().createTreeGenerator();
 }
 
 /*******************************************************************************
@@ -379,7 +379,9 @@
   std::size_t lPosition = to_xs_unsignedInt(position);
 
   if (lPosition < theXmlTrees.size() &&
-      BASE_NODE(theXmlTrees[lPosition])->getTreeId() == n->getTreeId())
+      GET_DEFAULT_TREE_ID_GENERATOR().equals(
+          BASE_NODE(theXmlTrees[lPosition])->getTreeId(),
+          n->getTreeId()))
   {
     return true;
   }
@@ -491,6 +493,13 @@
   activeICNames->close();
 }
 
+/*******************************************************************************
+
+********************************************************************************/
+TreeId_t SimpleCollection::createTreeId()
+{
+  return theTreeIdGenerator->create();
+}
 
 /*******************************************************************************
 

=== modified file 'src/store/naive/simple_collection.h'
--- src/store/naive/simple_collection.h	2012-01-15 09:18:22 +0000
+++ src/store/naive/simple_collection.h	2012-02-17 15:26:34 +0000
@@ -20,6 +20,7 @@
 
 #include "store/api/iterator.h"
 #include "store/api/collection.h"
+#include "tree_id.h"
 
 #include "zorbautils/latch.h"
 #include "zorbautils/checked_vector.h"
@@ -68,7 +69,7 @@
   checked_vector<store::Item_t>   theXmlTrees;
   bool                            theIsDynamic;
 
-  ulong                           theTreeCounter;
+  TreeIdGenerator*                theTreeIdGenerator;
 
   const std::vector<store::Annotation_t> theAnnotations;
   store::Item_t                   theNodeType;
@@ -100,7 +101,7 @@
   void getAnnotations(std::vector<store::Annotation_t>& annotations) const;
 
   // virtual to allow extension by subclasses
-  virtual ulong createTreeId() { return theTreeCounter++; }
+  virtual TreeId_t createTreeId();
 
   store::Iterator_t getIterator();
 

=== modified file 'src/store/naive/simple_item_factory.cpp'
--- src/store/naive/simple_item_factory.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/simple_item_factory.cpp	2012-02-17 15:26:34 +0000
@@ -120,7 +120,7 @@
 bool BasicItemFactory::createStructuralAnyURI(
     store::Item_t& result,
     ulong collectionId,
-    ulong treeId,
+    const TreeId_t& treeId,
     store::StoreConsts::NodeKind nodeKind,
     const OrdPath& ordPath)
 {
@@ -128,7 +128,7 @@
   std::ostringstream stream;
   stream   << "zorba:"
            << collectionId << "."
-           << treeId << "."
+           << treeId->toString() << "."
            << static_cast<int>(nodeKind) << "."
            << ordPath.serialize();
   zstring uri = stream.str();

=== modified file 'src/store/naive/simple_item_factory.h'
--- src/store/naive/simple_item_factory.h	2011-12-21 14:40:33 +0000
+++ src/store/naive/simple_item_factory.h	2012-02-17 15:26:34 +0000
@@ -91,7 +91,7 @@
   bool createStructuralAnyURI(
       store::Item_t& result,
       ulong collectionId,
-      ulong treeId,
+      const TreeId_t& treeId,
       store::StoreConsts::NodeKind nodeKind,
       const OrdPath& ordPath);
 

=== modified file 'src/store/naive/simple_store.cpp'
--- src/store/naive/simple_store.cpp	2012-02-15 10:25:02 +0000
+++ src/store/naive/simple_store.cpp	2012-02-17 15:26:34 +0000
@@ -56,6 +56,7 @@
 #include "name_iterator.h"
 #include "document_name_iterator.h"
 #include "pul_primitive_factory.h"
+#include "tree_id.h"
 
 #include "util/cxx_util.h"
 #include "util/uuid/uuid.h"
@@ -102,6 +103,7 @@
   theIteratorFactory(NULL),
   theNodeFactory(NULL),
   thePULFactory(NULL),
+  theTreeIdGeneratorFactory(NULL),
   theDocuments(CollectionSet::DEFAULT_COLLECTION_MAP_SIZE, true),
   theCollections(0),
   theIndices(0, NULL, CollectionSet::DEFAULT_COLLECTION_MAP_SIZE, true),
@@ -169,6 +171,8 @@
     theNodeFactory = createNodeFactory();
 
     thePULFactory = createPULPrimitiveFactory();
+    
+    theTreeIdGeneratorFactory = createTreeIdGeneratorFactory();
 
     theTraceLevel = store::Properties::instance()->storeTraceLevel();
 
@@ -400,6 +404,25 @@
 /*******************************************************************************
 
 *******************************************************************************/
+TreeIdGeneratorFactory*
+SimpleStore::createTreeIdGeneratorFactory() const
+{
+  return new ZorbaTreeIdGeneratorFactory();
+}
+
+
+/*******************************************************************************
+
+*******************************************************************************/
+void
+SimpleStore::destroyTreeIdGeneratorFactory(TreeIdGeneratorFactory* g) const
+{
+  delete g;
+}
+
+/*******************************************************************************
+
+*******************************************************************************/
 CollectionSet* SimpleStore::createCollectionSet() const
 {
   return new CollectionSet();
@@ -473,10 +496,10 @@
 /*******************************************************************************
   create a tree id for a new tree that does not belong to any collection.
 ********************************************************************************/
-ulong SimpleStore::createTreeId()
+TreeId_t SimpleStore::createTreeId()
 {
   SYNC_CODE(AutoMutex lock(&theTreeCounterMutex);)
-  return theTreeCounter++;
+  return getTreeIdGeneratorFactory().getDefaultTreeIdGenerator().create();
 }
 
 

=== modified file 'src/store/naive/simple_store.h'
--- src/store/naive/simple_store.h	2012-01-10 10:52:15 +0000
+++ src/store/naive/simple_store.h	2012-02-17 15:26:34 +0000
@@ -19,6 +19,7 @@
 #include "store/naive/shared_types.h"
 #include "store/naive/store_defs.h"
 #include "store/naive/hashmap_nodep.h"
+#include "tree_id.h"
 
 #if (defined (WIN32) || defined (WINCE))
 #include "store/naive/node_items.h"
@@ -48,6 +49,8 @@
   class ValueIndexSpecification;
 }
 
+class TreeIdGeneratorFactory;
+
 namespace simplestore
 {
 
@@ -125,6 +128,11 @@
   theNodeToReferencesMap:
   -----------------------
   A hashmap that maps nodes into their references
+  
+  theTreeIdGeneratorFactory:
+  --------------------------
+  An factory of ID generators (each collection can have its own in
+  addition to the default one).
 
 ********************************************************************************/
 class SimpleStore : public store::Store
@@ -173,7 +181,8 @@
   store::IteratorFactory      * theIteratorFactory;
   NodeFactory                 * theNodeFactory;
   PULPrimitiveFactory         * thePULFactory;
-
+  TreeIdGeneratorFactory      * theTreeIdGeneratorFactory;
+  
   DocumentSet                   theDocuments;
   CollectionSet*                theCollections;
   IndexSet                      theIndices;
@@ -203,6 +212,9 @@
 
   PULPrimitiveFactory& getPULFactory() const { return *thePULFactory; }
 
+  TreeIdGeneratorFactory& getTreeIdGeneratorFactory() const
+      { return *theTreeIdGeneratorFactory; }
+
   StringPool& getNamespacePool() const { return *theNamespacePool; }
 
   QNamePool& getQNamePool() const { return *theQNamePool; }
@@ -217,7 +229,7 @@
 
   ulong createCollectionId();
 
-  ulong createTreeId();
+  TreeId_t createTreeId();
 
   store::Collection_t createCollection(
       const store::Item_t& aName,
@@ -414,6 +426,10 @@
   virtual PULPrimitiveFactory* createPULPrimitiveFactory() const;
 
   virtual void destroyPULPrimitiveFactory(PULPrimitiveFactory*) const;
+  
+  virtual TreeIdGeneratorFactory* createTreeIdGeneratorFactory() const;
+
+  virtual void destroyTreeIdGeneratorFactory(TreeIdGeneratorFactory*) const;
 
   virtual CollectionSet* createCollectionSet() const;
 

=== modified file 'src/store/naive/store_defs.h'
--- src/store/naive/store_defs.h	2012-01-10 13:54:05 +0000
+++ src/store/naive/store_defs.h	2012-02-17 15:26:34 +0000
@@ -38,6 +38,9 @@
 
 #define GET_PUL_FACTORY() \
   (GET_STORE().getPULFactory())
+  
+#define GET_DEFAULT_TREE_ID_GENERATOR() \
+  (GET_STORE().getTreeIdGeneratorFactory().getDefaultTreeIdGenerator())
 
 
 #define BASE_NODE(item) (reinterpret_cast<XmlNode*>((item).getp()))

=== added file 'src/store/naive/tree_id.cpp'
--- src/store/naive/tree_id.cpp	1970-01-01 00:00:00 +0000
+++ src/store/naive/tree_id.cpp	2012-02-17 15:26:34 +0000
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2006-2008 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 "tree_id.h"
+#include <iostream>
+#include <sstream>
+
+namespace zorba {
+  
+ZorbaTreeId::ZorbaTreeId(ulong id)
+: theId(id)
+{};
+
+zstring ZorbaTreeId::toString()
+{
+  std::ostringstream oss;
+  oss << std::dec << theId;
+  return oss.str();
+}
+
+TreeId* ZorbaTreeId::copy()
+{
+  return new ZorbaTreeId(theId);
+}
+
+TreeId_t ZorbaTreeIdGenerator::create() {
+  TreeId_t lStoreRes(new ZorbaTreeId(theNextId));
+  ++theNextId;
+  return lStoreRes;
+}
+
+bool ZorbaTreeIdGenerator::equals(const TreeId_t& anId,
+                                  const TreeId_t& anotherId) const
+{
+  const ZorbaTreeId* lId1 = dynamic_cast<ZorbaTreeId*>(anId.get());
+  const ZorbaTreeId* lId2 = dynamic_cast<ZorbaTreeId*>(anotherId.get());
+  return lId1->theId == lId2->theId;
+}
+
+bool ZorbaTreeIdGenerator::isBefore(const TreeId_t& anId,
+                                    const TreeId_t& anotherId) const
+{
+  const ZorbaTreeId* lId1 = dynamic_cast<ZorbaTreeId*>(anId.get());
+  const ZorbaTreeId* lId2 = dynamic_cast<ZorbaTreeId*>(anotherId.get());
+  return lId1->theId < lId2->theId;
+}
+
+TreeId_t ZorbaTreeIdGenerator::fromString(const zstring& s) const
+{
+  std::istringstream iss(s.str());
+  ulong lId;
+  iss >> std::dec >> lId;
+  TreeId_t lRes(new ZorbaTreeId(lId));
+  return lRes;
+}
+
+TreeIdGenerator* ZorbaTreeIdGeneratorFactory::createTreeGenerator()
+{
+  return new ZorbaTreeIdGenerator();
+}
+
+TreeIdGenerator& ZorbaTreeIdGeneratorFactory::getDefaultTreeIdGenerator()
+{
+  static ZorbaTreeIdGenerator lSingleton;
+  return lSingleton;
+}
+
+}
\ No newline at end of file

=== added file 'src/store/naive/tree_id.h'
--- src/store/naive/tree_id.h	1970-01-01 00:00:00 +0000
+++ src/store/naive/tree_id.h	2012-02-17 15:26:34 +0000
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2006-2008 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.
+ */
+#ifndef ZORBA_TREE_ID_H
+#define ZORBA_TREE_ID_H
+
+#include "store/naive/shared_types.h"
+#include "zorbautils/hashmap_itemh.h"
+
+namespace zorba {
+
+/*
+This class is an abstract class for tree IDs.
+You can convert an ID to a string, or copy it to another instance.
+*/
+
+class TreeId {
+public:
+  virtual ~TreeId() {}
+public:
+  virtual zstring toString() = 0;
+  // Caller gets pointer ownership.
+  virtual TreeId* copy() = 0;
+};
+
+/*
+The type alias used everywhere in the program.
+Only assign IDs with = if lvalue is taking over ownership.
+Otherwise, copy.
+*/
+typedef std::auto_ptr<TreeId> TreeId_t;
+
+/*
+This class is an abstract class for tree ID generation. It provides
+a creation method, two comparison methods (= and <) as well as a way
+to parse a string back to an ID.
+*/
+class TreeIdGenerator {
+public:
+  virtual ~TreeIdGenerator() {}
+
+  virtual TreeId_t create() = 0;
+  virtual bool equals(const TreeId_t& id1, const TreeId_t& id2) const = 0;
+  virtual bool isBefore(const TreeId_t& id1, const TreeId_t& id2) const = 0;
+  virtual TreeId_t fromString(const zstring&) const = 0;
+};
+
+/*
+This class allows generation of independent tree ID generators (each of
+them might have its own counter).
+*/
+class TreeIdGeneratorFactory {
+public:
+  virtual ~TreeIdGeneratorFactory() {}
+  
+  virtual TreeIdGenerator* createTreeGenerator() = 0;
+  virtual TreeIdGenerator& getDefaultTreeIdGenerator() = 0;
+};
+
+/*
+Zorba's implementation of tree IDs, using an unsigned long.
+*/
+class ZorbaTreeId : public TreeId {
+friend class ZorbaTreeIdGenerator;
+private:
+  ulong theId;
+  ZorbaTreeId(ulong idKey);
+public:
+  zstring toString();  
+  virtual TreeId* copy();
+};
+
+/*
+Zorba's implementation of the tree ID generator.
+*/
+class ZorbaTreeIdGenerator : public TreeIdGenerator {
+private:
+  ulong theNextId;
+public:
+  ZorbaTreeIdGenerator() : theNextId(1) {}
+  virtual TreeId_t create();
+  virtual bool equals(const TreeId_t& id1, const TreeId_t& id2) const;
+  virtual bool isBefore(const TreeId_t& id1, const TreeId_t& id2) const;
+  virtual TreeId_t fromString(const zstring&) const;
+};
+
+/*
+Zorba's implementation of the tree ID generator factory.
+*/
+class ZorbaTreeIdGeneratorFactory : public TreeIdGeneratorFactory {
+public:
+  virtual TreeIdGenerator* createTreeGenerator();
+  virtual TreeIdGenerator& getDefaultTreeIdGenerator();
+};
+
+}
+
+#endif /* ZORBA_TREE_ID_H */


Follow ups