Matthias Brantner has proposed merging lp:~zorba-coders/zorba/caching into 
lp:~zorba-coders/zorba/zorba-experimental.

Requested reviews:
  Zorba Coders (zorba-coders)

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

try to create sandbox
-- 
https://code.launchpad.net/~zorba-coders/zorba/caching/+merge/81105
Your team Zorba Coders is requested to review the proposed merge of 
lp:~zorba-coders/zorba/caching into lp:~zorba-coders/zorba/zorba-experimental.
=== modified file 'doc/zorba/options.dox'
--- doc/zorba/options.dox	2011-09-14 06:15:19 +0000
+++ doc/zorba/options.dox	2011-11-03 02:56:35 +0000
@@ -278,6 +278,25 @@
 In order to be able to use the value twice, the <tt>string:materialize</tt> function must be used to materialize the entire contents of the file <tt>myfile.txt</tt> in memory.
 Otherwise, the error zerr:ZSTR0055 is raised.
 
+\paragraph caching_annotation Caching Results of Functions
+Caching of function results might improve the performance if computational expensive functions are invoked multiple times with the same arguments.
+
+Zorba automatically caches results of recursive, deterministic, and non-sequential functions whose parameters and return types are subtypes of xs:anyAtomicType.
+Specifically, if such a function is called twice with the same arguments, the result of the second call will return the same value without re-evaluating the function.
+
+For example, in the following recursive function computing a fibonacci number, each result is automatically cached and, hence, dramatically improves the performance.
+
+\include zorba/udf/udf-fib-rec.xq
+
+In order to explicitly disable function caching, the user can specify the <tt>%ann:no-cache</tt> annotation.
+
+In addition, the user can use the <tt>%ann:cache</tt> annotation to cache the results of functions other than the ones that are automatically cached.
+However, this will only work if the function is not updating and its parameter and return types are subtypes of xs:anyAtomicType.
+Zorba will raise a warning if caching is explicitly enabled but the function does not meet this criteria (zwarn:ZWST0005).
+
+Please note, that explicitly enforcing caching for sequential or nondeterministic functions might not give the intended result.
+In such cases, Zorba will raise a warning (zwarn:ZWST0006).
+
 \paragraph collection_index_annotations Annotations on Collections and Indexes
 
 The \ref xqddf uses annotations to assign properties to collections and indexes.

=== modified file 'include/zorba/pregenerated/diagnostic_list.h'
--- include/zorba/pregenerated/diagnostic_list.h	2011-10-19 15:28:51 +0000
+++ include/zorba/pregenerated/diagnostic_list.h	2011-11-03 02:56:35 +0000
@@ -752,6 +752,10 @@
 
 extern ZORBA_DLL_PUBLIC ZorbaWarningCode ZWST0004_AMBIGUOUS_SEQUENTIAL_FLWOR;
 
+extern ZORBA_DLL_PUBLIC ZorbaWarningCode ZWST0005_CACHING_NOT_POSSIBLE;
+
+extern ZORBA_DLL_PUBLIC ZorbaWarningCode ZWST0006_CACHING_MIGHT_NOT_BE_INTENDED;
+
 } // namespace zwarn
 } // namespace zorba
 #endif /* ZORBA_DIAGNOSTIC_LIST_API_H */

=== modified file 'modules/com/zorba-xquery/www/modules/pregenerated/warnings.xq'
--- modules/com/zorba-xquery/www/modules/pregenerated/warnings.xq	2011-10-19 15:28:51 +0000
+++ modules/com/zorba-xquery/www/modules/pregenerated/warnings.xq	2011-11-03 02:56:35 +0000
@@ -53,4 +53,23 @@
 
 (:~
 :)
-declare variable $zwarn:ZWST0004 as xs:QName := fn:QName($zwarn:NS, "zwarn:ZWST0004");
\ No newline at end of file
+declare variable $zwarn:ZWST0004 as xs:QName := fn:QName($zwarn:NS, "zwarn:ZWST0004");
+
+(:~
+ :
+ : This warning is raised if the user explicitly enables caching
+ : of function results (using the %ann:cache annotation) but the function
+ : is updating or its parameter and return types are not subtypes of
+ : xs:anyAtomicType.
+ : 
+:)
+declare variable $zwarn:ZWST0005 as xs:QName := fn:QName($zwarn:NS, "zwarn:ZWST0005");
+
+(:~
+ :
+ : This warning is raised if the user explicitly enables caching
+ : of function results (using the %ann:cache annotation) and the function
+ : is annotated as sequential or nondeterministic.
+ : 
+:)
+declare variable $zwarn:ZWST0006 as xs:QName := fn:QName($zwarn:NS, "zwarn:ZWST0006");
\ No newline at end of file

=== modified file 'src/annotations/annotations.cpp'
--- src/annotations/annotations.cpp	2011-08-11 17:19:25 +0000
+++ src/annotations/annotations.cpp	2011-11-03 02:56:35 +0000
@@ -307,6 +307,12 @@
   GENV_ITEMFACTORY->createQName(lTmp, ZORBA_ANNOTATIONS_NS, "", "streamable");
   aCtx->add_ann(StaticContextConsts::zann_streamable, lTmp);
 
+  GENV_ITEMFACTORY->createQName(lTmp, ZORBA_ANNOTATIONS_NS, "", "cache");
+  aCtx->add_ann(StaticContextConsts::zann_cache, lTmp);
+
+  GENV_ITEMFACTORY->createQName(lTmp, ZORBA_ANNOTATIONS_NS, "", "no-cache");
+  aCtx->add_ann(StaticContextConsts::zann_no_cache, lTmp);
+
   //
   // Zorba annotations - xqddf
   //
@@ -417,6 +423,10 @@
       ZANN(zann_read_only_nodes) |
       ZANN(zann_mutable_nodes)
     );
+  theRuleSet.push_back(
+      ZANN(zann_cache) |
+      ZANN(zann_no_cache)
+    );
 #undef ZANN
 }
 

=== modified file 'src/compiler/codegen/plan_visitor.cpp'
--- src/compiler/codegen/plan_visitor.cpp	2011-09-16 19:36:18 +0000
+++ src/compiler/codegen/plan_visitor.cpp	2011-11-03 02:56:35 +0000
@@ -98,6 +98,7 @@
 #endif
 
 #include "functions/function.h"
+#include "functions/udf.h"
 #include "functions/library.h"
 
 #include "types/typeops.h"
@@ -2277,11 +2278,14 @@
           dynamic_cast<EnclosedIterator*>(iter.getp())->setInUpdateExpr();
       }
     }
-#if 0
     else if (func->isUdf())
     {
+      // need to computeResultCaching here for iterprint to work
       const user_function* udf = static_cast<const user_function*>(func);
+      udf->computeResultCaching(theCCB->theXQueryDiagnostics);
+    }
 
+#if 0
       if (udf->isExiting())
       {
         TypeManager* tm = v.get_type_manager();

=== modified file 'src/compiler/translator/translator.cpp'
--- src/compiler/translator/translator.cpp	2011-10-18 21:29:50 +0000
+++ src/compiler/translator/translator.cpp	2011-11-03 02:56:35 +0000
@@ -1305,11 +1305,25 @@
     if (qname != NULL)
     {
       RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-      ERROR_PARAMS(qname->getStringValue()));
+      ERROR_PARAMS(
+        qname->getStringValue(),
+        "index",
+        n-1,
+        "multiple of 6"
+      )
+      );
     }
     else
     {
-      RAISE_ERROR_NO_PARAMS(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc);
+      RAISE_ERROR(
+          zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
+          ERROR_PARAMS(
+            "anonymous",
+            "index",
+            n-1,
+            "multiple of 6"
+          )
+      );
     }
   }
 

=== modified file 'src/context/static_context_consts.h'
--- src/context/static_context_consts.h	2011-07-21 23:02:27 +0000
+++ src/context/static_context_consts.h	2011-11-03 02:56:35 +0000
@@ -132,6 +132,8 @@
     zann_nonsequential,
     zann_variadic,
     zann_streamable,
+    zann_cache,
+    zann_no_cache,
     zann_unique,
     zann_nonunique,
     zann_value_equality,

=== modified file 'src/diagnostics/diagnostic_en.xml'
--- src/diagnostics/diagnostic_en.xml	2011-10-17 21:17:05 +0000
+++ src/diagnostics/diagnostic_en.xml	2011-11-03 02:56:35 +0000
@@ -1966,7 +1966,7 @@
     </diagnostic>
 
     <diagnostic code="ZDDY0025" name="INDEX_WRONG_NUMBER_OF_PROBE_ARGS">
-      <value>${"1": }invalid number of arguments in probe</value>
+      <value>${"1": }invalid number of arguments to $2 operation; given $3 expected $4</value>
     </diagnostic>
 
     <diagnostic code="ZDDY0026" name="INDEX_RANGE_PROBE_NOT_ALLOWED">
@@ -2307,6 +2307,34 @@
       <value>Sequential FLWOR expr may not have the semantics you expect</value>
     </diagnostic>
 
+    <diagnostic code="ZWST0005" name="CACHING_NOT_POSSIBLE">
+      <comment>
+        This warning is raised if the user explicitly enables caching
+        of function results (using the %ann:cache annotation) but the function
+        is updating or its parameter and return types are not subtypes of
+        xs:anyAtomicType.
+      </comment>
+      <value>"$1": function caching not possible; $2</value>
+      <entry key="RETURN_TYPE">
+        <value>return type ($3) is not subtype of xs:anyAtomicType</value>
+      </entry>
+      <entry key="PARAM_TYPE">
+        <value>type of parameter $3 is $4 which is not a subtype of xs:anyAtomicType</value>
+      </entry>
+      <entry key="UPDATING">
+        <value>function is updating</value>
+      </entry>
+    </diagnostic>
+
+    <diagnostic code="ZWST0006" name="CACHING_MIGHT_NOT_BE_INTENDED">
+      <comment>
+        This warning is raised if the user explicitly enables caching
+        of function results (using the %ann:cache annotation) and the function
+        is annotated as sequential or nondeterministic.
+      </comment>
+      <value>"$1": function caching might not give the intended result because the function is declared as $2</value>
+    </diagnostic>
+
   </namespace>
 
   <!--////////// Subvalues /////////////////////////////////////////////////-->

=== modified file 'src/diagnostics/pregenerated/diagnostic_list.cpp'
--- src/diagnostics/pregenerated/diagnostic_list.cpp	2011-10-19 15:28:51 +0000
+++ src/diagnostics/pregenerated/diagnostic_list.cpp	2011-11-03 02:56:35 +0000
@@ -1104,6 +1104,12 @@
 ZorbaWarningCode ZWST0004_AMBIGUOUS_SEQUENTIAL_FLWOR( "ZWST0004" );
 
 
+ZorbaWarningCode ZWST0005_CACHING_NOT_POSSIBLE( "ZWST0005" );
+
+
+ZorbaWarningCode ZWST0006_CACHING_MIGHT_NOT_BE_INTENDED( "ZWST0006" );
+
+
 } // namespace zwarn
 
 } // namespace zorba

=== modified file 'src/diagnostics/pregenerated/dict_en.cpp'
--- src/diagnostics/pregenerated/dict_en.cpp	2011-10-20 23:05:55 +0000
+++ src/diagnostics/pregenerated/dict_en.cpp	2011-11-03 02:56:35 +0000
@@ -286,7 +286,7 @@
   { "ZDDY0022", "\"$1\": index already exists" },
   { "ZDDY0023", "\"$1\": index does not exist" },
   { "ZDDY0024", "\"$1\": index uniqueness violation" },
-  { "ZDDY0025", "${\"1\": }invalid number of arguments in probe" },
+  { "ZDDY0025", "${\"1\": }invalid number of arguments to $2 operation; given $3 expected $4" },
   { "ZDDY0026", "\"$1\": index range probe not allowed" },
   { "ZDDY0027", "\"$1\": index multiple creates" },
   { "ZDDY0028", "\"$1\": index domain has duplicate nodes" },
@@ -359,6 +359,8 @@
   { "ZWST0002", "\"$1\": unknown or unsupported annotation" },
   { "ZWST0003", "\"$1\": function declared sequential, but has non-sequential body" },
   { "ZWST0004", "Sequential FLWOR expr may not have the semantics you expect" },
+  { "ZWST0005", "\"$1\": function caching not possible; $2" },
+  { "ZWST0006", "\"$1\": function caching might not give the intended result because the function is declared as $2" },
   { "ZXQD0001", "\"$1\": prefix not declared when calling function \"$2\" from $3" },
   { "ZXQD0002", "\"$1\": $2" },
   { "ZXQD0003", "inconsistent options to the parse-xml-fragment() function: $1" },
@@ -663,6 +665,9 @@
   { "~XUST0002_UDF_2", "\"$2\": function declared updating but body is not updating or vacuous" },
   { "~ZDST0060_unknown_localname", "unknown localname ($3)" },
   { "~ZDST0060_unknown_namespace", "unknown namespace ($3)" },
+  { "~ZWST0005_PARAM_TYPE", "type of parameter $3 is $4 which is not a subtype of xs:anyAtomicType" },
+  { "~ZWST0005_RETURN_TYPE", "return type ($3) is not subtype of xs:anyAtomicType" },
+  { "~ZWST0005_UPDATING", "function is updating" },
   { "~ZXQD0004_NON_NEGATIVE", "given value must be non-negative ($2)" },
   { "~ZXQD0004_NOT_WITHIN_RANGE", "not within allowed range ($2)" },
   { "~ZXQP0004_TypeOps_is_in_scope_ForFunctionItemTypes", "TypeOps::is_in_scope() for function-item types" },

=== modified file 'src/functions/udf.cpp'
--- src/functions/udf.cpp	2011-10-18 17:37:41 +0000
+++ src/functions/udf.cpp	2011-11-03 02:56:35 +0000
@@ -28,6 +28,8 @@
 #include "functions/udf.h"
 #include "functions/function_impl.h"
 
+#include "diagnostics/xquery_warning.h"
+
 #include "types/typeops.h"
 
 
@@ -54,7 +56,9 @@
   theIsExiting(false),
   theIsLeaf(true),
   theIsOptimized(false),
-  thePlanStateSize(0)
+  thePlanStateSize(0),
+  theCache(0),
+  theCacheResults(false)
 {
   setFlag(FunctionConsts::isUDF);
   resetFlag(FunctionConsts::isBuiltin);
@@ -318,7 +322,7 @@
 /*******************************************************************************
 
 ********************************************************************************/
-  PlanIter_t user_function::getPlan(CompilerCB* ccb, uint32_t& planStateSize)
+PlanIter_t user_function::getPlan(CompilerCB* ccb, uint32_t& planStateSize)
 {
   if (thePlan == NULL)
   {
@@ -372,9 +376,175 @@
 /*******************************************************************************
 
 ********************************************************************************/
-CODEGEN_DEF(user_function)
-{
-  return new UDFunctionCallIterator(aSctx, aLoc, aArgs, this);
+store::Index* user_function::getCache() const
+{
+  return theCache;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void user_function::setCache(store::Index* aCache)
+{
+  theCache = aCache;
+}
+
+
+/*******************************************************************************
+********************************************************************************/
+bool user_function::cacheResults() const
+{
+  return theCacheResults;
+}
+
+
+/*******************************************************************************
+ only cache recursive (non-sequential, non-updating, deterministic)
+ functions with singleton atomic input and output
+********************************************************************************/
+void user_function::computeResultCaching(XQueryDiagnostics* diag) const
+{
+  static_context& lCtx = GENV_ROOT_STATIC_CONTEXT;
+  // check necessary conditions
+  // %ann:cache or not %ann:no-cache
+  if (theAnnotationList)
+  {
+    if (theAnnotationList->contains(
+          lCtx.lookup_ann(StaticContextConsts::zann_no_cache)))
+    {
+      theCacheResults = false;
+      return;
+    }
+  }
+
+  // was the %ann:cache annotation given explicitly by the user
+  bool lExplicitCacheRequest = theAnnotationList
+    ?theAnnotationList->contains(
+        lCtx.lookup_ann(StaticContextConsts::zann_cache))
+    :false;
+
+  // parameter and return types are subtype of xs:anyAtomicType?
+  const xqtref_t& lRes = theSignature.returnType();
+  TypeManager* tm = lRes->get_manager();
+
+  if (!TypeOps::is_subtype(tm,
+                           *lRes,
+                           *GENV_TYPESYSTEM.ANY_ATOMIC_TYPE_ONE,
+                           theLoc))
+  {
+    if (lExplicitCacheRequest)
+    {
+      diag->add_warning(
+        NEW_XQUERY_WARNING(
+          zwarn::ZWST0005_CACHING_NOT_POSSIBLE,
+          WARN_PARAMS(
+            getName()->getStringValue(),
+            ZED( ZWST0005_RETURN_TYPE ),
+            lRes->toString()
+          ),
+          WARN_LOC(theLoc)
+        )
+      );
+    }
+    theCacheResults = false;
+    return;
+  }
+
+  size_t lArity = theSignature.paramCount();
+  for (size_t i = 0; i < lArity; ++i)
+  {
+    const xqtref_t& lArg = theSignature[i];
+    if (!TypeOps::is_subtype(tm,
+                             *lArg,
+                             *GENV_TYPESYSTEM.ANY_ATOMIC_TYPE_ONE,
+                             theLoc))
+    {
+      if (lExplicitCacheRequest)
+      {
+        diag->add_warning(
+          NEW_XQUERY_WARNING(
+            zwarn::ZWST0005_CACHING_NOT_POSSIBLE,
+            WARN_PARAMS(
+              getName()->getStringValue(),
+              ZED( ZWST0005_PARAM_TYPE ),
+              i+1,
+              lArg->toString()
+            ),
+            WARN_LOC(theLoc)
+          )
+        );
+      }
+      theCacheResults = false;
+      return;
+    }
+  }
+
+  // function updating?
+  if (isUpdating())
+  {
+    if (lExplicitCacheRequest)
+    {
+      diag->add_warning(
+        NEW_XQUERY_WARNING(
+          zwarn::ZWST0005_CACHING_NOT_POSSIBLE,
+          WARN_PARAMS(
+            getName()->getStringValue(),
+            ZED( ZWST0005_UPDATING )
+          ),
+          WARN_LOC(theLoc)
+        )
+      );
+    }
+    theCacheResults = false;
+    return;
+  }
+
+  if (lExplicitCacheRequest)
+  {
+    if (isSequential() || !isDeterministic())
+    {
+      if (lExplicitCacheRequest)
+      {
+        diag->add_warning(
+          NEW_XQUERY_WARNING(
+            zwarn::ZWST0006_CACHING_MIGHT_NOT_BE_INTENDED,
+            WARN_PARAMS(
+              getName()->getStringValue(),
+              (isSequential()?"sequential":"non-deterministic")
+            ),
+            WARN_LOC(theLoc)
+          )
+        );
+      }
+    }
+    theCacheResults = true;
+    return;
+  }
+
+  if (!lExplicitCacheRequest)
+  {
+    if (!isRecursive())
+    {
+      theCacheResults = false;
+      return;
+    }
+  }
+
+  theCacheResults = true;
+}
+
+/*******************************************************************************
+
+********************************************************************************/
+PlanIter_t user_function::codegen(
+      CompilerCB* cb,
+      static_context* sctx,
+      const QueryLoc& loc,
+      std::vector<PlanIter_t>& argv,
+      AnnotationHolder& ann) const
+{
+  return new UDFunctionCallIterator(sctx, loc, argv, this);
 }
 
 

=== modified file 'src/functions/udf.h'
--- src/functions/udf.h	2011-10-18 17:37:41 +0000
+++ src/functions/udf.h	2011-11-03 02:56:35 +0000
@@ -25,6 +25,11 @@
 namespace zorba 
 {
 
+  namespace store
+  {
+    class Index;
+  }
+
 
 /*******************************************************************************
   A udf with params $x1, $x2, ..., $xn and a body_expr is translated into a
@@ -102,6 +107,9 @@
   uint32_t                    thePlanStateSize;
   std::vector<ArgVarRefs>     theArgVarsRefs;
 
+  mutable store::Index*       theCache;
+  mutable bool                theCacheResults;
+
 public:
   SERIALIZABLE_CLASS(user_function)
   user_function(::zorba::serialization::Archiver& ar);
@@ -160,12 +168,21 @@
 
   const std::vector<ArgVarRefs>& getArgVarsRefs() const;
 
+  store::Index* getCache() const;
+
+  void setCache(store::Index* aCache);
+
+  bool cacheResults() const;
+
   PlanIter_t codegen(
         CompilerCB* cb,
         static_context* sctx,
         const QueryLoc& loc,
         std::vector<PlanIter_t>& argv,
         AnnotationHolder& ann) const;
+
+  void
+  computeResultCaching(XQueryDiagnostics*) const;
 };
 
 

=== modified file 'src/runtime/core/fncall_iterator.cpp'
--- src/runtime/core/fncall_iterator.cpp	2011-10-18 17:37:41 +0000
+++ src/runtime/core/fncall_iterator.cpp	2011-11-03 02:56:35 +0000
@@ -48,6 +48,10 @@
 
 #include "util/string_util.h"
 
+#include "store/api/index.h"
+#include "store/api/store.h"
+#include "store/api/iterator_factory.h"
+
 #ifdef ZORBA_WITH_DEBUGGER
 #include "debugger/debugger_commons.h"
 
@@ -93,7 +97,8 @@
   thePlanState(NULL),
   thePlanStateSize(0),
   theLocalDCtx(NULL),
-  thePlanOpen(false)
+  thePlanOpen(false),
+  theCache(0)
 {
 }
 
@@ -198,6 +203,113 @@
 /*******************************************************************************
 
 ********************************************************************************/
+bool UDFunctionCallIterator::isCached() const
+{
+  return theUDF->cacheResults();
+}
+
+/*******************************************************************************
+
+********************************************************************************/
+void UDFunctionCallIterator::createCache(
+    PlanState& planState,
+    UDFunctionCallIteratorState* state)
+{
+  store::Index_t lIndex = theUDF->getCache();
+  if (!lIndex && theUDF->cacheResults())
+  {
+    lIndex = planState.theGlobalDynCtx->getIndex(theUDF->getName());
+    if (!lIndex)
+    {
+      const signature& sig = theUDF->getSignature();
+
+      csize numArgs = theChildren.size();
+
+      store::IndexSpecification lSpec;
+      lSpec.theNumKeyColumns = numArgs;
+      lSpec.theKeyTypes.resize(numArgs);
+      lSpec.theCollations.resize(numArgs);
+      lSpec.theIsTemp = true;
+      lSpec.theIsUnique = true;
+      for (csize i = 0; i < numArgs; ++i)
+      {
+        lSpec.theKeyTypes[i] = sig[i]->get_qname().getp();
+      }
+      lIndex = GENV_STORE.createIndex(theUDF->getName(), lSpec, 0);
+      planState.theGlobalDynCtx->bindIndex(theUDF->getName(), lIndex);
+      theUDF->setCache(lIndex.getp()); // cache the cache in the function itself
+    }
+  }
+  state->theCache = lIndex.getp();
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool UDFunctionCallIterator::probeCache(
+    PlanState& planState,
+    UDFunctionCallIteratorState* state,
+    store::Item_t& result,
+    std::vector<store::Item_t>& aKey) const
+{
+  if (!state->theCache)
+    return false;
+
+  store::IndexCondition_t lCond =
+    state->theCache->createCondition(store::IndexCondition::POINT_VALUE);
+
+  std::vector<store::Iterator_t>::iterator lIter 
+    = state->theArgWrappers.begin();
+  for (; lIter != state->theArgWrappers.end(); ++lIter)
+  {
+    store::Iterator_t& argWrapper = (*lIter);
+    store::Item_t lArg;
+    argWrapper->next(lArg); // guaranteed to have exactly one result
+    aKey.push_back(lArg);
+    lCond->pushItem(lArg);
+  }
+  store::IndexProbeIterator_t lCacheHit = 
+    GENV_STORE.getIteratorFactory()->createIndexProbeIterator(state->theCache);
+  lCacheHit->init(lCond);
+  lCacheHit->open();
+  if (lCacheHit->next(result))
+  {
+    return true;
+  }
+  else
+  {
+    // reset the arguments for evaluating the function if not cached
+    resetImpl(planState);
+    return false;
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void UDFunctionCallIterator::insertCacheEntry(
+  UDFunctionCallIteratorState* state,
+  std::vector<store::Item_t>& aKey,
+  store::Item_t& aValue) const
+{
+  if (state->theCache)
+  {
+    std::auto_ptr<store::IndexKey> k(new store::IndexKey());
+    store::IndexKey* k2 = k.get();
+    k->theItems = aKey;
+    store::Item_t lTmp = aValue; // insert will eventually transfer the Item_t
+    if (!state->theCache->insert(k2, lTmp))
+    {
+      k.release();
+    }
+  }
+}
+
+/*******************************************************************************
+
+********************************************************************************/
 void UDFunctionCallIterator::openImpl(PlanState& planState, uint32_t& offset)
 {
   UDFunctionCallIteratorState* state;
@@ -229,6 +341,11 @@
   // the plan state (but not the state block) and dynamic context.
   state->open(planState, theUDF);
 
+  // if the results of the function should be cached (prereq: atomic in and out)
+  // this functions stores an index in the dynamic context that contains
+  // the cached results. The name of the index is the name of the function.
+  createCache(planState, state);
+
   // Create a wrapper over each subplan that computes an argument expr, if the
   // associated param is actually used anywhere in the function body.
   csize numArgs = theChildren.size();
@@ -301,6 +418,9 @@
 {
   try
   {
+    std::vector<store::Item_t> lKey;
+    bool lCacheHit;
+
     UDFunctionCallIteratorState* state;
     DEFAULT_STACK_INIT(UDFunctionCallIteratorState, state, planState);
 
@@ -314,15 +434,17 @@
       state->thePlanOpen = true;
     }
 
-    // Bind the args.
+    // check if there is a cache and the result is already in the cache
+    lCacheHit = probeCache(planState, state, result, lKey);
+
+    // if not in the cache, we bind the arguments to the function
+    if (!lCacheHit)
     {
       const std::vector<ArgVarRefs>& argsRefs = theUDF->getArgVarsRefs();
       std::vector<ArgVarRefs>::const_iterator argsRefsIte = argsRefs.begin();
       std::vector<ArgVarRefs>::const_iterator argsRefsEnd = argsRefs.end();
 
-      std::vector<store::Iterator_t>::iterator argWrapsIte = 
-      state->theArgWrappers.begin();
-
+      std::vector<store::Iterator_t>::iterator argWrapsIte = state->theArgWrappers.begin();
       for (; argsRefsIte != argsRefsEnd; ++argsRefsIte, ++argWrapsIte)
       {
         store::Iterator_t& argWrapper = (*argWrapsIte);
@@ -343,25 +465,34 @@
       }
     }
 
-#ifdef ZORBA_WITH_DEBUGGER
-    DEBUGGER_PUSH_FRAME;
-#endif
-
-    while (consumeNext(result, state->thePlan, *state->thePlanState))
-    {
-#ifdef ZORBA_WITH_DEBUGGER
-      DEBUGGER_POP_FRAME;
-#endif
-      STACK_PUSH(true, state);
-
-#ifdef ZORBA_WITH_DEBUGGER
-      DEBUGGER_PUSH_FRAME;
-#endif
-    }
-
-#ifdef ZORBA_WITH_DEBUGGER
-    DEBUGGER_POP_FRAME;
-#endif
+    if (lCacheHit)
+    {
+      STACK_PUSH(true, state);
+    }
+    else
+    {
+#ifdef ZORBA_WITH_DEBUGGER
+      DEBUGGER_PUSH_FRAME;
+#endif
+      while (consumeNext(result, state->thePlan, *state->thePlanState))
+      {
+#ifdef ZORBA_WITH_DEBUGGER
+      DEBUGGER_POP_FRAME;
+#endif
+
+      insertCacheEntry(state, lKey, result);
+      STACK_PUSH(true, state);
+
+#ifdef ZORBA_WITH_DEBUGGER
+      DEBUGGER_PUSH_FRAME;
+#endif
+      }
+
+#ifdef ZORBA_WITH_DEBUGGER
+      DEBUGGER_POP_FRAME;
+#endif
+
+    }
 
     STACK_END(state);
 

=== modified file 'src/runtime/core/fncall_iterator.h'
--- src/runtime/core/fncall_iterator.h	2011-10-18 17:37:41 +0000
+++ src/runtime/core/fncall_iterator.h	2011-11-03 02:56:35 +0000
@@ -79,6 +79,7 @@
   dynamic_context              * theLocalDCtx;
   bool                           thePlanOpen;
   std::vector<store::Iterator_t> theArgWrappers;
+  store::Index                 * theCache;
 
   UDFunctionCallIteratorState();
 
@@ -130,6 +131,8 @@
 
   void setDynamic() { theIsDynamic = true; }
 
+  bool isCached() const;
+
   void accept(PlanIterVisitor& v) const;
 
   void openImpl(PlanState& planState, uint32_t& offset);
@@ -139,6 +142,22 @@
   void closeImpl(PlanState& planState);
 
   bool nextImpl(store::Item_t& result, PlanState& planState) const;
+
+protected:
+  void createCache(
+    PlanState& planState,
+    UDFunctionCallIteratorState* state);
+
+  bool probeCache(
+    PlanState& planState,
+    UDFunctionCallIteratorState* state,
+    store::Item_t& result,
+    std::vector<store::Item_t>& aKey) const;
+
+  void insertCacheEntry(
+    UDFunctionCallIteratorState* state,
+    std::vector<store::Item_t>& aKey,
+    store::Item_t& aValue) const;
 };
 
 

=== modified file 'src/runtime/indexing/index_ddl.cpp'
--- src/runtime/indexing/index_ddl.cpp	2011-10-05 17:49:48 +0000
+++ src/runtime/indexing/index_ddl.cpp	2011-11-03 02:56:35 +0000
@@ -634,7 +634,12 @@
       if (state->theIndexDecl->getKeyExpressions().size() != numChildren-1)
       {
         RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-        ERROR_PARAMS(qnameItem->getStringValue()));
+        ERROR_PARAMS(
+          qnameItem->getStringValue(),
+          "index",
+          numChildren-1,
+          state->theIndexDecl->getKeyExpressions().size())
+        );
       }
 
       state->theIndex = (state->theIndexDecl->isTemp() ?
@@ -800,7 +805,12 @@
           numChildren != 2)
       {
         RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-        ERROR_PARAMS(qnameItem->getStringValue()));
+        ERROR_PARAMS(
+          qnameItem->getStringValue(),
+          "index",
+          numChildren-1,
+          state->theIndexDecl->getKeyExpressions().size())
+        );
       }
 
       state->theIndex = (state->theIndexDecl->isTemp() ?
@@ -973,13 +983,23 @@
     if ((numChildren-1) % 6 != 0)
     {
       RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-      ERROR_PARAMS(qname->getStringValue()));
+      ERROR_PARAMS(
+        qname->getStringValue(),
+        "index",
+        numChildren-1,
+        "multiple of 6"
+      ));
     }
 
     if (indexDecl->getKeyExpressions().size() * 6 > numChildren-1)
     {
       RAISE_ERROR(zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS, loc,
-      ERROR_PARAMS(qname->getStringValue()));
+      ERROR_PARAMS(
+        qname->getStringValue(),
+        "index",
+        numChildren-1,
+        indexDecl->getKeyExpressions().size() * 6
+      ));
     }
 
     state->theIndex = (indexDecl->isTemp() ?

=== modified file 'src/runtime/store/maps_impl.cpp'
--- src/runtime/store/maps_impl.cpp	2011-08-17 23:04:48 +0000
+++ src/runtime/store/maps_impl.cpp	2011-11-03 02:56:35 +0000
@@ -241,7 +241,11 @@
   {
     throw XQUERY_EXCEPTION(
       zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS,
-      ERROR_PARAMS( lQName->getStringValue() ),
+      ERROR_PARAMS(
+        lQName->getStringValue(),
+        "map",
+        theChildren.size() - 1,
+        lSpec.getNumColumns() ),
       ERROR_LOC( loc )
     );
   }
@@ -316,7 +320,7 @@
   {
     throw XQUERY_EXCEPTION(
       zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS,
-      ERROR_PARAMS( lQName->getStringValue() ),
+      ERROR_PARAMS( lQName->getStringValue(), theChildren.size() - 2, lSpec.getNumColumns() ),
       ERROR_LOC( loc )
     );
   }
@@ -386,7 +390,12 @@
   {
     throw XQUERY_EXCEPTION(
       zerr::ZDDY0025_INDEX_WRONG_NUMBER_OF_PROBE_ARGS,
-      ERROR_PARAMS( lQName->getStringValue() ),
+      ERROR_PARAMS(
+        lQName->getStringValue(),
+        "map",
+        theChildren.size() - 1,
+        lSpec.getNumColumns()
+      ),
       ERROR_LOC( loc )
     );
   }

=== modified file 'src/runtime/visitors/printer_visitor_impl.cpp'
--- src/runtime/visitors/printer_visitor_impl.cpp	2011-10-11 01:05:23 +0000
+++ src/runtime/visitors/printer_visitor_impl.cpp	2011-11-03 02:56:35 +0000
@@ -1209,7 +1209,23 @@
 
 #undef TYPED_VAL_CMP
 
-  PRINTER_VISITOR_DEFINITION (UDFunctionCallIterator)
+  void PrinterVisitor::beginVisit ( const UDFunctionCallIterator& a )
+  {
+    thePrinter.startBeginVisit("UDFunctionCallIterator", ++theId);
+    printCommons(  &a, theId );
+    if (a.isCached())
+    {
+      thePrinter.addAttribute("cached", "true");
+    }
+    thePrinter.endBeginVisit( theId);
+  }
+
+  void PrinterVisitor::endVisit ( const UDFunctionCallIterator& )
+  {
+    thePrinter.startEndVisit();
+    thePrinter.endEndVisit();
+  }
+
   PRINTER_VISITOR_DEFINITION (ExtFunctionCallIterator)
   PRINTER_VISITOR_DEFINITION (FnBooleanIterator)
   PRINTER_VISITOR_DEFINITION (OrIterator)

=== modified file 'src/store/api/index.h'
--- src/store/api/index.h	2011-09-30 14:06:33 +0000
+++ src/store/api/index.h	2011-11-03 02:56:35 +0000
@@ -389,6 +389,11 @@
    * Returns all keys stored in this index
    */
   virtual KeyIterator_t keys() const = 0;
+
+  virtual bool insert(
+        store::IndexKey*& key,
+        store::Item_t& item,
+        bool multikey = false) = 0;
 };
 
 

=== modified file 'src/store/naive/simple_store.cpp'
--- src/store/naive/simple_store.cpp	2011-10-10 12:56:57 +0000
+++ src/store/naive/simple_store.cpp	2011-11-03 02:56:35 +0000
@@ -557,6 +557,9 @@
     store::Iterator* aSourceIter,
     ulong aNumColumns)
 {
+  if (!aSourceIter)
+    return;
+
   store::Item_t domainItem;
   store::IndexKey* key = NULL;
 

=== added file 'test/rbkt/ExpCompilerResults/IterPlan/zorba/udf/udf-fib-rec.iter'
--- test/rbkt/ExpCompilerResults/IterPlan/zorba/udf/udf-fib-rec.iter	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpCompilerResults/IterPlan/zorba/udf/udf-fib-rec.iter	2011-11-03 02:56:35 +0000
@@ -0,0 +1,42 @@
+Iterator tree for main query:
+<UDFunctionCallIterator cached="true">
+  <SingletonIterator value="xs:integer(100)"/>
+</UDFunctionCallIterator>
+
+Iterator tree for local:fib:
+<flwor::FLWORIterator>
+  <ForVariable name="n">
+    <LetVarIterator varname="n"/>
+  </ForVariable>
+  <ReturnClause>
+    <IfThenElseIterator>
+      <TypedValueCompareIterator_INTEGER>
+        <ForVarIterator varname="n"/>
+        <SingletonIterator value="xs:integer(0)"/>
+      </TypedValueCompareIterator_INTEGER>
+      <SingletonIterator value="xs:integer(0)"/>
+      <IfThenElseIterator>
+        <TypedValueCompareIterator_INTEGER>
+          <ForVarIterator varname="n"/>
+          <SingletonIterator value="xs:integer(1)"/>
+        </TypedValueCompareIterator_INTEGER>
+        <SingletonIterator value="xs:integer(1)"/>
+        <SpecificNumArithIterator_AddOperation_INTEGER>
+          <UDFunctionCallIterator cached="true">
+            <SpecificNumArithIterator_SubtractOperation_INTEGER>
+              <ForVarIterator varname="n"/>
+              <SingletonIterator value="xs:integer(1)"/>
+            </SpecificNumArithIterator_SubtractOperation_INTEGER>
+          </UDFunctionCallIterator>
+          <UDFunctionCallIterator cached="true">
+            <SpecificNumArithIterator_SubtractOperation_INTEGER>
+              <ForVarIterator varname="n"/>
+              <SingletonIterator value="xs:integer(2)"/>
+            </SpecificNumArithIterator_SubtractOperation_INTEGER>
+          </UDFunctionCallIterator>
+        </SpecificNumArithIterator_AddOperation_INTEGER>
+      </IfThenElseIterator>
+    </IfThenElseIterator>
+  </ReturnClause>
+</flwor::FLWORIterator>
+

=== added file 'test/rbkt/ExpQueryResults/zorba/udf/udf-fib-rec.xml.res'
--- test/rbkt/ExpQueryResults/zorba/udf/udf-fib-rec.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/udf/udf-fib-rec.xml.res	2011-11-03 02:56:35 +0000
@@ -0,0 +1,1 @@
+3736710778780434371

=== added file 'test/rbkt/Queries/zorba/udf/udf-fib-rec.xq'
--- test/rbkt/Queries/zorba/udf/udf-fib-rec.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/udf/udf-fib-rec.xq	2011-11-03 02:56:35 +0000
@@ -0,0 +1,8 @@
+declare function local:fib($n as xs:integer) as xs:integer
+{
+  if ($n eq 0) then 0 
+  else if ($n eq 1) then 1 
+  else local:fib($n - 1) + local:fib($n - 2)
+};
+
+local:fib(100)

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