Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/markos-scratch 
into lp:zorba.

Commit message:
caching of the index view expression

Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch/+merge/166627

caching of the index view expression
-- 
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch/+merge/166627
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/compiler/rewriter/rules/index_matching_rule.cpp'
--- src/compiler/rewriter/rules/index_matching_rule.cpp	2013-05-30 22:25:30 +0000
+++ src/compiler/rewriter/rules/index_matching_rule.cpp	2013-05-31 01:51:28 +0000
@@ -81,40 +81,9 @@
   RewriteRule(RewriteRule::IndexJoin, "IndexJoin"),
   theIndexDecl(decl),
   theViewExpr(NULL),
-  theDoTrace(true),
+  theKeyClauses(NULL),
   theParentNode(NULL)
 {
-  theViewExpr = decl->getViewExpr();
-
-  csize numVClauses = theViewExpr->num_clauses();
-
-  theKeyClauses.reserve(numVClauses);
-
-  for (csize i = 1; i < numVClauses; ++i)
-  {
-    assert(theViewExpr->get_clause(i)->get_kind() == flwor_clause::let_clause);
-    let_clause* lc = static_cast<let_clause*>(theViewExpr->get_clause(i));
-    theKeyClauses.push_back(lc);
-  }
-  
-  // We apply the fold rules on the view expr in order to flatten a
-  std::ostringstream msg;
-  msg << "normalization of candidate index: " << decl->getName()->getStringValue();
-
-  RewriterContext rCtx(theViewExpr->get_ccb(),
-                       theViewExpr,
-                       theViewExpr->get_udf(),
-                       msg.str(),
-                       true);
-  FoldRules foldRules;
-  foldRules.rewrite(rCtx);
-  
-  if (Properties::instance()->printIntermediateOpt() && theDoTrace)
-  {
-    std::cout << "Canonical view expr for candidate index : " 
-              << decl->getName()->getStringValue() << std::endl;
-    theViewExpr->put(std::cout) << std::endl;
-  }
 }
 
 
@@ -132,6 +101,9 @@
 
   if (node->get_expr_kind() == flwor_expr_kind)
   {
+    if (theViewExpr == NULL)
+      theViewExpr = theIndexDecl->getViewExpr(theKeyClauses);
+
     theQueryExpr = static_cast<flwor_expr*>(node);
 
     bool matched = matchIndex();
@@ -204,7 +176,7 @@
   theUnmatchedQPreds.clear();
   theMatchedQPreds.clear();
   theProbeArgs.clear();
-  theProbeArgs.reserve(theKeyClauses.size() + 1);
+  theProbeArgs.reserve(theKeyClauses->size() + 1);
 
   expr::substitution_t subst;
 
@@ -459,7 +431,7 @@
     orderby_clause* ob = 
     static_cast<orderby_clause*>(theQueryExpr->get_clause(firstOrderByPos));
     
-    csize numKeys = theKeyClauses.size();
+    csize numKeys = theKeyClauses->size();
     csize numSortKeys = ob->num_columns();
 
     if (numSortKeys <= numKeys)
@@ -472,7 +444,7 @@
           break;
 
         if (!matchKeyExpr(ob->get_column_expr(i),
-                          theKeyClauses[i]->get_expr(),
+                          (*theKeyClauses)[i]->get_expr(),
                           subst))
           break;
       }
@@ -761,8 +733,8 @@
 
   bool matchedFirstKey = false;
 
-  std::vector<let_clause*>::const_iterator keyIte = theKeyClauses.begin();
-  std::vector<let_clause*>::const_iterator keyEnd = theKeyClauses.end();
+  std::vector<let_clause*>::const_iterator keyIte = theKeyClauses->begin();
+  std::vector<let_clause*>::const_iterator keyEnd = theKeyClauses->end();
 
   for (; keyIte != keyEnd; ++keyIte)
   {
@@ -904,7 +876,7 @@
     else
       theProbeArgs.push_back(falseExpr);
 
-    if (keyIte == theKeyClauses.begin() &&
+    if (keyIte == theKeyClauses->begin() &&
         (bounds[0] != NULL || bounds[1] != NULL))
       matchedFirstKey = true;
   }
@@ -925,8 +897,8 @@
     expr::substitution_t& subst,
     csize& lastMatchedWHEREpos)
 {
-  std::vector<let_clause*>::const_iterator keyIte = theKeyClauses.begin();
-  std::vector<let_clause*>::const_iterator keyEnd = theKeyClauses.end();
+  std::vector<let_clause*>::const_iterator keyIte = theKeyClauses->begin();
+  std::vector<let_clause*>::const_iterator keyEnd = theKeyClauses->end();
 
   for (; keyIte != keyEnd; ++keyIte)
   {

=== modified file 'src/compiler/rewriter/rules/index_matching_rule.h'
--- src/compiler/rewriter/rules/index_matching_rule.h	2013-05-04 20:20:05 +0000
+++ src/compiler/rewriter/rules/index_matching_rule.h	2013-05-31 01:51:28 +0000
@@ -52,9 +52,9 @@
 protected:
   IndexDecl                 * theIndexDecl;
   flwor_expr                * theViewExpr;
-  std::vector<let_clause*>    theKeyClauses;
+  std::vector<let_clause*>  * theKeyClauses;
+
   flwor_expr                * theQueryExpr;
-  bool                        theDoTrace;
 
   std::vector<PredInfo>       theUnmatchedQPreds;
   std::vector<PredInfo>       theMatchedQPreds;

=== modified file 'src/compiler/xqddf/value_index.cpp'
--- src/compiler/xqddf/value_index.cpp	2013-05-30 22:25:30 +0000
+++ src/compiler/xqddf/value_index.cpp	2013-05-31 01:51:28 +0000
@@ -31,6 +31,8 @@
 #include "compiler/expression/expr_iter.h"
 #include "compiler/expression/expr_manager.h"
 #include "compiler/codegen/plan_visitor.h"
+#include "compiler/rewriter/framework/rewriter_context.h"
+#include "compiler/rewriter/rules/fold_rules.h"
 
 #include "runtime/base/plan_iterator.h"
 #include "runtime/indexing/doc_indexer.h"
@@ -70,6 +72,7 @@
   theDomainExpr(NULL),
   theDomainVar(NULL),
   theDomainPosVar(NULL),
+  theViewExpr(NULL),
   theBuildExpr(NULL),
   theDocIndexerExpr(NULL)
 {
@@ -81,7 +84,14 @@
 ********************************************************************************/
 IndexDecl::IndexDecl(::zorba::serialization::Archiver& ar)
   :
-  SimpleRCObject(ar)
+  SimpleRCObject(ar),
+  theDomainClause(NULL),
+  theDomainExpr(NULL),
+  theDomainVar(NULL),
+  theDomainPosVar(NULL),
+  theViewExpr(NULL),
+  theBuildExpr(NULL),
+  theDocIndexerExpr(NULL)
 {
 }
 
@@ -475,10 +485,19 @@
   For now, this is done for value indexes only
 
   for $newdot at $newpos in cloned_domain_expr
+  let  $key_1 := new_key_expr_1
+  .....
+  let $key_N := new_key_expr_N
   return $newdot
 *******************************************************************************/
-flwor_expr* IndexDecl::getViewExpr()
+flwor_expr* IndexDecl::getViewExpr(std::vector<let_clause*>*& keyClauses)
 {
+  if (theViewExpr != NULL)
+  {
+    keyClauses = &theKeyClauses;
+    return theViewExpr;
+  }
+
   theDomainClause = NULL;
 
   expr* domainExpr = getDomainExpr();
@@ -539,6 +558,8 @@
   //std::vector<expr*> predExprs;
   csize numKeys = theKeyExprs.size();
 
+  theKeyClauses.reserve(numKeys);
+
   for (csize i = 0; i < numKeys; ++i)
   {
     // clone the key expr
@@ -548,6 +569,8 @@
 
     expr* keyClone = theKeyExprs[i]->clone(udf, subst);
 
+    keyClone->setNonDiscardable(ANNOTATION_TRUE_FIXED);
+
     const QueryLoc& keyloc = keyClone->get_loc();
 
     // create the LET clause
@@ -561,45 +584,27 @@
     let_clause* lc = theCCB->theEM->create_let_clause(sctx, keyloc, keyVar, keyClone);
 
     flworExpr->add_clause(lc);
-
-#if 0
-    // create the predicate
-    expr* op1 = theCCB->theEM->create_wrapper_expr(sctx, udf, keyloc, keyVar);
-
-    localName = "$$arg_" + ztd::to_string(i);
-    store::Item_t argVarName;
-    GENV_ITEMFACTORY->createQName(argVarName, "", "", localName);
-
-    expr* op2 = theCCB->theEM->
-    create_var_expr(sctx, udf, keyloc, var_expr::arg_var, keyVarName);
-
-    expr* pred = theCCB->theEM->
-    create_fo_expr(sctx, udf, keyloc, compFunc, op1, op2);
-
-    predExprs.push_back(pred);
-#endif
-  }
-
-#if 0
-  expr* whereExpr;
-
-  if (predExprs.size() > 1)
-  {
-    whereExpr = theCCB->theEM->
-    create_fo_expr(sctx, udf, domloc, BUILTIN_FUNC(OP_AND_N), predExprs);
-  }
-  else
-  {
-    whereExpr = predExprs[0];
-  }
-
-  where_clause* wc = theCCB->theEM->
-  create_where_clause(sctx, whereExpr->get_loc(), whereExpr);
-
-  flworExpr->add_clause(wc);
-#endif
-
-  return flworExpr;
+    theKeyClauses.push_back(lc);
+  }
+
+  theViewExpr = flworExpr;
+
+  // We apply the fold rules on the view expr in order to flatten a
+  std::ostringstream msg;
+  msg << "normalization of candidate index: " << getName()->getStringValue();
+
+  RewriterContext rCtx(theViewExpr->get_ccb(),
+                       theViewExpr,
+                       theViewExpr->get_udf(),
+                       msg.str(),
+                       true);
+  FoldRules foldRules;
+  foldRules.rewrite(rCtx);
+
+  ZORBA_ASSERT(theViewExpr == rCtx.getRoot());
+
+  keyClauses = &theKeyClauses;
+  return theViewExpr;
 }
 
 

=== modified file 'src/compiler/xqddf/value_index.h'
--- src/compiler/xqddf/value_index.h	2013-05-30 22:25:30 +0000
+++ src/compiler/xqddf/value_index.h	2013-05-31 01:51:28 +0000
@@ -368,7 +368,7 @@
 
   void analyze();
 
-  flwor_expr* getViewExpr();
+  flwor_expr* getViewExpr(std::vector<let_clause*>*& keyClauses);
 
   expr* getBuildExpr(const QueryLoc& loc);
 

-- 
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