From 198a291007cd218066124c2ca3a88c872f880a58 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 14 Feb 2012 14:39:47 +0000 Subject: Pull the script location back into the core and add support for caching of parsed script documents --- project2/common/environment.cpp | 20 ++++++++++++++++---- project2/common/environment.h | 4 ++++ project2/common/scripts.h | 1 + project2/xml/xmlScriptParser.cpp | 20 +++++++++----------- project2/xml/xmlScriptParser.h | 1 + 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp index c0f84a2..9d79ca0 100644 --- a/project2/common/environment.cpp +++ b/project2/common/environment.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -109,11 +110,22 @@ ScriptReaderPtr Environment::resolveScript(const std::string & group, const std::string & name, bool ii) const { typedef std::map > ReaderLoaders; - BOOST_FOREACH(const ReaderLoaders::value_type & rl, *LoaderBase::objLoaders()) { - ScriptReaderPtr rs = rl.second->resolveScript(group, name); - if (rs) { - return rs; + boost::filesystem::path e(name); + while (!e.empty()) { + ScriptKey sk(group, e.string()); + ScriptCache::const_iterator i = scriptCache.find(sk); + if (i != scriptCache.end() && i->second->isCurrent()) { + return i->second; } + else { + BOOST_FOREACH(const ReaderLoaders::value_type & rl, *LoaderBase::objLoaders()) { + ScriptReaderPtr rs = rl.second->resolveScript(group, e.string()); + if (rs) { + return (scriptCache[sk] = rs); + } + } + } + e = e.parent_path(); } if (ii) { throw DependencyNotFound(group, name); diff --git a/project2/common/environment.h b/project2/common/environment.h index 944010a..accbb80 100644 --- a/project2/common/environment.h +++ b/project2/common/environment.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "optionsSource.h" #include "exceptions.h" @@ -40,6 +41,9 @@ class Environment { int slLevel; typedef std::vector ConfigsMap; ConfigsMap configs; + typedef boost::tuple ScriptKey; + typedef std::map ScriptCache; + mutable ScriptCache scriptCache; public: std::string datasourceRoot; diff --git a/project2/common/scripts.h b/project2/common/scripts.h index 497ea6d..020cf50 100644 --- a/project2/common/scripts.h +++ b/project2/common/scripts.h @@ -53,6 +53,7 @@ class ScriptReader : public virtual IntrusivePtrBase { public: virtual ScriptNodePtr root() const = 0; virtual void load(const CommonObjects *, bool childrenOnly) const; + virtual bool isCurrent() const = 0; mutable LoaderBase loader; }; diff --git a/project2/xml/xmlScriptParser.cpp b/project2/xml/xmlScriptParser.cpp index 47db20f..abb4cbe 100644 --- a/project2/xml/xmlScriptParser.cpp +++ b/project2/xml/xmlScriptParser.cpp @@ -37,20 +37,18 @@ XmlScriptParser::load(const CommonObjects * co, bool childrenOnly) const _root.reset(); } +bool +XmlScriptParser::isCurrent() const +{ + return false; +} + class XmlScriptReaderLoader : public ScriptReaderLoader { public: ScriptReaderPtr resolveScript(const std::string & group, const std::string & name) const { - boost::filesystem::path script(boost::filesystem::current_path() / group); - BOOST_FOREACH(const boost::filesystem::path & e, boost::filesystem::path(name)) { - if (boost::filesystem::is_directory(script / e)) { - script /= e; - } - else { - boost::filesystem::path path = (script / e).string() + ".xml"; - if (boost::filesystem::is_regular_file(path)) { - return new XmlScriptParser(path); - } - } + boost::filesystem::path script(boost::filesystem::current_path() / group / (name + ".xml")); + if (boost::filesystem::is_regular_file(script)) { + return new XmlScriptParser(script); } return NULL; } diff --git a/project2/xml/xmlScriptParser.h b/project2/xml/xmlScriptParser.h index 39e27bc..c85795a 100644 --- a/project2/xml/xmlScriptParser.h +++ b/project2/xml/xmlScriptParser.h @@ -40,6 +40,7 @@ class XmlScriptParser : public xmlpp::DomParser, public ScriptReader { XmlScriptParser(const boost::filesystem::path & file); ScriptNodePtr root() const; void load(const CommonObjects *, bool childrenOnly) const; + bool isCurrent() const; private: mutable ScriptNodePtr _root; }; -- cgit v1.2.3