← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~matthias-brantner/zorba/indexmgtm into lp:zorba

 

Matthias Brantner has proposed merging lp:~matthias-brantner/zorba/indexmgtm into lp:zorba.

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

For more details, see:
https://code.launchpad.net/~matthias-brantner/zorba/indexmgtm/+merge/85207

Added index management functions to the C++ api's StaticCollectionManager
-- 
https://code.launchpad.net/~matthias-brantner/zorba/indexmgtm/+merge/85207
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'ChangeLog'
--- ChangeLog	2011-12-01 16:19:52 +0000
+++ ChangeLog	2011-12-10 00:15:30 +0000
@@ -1,5 +1,8 @@
 Zorba - The XQuery Processor
 
+version 2.2
+  * Added index management function to the C++ api's StaticCollectionManager.
+
 version 2.1
 
 New Features:

=== modified file 'include/zorba/static_collection_manager.h'
--- include/zorba/static_collection_manager.h	2011-09-12 22:42:28 +0000
+++ include/zorba/static_collection_manager.h	2011-12-10 00:15:30 +0000
@@ -23,14 +23,14 @@
 namespace zorba {
 
   /** \brief Using the StaticCollectionManager one can retrieve information
-   *   about statically declared collections as well as manage them.
+   *   about statically declared collections and indexes as well as manage them.
    *
    * The StaticCollectionManager can be retrieved from (1) a compiled XQuery
    * or (2) a StaticContext object. In both cases, this class provides access
-   * to information for the collections that are declared in (1) all the
+   * to information for the collections and indexes that are declared in (1) all the
    * modules (transitively) imported by the main query or (2) the module
    * that resulted in the compilation of the StaticContext, respectively.
-   * Moreover, this class allows to create or delete such collections.
+   * Moreover, this class allows to create or delete such collections and indexes.
    *
    */
   class ZORBA_DLL_PUBLIC StaticCollectionManager : public CollectionManager
@@ -56,6 +56,69 @@
     virtual bool
     isDeclaredCollection(const Item& aQName) const = 0;
 
+    /**
+     * Create the index with the given name.
+     *
+     * @param aName The name of the index to create.
+     *
+     * @throw zerr:ZDDY0021 if a index with the given name is not declared.
+     *
+     * @throw zerr:ZDDY0022 if a index with the given name already exists.
+     */
+    virtual void
+    createIndex(const Item& aQName) = 0;
+
+    /**
+     * Create the index with the given name.
+     *
+     * @param aName The name of the index to create.
+     *
+     * @throw zerr:ZDDY0021 if a index with the given name is not declared.
+     *
+     * @throw zerr:ZDDY0009 if a index with the given name does not exist
+     */
+    virtual void
+    deleteIndex(const Item& aQName) = 0;
+
+    /**
+     * This function returns a sequence of names of the indexes
+     * that are available.
+     *
+     * @return The list of names of the available indexes.
+     */
+    virtual ItemSequence_t
+    availableIndexes() const = 0;
+
+    /**
+     * This function returns true if a index with the given name is available.
+     *
+     * @param aName The name of the index that is being checked.
+     *
+     * @return true if the index is available and false otherwise.
+     */
+    virtual bool
+    isAvailableIndex(const Item& aQName) const = 0;
+
+    /**
+     * List all the indexes that are declared in the XQuery or the
+     * StaticContext that was used to retrieve this StaticCollectionManager.
+     *
+     * @return a sequence of QNames of all said indexes
+     */
+    virtual ItemSequence_t
+    declaredIndexes() const = 0;
+
+    /**
+     * Checks if a index with a given QName is declared in the XQuery
+     * or the StaticContext that was used to retrieve this
+     * StaticCollectionManager.
+     *
+     * @return true if a collection with the given name is declared,
+     *   false otherwise.
+     */
+    virtual bool
+    isDeclaredIndex(const Item& aQName) const = 0;
+
     virtual ~StaticCollectionManager() {}
 
   }; /* class StaticCollectionManager */

=== modified file 'src/api/staticcollectionmanagerimpl.cpp'
--- src/api/staticcollectionmanagerimpl.cpp	2011-09-16 13:26:06 +0000
+++ src/api/staticcollectionmanagerimpl.cpp	2011-12-10 00:15:30 +0000
@@ -68,6 +68,7 @@
   theFactory(aFactory),
   theColDDLNamespace("http://www.zorba-xquery.com/modules/store/static/collections/ddl";),
   theColDMLNamespace("http://www.zorba-xquery.com/modules/store/static/collections/dml";),
+  theIdxDDLNamespace("http://www.zorba-xquery.com/modules/store/static/indexes/ddl";),
   theDiagnosticHandler(aDiagnosticHandler)
 {
   // the context passed as parameter is not used anywhere in here.
@@ -104,6 +105,7 @@
   Zorba_CompilerHints_t lHints;
   std::ostringstream lProlog;
   lProlog << "import module namespace d = '" << theColDDLNamespace << "';";
+  lProlog << "import module namespace iddl = '" << theIdxDDLNamespace << "';";
   aCtx->loadProlog(lProlog.str(), lHints);
 }
 
@@ -245,6 +247,165 @@
 }
 
 
+/*******************************************************************************
+
+********************************************************************************/
+void
+StaticCollectionManagerImpl::createIndex(const Item& aQName)
+{
+  ZORBA_DM_TRY
+  {
+    Item lFunc = theFactory->createQName(theIdxDDLNamespace, "create");
+
+    std::vector<ItemSequence_t> lArgs;
+    lArgs.push_back(new SingletonItemSequence(aQName));
+
+    ItemSequence_t lSeq = theContext->invoke(lFunc, lArgs);
+    Iterator_t lIter = lSeq->getIterator();
+    lIter->open();
+    Item lRes;
+    lIter->next(lRes);
+  }
+  ZORBA_DM_CATCH
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void
+StaticCollectionManagerImpl::deleteIndex(const Item& aQName)
+{
+  // do a check here in order to get better (non-confusing) error messages
+  if (!isDeclaredIndex(aQName)) 
+  {
+    throw ZORBA_EXCEPTION(zerr::ZDDY0021_INDEX_NOT_DECLARED,
+    ERROR_PARAMS(aQName.getStringValue()));
+  }
+
+  ZORBA_DM_TRY
+  {
+    if (!isAvailableIndex(aQName)) 
+    {
+      throw ZORBA_EXCEPTION(zerr::ZDDY0023_INDEX_DOES_NOT_EXIST,
+      ERROR_PARAMS(aQName.getStringValue()));
+    }
+
+    Item lFunc = theFactory->createQName(theIdxDDLNamespace, "delete");
+
+    std::vector<ItemSequence_t> lArgs;
+    lArgs.push_back(new SingletonItemSequence(aQName));
+
+    ItemSequence_t lSeq = theContext->invoke(lFunc, lArgs);
+    Iterator_t lIter = lSeq->getIterator();
+    lIter->open();
+    Item lRes;
+    lIter->next(lRes);
+  }
+  ZORBA_DM_CATCH
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+ItemSequence_t
+StaticCollectionManagerImpl::availableIndexes() const
+{
+  ZORBA_DM_TRY
+  {
+    Item lFunc = theFactory->createQName(theIdxDDLNamespace,
+                                         "available-indexes");
+
+    std::vector<ItemSequence_t> lArgs;
+    return theContext->invoke(lFunc, lArgs);
+  }
+  ZORBA_DM_CATCH
+  return 0;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+StaticCollectionManagerImpl::isAvailableIndex(const Item& aQName) const
+{
+  // do a check here in order to get better (non-confusing) error messages
+  if (!isDeclaredIndex(aQName)) 
+  {
+    throw ZORBA_EXCEPTION(zerr::ZDDY0001_COLLECTION_NOT_DECLARED,
+    ERROR_PARAMS(aQName.getStringValue()));
+  }
+
+  ZORBA_DM_TRY
+  {
+    Item lFunc = theFactory->createQName(theIdxDDLNamespace,
+                                         "is-available-index");
+
+    std::vector<ItemSequence_t> lArgs;
+    lArgs.push_back(new SingletonItemSequence(aQName));
+
+    ItemSequence_t lSeq = theContext->invoke(lFunc, lArgs);
+    Iterator_t lIter = lSeq->getIterator();
+    lIter->open();
+    Item lRes;
+    if (!lIter->next(lRes))
+      return false;
+
+    return lRes.getBooleanValue();
+  }
+  ZORBA_DM_CATCH
+  return false;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+ItemSequence_t
+StaticCollectionManagerImpl::declaredIndexes() const
+{
+  ZORBA_DM_TRY
+  {
+    Item lFunc = theFactory->createQName(theIdxDDLNamespace,
+                                         "declared-indexes");
+
+    std::vector<ItemSequence_t> lArgs;
+    return theContext->invoke(lFunc, lArgs);
+  }
+  ZORBA_DM_CATCH
+  return 0;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+StaticCollectionManagerImpl::isDeclaredIndex(const Item& aQName) const
+{
+  ZORBA_DM_TRY
+  {
+    Item lFunc = theFactory->createQName(theIdxDDLNamespace,
+                                         "is-declared-index");
+
+    std::vector<ItemSequence_t> lArgs;
+    lArgs.push_back(new SingletonItemSequence(aQName));
+
+    ItemSequence_t lSeq = theContext->invoke(lFunc, lArgs);
+    Iterator_t lIter = lSeq->getIterator();
+    lIter->open();
+    Item lRes;
+    if (!lIter->next(lRes))
+      return false;
+    return lRes.getBooleanValue();
+  }
+  ZORBA_DM_CATCH
+  return false;
+}
+
+
 /////////////////////////////////////////////////////////////////////////////////
 //                                                                             //
 //  StaticCollectionManagerSetImpl                                             //
@@ -451,6 +612,132 @@
 
 ********************************************************************************/
 void
+StaticCollectionManagerSetImpl::createIndex(const Item& aQName)
+{
+  for (MgrSet::iterator lIter = theMgrs.begin();
+       lIter != theMgrs.end();
+       ++lIter) 
+  {
+    if ((*lIter)->isDeclaredIndex(aQName)) 
+    {
+        (*lIter)->createIndex(aQName);
+        return;
+    }
+  }
+
+  throw ZORBA_EXCEPTION(zerr::ZDDY0021_INDEX_NOT_DECLARED,
+  ERROR_PARAMS(aQName.getStringValue()));
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void
+StaticCollectionManagerSetImpl::deleteIndex(const Item& aQName)
+{
+  for (MgrSet::iterator lIter = theMgrs.begin();
+       lIter != theMgrs.end();
+       ++lIter) 
+  {
+    if ((*lIter)->isDeclaredIndex(aQName)) 
+    {
+        (*lIter)->deleteIndex(aQName);
+        return;
+    }
+  }
+
+  throw ZORBA_EXCEPTION(zerr::ZDDY0021_INDEX_NOT_DECLARED,
+  ERROR_PARAMS(aQName.getStringValue()));
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+ItemSequence_t
+StaticCollectionManagerSetImpl::availableIndexes() const
+{
+  std::vector<ItemSequence_t> lSequences;
+  for (MgrSet::const_iterator lIter = theMgrs.begin();
+       lIter != theMgrs.end();
+       ++lIter)
+  {
+    lSequences.push_back((*lIter)->availableIndexes());
+  }
+  // need to do duplicate elimination because
+  // indexes are coming from static contexts
+  // which might be imported multiple times
+  return new ItemSequenceChainer(lSequences, true);
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+StaticCollectionManagerSetImpl::isAvailableIndex(const Item& aQName) const
+{
+  for (MgrSet::const_iterator lIter = theMgrs.begin();
+       lIter != theMgrs.end(); 
+       ++lIter) 
+  {
+    if ((*lIter)->isDeclaredIndex(aQName) &&
+        (*lIter)->isAvailableIndex(aQName)) 
+    {
+      return true;
+    }
+  }
+  return false;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+ItemSequence_t
+StaticCollectionManagerSetImpl::declaredIndexes() const
+{
+  std::vector<ItemSequence_t> lSequences;
+  for (MgrSet::const_iterator lIter = theMgrs.begin();
+       lIter != theMgrs.end(); 
+       ++lIter) 
+  {
+    lSequences.push_back((*lIter)->declaredIndexes());
+  }
+  // we need to do duplicate elimination of the sequence because
+  // the contexts might contain the declaration of a index
+  // multiple times. This happens if the set of contexts contains
+  // the context that declares the index as well as some
+  // contexts that result from compiling modules that import
+  // the module declaring the index.
+  return new ItemSequenceChainer(lSequences, true);
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool
+StaticCollectionManagerSetImpl::isDeclaredIndex(const Item& aQName) const
+{
+  for (MgrSet::const_iterator lIter = theMgrs.begin();
+       lIter != theMgrs.end(); 
+       ++lIter) 
+  {
+    if ((*lIter)->isDeclaredIndex(aQName)) 
+    {
+      return true;
+    }
+  }
+  return false;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void
 StaticCollectionManagerSetImpl::registerDiagnosticHandler(
     DiagnosticHandler* aDiagnosticHandler)
 {

=== modified file 'src/api/staticcollectionmanagerimpl.h'
--- src/api/staticcollectionmanagerimpl.h	2011-09-14 11:08:52 +0000
+++ src/api/staticcollectionmanagerimpl.h	2011-12-10 00:15:30 +0000
@@ -80,6 +80,7 @@
   ItemFactory            * theFactory;
   std::string              theColDDLNamespace;
   std::string              theColDMLNamespace;
+  std::string              theIdxDDLNamespace;
   
   StaticContext_t          theContext;
   CollectionManagerImpl*   theCollMgr;
@@ -125,6 +126,24 @@
   virtual bool
   isDeclaredCollection(const Item& aQName) const;
 
+  virtual void
+  createIndex(const Item& aQName);
+
+  virtual void
+  deleteIndex(const Item& aQName);
+
+  virtual ItemSequence_t
+  availableIndexes() const;
+
+  virtual bool
+  isAvailableIndex(const Item& aQName) const;
+
+  virtual ItemSequence_t
+  declaredIndexes() const;
+
+  virtual bool
+  isDeclaredIndex(const Item& aQName) const;
+
 }; /* class StaticCollectionManagerImpl */
 
 
@@ -187,6 +206,24 @@
   virtual bool
   isDeclaredCollection(const Item& aQName) const;
 
+  virtual void
+  createIndex(const Item& aQName);
+
+  virtual void
+  deleteIndex(const Item& aQName);
+
+  virtual ItemSequence_t
+  availableIndexes() const;
+
+  virtual bool
+  isAvailableIndex(const Item& aQName) const;
+
+  virtual ItemSequence_t
+  declaredIndexes() const;
+
+  virtual bool
+  isDeclaredIndex(const Item& aQName) const;
+
 }; /* class StaticCollectionManagerSetImpl */
 
 

=== modified file 'test/unit/module2.xq'
--- test/unit/module2.xq	2011-08-18 17:07:50 +0000
+++ test/unit/module2.xq	2011-12-10 00:15:30 +0000
@@ -16,8 +16,14 @@
 
 module namespace mod2 = "http://www.mod2.com/";;
 
+import module namespace ddl = "http://www.zorba-xquery.com/modules/store/static/collections/dml";;
+
 import module namespace mod3 = "http://www.mod3.com/"; at "file:///${CMAKE_CURRENT_BINARY_DIR}/module3.xq";
 
 declare namespace ann = "http://www.zorba-xquery.com/annotations";;
 
 declare %ann:ordered collection mod2:coll as node()*;
+
+declare index mod2:index
+  on nodes ddl:collection(xs:QName("mod2:coll"))
+  by data(@id) as xs:string;

=== modified file 'test/unit/staticcollectionmanager.cpp'
--- test/unit/staticcollectionmanager.cpp	2011-09-12 22:42:28 +0000
+++ test/unit/staticcollectionmanager.cpp	2011-12-10 00:15:30 +0000
@@ -40,6 +40,7 @@
 
   ItemFactory* lFac = z->getItemFactory();
   Item lCollName2 = lFac->createQName("http://www.mod2.com/";, "coll");
+  Item lIdxName   = lFac->createQName("http://www.mod2.com/";, "index");
   Item lCollName3 = lFac->createQName("http://www.mod3.com/";, "coll");
 
   ItemSequence_t lSeq = lColMgr->declaredCollections();
@@ -56,16 +57,35 @@
     return false;
   }
 
+  int num_idxs = 0;
+  lSeq = lColMgr->declaredIndexes();
+  lIter = lSeq->getIterator();
+  lIter->open();
+  while (lIter->next(lTmp)) {
+    std::cout << "name " << lTmp.getStringValue() << std::endl;
+    ++num_idxs;
+  }
+
+  if (num_idxs != 1) {
+    return false;
+  }
+
   if (!lColMgr->isDeclaredCollection(lCollName2) ||
       !lColMgr->isDeclaredCollection(lCollName3)) {
     return false;
   }
 
+  if (!lColMgr->isDeclaredIndex(lIdxName)) {
+    return false;
+  }
+
   lColMgr->createCollection(lCollName2);
   lColMgr->createCollection(lCollName3);
+  lColMgr->createIndex(lIdxName);
 
   if (!lColMgr->isAvailableCollection(lCollName2) ||
-      !lColMgr->isAvailableCollection(lCollName3)) {
+      !lColMgr->isAvailableCollection(lCollName3) ||
+      !lColMgr->isAvailableIndex(lIdxName)) {
     return false;
   }
 
@@ -82,11 +102,13 @@
     return false;
   }
 
+  lColMgr->deleteIndex(lIdxName);
   lColMgr->deleteCollection(lCollName2);
   lColMgr->deleteCollection(lCollName3);
 
   if (lColMgr->isAvailableCollection(lCollName2) ||
-      lColMgr->isAvailableCollection(lCollName3)) {
+      lColMgr->isAvailableCollection(lCollName3) ||
+      lColMgr->isAvailableIndex(lIdxName)) {
     return false;
   }
 


Follow ups