← Back to team overview

zorba-coders team mailing list archive

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

 

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

Requested reviews:
  Markos Zaharioudakis (markos-za)

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

removed get_is_temp_field_also_for_ptr() method and reduced use of set_is_temp_field_one_level(), 
changed the serializat5ion of maps (std and hashmaps)
added --serialize-plan option to zorbacmd
-- 
https://code.launchpad.net/~zorba-coders/zorba/serializer/+merge/102747
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'bin/zorbacmd.cpp'
--- bin/zorbacmd.cpp	2012-04-17 15:35:48 +0000
+++ bin/zorbacmd.cpp	2012-04-19 19:25:27 +0000
@@ -73,29 +73,25 @@
 OneToOneURIMapper theStopWordsMapper(EntityData::STOP_WORDS);
 OneToOneURIMapper theThesaurusMapper(EntityData::THESAURUS);
 #endif
+
+
 class URIMapperSerializationCallback : public SerializationCallback
 {
-  private:
-    std::vector<URIMapper*> theURIMappers;
-
-  public:
-    void addURIMapper(URIMapper* lURIMapper)
-    {
-      theURIMappers.push_back(lURIMapper);
-    }
-
-    virtual URIMapper*
-    getURIMapper(size_t i) const
-    {   
-      return theURIMappers[i];
-    }   
+private:
+  std::vector<URIMapper*>   theURIMappers;
+
+public:
+  void addURIMapper(URIMapper* mapper) { theURIMappers.push_back(mapper); }
+
+  URIMapper* getURIMapper(size_t i) const { return theURIMappers[i]; }
 };
+
+
 URIMapperSerializationCallback theSerializationCallback;
 
+
 bool
-populateStaticContext(
-    zorba::StaticContext_t& aStaticContext,
-    const ZorbaCMDProperties& aProperties)
+populateStaticContext(zorba::StaticContext_t& sctx, const ZorbaCMDProperties& props)
 {
   try
   {
@@ -105,85 +101,107 @@
     // 3. current working directory
     {
       std::vector<String> lModulePath;
-      PathUtil::setPathsOnContext(aProperties, aStaticContext);
-    }
-
-    if (aProperties.boundarySpace().size() != 0 )
-      aStaticContext->setBoundarySpacePolicy(
-                         (aProperties.boundarySpace().compare("preserve") == 0
-                          ? preserve_space
-                          : strip_space));
-
-    if (aProperties.constructionMode().size() != 0)
-      aStaticContext->setConstructionMode( aProperties.boundarySpace().compare("preserve") == 0
-                                           ? preserve_cons
-                                           : strip_cons );
-
-    if (aProperties.orderingMode().size() != 0 )
-    {
-      aStaticContext->setOrderingMode( aProperties.boundarySpace().compare("ordered") == 0
-                                       ? ordered
-                                       : unordered );
-    }
-
-    if (aProperties.baseUri().size() != 0 )
-      aStaticContext->setBaseURI( aProperties.baseUri() );
+      PathUtil::setPathsOnContext(props, sctx);
+    }
+
+    if (props.boundarySpace().size() != 0 )
+    {
+      sctx->setBoundarySpacePolicy(props.boundarySpace().compare("preserve") == 0 ?
+                                   preserve_space : 
+                                   strip_space);
+    }
+
+    if (props.constructionMode().size() != 0)
+    {
+      sctx->setConstructionMode(props.boundarySpace().compare("preserve") == 0 ? 
+                                preserve_cons :
+                                strip_cons);
+    }
+
+    if (props.orderingMode().size() != 0 )
+    {
+      sctx->setOrderingMode(props.boundarySpace().compare("ordered") == 0 ?
+                            ordered :
+                            unordered);
+    }
+
+    if (props.baseUri().size() != 0 )
+      sctx->setBaseURI( props.baseUri() );
   }
-  catch (zorba::ZorbaException const& ze) {
+  catch (const zorba::ZorbaException& ze) 
+  {
     std::cerr << ze << std::endl;
     return false;
   }
 
-  if (aProperties.defaultCollation().size() != 0 )
-  {
-    try {
-      aStaticContext->addCollation( aProperties.defaultCollation() );
-    } catch (zorba::ZorbaException const&) {
-      std::cerr << "the given collation {" << aProperties.defaultCollation() << "} is not a valid collation." << std::endl;
-      return false;
-    }
-    aStaticContext->setDefaultCollation( aProperties.defaultCollation() );
-  }
-
-  {
-    ZorbaCMDProperties::Options_t::const_iterator lIter = aProperties.optionsBegin();
-    ZorbaCMDProperties::Options_t::const_iterator end = aProperties.optionsEnd();
-    for (; lIter != end; ++lIter)
-    {
-      try {
-        zorba::Zorba* lZorba = zorba::Zorba::getInstance(0);
-        Item lQName = lZorba->getItemFactory()->createQName(lIter->clark_qname);
-        aStaticContext->declareOption(lQName, lIter->value);
-      } catch (zorba::ZorbaException const& /* e */) {
-        std::cerr << "unable to set static context option with qname "
-                  << lIter->clark_qname << std::endl;
-        return false;
-      }
-    }
-  }
+  if (props.defaultCollation().size() != 0 )
+  {
+    try 
+    {
+      sctx->addCollation( props.defaultCollation() );
+    }
+    catch (zorba::ZorbaException const&)
+    {
+      std::cerr << "the given collation {" << props.defaultCollation()
+                << "} is not a valid collation." << std::endl;
+      return false;
+    }
+
+    sctx->setDefaultCollation( props.defaultCollation() );
+  }
+
+  ZorbaCMDProperties::Options_t::const_iterator lIter = props.optionsBegin();
+  ZorbaCMDProperties::Options_t::const_iterator end = props.optionsEnd();
+  for (; lIter != end; ++lIter)
+  {
+    try 
+    {
+      zorba::Zorba* lZorba = zorba::Zorba::getInstance(0);
+      Item lQName = lZorba->getItemFactory()->createQName(lIter->clark_qname);
+      sctx->declareOption(lQName, lIter->value);
+    }
+    catch (zorba::ZorbaException const& /* e */) 
+    {
+      std::cerr << "unable to set static context option with qname "
+                << lIter->clark_qname << std::endl;
+      return false;
+    }
+  }
+
 #ifndef ZORBA_NO_FULL_TEXT
   {
-    ZorbaCMDProperties::FullText_t::const_iterator lIter = aProperties.stopWordsBegin();
-    ZorbaCMDProperties::FullText_t::const_iterator end = aProperties.stopWordsEnd();
-    for (; lIter != end; ++lIter) {
+    ZorbaCMDProperties::FullText_t::const_iterator lIter = props.stopWordsBegin();
+    ZorbaCMDProperties::FullText_t::const_iterator end = props.stopWordsEnd();
+    for (; lIter != end; ++lIter) 
+    {
       theStopWordsMapper.addMapping(lIter->uri, lIter->value);
     }
-    if(aProperties.executePlan()) {
+
+    if (props.serializePlan()) 
+    {
       theSerializationCallback.addURIMapper(&theStopWordsMapper);
-    } else {
-      aStaticContext->registerURIMapper(&theStopWordsMapper);
+    }
+    else
+    {
+      sctx->registerURIMapper(&theStopWordsMapper);
     }
   }
+
   {
-    ZorbaCMDProperties::FullText_t::const_iterator lIter = aProperties.thesaurusBegin();
-    ZorbaCMDProperties::FullText_t::const_iterator end = aProperties.thesaurusEnd();
-    for (; lIter != end; ++lIter) {
+    ZorbaCMDProperties::FullText_t::const_iterator lIter = props.thesaurusBegin();
+    ZorbaCMDProperties::FullText_t::const_iterator end = props.thesaurusEnd();
+    for (; lIter != end; ++lIter) 
+    {
       theThesaurusMapper.addMapping(lIter->uri, lIter->value);
     }
-    if(aProperties.executePlan()) {
+
+    if (props.serializePlan()) 
+    {
       theSerializationCallback.addURIMapper(&theStopWordsMapper);
-    } else {
-      aStaticContext->registerURIMapper(&theThesaurusMapper);
+    }
+    else
+    {
+      sctx->registerURIMapper(&theThesaurusMapper);
     }
   }
 #endif
@@ -193,20 +211,21 @@
 
 bool
 populateDynamicContext(
-  Zorba* zorba,
-  zorba::DynamicContext* aDynamicContext,
-  const ZorbaCMDProperties& aProperties)
+    Zorba* zorba,
+    zorba::DynamicContext* aDynamicContext,
+    const ZorbaCMDProperties& props)
 {
-  if ( aProperties.contextItem().size() != 0 ) {
+  if ( props.contextItem().size() != 0 ) 
+  {
     XmlDataManager* lXmlMgr = zorba->getXmlDataManager();
-    std::ifstream lInStream(aProperties.contextItem().c_str());
+    std::ifstream lInStream(props.contextItem().c_str());
     Item lDoc = lXmlMgr->parseXML(lInStream);
     aDynamicContext->setContextItem(lDoc);
   }
 
   ZorbaCMDProperties::ExternalVars_t::const_iterator lIter;
-  ZorbaCMDProperties::ExternalVars_t::const_iterator end = aProperties.externalVarsEnd();
-  for (lIter = aProperties.externalVarsBegin();
+  ZorbaCMDProperties::ExternalVars_t::const_iterator end = props.externalVarsEnd();
+  for (lIter = props.externalVarsBegin();
        lIter != end;
        ++lIter)
   {
@@ -241,20 +260,20 @@
 bool
 createSerializerOptions(
     Zorba_SerializerOptions_t& lSerOptions,
-    const ZorbaCMDProperties& aProperties)
+    const ZorbaCMDProperties& props)
 {
-  if ( aProperties.indent() )
+  if ( props.indent() )
     lSerOptions.indent = ZORBA_INDENT_YES;
 
-  if ( aProperties.omitXmlDeclaration() )
+  if ( props.omitXmlDeclaration() )
     lSerOptions.omit_xml_declaration = ZORBA_OMIT_XML_DECLARATION_YES;
 
-  if ( aProperties.byteOrderMark() )
+  if ( props.byteOrderMark() )
     lSerOptions.byte_order_mark = ZORBA_BYTE_ORDER_MARK_YES;
 
-  if ( aProperties.serializeHtml() )
+  if ( props.serializeHtml() )
     lSerOptions.ser_method = ZORBA_SERIALIZATION_METHOD_HTML;
-  else if ( aProperties.serializeText() )
+  else if ( props.serializeText() )
     lSerOptions.ser_method = ZORBA_SERIALIZATION_METHOD_TEXT;
 
   return true;
@@ -265,7 +284,7 @@
 /// or if not requested, infers -f for file:// queries.
 /// Returns an URI or the empty string.
 std::string
-parseFileURI (bool asPath, const std::string &str)
+parseFileURI(bool asPath, const std::string &str)
 {
   if (asPath)
     return str;
@@ -531,11 +550,26 @@
   unsigned long lNumExecutions = properties.multiple();
   bool lIndent = properties.indent();
   bool doTiming = properties.timing();
-  bool compilePlan = properties.compilePlan();
-  bool executePlan = properties.executePlan();
+  bool serializePlan = properties.serializePlan();
   std::ostringstream lOut;
   Zorba_CompilerHints lHints;
 
+  std::auto_ptr<std::fstream> planFile;
+  std::fstream* planFilep = NULL;
+
+  if (serializePlan)
+  {
+    std::string planFilePath = qfilepath;
+    planFilePath += ".plan";
+    planFile.reset(new std::fstream(planFilePath.c_str(), 
+                                    std::fstream::in | 
+                                    std::fstream::out |
+                                    std::fstream::trunc |
+                                    std::fstream::binary));
+    planFilep = planFile.get();
+    assert(planFilep->good());
+  }
+
   // default is O1 in the Zorba_CompilerHints constructor
   if (properties.optimizationLevel() == "O0") 
   {
@@ -569,7 +603,14 @@
 
   for (unsigned long i = 0; i < lNumExecutions; ++i)
   {
-    // Perform compilation and create dynamic context only once, unless timing is needed
+    // start the total timer
+    if (doTiming)
+      timing.startTimer(TimingInfo::TOTAL_TIMER, i);
+    
+    //
+    // Compile the query
+    // Compilation is done only once, unless timing is needed
+    //
     if (doTiming || i == 0)
     {
       // go back to the beginning of the stream
@@ -577,38 +618,33 @@
       qfile.seekg(0);
       assert (qfile.tellg() >= 0);
 
-      if (doTiming)
-      {
-        // start the total timer
-        timing.startTimer(TimingInfo::TOTAL_TIMER, i);
-      }
-
-      //
-      // Compile the query
-      //
       try
       {
+        // start the compilation timer
         if (doTiming)
-        {
-          // start the compilation timer
           timing.startTimer(TimingInfo::COMP_TIMER, i);
-        }
 
+        // Create and compile the query
         query = zorbaInstance->createQuery();
         query->registerDiagnosticHandler(&diagnosticHandler);
         query->setFileName(qfilepath);
 
-        if(executePlan) {
-          query->loadExecutionPlan(qfile, &theSerializationCallback);
-        } else {
-          query->compile(qfile, staticContext, lHints);
+        query->compile(qfile, staticContext, lHints);
+
+        // Serialize the execution plan, if requested
+        if (serializePlan) 
+        {
+          planFilep->clear();
+          planFilep->seekp(0);
+
+          query->saveExecutionPlan(*planFilep, ZORBA_USE_BINARY_ARCHIVE);
+
+          planFilep->flush();
         }
 
+        // stop the compilation timer
         if (doTiming)
-        {
-          // stop the compilation timer
           timing.stopTimer(TimingInfo::COMP_TIMER, i);
-        }
       }
       catch (zorba::XQueryException const& qe)
       {
@@ -620,68 +656,69 @@
         std::cerr << ze << std::endl;
         return 12;
       }
-
-      //
-      // Create and populate the dynamic context
-      //
-      zorba::DynamicContext* lDynamicContext = query->getDynamicContext();
-      try
-      {
-        if ( ! populateDynamicContext(zorbaInstance, lDynamicContext, properties) )
-        {
-          properties.printHelp(std::cout);
-          return 21;
-        }
-      }
-      catch (zorba::XQueryException const& qe)
-      {
-        ErrorPrinter::print(qe, std::cerr, properties.printErrorsAsXml(), lIndent);
-        return 22;
-      }
-      catch (zorba::ZorbaException const& ze)
-      {
-        std::cerr << ze << std::endl;
-        return 23;
-      }
     } // if (doTiming || i == 0)
 
-    // libModule assumes compileOnly even if compileOnly is false
+    //
+    // Run the query, unless compileOnly has been requested.
+    //
     if ( ! properties.compileOnly() && ! properties.libModule() )
     {
-      //
-      // Run the query
-      //
       try
       {
+        // start the execution timer
         if (doTiming)
-        {
-          // start the execution timer
           timing.startTimer(TimingInfo::EXEC_TIMER, i);
-        }
-
+
+        // load the execution plan, if requested
+        if (serializePlan) 
+        {
+          planFilep->seekg(0);
+          assert(planFilep->good());
+          
+          query = zorbaInstance->createQuery();
+          query->loadExecutionPlan(*planFilep, &theSerializationCallback);
+        }
+
+        // Populate the dynamic context
+        zorba::DynamicContext* lDynamicContext = query->getDynamicContext();
+        try
+        {
+          if ( ! populateDynamicContext(zorbaInstance, lDynamicContext, properties) )
+          {
+            properties.printHelp(std::cout);
+            return 21;
+          }
+        }
+        catch (zorba::XQueryException const& qe)
+        {
+          ErrorPrinter::print(qe, std::cerr, properties.printErrorsAsXml(), lIndent);
+          return 22;
+        }
+        catch (zorba::ZorbaException const& ze)
+        {
+          std::cerr << ze << std::endl;
+          return 23;
+        }
+
+        // run the query
         if (properties.noSerializer())
         {
           query->executeSAX();
         }
         else
         {
-          if (compilePlan) {
-            query->saveExecutionPlan(outputStream, ZORBA_USE_BINARY_ARCHIVE, SAVE_UNUSED_FUNCTIONS);
-          } else {
-            query->execute(outputStream, &lSerOptions);
-          }
-          if (properties.trailingNl()) {
-            outputStream << std::endl;
-          }
+          query->execute(outputStream, &lSerOptions);
         }
 
+        if (properties.trailingNl()) 
+          outputStream << std::endl;
+ 
+         // stop the execution timer
         if (doTiming)
-        {
-          // stop the execution timer
           timing.stopTimer(TimingInfo::EXEC_TIMER, i);
-        }
 
-        if (i > 0 || lNumExecutions == 1) {
+        if (i > 0 || lNumExecutions == 1) 
+        {
           timing.elapsedLoadWalltime += query->getDocLoadingTime();
           timing.elapsedLoadCputime += query->getDocLoadingUserTime();
         }
@@ -698,12 +735,11 @@
       }
     }
 
-    // only delete the docs from the store if timing is needed
+    //
+    // Delete all loaded docs from the store, if timing has been requested
+    //
     if (doTiming)
     {
-      //
-      // Delete all loaded docs from the store
-      //
       XmlDataManager* store = zorbaInstance->getXmlDataManager();
 
       timing.startTimer(TimingInfo::UNLOAD_TIMER, i);
@@ -719,9 +755,11 @@
       }
 
       timing.stopTimer(TimingInfo::UNLOAD_TIMER, i);
+    }
 
+    // stop the total timer
+    if (doTiming)
       timing.stopTimer(TimingInfo::TOTAL_TIMER, i);
-    }
   } // for each execution
 
   return 0;

=== modified file 'bin/zorbacmdproperties_base.h'
--- bin/zorbacmdproperties_base.h	2012-04-17 15:35:48 +0000
+++ bin/zorbacmdproperties_base.h	2012-04-19 19:25:27 +0000
@@ -84,10 +84,10 @@
   bool theTrailingNl;
   std::vector<std::string> theStopWords;
   std::vector<std::string> theThesaurus;
-  bool theCompilePlan;
-  bool theExecutePlan;
+  bool theSerializePlan;
 
-  void initialize () {
+  void initialize () 
+  {
     theTiming = false;
     theSerializeHtml = false;
     theSerializeText = false;
@@ -109,9 +109,9 @@
     theNoLogo = false;
     theTimeout = -1;
     theTrailingNl = false;
-    theCompilePlan = false;
-    theExecutePlan = false;
+    theSerializePlan = false;
   }
+
 public:
   const bool &timing () const { return theTiming; }
   const std::string &outputFile () const { return theOutputFile; }
@@ -151,56 +151,75 @@
   const bool &trailingNl () const { return theTrailingNl; }
   const std::vector<std::string> &stopWords () const { return theStopWords; }
   const std::vector<std::string> &thesaurus () const { return theThesaurus; }
-  const bool &compilePlan () const { return theCompilePlan; }
-  const bool &executePlan () const { return theExecutePlan; }
+  const bool& serializePlan () const { return theSerializePlan; }
 
-  std::string load_argv (int argc, const char **argv) {
+  std::string load_argv (int argc, const char **argv) 
+  {
     if (argv == NULL) return "";
 
     std::string result;
-    for (++argv; *argv != NULL; ++argv) {
+    for (++argv; *argv != NULL; ++argv) 
+    {
       if (strcmp (*argv, "--help") == 0 || strcmp (*argv, "-h") == 0)
         return "!HELP";
+
       else if (strcmp (*argv, "--version") == 0)
         return "!VER";
-      else if (strcmp (*argv, "--timing") == 0 || strncmp (*argv, "-t", 2) == 0) {
+
+      else if (strcmp (*argv, "--timing") == 0 || strncmp (*argv, "-t", 2) == 0) 
+      {
         theTiming = true;
       }
-      else if (strcmp (*argv, "--output-file") == 0 || strncmp (*argv, "-o", 2) == 0) {
+      else if (strcmp (*argv, "--output-file") == 0 || strncmp (*argv, "-o", 2) == 0) 
+      {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+<<<<<<< TREE
         if (*argv == NULL) { result = "No value given for --output-file option"; break; }
 
         init_val (*argv, theOutputFile, d);
+=======
+        if (*argv == NULL) { result = "No value given for --output-file option"; break; }
+        init_val (*argv, theOutputFile, d);
+>>>>>>> MERGE-SOURCE
       }
-      else if (strcmp (*argv, "--serialization-parameter") == 0 || strncmp (*argv, "-z", 2) == 0) {
+      else if (strcmp (*argv, "--serialization-parameter") == 0 || strncmp (*argv, "-z", 2) == 0) 
+      {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
         if (*argv == NULL) { result = "No value given for --serialization-parameter option"; break; }
 
         init_val (*argv, theSerializationParameter, d);
       }
-      else if (strcmp (*argv, "--serialize-html") == 0) {
+      else if (strcmp (*argv, "--serialize-html") == 0) 
+      {
         theSerializeHtml = true;
       }
-      else if (strcmp (*argv, "--serialize-text") == 0) {
+      else if (strcmp (*argv, "--serialize-text") == 0) 
+      {
         theSerializeText = true;
       }
-      else if (strcmp (*argv, "--indent") == 0 || strncmp (*argv, "-i", 2) == 0) {
+      else if (strcmp (*argv, "--indent") == 0 || strncmp (*argv, "-i", 2) == 0) 
+      {
         theIndent = true;
       }
-      else if (strcmp (*argv, "--print-query") == 0) {
+      else if (strcmp (*argv, "--print-query") == 0) 
+      {
         thePrintQuery = true;
       }
-      else if (strcmp (*argv, "--print-errors-as-xml") == 0 || strncmp (*argv, "-x", 2) == 0) {
+      else if (strcmp (*argv, "--print-errors-as-xml") == 0 || strncmp (*argv, "-x", 2) == 0)
+      {
         thePrintErrorsAsXml = true;
       }
-      else if (strcmp (*argv, "--byte-order-mark") == 0) {
+      else if (strcmp (*argv, "--byte-order-mark") == 0) 
+      {
         theByteOrderMark = true;
       }
-      else if (strcmp (*argv, "--omit-xml-declaration") == 0 || strncmp (*argv, "-r", 2) == 0) {
+      else if (strcmp (*argv, "--omit-xml-declaration") == 0 || strncmp (*argv, "-r", 2) == 0) 
+      {
         theOmitXmlDeclaration = true;
       }
+<<<<<<< TREE
       else if (strcmp (*argv, "--base-uri") == 0) {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
@@ -223,6 +242,31 @@
         init_val (*argv, theDefaultCollation, d);
       }
       else if (strcmp (*argv, "--construction-mode") == 0) {
+=======
+      else if (strcmp (*argv, "--base-uri") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --base-uri option"; break; }
+        init_val (*argv, theBaseUri, d);
+      }
+      else if (strcmp (*argv, "--boundary-space") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --boundary-space option"; break; }
+        init_val (*argv, theBoundarySpace, d);
+      }
+      else if (strcmp (*argv, "--default-collation") == 0)
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --default-collation option"; break; }
+        init_val (*argv, theDefaultCollation, d);
+      }
+      else if (strcmp (*argv, "--construction-mode") == 0)
+      {
+>>>>>>> MERGE-SOURCE
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
         if (*argv == NULL) { result = "No value given for --construction-mode option"; break; }
@@ -236,6 +280,7 @@
 
         init_val (*argv, theOrderingMode, d);
       }
+<<<<<<< TREE
       else if (strcmp (*argv, "--multiple") == 0 || strncmp (*argv, "-m", 2) == 0) {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
@@ -251,44 +296,100 @@
         init_val (*argv, theQueriesOrFiles, d);
       }
       else if (strcmp (*argv, "--as-files") == 0 || strncmp (*argv, "-f", 2) == 0) {
+=======
+      else if (strcmp (*argv, "--multiple") == 0 || strncmp (*argv, "-m", 2) == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL)
+        {
+          result = "No value given for --multiple option"; break;
+        }
+        init_val(*argv, theMultiple, d);
+      }
+      else if (strcmp (*argv, "--query") == 0 || strncmp (*argv, "-q", 2) == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL)
+        {
+          result = "No value given for --query option"; break;
+        }
+        init_val (*argv, theQueriesOrFiles, d);
+      }
+      else if (strcmp (*argv, "--as-files") == 0 || strncmp (*argv, "-f", 2) == 0) 
+      {
+>>>>>>> MERGE-SOURCE
         theAsFiles = true;
       }
-      else if (strcmp (*argv, "--external-variable") == 0 || strncmp (*argv, "-e", 2) == 0) {
+      else if (strcmp (*argv, "--external-variable") == 0 || strncmp (*argv, "-e", 2) == 0) 
+      {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+<<<<<<< TREE
         if (*argv == NULL) { result = "No value given for --external-variable option"; break; }
 
         init_val (*argv, theExternalVariable, d);
+=======
+        if (*argv == NULL) 
+        {
+          result = "No value given for --external-variable option"; break;
+        }
+        init_val(*argv, theExternalVariable, d);
+>>>>>>> MERGE-SOURCE
       }
-      else if (strcmp (*argv, "--context-item") == 0) {
+      else if (strcmp (*argv, "--context-item") == 0) 
+      {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+<<<<<<< TREE
         if (*argv == NULL) { result = "No value given for --context-item option"; break; }
 
         init_val (*argv, theContextItem, d);
+=======
+        if (*argv == NULL) 
+        {
+          result = "No value given for --context-item option"; break;
+        }
+        init_val (*argv, theContextItem, d);
+>>>>>>> MERGE-SOURCE
       }
       else if (strcmp (*argv, "--optimization-level") == 0) {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+<<<<<<< TREE
         if (*argv == NULL) { result = "No value given for --optimization-level option"; break; }
 
         init_val (*argv, theOptimizationLevel, d);
+=======
+        if (*argv == NULL)
+        {
+          result = "No value given for --optimization-level option"; break;
+        }
+        init_val(*argv, theOptimizationLevel, d);
+>>>>>>> MERGE-SOURCE
       }
-      else if (strcmp (*argv, "--lib-module") == 0 || strncmp (*argv, "-l", 2) == 0) {
+      else if (strcmp (*argv, "--lib-module") == 0 || strncmp (*argv, "-l", 2) == 0) 
+      {
         theLibModule = true;
       }
-      else if (strcmp (*argv, "--parse-only") == 0) {
+      else if (strcmp (*argv, "--parse-only") == 0) 
+      {
         theParseOnly = true;
       }
-      else if (strcmp (*argv, "--compile-only") == 0) {
+      else if (strcmp (*argv, "--compile-only") == 0) 
+      {
         theCompileOnly = true;
       }
-      else if (strcmp (*argv, "--no-serializer") == 0) {
+      else if (strcmp (*argv, "--no-serializer") == 0) 
+      {
         theNoSerializer = true;
       }
-      else if (strcmp (*argv, "--debug") == 0 || strncmp (*argv, "-d", 2) == 0) {
+      else if (strcmp (*argv, "--debug") == 0 || strncmp (*argv, "-d", 2) == 0) 
+      {
         theDebug = true;
       }
+<<<<<<< TREE
       else if (strcmp (*argv, "--debug-host") == 0 || strncmp (*argv, "-h", 2) == 0) {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
@@ -304,8 +405,33 @@
         init_val (*argv, theDebugPort, d);
       }
       else if (strcmp (*argv, "--no-logo") == 0) {
+=======
+      else if (strcmp (*argv, "--debug-host") == 0 || strncmp (*argv, "-h", 2) == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) 
+        {
+          result = "No value given for --debug-host option"; break;
+        }
+        init_val (*argv, theDebugHost, d);
+      }
+      else if (strcmp (*argv, "--debug-port") == 0 || strncmp (*argv, "-p", 2) == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) 
+        {
+          result = "No value given for --debug-port option"; break;
+        }
+        init_val (*argv, theDebugPort, d);
+      }
+      else if (strcmp (*argv, "--no-logo") == 0) 
+      {
+>>>>>>> MERGE-SOURCE
         theNoLogo = true;
       }
+<<<<<<< TREE
       else if (strcmp (*argv, "--timeout") == 0) {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
@@ -349,8 +475,48 @@
         init_val (*argv, theOption, d);
       }
       else if (strcmp (*argv, "--trailing-nl") == 0) {
+=======
+      else if (strcmp (*argv, "--timeout") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --timeout option"; break; }
+        init_val (*argv, theTimeout, d);
+      }
+      else if (strcmp (*argv, "--uri-path") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --uri-path option"; break; }
+        init_val (*argv, theUriPath, d);
+      }
+      else if (strcmp (*argv, "--lib-path") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --lib-path option"; break; }
+        init_val (*argv, theLibPath, d);
+      }
+      else if (strcmp (*argv, "--module-path") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --module-path option"; break; }
+        init_val (*argv, theModulePath, d);
+      }
+      else if (strcmp (*argv, "--option") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --option option"; break; }
+        init_val (*argv, theOption, d);
+      }
+      else if (strcmp (*argv, "--trailing-nl") == 0) 
+      {
+>>>>>>> MERGE-SOURCE
         theTrailingNl = true;
       }
+<<<<<<< TREE
       else if (strcmp (*argv, "--stop-words") == 0) {
         int d = 2;
         if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
@@ -372,11 +538,37 @@
         theExecutePlan = true;
       }
       else if (strcmp (*argv, "--") == 0) {
+=======
+      else if (strcmp (*argv, "--stop-words") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --stop-words option"; break; }
+        init_val (*argv, theStopWords, d);
+      }
+      else if (strcmp (*argv, "--thesaurus") == 0) 
+      {
+        int d = 2;
+        if ((*argv) [1] == '-' || (*argv) [2] == '\0') { d = 0; ++argv; }
+        if (*argv == NULL) { result = "No value given for --thesaurus option"; break; }
+        init_val (*argv, theThesaurus, d);
+      }
+      else if (strcmp (*argv, "--serialize-plan") == 0 || strncmp (*argv, "-s", 2) == 0)
+      {
+        theSerializePlan = true;
+      }
+      else if (strcmp (*argv, "--") == 0)
+      {
+>>>>>>> MERGE-SOURCE
         copy_args (++argv);
         break;
-      } else if ((*argv) [0] == '-') {
+      }
+      else if ((*argv) [0] == '-')
+      {
         result = "unknown command line option "; result += *argv; break; 
-      } else {
+      }
+      else
+      {
         copy_args (argv);
         break;
       }

=== modified file 'src/api/dynamiccontextimpl.cpp'
--- src/api/dynamiccontextimpl.cpp	2012-04-16 20:56:43 +0000
+++ src/api/dynamiccontextimpl.cpp	2012-04-19 19:25:27 +0000
@@ -161,8 +161,8 @@
 
   if (theQuery != NULL)
   {
-    std::map<short, static_context_t>& lMap = theQuery->theCompilerCB->theSctxMap;
-    std::map<short, static_context_t>::const_iterator ite;
+    CompilerCB::SctxMap& lMap = theQuery->theCompilerCB->theSctxMap;
+    CompilerCB::SctxMap::const_iterator ite;
     for (ite = lMap.begin(); ite != lMap.end(); ++ite)
     {
       var = ite->second->lookup_var(qname, QueryLoc::null, zerr::ZXQP0000_NO_ERROR);

=== modified file 'src/api/staticcontextimpl.h'
--- src/api/staticcontextimpl.h	2012-04-17 15:35:48 +0000
+++ src/api/staticcontextimpl.h	2012-04-19 19:25:27 +0000
@@ -52,7 +52,7 @@
 protected:
   static_context_t                    theCtx;
 
-  std::map<short, static_context_t>   theSctxMap;
+  std::map<int, static_context_t>     theSctxMap;
 
   DiagnosticHandler                 * theDiagnosticHandler;
   bool                                theUserDiagnosticHandler;

=== modified file 'src/api/xqueryimpl.cpp'
--- src/api/xqueryimpl.cpp	2012-04-16 20:56:43 +0000
+++ src/api/xqueryimpl.cpp	2012-04-19 19:25:27 +0000
@@ -553,7 +553,7 @@
   theStaticContext->set_entity_retrieval_uri(url);
 
   theCompilerCB->theRootSctx = theStaticContext;
-  const short sctxid = (short)theCompilerCB->theSctxMap.size() + 1;
+  int sctxid = (int)theCompilerCB->theSctxMap.size() + 1;
   (theCompilerCB->theSctxMap)[sctxid] = theStaticContext;
 
   // Set the compiler config.
@@ -686,7 +686,7 @@
     clone->theCompilerCB->theRootSctx = clone->theStaticContext;
     clone->theCompilerCB->theSctxMap = theCompilerCB->theSctxMap;
 
-    const short sctxid = (short)clone->theCompilerCB->theSctxMap.size() + 1;
+    int sctxid = (int)clone->theCompilerCB->theSctxMap.size() + 1;
     (clone->theCompilerCB->theSctxMap)[sctxid] = theStaticContext;
 
     clone->thePlanProxy = thePlanProxy;
@@ -746,11 +746,8 @@
 
     std::vector<var_expr_t> lVars;
 
-    std::map<short, static_context_t>::const_iterator lIte = 
-    theCompilerCB->theSctxMap.begin();
-
-    std::map<short, static_context_t>::const_iterator lEnd = 
-    theCompilerCB->theSctxMap.end();
+    CompilerCB::SctxMap::const_iterator lIte = theCompilerCB->theSctxMap.begin();
+    CompilerCB::SctxMap::const_iterator lEnd = theCompilerCB->theSctxMap.end();
 
     for(; lIte != lEnd; ++lIte)
     {
@@ -794,9 +791,9 @@
     store::Item_t qname;
     GENV_ITEMFACTORY->createQName(qname, nameSpace, zstring(), localName);
     
-    std::map<short, static_context_t>& lMap = theCompilerCB->theSctxMap;
-    std::map<short, static_context_t>::const_iterator lIte = lMap.begin();
-    std::map<short, static_context_t>::const_iterator lEnd = lMap.end();
+    CompilerCB::SctxMap& lMap = theCompilerCB->theSctxMap;
+    CompilerCB::SctxMap::const_iterator lIte = lMap.begin();
+    CompilerCB::SctxMap::const_iterator lEnd = lMap.end();
 
     for (; lIte != lEnd; ++lIte)
     {
@@ -887,7 +884,7 @@
 
 
 /*******************************************************************************
-  Serialize the execution plan inot the given output stream.
+  Serialize the execution plan into the given output stream.
 ********************************************************************************/
 bool XQueryImpl::saveExecutionPlan(
     std::ostream& os,
@@ -903,9 +900,9 @@
 
     if (archive_format == ZORBA_USE_XML_ARCHIVE)
     {
-      zorba::serialization::XmlArchiver   xmlar(&os);
+      zorba::serialization::XmlArchiver xmlar(&os);
 
-      if((save_options & 0x01) != DONT_SAVE_UNUSED_FUNCTIONS)
+      if ((save_options & 0x01) != DONT_SAVE_UNUSED_FUNCTIONS)
         xmlar.set_serialize_everything();
 
       serialize(xmlar);
@@ -913,9 +910,9 @@
     }
     else//ZORBA_USE_BINARY_ARCHIVE
     {
-      zorba::serialization::BinArchiver   bin_ar(&os);
+      zorba::serialization::BinArchiver bin_ar(&os);
 
-      if((save_options & 0x01) != DONT_SAVE_UNUSED_FUNCTIONS)
+      if ((save_options & 0x01) != DONT_SAVE_UNUSED_FUNCTIONS)
         bin_ar.set_serialize_everything();
 
       serialize(bin_ar);
@@ -960,7 +957,7 @@
     try
     {
       // try the binary format first
-      zorba::serialization::BinArchiver   bin_ar(&is);
+      zorba::serialization::BinArchiver bin_ar(&is);
       bin_ar.setUserCallback(aCallback);
       serialize(bin_ar);
       bin_ar.finalize_input_serialization();
@@ -973,7 +970,7 @@
       //else go try xml archive reader
     }
     is.seekg(0);
-    zorba::serialization::XmlArchiver   xmlar(&is);
+    zorba::serialization::XmlArchiver xmlar(&is);
     xmlar.setUserCallback(aCallback);
     serialize(xmlar);
     xmlar.finalize_input_serialization();
@@ -1338,10 +1335,7 @@
 /*******************************************************************************
 
 ********************************************************************************/
-void
-XQueryImpl::debug(
-  const std::string& aHost,
-  unsigned short aPort)
+void XQueryImpl::debug(const std::string& aHost, unsigned short aPort)
 {
   Zorba_SerializerOptions lSerOptions;
   lSerOptions.omit_xml_declaration = ZORBA_OMIT_XML_DECLARATION_YES;
@@ -1389,10 +1383,15 @@
 
     theExecuting = true;
 
-    DebuggerServer aDebuggerServer(
-      this, aSerOptions, aOutStream, aCallbackFunction,
-      aCallbackData, aHost, aPort);
-    if (!aDebuggerServer.run()) {
+    DebuggerServer aDebuggerServer(this,
+                                   aSerOptions,
+                                   aOutStream,
+                                   aCallbackFunction,
+                                   aCallbackData,
+                                   aHost,
+                                   aPort);
+    if (!aDebuggerServer.run()) 
+    {
       aDebuggerServer.throwError();
     }
 

=== modified file 'src/compiler/api/compilercb.cpp'
--- src/compiler/api/compilercb.cpp	2012-04-16 20:56:43 +0000
+++ src/compiler/api/compilercb.cpp	2012-04-19 19:25:27 +0000
@@ -225,9 +225,9 @@
 /*******************************************************************************
 
 ********************************************************************************/
-static_context* CompilerCB::getStaticContext(short c)
+static_context* CompilerCB::getStaticContext(int c)
 {
-  std::map<short, static_context_t>::iterator lIter;
+  SctxMap::iterator lIter;
   lIter = theSctxMap.find(c);
   assert(lIter != theSctxMap.end());
   return lIter->second.getp();

=== modified file 'src/compiler/api/compilercb.h'
--- src/compiler/api/compilercb.h	2012-04-16 20:56:43 +0000
+++ src/compiler/api/compilercb.h	2012-04-19 19:25:27 +0000
@@ -139,7 +139,7 @@
     void serialize(::zorba::serialization::Archiver& ar);
   };
 
-  typedef std::map<short, static_context_t> SctxMap;
+  typedef std::map<int, static_context_t> SctxMap;
 
 public:  
   XQueryDiagnostics       * theXQueryDiagnostics;
@@ -194,7 +194,7 @@
 
   bool isSequential() const { return theIsSequential;}
 
-  static_context* getStaticContext(short id);
+  static_context* getStaticContext(int id);
 };
 
 

=== modified file 'src/compiler/translator/translator.cpp'
--- src/compiler/translator/translator.cpp	2012-04-16 21:52:47 +0000
+++ src/compiler/translator/translator.cpp	2012-04-19 19:25:27 +0000
@@ -117,7 +117,7 @@
     TranslatorImpl* rootTranslator,
     const parsenode& root,
     static_context* rootSctx,
-    short rootSctxId,
+    int rootSctxId,
     ModulesInfo* minfo,
     const std::map<zstring, zstring>& modulesStack,
     bool isLibModule,
@@ -559,7 +559,7 @@
 
   std::set<std::string>                  theImportedSchemas;
 
-  short                                  theCurrSctxId;
+  int                                    theCurrSctxId;
 
   static_context                       * theRootSctx;
 
@@ -567,7 +567,7 @@
 
   std::vector<static_context_t>          theSctxList;
 
-  std::stack<short>                      theSctxIdStack;
+  std::stack<int>                        theSctxIdStack;
 
   static_context                       * export_sctx;
 
@@ -644,7 +644,7 @@
 TranslatorImpl(
     TranslatorImpl* rootTranslator,
     static_context* rootSctx,
-    short rootSctxId,
+    int rootSctxId,
     ModulesInfo* minfo,
     const std::map<zstring, zstring>& modulesStack,
     bool isLibModule,
@@ -919,7 +919,7 @@
 /******************************************************************************
 
 *******************************************************************************/
-inline short sctxid()
+inline int sctxid()
 {
   return theCurrSctxId;
 }
@@ -941,7 +941,7 @@
     // this allows the debugger to introspect (during runtime)
     // all variables in scope
     theSctxIdStack.push(sctxid());
-    theCurrSctxId = (short)theCCB->theSctxMap.size() + 1;
+    theCurrSctxId = (int)theCCB->theSctxMap.size() + 1;
     (theCCB->theSctxMap)[sctxid()] = theSctx;
   }
   else
@@ -2970,7 +2970,7 @@
       moduleRootSctx->set_entity_retrieval_uri(compURI);
       moduleRootSctx->set_module_namespace(targetNS);
       moduleRootSctx->set_typemanager(new TypeManagerImpl(&GENV_TYPESYSTEM));
-      short moduleRootSctxId = (short)theCCB->theSctxMap.size() + 1;
+      int moduleRootSctxId = (int)theCCB->theSctxMap.size() + 1;
       (theCCB->theSctxMap)[moduleRootSctxId] = moduleRootSctx;
 
       // Create an sctx where the imported module is going to register
@@ -13299,7 +13299,7 @@
     TranslatorImpl* rootTranslator,
     const parsenode& root,
     static_context* rootSctx,
-    short rootSctxId,
+    int rootSctxId,
     ModulesInfo* minfo,
     const std::map<zstring, zstring>& modulesStack,
     bool isLibModule,
@@ -13342,7 +13342,7 @@
   return translate_aux(NULL,
                        root,
                        ccb->theRootSctx,
-                       (short)ccb->theSctxMap.size(),
+                       (int)ccb->theSctxMap.size(),
                        &minfo,
                        modulesStack,
                        false);

=== modified file 'src/context/static_context.cpp'
--- src/context/static_context.cpp	2012-04-17 15:35:48 +0000
+++ src/context/static_context.cpp	2012-04-19 19:25:27 +0000
@@ -791,7 +791,9 @@
 ********************************************************************************/
 void static_context::serialize_resolvers(serialization::Archiver& ar)
 {
-  size_t lNumURIMappers, lNumURLResolvers;
+  csize lNumURIMappers;
+  csize lNumURLResolvers;
+
   if (ar.is_serializing_out())
   {
     lNumURIMappers = theURIMappers.size();
@@ -817,33 +819,36 @@
     // callback required but not available
     if ((lNumURIMappers || lNumURLResolvers) && !lCallback)
     {
-      throw ZORBA_EXCEPTION(
-        zerr::ZCSE0013_UNABLE_TO_LOAD_QUERY,
-        ERROR_PARAMS( ZED( NoSerializationCallbackForDocColMod ) )
-      );
+      throw ZORBA_EXCEPTION(zerr::ZCSE0013_UNABLE_TO_LOAD_QUERY,
+      ERROR_PARAMS(ZED(NoSerializationCallbackForDocColMod)));
     }
 
-    if (lNumURIMappers) {
-      for (size_t i = 0; i < lNumURIMappers; ++i) {
+    if (lNumURIMappers) 
+    {
+      for (size_t i = 0; i < lNumURIMappers; ++i) 
+      {
         zorba::URIMapper* lURIMapper = lCallback->getURIMapper(i);
-        if (!lURIMapper) {
-          throw ZORBA_EXCEPTION(
-            zerr::ZCSE0013_UNABLE_TO_LOAD_QUERY,
-            ERROR_PARAMS( ZED( NoModuleURIResolver ) )
-          );
+        if (!lURIMapper) 
+        {
+          throw ZORBA_EXCEPTION(zerr::ZCSE0013_UNABLE_TO_LOAD_QUERY,
+          ERROR_PARAMS(ZED(NoModuleURIResolver)));
         }
+
         add_uri_mapper(new URIMapperWrapper(*lURIMapper));
       }
     }
-    if (lNumURLResolvers) {
-      for (size_t i = 0; i < lNumURLResolvers; ++i) {
+
+    if (lNumURLResolvers) 
+    {
+      for (size_t i = 0; i < lNumURLResolvers; ++i) 
+      {
         zorba::URLResolver* lURLResolver = lCallback->getURLResolver(i);
-        if (!lURLResolver) {
-          throw ZORBA_EXCEPTION(
-            zerr::ZCSE0013_UNABLE_TO_LOAD_QUERY,
-            ERROR_PARAMS( ZED( NoModuleURIResolver ) )
-          );
+        if (!lURLResolver) 
+        {
+          throw ZORBA_EXCEPTION(zerr::ZCSE0013_UNABLE_TO_LOAD_QUERY,
+          ERROR_PARAMS(ZED(NoModuleURIResolver)));
         }
+
         add_url_resolver(new URLResolverWrapper(*lURLResolver));
       }
     }

=== modified file 'src/debugger/debugger_commons.cpp'
--- src/debugger/debugger_commons.cpp	2012-04-16 20:56:43 +0000
+++ src/debugger/debugger_commons.cpp	2012-04-19 19:25:27 +0000
@@ -52,13 +52,6 @@
 
 // ****************************************************************************
 
-SERIALIZABLE_CLASS_VERSIONS(QueryLocComparator)
-
-
-void
-QueryLocComparator::serialize(serialization::Archiver& ar) {
-}
-
 bool
 QueryLocComparator::operator()(const QueryLoc& a, const QueryLoc& b) const
 {
@@ -159,22 +152,61 @@
   thePlanState = NULL;
 }
 
+
 DebuggerCommons::~DebuggerCommons()
 {
 }
 
+
 void
 DebuggerCommons::serialize(::zorba::serialization::Archiver& ar)
 {
   ar & theBreakables;
-  ar & theBreakableIDs;
+
+  if (ar.is_serializing_out())
+  {
+    ar.set_is_temp_field(true);
+    int s = (int)theBreakableIDs.size();
+    ar & s;
+    ar.set_is_temp_field(false);
+
+    BreakableIdMap::iterator it = theBreakableIDs.begin();
+    BreakableIdMap::iterator end = theBreakableIDs.end();
+
+    for (; it != end; ++it)
+    {
+      QueryLoc loc = (*it).first;
+      ar & loc;
+      ar & (*it).second;
+    }
+  }
+  else
+  {
+    ar.set_is_temp_field(true);
+    int s;
+    ar & s;
+    ar.set_is_temp_field(false);
+
+    std::pair<QueryLoc, unsigned int> p;
+
+    for (int i = 0; i < s; ++i)
+    {
+      ar & p.first;
+      ar & p.second;
+
+      theBreakableIDs.insert(p);
+    }
+  }
+
   ar & theStackTrace;
   ar & theUriFileMappingMap;
 
-  if(ar.is_serializing_out())
+  if (ar.is_serializing_out())
     theRuntime = NULL;
+
   ar & theCurrentStaticContext;
-  if(ar.is_serializing_out())
+
+  if (ar.is_serializing_out())
     theCurrentDynamicContext = NULL;
 
   ar & theBreak;
@@ -182,8 +214,9 @@
   ar & theIteratorStack;
   ar & theBreakCondition;
 
-  if(ar.is_serializing_out())
+  if (ar.is_serializing_out())
     thePlanState = NULL;
+
   ar & theEvalItem;
   ar & theExecEval;
   ar & theStepping;

=== modified file 'src/debugger/debugger_commons.h'
--- src/debugger/debugger_commons.h	2012-04-16 20:56:43 +0000
+++ src/debugger/debugger_commons.h	2012-04-19 19:25:27 +0000
@@ -45,20 +45,17 @@
   class PlanState;
   class DebugIteratorState;
 
-struct QueryLocComparator : public serialization::SerializeBaseClass {
+
+struct QueryLocComparator 
+{
   public:
     QueryLocComparator() {}
 
     bool
     operator()(const QueryLoc& a, const QueryLoc& b) const;
-
-  public:
-
-    SERIALIZABLE_CLASS(QueryLocComparator)
-    SERIALIZABLE_CLASS_CONSTRUCTOR(QueryLocComparator)
-    void serialize(serialization::Archiver& ar);
 };
 
+
 class Breakable : public serialization::SerializeBaseClass {
   private:
     QueryLoc theLocation;

=== modified file 'src/functions/function.cpp'
--- src/functions/function.cpp	2012-04-16 20:56:43 +0000
+++ src/functions/function.cpp	2012-04-19 19:25:27 +0000
@@ -52,7 +52,7 @@
   *::zorba::serialization::ClassSerializer::getInstance()->
   getArchiverForHardcodedObjects();
 
-  if(ar.is_loading_hardcoded_objects())
+  if (ar.is_loading_hardcoded_objects())
   {
     // register this hardcoded object to help plan serialization
     function* this_ptr = this;

=== modified file 'src/types/schema/schema.cpp'
--- src/types/schema/schema.cpp	2012-04-16 20:56:43 +0000
+++ src/types/schema/schema.cpp	2012-04-19 19:25:27 +0000
@@ -2034,7 +2034,7 @@
     return false;
 }
 
-void Schema::serialize(::zorba::serialization::Archiver &ar)
+void Schema::serialize(::zorba::serialization::Archiver& ar)
 {
   SERIALIZE_TYPEMANAGER(TypeManager, theTypeManager);
 
@@ -2042,24 +2042,32 @@
    ar & theUdTypesCache;
 
    bool is_grammar_NULL = (theGrammarPool == NULL);
-   ar.set_is_temp_field_one_level(true, true);
+
+   ar.set_is_temp_field(true);
+
    ar & is_grammar_NULL;
+
    unsigned long size_of_size_t = sizeof(size_t);
+
    union
    {
      unsigned long lvalue;
      unsigned char cvalue[4];
-   }le_be_value;
+   } le_be_value;
+
    le_be_value.lvalue = 0x11223344;
-   if(ar.is_serializing_out())
+
+   if (ar.is_serializing_out())
    {
      ar & size_of_size_t;
      ar & le_be_value.cvalue[0];
-     if(!is_grammar_NULL)
+
+     if (!is_grammar_NULL)
      {
-       BinMemOutputStream    binmemoutputstream;
+       BinMemOutputStream binmemoutputstream;
        unsigned int  size = 0;
-       unsigned char *binchars = NULL;
+       unsigned char* binchars = NULL;
+
        try
        {
          theGrammarPool->serializeGrammars(&binmemoutputstream);
@@ -2071,6 +2079,7 @@
        }
 
        ar & size;
+
        if(size)
          serialize_array(ar, binchars, size);
      }
@@ -2081,14 +2090,16 @@
      unsigned char le_be_value_first_char;
      ar & size_of_size_t2;
      ar & le_be_value_first_char;
-     if((size_of_size_t2 != size_of_size_t) || (le_be_value_first_char != le_be_value.cvalue[0]))
+     if (size_of_size_t2 != size_of_size_t ||
+         le_be_value_first_char != le_be_value.cvalue[0])
      {
        throw ZORBA_EXCEPTION(zerr::ZCSE0015_INCOMPATIBLE_BETWEEN_32_AND_64_BITS_OR_LE_AND_BE);
      }
-     if(!is_grammar_NULL)
+
+     if (!is_grammar_NULL)
      {
-       unsigned int  size;
-       unsigned char *binchars;
+       unsigned int size;
+       unsigned char* binchars;
 
        ar & size;
        if(size)
@@ -2106,7 +2117,8 @@
        theGrammarPool = NULL;
      }
    }
-   ar.set_is_temp_field_one_level(false);
+
+   ar.set_is_temp_field(false);
 #endif
 }
 

=== modified file 'src/util/hashmap.h'
--- src/util/hashmap.h	2012-04-16 20:56:43 +0000
+++ src/util/hashmap.h	2012-04-19 19:25:27 +0000
@@ -138,30 +138,33 @@
 public:
 #ifdef ZORBA_HASHMAP_WITH_SERIALIZATION
   SERIALIZABLE_TEMPLATE_CLASS(serializable_hashmap)
-  serializable_hashmap(::zorba::serialization::Archiver &ar) : ::zorba::serialization::SerializeBaseClass()
+  serializable_hashmap(::zorba::serialization::Archiver& ar) : ::zorba::serialization::SerializeBaseClass()
   {
   }
-  void serialize(::zorba::serialization::Archiver &ar)
+
+  void serialize(::zorba::serialization::Archiver& ar)
   {
     ar & sz;
     ar & tsz;
     ar & ld;
     ar & minsz;
     ar & v;
-    //ar & tab;
+
     //compress tab
     int tab_size = (int)tab.size();
-    ar.set_is_temp_field_one_level(true);
+
+    ar.set_is_temp_field(true);
     ar & tab_size;
-    if(ar.is_serializing_out())
+
+    if (ar.is_serializing_out())
     {
-      typename checked_vector<int>::iterator tab_it;
-      tab_it = tab.begin();
+      checked_vector<int>::iterator tab_it = tab.begin();
       int last_val = *tab_it;
       int nr_dupl = 1;
-      for(tab_it++; tab_it != tab.end(); tab_it++)
+
+      for (tab_it++; tab_it != tab.end(); ++tab_it)
       {
-        if((*tab_it) != last_val)
+        if ((*tab_it) != last_val)
         {
           ar & nr_dupl;
           ar & last_val;
@@ -169,8 +172,11 @@
           nr_dupl = 1;
         }
         else
+        {
           nr_dupl++;
+        }
       }
+
       ar & nr_dupl;
       ar & last_val;
       nr_dupl = 0;
@@ -181,16 +187,19 @@
       tab.resize(tab_size);
       int last_val;
       int nr_dupl, i;
-      typename checked_vector<int>::iterator  tab_it = tab.begin();
+      checked_vector<int>::iterator  tab_it = tab.begin();
+
       while(1)
       {
         ar & nr_dupl;
-        if(!nr_dupl)
+
+        if (!nr_dupl)
           break;
+
         ar & last_val;
-        for(i=0;i<nr_dupl;i++)
+
+        for (i = 0; i < nr_dupl; i++)
         {
-          //tab.push_back(last_val);
           *tab_it = last_val;
           tab_it++;
         }
@@ -198,7 +207,8 @@
 
       assert((int)tab.size() == (int)tab_size);
     }
-    ar.set_is_temp_field_one_level(false);
+
+    ar.set_is_temp_field(false);
   }
 #endif
 public:

=== modified file 'src/zorbaserialization/archiver.cpp'
--- src/zorbaserialization/archiver.cpp	2012-04-18 10:54:35 +0000
+++ src/zorbaserialization/archiver.cpp	2012-04-19 19:25:27 +0000
@@ -126,7 +126,6 @@
   theNonClassFieldsMap(0),
   theClassFieldsMap(0),
   all_reference_list(0),
-  read_optional(false),
   internal_archive(internal_archive),
   theOnlyForEval(0),
   theSerializeEverything(false),
@@ -159,7 +158,7 @@
   if (!internal_archive)
   {
     Archiver* har = ClassSerializer::getInstance()->harcoded_objects_archive;
-    theFieldCounter = har->get_num_fields();
+    theFieldCounter = har->theFieldCounter;
   }
 }
 
@@ -183,81 +182,48 @@
 
 
 /*******************************************************************************
-  Return the left sibling of a given field (NULL if there is no left sibling).
-********************************************************************************/
-archive_field* Archiver::get_prev(archive_field* field)
-{
-  archive_field* temp;
-  temp = field->theParent->theFirstChild;
-
-  if (temp == field)
-    return NULL;
-
-  while (temp)
-  {
-    if (temp->theNextSibling == field)
-      return temp;
-
-    temp = temp->theNextSibling;
-  }
-
-  assert(false);
-  return NULL;
-}
-
-
-/*******************************************************************************
-  Check whether there exists already a field for the nonclass object at the given
-  memory address.
-********************************************************************************/
-archive_field* Archiver::lookup_nonclass_field(const char* type, const void* ptr)
-{
-  if (!ptr)
-    return NULL;
-
-  archive_field* duplicate_field = NULL;
-
-  SIMPLE_HASHOUT_FIELD f(type, ptr);
-
-  theNonClassFieldsMap->get(f, duplicate_field);
-
-  if (!duplicate_field)
-  {
-    Archiver* har = ::zorba::serialization::ClassSerializer::getInstance()->
-    getArchiverForHardcodedObjects();
-
-    if (har != this)
-      duplicate_field = har->lookup_nonclass_field(type, ptr);
-  }
-
-  return duplicate_field;
-}
-
-
-/*******************************************************************************
-  Check whether there exists already a field for the class object at the given
-  memory address.
-********************************************************************************/
-archive_field* Archiver::lookup_class_field(const SerializeBaseClass* ptr)
-{
-  if (!ptr)
-    return NULL;
-
-  archive_field* duplicate_field = NULL;
-
-  theClassFieldsMap->get((uint64_t)ptr, duplicate_field);
-
-  if (!duplicate_field)
-  {
-    Archiver* har = ::zorba::serialization::ClassSerializer::getInstance()->
-    getArchiverForHardcodedObjects();
-
-    if (har != this)
-      duplicate_field = har->lookup_class_field(ptr);
-  }
-
-  return duplicate_field;
-}
+
+********************************************************************************/
+bool Archiver::get_is_temp_field_one_level()
+{
+  if (limit_temp_level_stack.empty())
+  {
+    return false;
+  }
+  else if (limit_temp_level_stack.top() == theCurrentLevel)
+  {
+    return true;
+  }
+  else
+  {
+    unsigned int lastlevel = limit_temp_level_stack.top();
+    archive_field* temp_field = theCurrentCompoundField;
+
+    assert(!temp_field || temp_field->theLevel <= theCurrentLevel);
+    assert(!temp_field || temp_field->theLevel >= lastlevel);
+    
+    while (temp_field && (temp_field->theLevel >= lastlevel))
+    {
+      if (temp_field->theKind == ARCHIVE_FIELD_PTR ||
+          temp_field->theKind == ARCHIVE_FIELD_REFERENCING)
+        return false;
+      
+      temp_field = temp_field->theParent;
+    }
+    
+    if (temp_field)
+      return true;
+    else
+      return false;
+  }
+} 
+
+
+////////////////////////////////////////////////////////////////////////////////
+//                                                                            //
+//  Serialization only                                                        //
+//                                                                            //
+////////////////////////////////////////////////////////////////////////////////
 
 
 /*******************************************************************************
@@ -284,7 +250,7 @@
   assert(fieldKind != ARCHIVE_FIELD_BASECLASS);
   assert(fieldKind != ARCHIVE_FIELD_REFERENCING);
 
-  theCurrentLevel++;
+  ++theCurrentLevel;
 
   if (!ptr)
   {
@@ -295,8 +261,7 @@
     assert(fieldKind == ARCHIVE_FIELD_NORMAL || fieldKind == ARCHIVE_FIELD_PTR);
 
     if (!get_is_temp_field() &&
-        (!get_is_temp_field_one_level() ||
-         (fieldKind == ARCHIVE_FIELD_PTR && !get_is_temp_field_also_for_ptr())))
+        (!get_is_temp_field_one_level() || fieldKind == ARCHIVE_FIELD_PTR))
     {
       ref_field = lookup_nonclass_field(type, ptr);
     }
@@ -340,8 +305,7 @@
   if (!ref_field &&
       ptr &&
       !get_is_temp_field() &&
-      (!get_is_temp_field_one_level() ||
-       (fieldKind == ARCHIVE_FIELD_PTR && !get_is_temp_field_also_for_ptr())))
+      (!get_is_temp_field_one_level() || fieldKind == ARCHIVE_FIELD_PTR))
   {
     assert(fieldKind == ARCHIVE_FIELD_NORMAL || fieldKind == ARCHIVE_FIELD_PTR);
 
@@ -361,7 +325,7 @@
   
   theCurrentCompoundField->theLastChild = new_field;
 
-  theCurrentLevel--;
+  --theCurrentLevel;
 
   return ref_field != NULL;
 }
@@ -380,17 +344,16 @@
   archive_field* new_field;
   archive_field* ref_field = NULL;
 
-  theCurrentLevel++;
+  ++theCurrentLevel;
 
   if (!ptr)
   {
     fieldKind = ARCHIVE_FIELD_NULL;
-    theCurrentLevel--;
+    --theCurrentLevel;
   }
   else if (fieldKind != ARCHIVE_FIELD_BASECLASS &&
            !get_is_temp_field() &&
-           (!get_is_temp_field_one_level() ||
-            (fieldKind == ARCHIVE_FIELD_PTR && !get_is_temp_field_also_for_ptr())))
+           (!get_is_temp_field_one_level() || fieldKind == ARCHIVE_FIELD_PTR))
   {
     if (!is_class)
       ref_field = lookup_nonclass_field(type, ptr);
@@ -432,8 +395,7 @@
       fieldKind != ARCHIVE_FIELD_BASECLASS &&
       ptr &&
       !get_is_temp_field() &&
-      (!get_is_temp_field_one_level() ||
-       (fieldKind == ARCHIVE_FIELD_PTR && !get_is_temp_field_also_for_ptr())))
+      (!get_is_temp_field_one_level() || fieldKind == ARCHIVE_FIELD_PTR))
   {
     if (!is_class)
     {
@@ -461,7 +423,7 @@
     theCurrentCompoundField = new_field;
 
   if (ref_field)
-    theCurrentLevel--;
+    --theCurrentLevel;
 
   return ref_field != NULL;
 }
@@ -478,6 +440,60 @@
 
 
 /*******************************************************************************
+  Check whether there exists already a field for the nonclass object at the given
+  memory address.
+********************************************************************************/
+archive_field* Archiver::lookup_nonclass_field(const char* type, const void* ptr)
+{
+  if (!ptr)
+    return NULL;
+
+  archive_field* duplicate_field = NULL;
+
+  SIMPLE_HASHOUT_FIELD f(type, ptr);
+
+  theNonClassFieldsMap->get(f, duplicate_field);
+
+  if (!duplicate_field)
+  {
+    Archiver* har = ::zorba::serialization::ClassSerializer::getInstance()->
+    getArchiverForHardcodedObjects();
+
+    if (har != this)
+      duplicate_field = har->lookup_nonclass_field(type, ptr);
+  }
+
+  return duplicate_field;
+}
+
+
+/*******************************************************************************
+  Check whether there exists already a field for the class object at the given
+  memory address.
+********************************************************************************/
+archive_field* Archiver::lookup_class_field(const SerializeBaseClass* ptr)
+{
+  if (!ptr)
+    return NULL;
+
+  archive_field* duplicate_field = NULL;
+
+  theClassFieldsMap->get((uint64_t)ptr, duplicate_field);
+
+  if (!duplicate_field)
+  {
+    Archiver* har = ::zorba::serialization::ClassSerializer::getInstance()->
+    getArchiverForHardcodedObjects();
+
+    if (har != this)
+      duplicate_field = har->lookup_class_field(ptr);
+  }
+
+  return duplicate_field;
+}
+
+
+/*******************************************************************************
   Place new_field in the position occupied by ref_field, and disconnect 
   ref_field from the tree.
 ********************************************************************************/
@@ -510,275 +526,26 @@
 
 
 /*******************************************************************************
-
-********************************************************************************/
-bool Archiver::read_next_field(
-    char** type,
-    std::string* value,
-    int* id,
-    bool* is_simple,
-    bool* is_class,
-    enum ArchiveFieldKind* fieldKind,
-    int* referencing)
-{
-  bool retval = read_next_field_impl(type, value, id,
-                                     is_simple, is_class, fieldKind, referencing);
-
-  if (retval && !*is_simple && (*fieldKind != ARCHIVE_FIELD_REFERENCING))
-    theCurrentLevel++;
-
-  return retval;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::read_end_current_level()
-{
-  theCurrentLevel--;
-  read_end_current_level_impl();
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::check_simple_field(
-    bool retval,
-    const char* type,
-    const char* required_type,
-    bool is_simple,
-    enum ArchiveFieldKind field_treat,
-    enum ArchiveFieldKind required_field_treat,
-    int id)
-{
-  if (!retval)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0001_NONEXISTENT_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-
-#ifndef NDEBUG
-  if (!is_simple)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-#endif
-
-  if(field_treat == ARCHIVE_FIELD_NULL)
-    return;
-
-#ifndef NDEBUG
-  if (strcmp(type, required_type))
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-#endif
-
-  if ((required_field_treat != (enum ArchiveFieldKind)-1) &&
-      field_treat != required_field_treat)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::check_nonclass_field(
-    bool retval,
-    const char* type,
-    const char* required_type,
-    bool is_simple,
-    bool is_class,
-    enum ArchiveFieldKind field_treat,
-    enum ArchiveFieldKind required_field_treat,
-    int id)
-{
-  if (!retval)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0001_NONEXISTENT_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-
-#ifndef NDEBUG
-  if (is_simple || is_class)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-#endif
-
-  if (field_treat == ARCHIVE_FIELD_NULL)
-    return;
-
-#ifndef NDEBUG
-  if (strcmp(type, required_type))
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-#endif
-
-  if ((required_field_treat != (enum ArchiveFieldKind)-1) &&
-      field_treat != required_field_treat)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::check_class_field(
-    bool retval,
-    const char* type,
-    const char* required_type,
-    bool is_simple,
-    bool is_class,
-    enum ArchiveFieldKind field_treat,
-    enum ArchiveFieldKind required_field_treat,
-    int id)
-{
-  if (!retval)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0001_NONEXISTENT_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-
-#ifndef NDEBUG
-  if (is_simple || !is_class)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-#endif
-
-  if (field_treat == ARCHIVE_FIELD_NULL)
-    return;
-
-#ifndef NDEBUG
-  if (strcmp(type, required_type))
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-#endif
-
-  if ((required_field_treat != (enum ArchiveFieldKind)-1) && 
-     field_treat != required_field_treat)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::register_reference(
-    int id,
-    enum ArchiveFieldKind field_treat,
-    const void* ptr)
-{
-  if (get_is_temp_field())// && (field_treat != ARCHIVE_FIELD_PTR))
-    return;
-
-  if (get_is_temp_field_one_level() &&
-      ((field_treat != ARCHIVE_FIELD_PTR) || get_is_temp_field_also_for_ptr()))
-    return;
-
-  all_reference_list[id] = (void*)ptr;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::register_item(store::Item* i)
-{
-  if (i)
-    registered_items.push_back(i);
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::register_pointers_internal(archive_field* fields)
-{
-  archive_field* child;
-  child = fields->theFirstChild;
-  while(child)
-  {
-    register_reference(child->theId, child->theKind, child->assoc_ptr);
-    if(!child->theIsSimple)
-    {
-      register_pointers_internal(child);
-    }
-    child = child->theNextSibling;
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void* Archiver::get_reference_value(int refid)
-{
-  if (internal_archive && !all_reference_list)
-  {
-    //construct all_reference_list
-    root_tag_is_read();
-    register_pointers_internal(theRootField);
-  }
-
-  void* assoc_ptr = NULL;
-  //if(!all_reference_list->get((uint32_t)refid, assoc_ptr))
-  if(!(assoc_ptr = all_reference_list[refid]))
-  {
-    Archiver* har = ::zorba::serialization::ClassSerializer::getInstance()->getArchiverForHardcodedObjects();
-    if(har != this)
-      assoc_ptr = har->get_reference_value(refid);
-  }
-  return assoc_ptr;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::finalize_input_serialization()
-{
-  //decrement RC on Items
-  std::vector<store::Item*>::iterator item_it;
-  int j = 0;
-
-  for (item_it = registered_items.begin(); item_it != registered_items.end(); item_it++)
-  {
-    j++;
-    if((*item_it)->isNode())
-    {
-      assert(*(*item_it)->getSharedRefCounter() > 1);
-    }
-    else
-    {
-      assert((*item_it)->getRefCount() > 1);
-    }
-    (*item_it)->removeReference();
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void Archiver::root_tag_is_read()
-{
-  if (theArchiveVersion != ClassSerializer::g_zorba_classes_version)
-  {
-    throw ZORBA_EXCEPTION(zerr::ZCSE0012_INCOMPATIBLE_ARCHIVE_VERSION,
-    ERROR_PARAMS(theArchiveVersion, ClassSerializer::g_zorba_classes_version));
-  }
-
-  all_reference_list = new void*[theFieldCounter+1];
-  memset(all_reference_list, 0, sizeof(void*)*(theFieldCounter+1));
+  Return the left sibling of a given field (NULL if there is no left sibling).
+********************************************************************************/
+archive_field* Archiver::get_prev(archive_field* field)
+{
+  archive_field* temp;
+  temp = field->theParent->theFirstChild;
+
+  if (temp == field)
+    return NULL;
+
+  while (temp)
+  {
+    if (temp->theNextSibling == field)
+      return temp;
+
+    temp = temp->theNextSibling;
+  }
+
+  assert(false);
+  return NULL;
 }
 
 
@@ -801,114 +568,6 @@
 /*******************************************************************************
 
 ********************************************************************************/
-archive_field* Archiver::replace_with_null(archive_field* current_field)
-{
-  if (current_field->theParent)
-  {
-    archive_field* null_field = new archive_field("NULL",
-                                                  current_field->theIsSimple,
-                                                  current_field->theIsClass,
-                                                  "",
-                                                  NULL,
-                                                  ARCHIVE_FIELD_NULL,
-                                                  NULL,
-                                                  false,
-                                                  ALLOW_DELAY,
-                                                  current_field->theLevel);
-    null_field->theId = ++theFieldCounter;
-    replace_field(null_field, current_field);
-    current_field->theParent = NULL;
-    current_field->theNextSibling = NULL;
-
-    return null_field;
-  }
-  //otherwise it is orphan
-  return NULL;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-int Archiver::compute_field_depth(archive_field* field)
-{
-  archive_field* temp;
-  int i = 0;
-  temp = field->theParent;
-  while(temp)
-  {
-    temp = temp->theParent;
-    i++;
-  }
-  return i;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-int Archiver::get_only_for_eval(archive_field* field)
-{
-  if (field->theOnlyForEval)
-    return field->theOnlyForEval;
-
-  archive_field* child;
-  for (child = field->theFirstChild; child; child = child->theNextSibling)
-  {
-    if (child->theOnlyForEval)
-      return child->theOnlyForEval;
-  }
-
-  return 0;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-archive_field* Archiver::find_top_most_eval_only_field(archive_field* parent_field)
-{
-  int ref_depth = -1;
-  archive_field *refering_field = NULL;
-  archive_field *child;
-  child = parent_field->theFirstChild;
-  while(child)
-  {
-    if(child->theOnlyForEval)
-    {
-    }
-    else if((child->theKind == ARCHIVE_FIELD_REFERENCING) &&
-        get_only_for_eval(child->theReferredField))
-    {
-      int new_depth = compute_field_depth(child->theReferredField);
-      if(!refering_field || (ref_depth > new_depth))
-      {
-        ref_depth = new_depth;
-        refering_field = child;
-      }
-    }
-    else if(!child->theIsSimple && !child->theReferredField)
-    {
-      archive_field *new_refering = find_top_most_eval_only_field(child);
-      if(new_refering)
-      {
-        int new_depth = compute_field_depth(new_refering->theReferredField);
-        if(!refering_field || (ref_depth > new_depth))
-        {
-          ref_depth = new_depth;
-          refering_field = new_refering;
-        }
-      }
-    }
-    child = child->theNextSibling;
-  }
-  return refering_field;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
 void Archiver::check_compound_fields(archive_field* parent_field)
 {
   //resolve all references first
@@ -944,7 +603,7 @@
                         get_only_for_eval(refering_field->theReferredField));
   }
 
-  while(check_only_for_eval_nondelay_referencing(parent_field))
+  while (check_only_for_eval_nondelay_referencing(parent_field))
   {
   }
 
@@ -955,25 +614,126 @@
 /*******************************************************************************
 
 ********************************************************************************/
+archive_field* Archiver::find_top_most_eval_only_field(archive_field* parent_field)
+{
+  int ref_depth = -1;
+  archive_field* refering_field = NULL;
+
+  archive_field* child = parent_field->theFirstChild;
+  
+  while (child)
+  {
+    if (child->theOnlyForEval)
+    {
+    }
+    else if (child->theKind == ARCHIVE_FIELD_REFERENCING &&
+             get_only_for_eval(child->theReferredField))
+    {
+      int new_depth = compute_field_depth(child->theReferredField);
+      if(!refering_field || (ref_depth > new_depth))
+      {
+        ref_depth = new_depth;
+        refering_field = child;
+      }
+    }
+    else if (!child->theIsSimple && !child->theReferredField)
+    {
+      archive_field* new_refering = find_top_most_eval_only_field(child);
+
+      if (new_refering)
+      {
+        int new_depth = compute_field_depth(new_refering->theReferredField);
+        if (!refering_field || (ref_depth > new_depth))
+        {
+          ref_depth = new_depth;
+          refering_field = new_refering;
+        }
+      }
+    }
+
+    child = child->theNextSibling;
+  }
+
+  return refering_field;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+int Archiver::compute_field_depth(archive_field* field)
+{
+  archive_field* temp;
+  int i = 0;
+  temp = field->theParent;
+  while(temp)
+  {
+    temp = temp->theParent;
+    i++;
+  }
+  return i;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+int Archiver::get_only_for_eval(archive_field* field)
+{
+  if (field->theOnlyForEval)
+    return field->theOnlyForEval;
+
+  archive_field* child;
+  for (child = field->theFirstChild; child; child = child->theNextSibling)
+  {
+    if (child->theOnlyForEval)
+      return child->theOnlyForEval;
+  }
+
+  return 0;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::clean_only_for_eval(archive_field* field, int substract_value)
+{
+  if (field->theOnlyForEval >= substract_value)
+    field->theOnlyForEval -= substract_value;
+  else
+    field->theOnlyForEval = 0;
+
+  if (!field->theIsSimple)
+  {
+    archive_field* child = field->theFirstChild;
+    while (child)
+    {
+      clean_only_for_eval(child, substract_value);
+      child = child->theNextSibling;
+    }
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
 bool Archiver::check_only_for_eval_nondelay_referencing(archive_field* parent_field)
 {
   archive_field* current_field = parent_field->theFirstChild;
 
-  while(current_field)
+  while (current_field)
   {
-    if(current_field->theOnlyForEval && (current_field->theKind != ARCHIVE_FIELD_NORMAL))
+    if (current_field->theOnlyForEval && current_field->theKind != ARCHIVE_FIELD_NORMAL)
     {
-      if((current_field->theKind == ARCHIVE_FIELD_REFERENCING) &&
-        (current_field->theAllowDelay2 != ALLOW_DELAY) &&
-        (!current_field->theReferredField->theOnlyForEval))
+      if (current_field->theKind == ARCHIVE_FIELD_REFERENCING &&
+          current_field->theAllowDelay2 != ALLOW_DELAY &&
+          !current_field->theReferredField->theOnlyForEval)
       {
-        //exchange fields
-      //  exchange_mature_fields(current_field, current_field->theReferredField);
-      //  current_field->theOnlyForEval = false;
-
         //must preserve this serialization
-        archive_field *temp_field = current_field->theParent;
-        while(temp_field)
+        archive_field* temp_field = current_field->theParent;
+        while (temp_field)
         {
           temp_field->theOnlyForEval = 0;
           temp_field = temp_field->theParent;
@@ -983,11 +743,12 @@
       }
     }
 
-    if(!current_field->theIsSimple)
+    if (!current_field->theIsSimple)
     {
-      if(check_only_for_eval_nondelay_referencing(current_field))
+      if (check_only_for_eval_nondelay_referencing(current_field))
         return true;
     }
+
     current_field = current_field->theNextSibling;
   }
   return false;
@@ -1027,22 +788,30 @@
 /*******************************************************************************
 
 ********************************************************************************/
-void Archiver::clean_only_for_eval(archive_field* field, int substract_value)
+archive_field* Archiver::replace_with_null(archive_field* current_field)
 {
-  if (field->theOnlyForEval >= substract_value)
-    field->theOnlyForEval -= substract_value;
-  else
-    field->theOnlyForEval = 0;
-
-  if (!field->theIsSimple)
+  if (current_field->theParent)
   {
-    archive_field* child = field->theFirstChild;
-    while (child)
-    {
-      clean_only_for_eval(child, substract_value);
-      child = child->theNextSibling;
-    }
+    archive_field* null_field = new archive_field("NULL",
+                                                  current_field->theIsSimple,
+                                                  current_field->theIsClass,
+                                                  "",
+                                                  NULL,
+                                                  ARCHIVE_FIELD_NULL,
+                                                  NULL,
+                                                  false,
+                                                  ALLOW_DELAY,
+                                                  current_field->theLevel);
+    null_field->theId = ++theFieldCounter;
+    replace_field(null_field, current_field);
+    current_field->theParent = NULL;
+    current_field->theNextSibling = NULL;
+
+    return null_field;
   }
+
+  //otherwise it is orphan
+  return NULL;
 }
 
 
@@ -1058,6 +827,12 @@
   while (child)
   {
     if (child->theKind == ARCHIVE_FIELD_REFERENCING &&
+        check_order(child, child->theReferredField) < 1)
+    {
+      ZORBA_ASSERT(false);
+    }
+
+    if (child->theKind == ARCHIVE_FIELD_REFERENCING &&
        ((child->theAllowDelay2 == DONT_ALLOW_DELAY &&
          check_order(child, child->theReferredField) < 1) ||
         child->theAllowDelay2 == SERIALIZE_NOW))
@@ -1154,6 +929,290 @@
 }
 
 
+////////////////////////////////////////////////////////////////////////////////
+//                                                                            //
+//  De-Serialization only                                                     //
+//                                                                            //
+////////////////////////////////////////////////////////////////////////////////
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::root_tag_is_read()
+{
+  if (theArchiveVersion != ClassSerializer::g_zorba_classes_version)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0012_INCOMPATIBLE_ARCHIVE_VERSION,
+    ERROR_PARAMS(theArchiveVersion, ClassSerializer::g_zorba_classes_version));
+  }
+
+  all_reference_list = new void*[theFieldCounter+1];
+
+  memset(all_reference_list, 0, sizeof(void*)*(theFieldCounter+1));
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+bool Archiver::read_next_field(
+    char** type,
+    std::string* value,
+    int* id,
+    bool* is_simple,
+    bool* is_class,
+    enum ArchiveFieldKind* fieldKind,
+    int* referencing)
+{
+  bool retval = read_next_field_impl(type, value, id,
+                                     is_simple, is_class, fieldKind, referencing);
+
+  if (retval && !*is_simple && (*fieldKind != ARCHIVE_FIELD_REFERENCING))
+    theCurrentLevel++;
+
+  return retval;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::read_end_current_level()
+{
+  theCurrentLevel--;
+  read_end_current_level_impl();
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::check_simple_field(
+    bool retval,
+    const char* type,
+    const char* required_type,
+    bool is_simple,
+    enum ArchiveFieldKind field_treat,
+    enum ArchiveFieldKind required_field_treat,
+    int id)
+{
+  if (!retval)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0001_NONEXISTENT_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+
+#ifndef NDEBUG
+  if (!is_simple)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+#endif
+
+  if(field_treat == ARCHIVE_FIELD_NULL)
+    return;
+
+#ifndef NDEBUG
+  if (strcmp(type, required_type))
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+#endif
+
+  if ((required_field_treat != (enum ArchiveFieldKind)-1) &&
+      field_treat != required_field_treat)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::check_nonclass_field(
+    bool retval,
+    const char* type,
+    const char* required_type,
+    bool is_simple,
+    bool is_class,
+    enum ArchiveFieldKind field_treat,
+    enum ArchiveFieldKind required_field_treat,
+    int id)
+{
+  if (!retval)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0001_NONEXISTENT_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+
+#ifndef NDEBUG
+  if (is_simple || is_class)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+#endif
+
+  if (field_treat == ARCHIVE_FIELD_NULL)
+    return;
+
+#ifndef NDEBUG
+  if (strcmp(type, required_type))
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+#endif
+
+  if ((required_field_treat != (enum ArchiveFieldKind)-1) &&
+      field_treat != required_field_treat)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::check_class_field(
+    bool retval,
+    const char* type,
+    const char* required_type,
+    bool is_simple,
+    bool is_class,
+    enum ArchiveFieldKind field_treat,
+    enum ArchiveFieldKind required_field_treat,
+    int id)
+{
+  if (!retval)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0001_NONEXISTENT_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+
+#ifndef NDEBUG
+  if (is_simple || !is_class)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+#endif
+
+  if (field_treat == ARCHIVE_FIELD_NULL)
+    return;
+
+#ifndef NDEBUG
+  if (strcmp(type, required_type))
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+#endif
+
+  if ((required_field_treat != (enum ArchiveFieldKind)-1) && 
+     field_treat != required_field_treat)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(id));
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::register_reference(
+    int id,
+    enum ArchiveFieldKind field_treat,
+    const void* ptr)
+{
+  if (get_is_temp_field())
+    return;
+
+  if (get_is_temp_field_one_level() && field_treat != ARCHIVE_FIELD_PTR)
+    return;
+
+  all_reference_list[id] = (void*)ptr;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::register_item(store::Item* i)
+{
+  if (i)
+    registered_items.push_back(i);
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void* Archiver::get_reference_value(int refid)
+{
+  if (internal_archive && !all_reference_list)
+  {
+    //construct all_reference_list
+    root_tag_is_read();
+    register_pointers_internal(theRootField);
+  }
+
+  void* assoc_ptr = NULL;
+  //if(!all_reference_list->get((uint32_t)refid, assoc_ptr))
+  if (!(assoc_ptr = all_reference_list[refid]))
+  {
+    Archiver* har = ::zorba::serialization::ClassSerializer::getInstance()->
+                    getArchiverForHardcodedObjects();
+
+    if (har != this)
+      assoc_ptr = har->get_reference_value(refid);
+  }
+
+  return assoc_ptr;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::register_pointers_internal(archive_field* fields)
+{
+  archive_field* child;
+  child = fields->theFirstChild;
+  while (child)
+  {
+    register_reference(child->theId, child->theKind, child->assoc_ptr);
+    if (!child->theIsSimple)
+    {
+      register_pointers_internal(child);
+    }
+    child = child->theNextSibling;
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void Archiver::finalize_input_serialization()
+{
+  //decrement RC on Items
+  std::vector<store::Item*>::iterator item_it;
+  int j = 0;
+
+  for (item_it = registered_items.begin(); item_it != registered_items.end(); item_it++)
+  {
+    j++;
+    if ((*item_it)->isNode())
+    {
+      assert(*(*item_it)->getSharedRefCounter() > 1);
+    }
+    else
+    {
+      assert((*item_it)->getRefCount() > 1);
+    }
+
+    (*item_it)->removeReference();
+  }
+}
+
+
 } // namsspace serialization
 } // namespace zorba
 /* vim:set et sw=2 ts=2: */

=== modified file 'src/zorbaserialization/archiver.h'
--- src/zorbaserialization/archiver.h	2012-04-18 10:54:35 +0000
+++ src/zorbaserialization/archiver.h	2012-04-19 19:25:27 +0000
@@ -314,7 +314,7 @@
   ---------------
   If > 0, then the next obj to (de)serialize should not be registered or looked
   up in any of the fields registries. This is the case when, for example, we are
-  seriazing an obj that resides on the program stack
+  seriazing an obj that resides on the program stack.
 
   theFieldCounter:
   ----------------
@@ -394,6 +394,9 @@
 
   int                           theIsTempField;
   
+  std::stack<unsigned int>      limit_temp_level_stack;
+
+
   int                           theFieldCounter;
 
   archive_field               * theRootField;
@@ -406,21 +409,16 @@
 
   hash64map<archive_field*>   * theClassFieldsMap;
 
+  int                           theOnlyForEval;
+
 
   void                       ** all_reference_list;
 
+
   std::vector<archive_field*>   orphan_fields;
 
-  int                           current_class_version;
-
-  bool                          read_optional;
-
-  std::stack< std::pair<unsigned int, bool> >   limit_temp_level_stack;
-
   bool                          internal_archive;
 
-  int                           theOnlyForEval;
-
   bool                          theSerializeEverything;
 
   bool                          loading_hardcoded_objects;
@@ -441,6 +439,10 @@
 
   bool is_serializing_out() { return theSerializingOut; }
 
+  void set_loading_hardcoded_objects(bool v) { loading_hardcoded_objects = v; }
+
+  bool is_loading_hardcoded_objects() { return loading_hardcoded_objects; }
+
   SerializationCallback* getUserCallback() const { return theUserCallback; }
 
   void setUserCallback(SerializationCallback* cb) { theUserCallback = cb; }
@@ -464,6 +466,10 @@
 
   void add_end_compound_field();
 
+  bool is_serialize_everything() { return theSerializeEverything; }
+
+  void set_serialize_everything() { theSerializeEverything = true; }
+
   //
   // Methods used during de-serialization only
   //
@@ -489,6 +495,82 @@
 
   virtual void read_end_current_level_impl() = 0;
 
+  void check_simple_field(
+      bool retval, 
+      const char* type,
+      const char* required_type, 
+      bool is_simple, 
+      enum ArchiveFieldKind field_treat,
+      enum ArchiveFieldKind required_field_treat,
+      int id);
+
+  void check_nonclass_field(
+      bool retval, 
+      const char* type,
+      const char* required_type, 
+      bool is_simple, 
+      bool is_class, 
+      enum ArchiveFieldKind field_treat,
+      enum ArchiveFieldKind required_field_treat,
+      int id);
+
+  void check_class_field(
+      bool retval, 
+      const char* type,
+      const char* required_type, 
+      bool is_simple, 
+      bool is_class, 
+      enum ArchiveFieldKind field_treat,
+      enum ArchiveFieldKind required_field_treat,
+      int id);
+
+  void register_reference(
+      int id, 
+      enum ArchiveFieldKind field_treat,
+      const void* ptr);
+
+  void* get_reference_value(int refid);
+
+  void finalize_input_serialization();
+
+  //
+  // Methods used during both serialization and deserialization.
+  //
+  void set_serialize_base_class(bool s)
+  {
+    if (s)
+      theSerializeBaseClass++;
+    else
+      theSerializeBaseClass--;
+
+    assert(theSerializeBaseClass >= 0);
+    assert(theSerializeBaseClass <= 1);
+  }
+
+  bool is_serialize_base_class() { return theSerializeBaseClass > 0; }
+
+  void set_is_temp_field(bool is_temp)
+  {
+    if (is_temp)
+      theIsTempField++;
+    else
+      theIsTempField--;
+
+    assert(theIsTempField >= 0);
+  }
+
+  bool get_is_temp_field() { return (theIsTempField > 0); }
+
+ void set_is_temp_field_one_level(bool is_temp)
+  {
+    if (is_temp)
+      limit_temp_level_stack.push(theCurrentLevel + 1);
+    else
+      limit_temp_level_stack.pop();
+  }
+
+  bool get_is_temp_field_one_level();
+
 protected:
 
   //
@@ -499,9 +581,27 @@
 
   archive_field* lookup_class_field(const SerializeBaseClass* ptr);
 
+  void prepare_serialize_out();
+
   archive_field* get_prev(archive_field* field);
 
-  void prepare_serialize_out();
+  void replace_field(archive_field* new_field, archive_field* ref_field);
+
+  archive_field* replace_with_null(archive_field* current_field);
+
+  void register_pointers_internal(archive_field* fields);
+
+  int compute_field_depth(archive_field* field);
+
+  int get_only_for_eval(archive_field* field);
+
+  archive_field* find_top_most_eval_only_field(archive_field* parent_field);
+
+  bool check_only_for_eval_nondelay_referencing(archive_field* parent_field);
+
+  void replace_only_for_eval_with_null(archive_field* parent_field);
+
+  void clean_only_for_eval(archive_field* field, int substract_value);
 
   void check_compound_fields(archive_field* parent_field);
 
@@ -529,154 +629,11 @@
     *nr_ids = this->theFieldCounter;
   }
 
-  //
-  //
-  //
-  void replace_field(archive_field* new_field, archive_field* ref_field);
-
   void root_tag_is_read();
 
-  void register_pointers_internal(archive_field* fields);
-
-  archive_field* replace_with_null(archive_field* current_field);
-
-  int compute_field_depth(archive_field* field);
-
-  int get_only_for_eval(archive_field* field);
-
-  archive_field* find_top_most_eval_only_field(archive_field* parent_field);
-
-  bool check_only_for_eval_nondelay_referencing(archive_field* parent_field);
-
-  void replace_only_for_eval_with_null(archive_field* parent_field);
-
-  void clean_only_for_eval(archive_field* field, int substract_value);
-
 public:
-  void set_serialize_base_class(bool s)
-  {
-    if (s)
-      theSerializeBaseClass++;
-    else
-      theSerializeBaseClass--;
-
-    assert(theSerializeBaseClass >= 0);
-    assert(theSerializeBaseClass <= 1);
-  }
-
-  bool is_serialize_base_class() { return theSerializeBaseClass > 0; }
-
-  void set_is_temp_field(bool is_temp)
-  {
-    if (is_temp)
-      theIsTempField++;
-    else
-      theIsTempField--;
-
-    assert(theIsTempField >= 0);
-  }
-
-  bool get_is_temp_field() { return (theIsTempField > 0); }
-
-  int get_num_fields() { return theFieldCounter; }
-
-  void check_simple_field(
-      bool retval, 
-      const char* type,
-      const char* required_type, 
-      bool is_simple, 
-      enum ArchiveFieldKind field_treat,
-      enum ArchiveFieldKind required_field_treat,
-      int id);
-
-  void check_nonclass_field(
-      bool retval, 
-      const char* type,
-      const char* required_type, 
-      bool is_simple, 
-      bool is_class, 
-      enum ArchiveFieldKind field_treat,
-      enum ArchiveFieldKind required_field_treat,
-      int id);
-
-  void check_class_field(
-      bool retval, 
-      const char* type,
-      const char* required_type, 
-      bool is_simple, 
-      bool is_class, 
-      enum ArchiveFieldKind field_treat,
-      enum ArchiveFieldKind required_field_treat,
-      int id);
-
-  void register_reference(
-      int id, 
-      enum ArchiveFieldKind field_treat,
-      const void* ptr);
-
   void register_item(store::Item* i);
 
-  void* get_reference_value(int refid);
-
-  void finalize_input_serialization();
-
-  void set_read_optional_field(bool activate_optional)
-  {
-    this->read_optional = activate_optional;
-  }
-
-  bool get_read_optional_field() { return this->read_optional; }
-
-  void set_is_temp_field_one_level(bool is_temp, bool also_for_ptr = false)
-  {
-    if (is_temp)
-      limit_temp_level_stack.push(std::pair<int, bool>(theCurrentLevel + 1,
-                                                       also_for_ptr));
-    else
-      limit_temp_level_stack.pop();
-  }
-
-  bool get_is_temp_field_one_level() 
-  {
-    if (limit_temp_level_stack.empty())
-    {
-      return false;
-    }
-    else if (limit_temp_level_stack.top().first == theCurrentLevel)
-    {
-      return true;
-    }
-    else
-    {
-      unsigned int lastlevel = limit_temp_level_stack.top().first;
-      archive_field* temp_field = theCurrentCompoundField;
-
-      while (temp_field && (temp_field->theLevel >= lastlevel))
-      {
-        if (temp_field->theKind == ARCHIVE_FIELD_PTR ||
-            temp_field->theKind == ARCHIVE_FIELD_REFERENCING)
-           return false;
-
-        temp_field = temp_field->theParent;
-      }
-
-      if (temp_field)
-        return true;
-      else
-        return false;
-    }
-  } 
-
-  bool get_is_temp_field_also_for_ptr() 
-  {
-    return (limit_temp_level_stack.top().first == theCurrentLevel &&
-            limit_temp_level_stack.top().second);
-  } 
-
-  bool is_serialize_everything() { return theSerializeEverything; }
-
-  void set_serialize_everything() { theSerializeEverything = true; }
-
   int get_serialize_only_for_eval() { return theOnlyForEval; }
 
   void set_serialize_only_for_eval(bool evalonly)
@@ -689,13 +646,6 @@
     assert(theOnlyForEval >= 0);
   }
 
-  void set_loading_hardcoded_objects(bool set_hardcoded)
-  {
-    this->loading_hardcoded_objects = set_hardcoded;
-  }
-
-  bool is_loading_hardcoded_objects() { return loading_hardcoded_objects; }
-
   void dont_allow_delay(ENUM_ALLOW_DELAY d = DONT_ALLOW_DELAY)
   {
     theAllowDelay2 = d;

=== modified file 'src/zorbaserialization/bin_archiver.cpp'
--- src/zorbaserialization/bin_archiver.cpp	2012-04-18 10:54:35 +0000
+++ src/zorbaserialization/bin_archiver.cpp	2012-04-19 19:25:27 +0000
@@ -830,28 +830,31 @@
   if(*field_treat != ARCHIVE_FIELD_NULL)
   {
 #ifdef NDEBUG
-    if(*is_class && (*field_treat == ARCHIVE_FIELD_PTR))
+    if (*is_class && (*field_treat == ARCHIVE_FIELD_PTR))
 #endif
     {
-      unsigned int field_type_pos;
-      //read_string(field_type);
-      field_type_pos = read_int_exp2();
-      if(field_type_pos)
+      unsigned int field_type_pos = read_int_exp2();
+
+      if (field_type_pos)
         *type = (char*)strings.at(field_type_pos-1).str.c_str();
       else
         *type = NULL;
     }
+
     *id = read_int_exp() + this->last_id;
     this->last_id = *id;
-    if(*field_treat != ARCHIVE_FIELD_REFERENCING)
+
+    if (*field_treat != ARCHIVE_FIELD_REFERENCING)
     {
       unsigned int value_pos;
       value_pos = read_int_exp2();
-      if(value_pos)
+      if (value_pos)
        *value = strings.at(value_pos-1).str;
     }
     else
+    {
      *referencing = read_int();
+    }
   }
 
   return true;
@@ -867,7 +870,7 @@
   unsigned char  tempbyte = 0;
 
   tempbyte = read_bits(8);
-  if(tempbyte != 0xFF)
+  if (tempbyte != 0xFF)
   {
     throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS(last_id));
   }

=== modified file 'src/zorbaserialization/serialize_basic_types.cpp'
--- src/zorbaserialization/serialize_basic_types.cpp	2012-04-18 08:26:51 +0000
+++ src/zorbaserialization/serialize_basic_types.cpp	2012-04-19 19:25:27 +0000
@@ -59,24 +59,12 @@
     int   referencing;
     bool  retval;
 
-    retval = ar.read_next_field(&type,
-                                &value,
-                                &id,
-                                &is_simple,
-                                &is_class,
-                                &field_treat,
-                                &referencing);
-
-    if (!retval && ar.get_read_optional_field())
-      return;
-
-    ar.check_simple_field(retval,
-                          type,
-                          "number",
-                          is_simple,
-                          field_treat,
-                          ARCHIVE_FIELD_NORMAL,
-                          id);
+    retval = ar.read_next_field(&type, &value, &id,
+                                &is_simple, &is_class,
+                                &field_treat, &referencing);
+
+    ar.check_simple_field(retval, type, "number", is_simple,
+                          field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     sscanf(value.c_str(), "%d", &obj);
 
@@ -88,6 +76,25 @@
 /*******************************************************************************
 
 ********************************************************************************/
+void operator&(Archiver& ar, const int& obj)
+{
+  if (ar.is_serializing_out())
+  {
+    char strtemp[30];
+    sprintf(strtemp, "%d", obj);
+
+    ar.add_simple_field("number", strtemp, &obj, ARCHIVE_FIELD_NORMAL);
+  }
+  else
+  {
+    ZORBA_ASSERT(false);
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
 void operator&(Archiver& ar, uint32_t& obj)
 {
   if (ar.is_serializing_out())
@@ -108,24 +115,12 @@
     int   referencing;
     bool  retval;
 
-    retval = ar.read_next_field(&type,
-                                &value,
-                                &id,
-                                &is_simple,
-                                &is_class,
-                                &field_treat,
-                                &referencing);
-
-    if(!retval && ar.get_read_optional_field())
-      return;
-
-    ar.check_simple_field(retval,
-                          type,
-                          "unsigned number",
-                          is_simple,
-                          field_treat,
-                          ARCHIVE_FIELD_NORMAL,
-                          id);
+    retval = ar.read_next_field(&type, &value, &id,
+                                &is_simple, &is_class,
+                                &field_treat, &referencing);
+
+    ar.check_simple_field(retval, type, "unsigned number", is_simple,
+                          field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     sscanf(value.c_str(), "%u", &obj);
 
@@ -157,24 +152,12 @@
     int   referencing;
     bool  retval;
 
-    retval = ar.read_next_field(&type,
-                                &value,
-                                &id,
-                                &is_simple,
-                                &is_class,
-                                &field_treat,
-                                &referencing);
-
-    if (!retval && ar.get_read_optional_field())
-      return;
-
-    ar.check_simple_field(retval,
-                          type,
-                          "number",
-                          is_simple,
-                          field_treat,
-                          ARCHIVE_FIELD_NORMAL, 
-                          id);
+    retval = ar.read_next_field(&type, &value, &id,
+                                &is_simple, &is_class,
+                                &field_treat, &referencing);
+
+    ar.check_simple_field(retval, type, "number", is_simple,
+                          field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     sscanf(value.c_str(), "%ld", &obj);
 
@@ -214,9 +197,6 @@
                                 &field_treat,
                                 &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval,
                           type,
                           "unsigned number",
@@ -263,9 +243,6 @@
                                 &field_treat,
                                 &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval,
                           type,
                           "number",
@@ -307,9 +284,6 @@
     retval = ar.read_next_field(&type, &value, &id, 
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "unsigned number", 
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -346,9 +320,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "number", 
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -385,9 +356,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "unsigned number",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -424,9 +392,6 @@
     retval = ar.read_next_field(&type, &value, &id, 
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "char",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -465,9 +430,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "char*",
                           is_simple, field_treat, (ArchiveFieldKind)-1, id);
 
@@ -522,9 +484,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "char",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -563,9 +522,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "unsigned char",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -611,9 +567,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "float",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -657,9 +610,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "double", 
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -694,9 +644,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "bool",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -741,9 +688,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "string",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
     obj = value;
@@ -756,6 +700,22 @@
 /*******************************************************************************
 
 ********************************************************************************/
+void operator&(Archiver& ar, const std::string& obj)
+{
+  if (ar.is_serializing_out())
+  {
+    ar.add_simple_field("string", obj.c_str(), &obj, ARCHIVE_FIELD_NORMAL);
+  }
+  else
+  {
+    ZORBA_ASSERT(false);
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
 void operator&(Archiver& ar, std::string*& obj)
 {
   if (ar.is_serializing_out())
@@ -778,9 +738,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if(!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "std::string*",
                           is_simple, field_treat, (ArchiveFieldKind)-1, id);
 
@@ -836,9 +793,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "char[]",
                           is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 

=== modified file 'src/zorbaserialization/serialize_basic_types.h'
--- src/zorbaserialization/serialize_basic_types.h	2012-04-18 08:26:51 +0000
+++ src/zorbaserialization/serialize_basic_types.h	2012-04-19 19:25:27 +0000
@@ -32,6 +32,8 @@
 
 void operator&(Archiver& ar, int& obj);
 
+void operator&(Archiver& ar, const int& obj);
+
 void operator&(Archiver& ar, uint32_t& obj);
 
 void operator&(Archiver& ar, long& obj);
@@ -60,9 +62,11 @@
 
 void operator&(Archiver& ar, std::string& obj);
 
+void operator&(Archiver& ar, const std::string& obj);
+
 void operator&(Archiver& ar, std::string*& obj);
 
-void operator&(Archiver& ar, char*& obj);//like char *p=strdup("io");
+void operator&(Archiver& ar, char*& obj);
 
 void serialize_array(Archiver& ar, unsigned char* obj, int len);
 

=== modified file 'src/zorbaserialization/serialize_template_types.h'
--- src/zorbaserialization/serialize_template_types.h	2012-04-18 08:26:51 +0000
+++ src/zorbaserialization/serialize_template_types.h	2012-04-19 19:25:27 +0000
@@ -97,9 +97,6 @@
                                      &is_simple, &is_class, &field_treat,
                                      &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_class_field(retval, type, obj.get_class_name_str(),
                          is_simple, is_class, 
                          field_treat, ARCHIVE_FIELD_NORMAL, 
@@ -169,9 +166,6 @@
                                       &is_simple, &is_class, &field_treat,
                                       &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_class_field(retval, "", "",
                          is_simple, is_class,
                          field_treat, (ArchiveFieldKind)-1, id);
@@ -319,9 +313,6 @@
     bool retval = ar.read_next_field(&type, &value, &id,
                                      &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "checked_vector<T>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -387,9 +378,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::vector<T>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -467,9 +455,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::vector<T>*",
                             is_simple, is_class, field_treat, (ArchiveFieldKind)-1, id);
 
@@ -566,9 +551,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::vector<T*>", 
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -635,9 +617,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id, 
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::list<T>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -692,9 +671,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::pair<T1, T2>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -755,9 +731,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::pair<T1, T2>",
                             is_simple, is_class, field_treat, (ArchiveFieldKind)-1, id);
 
@@ -830,25 +803,22 @@
 
     if (!is_ref)
     {
-      ar.set_is_temp_field_one_level(true);
+      ar.set_is_temp_field(true);
 
       int s = (int)obj->size();
       ar & s;
 
+      ar.set_is_temp_field(false);
+
       typename std::map<T1, T2>::iterator it = obj->begin();
       typename std::map<T1, T2>::iterator end = obj->end();
 
-      for(; it != end; ++it)
+      for (; it != end; ++it)
       {
-        ar.dont_allow_delay();
-        T1  t1 = (*it).first;
-        ar & t1;
-        ar.dont_allow_delay();
-
+        ar & (*it).first;
         ar & (*it).second;
       }
 
-      ar.set_is_temp_field_one_level(false);
 
       ar.add_end_compound_field();
     }
@@ -866,9 +836,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::map<T1, T2>",
                             is_simple, is_class, field_treat, (ArchiveFieldKind)-1, id);
 
@@ -887,10 +854,10 @@
 
       ar.register_reference(id, field_treat, obj);
 
-      ar.set_is_temp_field_one_level(true);
-
+      ar.set_is_temp_field(true);
       int s;
       ar & s;
+      ar.set_is_temp_field(false);
 
       std::pair<T1, T2> p;
 
@@ -902,8 +869,6 @@
         obj->insert(p);
       }
 
-      ar.set_is_temp_field_one_level(false);
-
       ar.read_end_current_level();
     }
     else if (field_treat == ARCHIVE_FIELD_BASECLASS)
@@ -941,27 +906,22 @@
                                         ARCHIVE_FIELD_NORMAL);
     if (!is_ref)
     {
-      ar.set_is_temp_field_one_level(true);
+      ar.set_is_temp_field(true);
 
       int s = (int)obj.size();
       ar & s;
 
+      ar.set_is_temp_field(false);
+
       typename std::map<T1, T2, Tcomp>::iterator it = obj.begin();
       typename std::map<T1, T2, Tcomp>::iterator end = obj.end();
 
       for (; it != end; ++it)
       {
-        T1 t1 = (*it).first;
-
-        ar.dont_allow_delay();
-        ar & t1;
-        ar.dont_allow_delay();
-
+        ar & (*it).first;
         ar & (*it).second;
       }
 
-      ar.set_is_temp_field_one_level(false);
-
       ar.add_end_compound_field();
     }
   }
@@ -978,22 +938,19 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "std::map<T1, T2>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     ar.register_reference(id, field_treat, &obj);
 
-    ar.set_is_temp_field_one_level(true);
-
+    ar.set_is_temp_field(true);
     int s;
     ar & s;
-
-    std::pair<T1, T2>   p;
-
-    for(int i = 0; i < s; ++i)
+    ar.set_is_temp_field(false);
+
+    std::pair<T1, T2> p;
+
+    for (int i = 0; i < s; ++i)
     {
       ar & p.first;
       ar & p.second;
@@ -1001,8 +958,6 @@
       obj.insert(p);
     }
 
-    ar.set_is_temp_field_one_level(false);
-
     ar.read_end_current_level();
   }
 }
@@ -1045,9 +1000,6 @@
       bool retval = ar.read_next_field(&type, &value, &id,
                                        &is_simple, &is_class, &field_treat, &referencing);
 
-      if (!retval && ar.get_read_optional_field())
-        return;
-
       ar.check_simple_field(retval, type, "rstring",
                             is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -1088,17 +1040,6 @@
 }
 
 
-/*******************************************************************************
-
-********************************************************************************/
-template<class T>
-void read_optional_field(Archiver& ar, T& obj)
-{
-  ar.set_read_optional_field(true);
-  ar & obj;
-  ar.set_read_optional_field(false);
-}
-
 } // namespace serialization
 } // namespace zorba
 

=== modified file 'src/zorbaserialization/serialize_zorba_types.cpp'
--- src/zorbaserialization/serialize_zorba_types.cpp	2012-04-18 08:26:51 +0000
+++ src/zorbaserialization/serialize_zorba_types.cpp	2012-04-19 19:25:27 +0000
@@ -160,9 +160,6 @@
                                      &is_simple, &is_class, &field_treat,
                                      &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_simple_field(retval, type, "XQPCollator*",
                           is_simple, field_treat, ARCHIVE_FIELD_PTR, id);
 
@@ -225,8 +222,7 @@
     int   referencing;
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
-    if(!retval && ar.get_read_optional_field())
-      return;
+
     ar.check_simple_field(retval, type, "MAPM", is_simple, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     obj = value.c_str();
@@ -419,9 +415,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_class_field(retval, type, "store::Item*",
                          is_simple, is_class, field_treat, (enum  ArchiveFieldKind)-1, id);
 
@@ -781,64 +774,66 @@
       }
 EndAtomicItem:;
     }
-    else if(is_node)
+    else if (is_node)
     {
       serialize_node_tree(ar, obj, true);
     }
-    else if(is_pul)
+    else if (is_pul)
     {
-      throw ZORBA_EXCEPTION(
-        zerr::ZCSE0010_ITEM_TYPE_NOT_SERIALIZABLE, ERROR_PARAMS( "Pul" )
-      );
+      throw ZORBA_EXCEPTION(zerr::ZCSE0010_ITEM_TYPE_NOT_SERIALIZABLE,
+      ERROR_PARAMS("Pul"));
     }
-    else if(is_error)
+    else if (is_error)
     {
       ZORBA_ASSERT(false);
       SERIALIZE_FIELD(ZorbaException*, value, getError());
       FINALIZE_SERIALIZE(createError, (result, value));
     }
-    else  if(is_function)
+    else if (is_function)
     {
-      FunctionItem   *fitem = NULL;
-      if(ar.is_serializing_out())
+      FunctionItem* fitem = NULL;
+      if (ar.is_serializing_out())
       {
         fitem = dynamic_cast<FunctionItem*>(obj);
       }
+
       ar.dont_allow_delay();
       ar.set_is_temp_field(false);                    
+
       ar & fitem;
-      if(!ar.is_serializing_out())
+
+      if (!ar.is_serializing_out())
       {
         assert(fitem);
         obj = fitem;
-        if(obj)                                         
+        if (obj)                                         
           obj->addReference();     
+
         ar.register_reference(id, ARCHIVE_FIELD_PTR, obj);                
       }
+
       ar.set_is_temp_field(true);    
     }
     else
     {
-      throw ZORBA_EXCEPTION(
-        zerr::ZCSE0010_ITEM_TYPE_NOT_SERIALIZABLE,
-        ERROR_PARAMS( "[Unknown item type]" )
-      );
+      throw ZORBA_EXCEPTION(zerr::ZCSE0010_ITEM_TYPE_NOT_SERIALIZABLE,
+      ERROR_PARAMS("[Unknown item type]"));
     }
 
     ar.set_is_temp_field(false);
     ar.set_is_temp_field_one_level(false);
   }
 
-  if(ar.is_serializing_out())
+  if (ar.is_serializing_out())
   {
-    if(!is_ref)
+    if (!is_ref)
       ar.add_end_compound_field();
   }
   else
   {
-    if(!is_ref)
+    if (!is_ref)
     {
-      if(!is_node)
+      if (!is_node)
         ar.register_item(obj);
 
       ar.read_end_current_level();
@@ -846,14 +841,13 @@
     else
     {
       store::Item  *new_obj = NULL;
-      if((new_obj = (store::Item*)ar.get_reference_value(referencing)))// ARCHIVE_FIELD_REFERENCING
+      if ((new_obj = (store::Item*)ar.get_reference_value(referencing)))// ARCHIVE_FIELD_REFERENCING
       {
         obj = dynamic_cast<store::Item*>(new_obj);
-        if(!obj)
+        if (!obj)
         {
-          throw ZORBA_EXCEPTION(
-            zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, ERROR_PARAMS( id )
-          );
+          throw ZORBA_EXCEPTION(zerr::ZCSE0002_INCOMPATIBLE_INPUT_FIELD, 
+          ERROR_PARAMS(id));
         }
       }
       else if (!ar.get_is_temp_field() && !ar.get_is_temp_field_one_level())
@@ -861,7 +855,9 @@
         ZORBA_ASSERT(false);
       }
       else
+      {
         obj = NULL;
+      }
     }
   }
 
@@ -933,9 +929,6 @@
                                       &is_simple, &is_class, &field_treat,
                                       &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     if (field_treat == ARCHIVE_FIELD_NULL)
     {
       obj = NULL;
@@ -1167,8 +1160,7 @@
     int   referencing;
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
-    if(!retval && ar.get_read_optional_field())
-      return;
+
     ar.check_nonclass_field(retval, type, "Diagnostic*", is_simple, is_class, field_treat, (ArchiveFieldKind)-1, id);
     if(field_treat == ARCHIVE_FIELD_NULL)
     {
@@ -1279,8 +1271,7 @@
     int   referencing;
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
-    if(!retval && ar.get_read_optional_field())
-      return;
+
     ar.check_nonclass_field(retval, type, "ZorbaException*", is_simple, is_class, field_treat, (ArchiveFieldKind)-1, id);
     if(field_treat == ARCHIVE_FIELD_NULL)
     {
@@ -1375,8 +1366,7 @@
     int   referencing;
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
-    if(!retval && ar.get_read_optional_field())
-      return;
+
     ar.check_nonclass_field(retval, type, "internal::diagnostic::location", is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     ar & obj.file_;
@@ -1419,8 +1409,7 @@
     int   referencing;
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
-    if(!retval && ar.get_read_optional_field())
-      return;
+
     ar.check_nonclass_field(retval, type, "zorba::Item", is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     ar & obj.m_item;
@@ -1459,8 +1448,7 @@
     int   referencing;
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
-    if(!retval && ar.get_read_optional_field())
-      return;
+
     ar.check_nonclass_field(retval, type, "XQueryStackTrace", is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     ar & obj.trace_;
@@ -1506,9 +1494,6 @@
     bool  retval;
     retval = ar.read_next_field(&type, &value, &id, &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "XQueryStackTrace::Entry", is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
     ar & obj.getFnNameRef();

=== modified file 'src/zorbaserialization/serialize_zorba_types.h'
--- src/zorbaserialization/serialize_zorba_types.h	2012-04-18 08:26:51 +0000
+++ src/zorbaserialization/serialize_zorba_types.h	2012-04-19 19:25:27 +0000
@@ -68,29 +68,30 @@
 void operator&(Archiver &ar, zorba::XQueryStackTrace::Entry &obj);
 
 
-#define SERIALIZE_TYPEMANAGER(type_mgr_type, type_mgr)                             \
-  bool is_root_type_mgr = ar.is_serializing_out() && (!GENV.isRootStaticContextInitialized() || ((TypeManager*)type_mgr == (TypeManager*)&GENV_TYPESYSTEM)) && ar.is_serializing_out();            \
-  ar.set_is_temp_field_one_level(true);                             \
-  ar & is_root_type_mgr;                                            \
-  ar.set_is_temp_field_one_level(false);                            \
-  if(is_root_type_mgr)                                              \
-  {                                                                 \
-    if(!ar.is_serializing_out())                                    \
-    {                                                               \
-      type_mgr = (type_mgr_type*)&GENV_TYPESYSTEM;                  \
-      /*RCHelper::addReference(type_mgr);*/                         \
-    }                                                               \
-  }                                                                 \
-  else                                                              \
-  {                                                                 \
-    ar & type_mgr;                                                  \
+#define SERIALIZE_TYPEMANAGER(type_mgr_type, type_mgr)                  \
+  bool is_root_type_mgr = ar.is_serializing_out() && (!GENV.isRootStaticContextInitialized() || ((TypeManager*)type_mgr == (TypeManager*)&GENV_TYPESYSTEM)) ; \
+  ar.set_is_temp_field(true);                                           \
+  ar & is_root_type_mgr;                                                \
+  ar.set_is_temp_field(false);                                          \
+                                                                        \
+  if (is_root_type_mgr)                                                 \
+  {                                                                     \
+    if (!ar.is_serializing_out())                                       \
+    {                                                                   \
+      type_mgr = (type_mgr_type*)&GENV_TYPESYSTEM;                      \
+      /*RCHelper::addReference(type_mgr);*/                             \
+    }                                                                   \
+  }                                                                     \
+  else                                                                  \
+  {                                                                     \
+    ar & type_mgr;                                                      \
   }
 
 #define SERIALIZE_TYPEMANAGER_RCHANDLE(type_mgr_type, type_mgr)                             \
   bool is_root_type_mgr = (!GENV.isRootStaticContextInitialized() || ((TypeManager*)type_mgr.getp() == (TypeManager*)&GENV_TYPESYSTEM));            \
-  ar.set_is_temp_field_one_level(true);                             \
+  ar.set_is_temp_field(true);                                           \
   ar & is_root_type_mgr;                                            \
-  ar.set_is_temp_field_one_level(false);                            \
+  ar.set_is_temp_field(false);                                      \
   if(is_root_type_mgr)                                              \
   {                                                                 \
     if(!ar.is_serializing_out())                                    \
@@ -128,9 +129,9 @@
       {
         is_temp = true;
 
-        ar.set_is_temp_field_one_level(true, ar.get_is_temp_field_also_for_ptr());
+        ar.set_is_temp_field_one_level(true);
 
-        if(!ar.get_is_temp_field_also_for_ptr() && allow_delay == ALLOW_DELAY)
+        if (allow_delay == ALLOW_DELAY)
           ar.dont_allow_delay();
       }
 
@@ -159,9 +160,6 @@
     bool retval = ar.read_next_field(&type, &value, &id,
                                      &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "ItemHandle<T>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
     //ar.register_reference(id, field_treat, &obj);
@@ -171,7 +169,7 @@
     if (ar.get_is_temp_field_one_level())
     {
       is_temp = true;
-      ar.set_is_temp_field_one_level(true, ar.get_is_temp_field_also_for_ptr());
+      ar.set_is_temp_field_one_level(true);
     }
 
     T* p;
@@ -214,15 +212,15 @@
       if (ar.get_is_temp_field_one_level())
       {
         is_temp = true;
-        ar.set_is_temp_field_one_level(true, ar.get_is_temp_field_also_for_ptr());
+        ar.set_is_temp_field_one_level(true);
 
-        if (!ar.get_is_temp_field_also_for_ptr() && allow_delay == ALLOW_DELAY)
+        if (allow_delay == ALLOW_DELAY)
           ar.dont_allow_delay();
       }
 
       ar & p;
 
-      if(is_temp)
+      if (is_temp)
         ar.set_is_temp_field_one_level(false);
 
       ar.add_end_compound_field();
@@ -241,9 +239,6 @@
     bool  retval = ar.read_next_field(&type, &value, &id,
                                       &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "rchandle<T>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -252,7 +247,7 @@
     if (ar.get_is_temp_field_one_level())
     {
       is_temp = true;
-      ar.set_is_temp_field_one_level(true, ar.get_is_temp_field_also_for_ptr());
+      ar.set_is_temp_field_one_level(true);
     }
 
     T* p;
@@ -294,8 +289,9 @@
       if (ar.get_is_temp_field_one_level())
       {
         is_temp = true;
-        ar.set_is_temp_field_one_level(true, ar.get_is_temp_field_also_for_ptr());
-        if(!ar.get_is_temp_field_also_for_ptr() && (allow_delay == ALLOW_DELAY))
+        ar.set_is_temp_field_one_level(true);
+
+        if (allow_delay == ALLOW_DELAY)
           ar.dont_allow_delay();
       }
       ar & p;
@@ -323,9 +319,6 @@
     bool retval = ar.read_next_field(&type, &value, &id,
                                      &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "const_rchandle<T>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 
@@ -334,7 +327,7 @@
     if (ar.get_is_temp_field_one_level())
     {
       is_temp = true;
-      ar.set_is_temp_field_one_level(true, ar.get_is_temp_field_also_for_ptr());
+      ar.set_is_temp_field_one_level(true);
     }
 
     T* p;
@@ -388,9 +381,6 @@
     retval = ar.read_next_field(&type, &value, &id,
                                 &is_simple, &is_class, &field_treat, &referencing);
 
-    if (!retval && ar.get_read_optional_field())
-      return;
-
     ar.check_nonclass_field(retval, type, "VariableQName<StringType>",
                             is_simple, is_class, field_treat, ARCHIVE_FIELD_NORMAL, id);
 

=== modified file 'src/zorbautils/hashmap.h'
--- src/zorbautils/hashmap.h	2012-04-16 20:56:43 +0000
+++ src/zorbautils/hashmap.h	2012-04-19 19:25:27 +0000
@@ -175,6 +175,15 @@
         thePos++;
     }
 
+    T& getKeyNonConst() const
+    {
+      ZORBA_FATAL(thePos < theHashTab->size(), "");
+
+      HASHENTRY<T, V>& entry = (*theHashTab)[thePos];
+
+      return entry.theItem;
+    }
+
   public:
     iterator() : theHashTab(NULL), thePos(-1) {}
 
@@ -228,11 +237,11 @@
       return entry.theItem;
     }
 
-    const V& getValue() const
+    V& getValue() const
     {
       ZORBA_FATAL(thePos < theHashTab->size(), "");
 
-      const HASHENTRY<T, V>& entry = (*theHashTab)[thePos];
+      HASHENTRY<T, V>& entry = (*theHashTab)[thePos];
 
       return entry.theValue;
     }
@@ -283,16 +292,19 @@
     ar & theHashTabSize;
     ar & theCompareFunction;
     bool sync = false;
-    if(ar.is_serializing_out())
+
+    if (ar.is_serializing_out())
     {
       SYNC_CODE(sync = (theMutexp == &theMutex));
     }
+
     ar.set_is_temp_field(true);
     ar & sync;
     ar.set_is_temp_field(false);
+
     ar & theUseTransfer;
 
-    if(!ar.is_serializing_out())
+    if (!ar.is_serializing_out())
     {
       //simulate constructor
       theNumEntries = 0;
@@ -307,30 +319,23 @@
     }
 
     ulong num_entries = theNumEntries;
+
     ar.set_is_temp_field(true);
     ar & num_entries;
     ar.set_is_temp_field(false);
 
-    if(ar.is_serializing_out())
+    if (ar.is_serializing_out())
     {
       iterator it;
-      ulong i = 0;
-      ar.set_is_temp_field_one_level(true);
-      for(it=begin(); it!=end(); ++it)
+      for (it = begin(); it != end(); ++it)
       {
-        T t = (*it).first;
-        V v = (*it).second;
-        ar & t;
-        ar & v;
-        i++;
+        ar & it.getKeyNonConst();
+        ar & it.getValue();
       }
-      ar.set_is_temp_field_one_level(false);
-      assert(i == num_entries);
     }
     else
     {
-      ar.set_is_temp_field_one_level(true);
-      for(ulong i = 0; i < num_entries; i++)
+      for (ulong i = 0; i < num_entries; ++i)
       {
         T t;
         V v;
@@ -340,9 +345,7 @@
         assert(insert_ret);
         (void)insert_ret;
       }
-      ar.set_is_temp_field_one_level(false);
     }
-
   }
 #endif
 

=== modified file 'test/rbkt/testdriver.cpp'
--- test/rbkt/testdriver.cpp	2012-04-16 20:56:43 +0000
+++ test/rbkt/testdriver.cpp	2012-04-19 19:25:27 +0000
@@ -390,10 +390,14 @@
         std::cout << "cannot open plan " << binary_path << std::endl;
         return 15;
       }
+
       bool load_ret;
-      zorba::TestSerializationCallback  serl_callback
-          (driverContext.theURIMappers, driverContext.theURLResolvers);
+
+      zorba::TestSerializationCallback serl_callback(driverContext.theURIMappers,
+                                                     driverContext.theURLResolvers);
+
       load_ret = lQuery->loadExecutionPlan(ifbinary, &serl_callback);
+
       if(!load_ret)
       {
         std::cout << "cannot load plan " << binary_path << std::endl;


Follow ups