mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-26 15:47:19 +02:00
DOM Caching
- Implemented DOM caching on name. May need a 'clear' method later.
This commit is contained in:
parent
b49523c06d
commit
5808e2073f
2 changed files with 35 additions and 18 deletions
50
src/DOM.cpp
50
src/DOM.cpp
|
|
@ -71,18 +71,26 @@ DOM::~DOM ()
|
||||||
// system.os
|
// system.os
|
||||||
const std::string DOM::get (const std::string& name)
|
const std::string DOM::get (const std::string& name)
|
||||||
{
|
{
|
||||||
|
// Cache test.
|
||||||
|
std::map <std::string, std::string>::iterator hit = _cache.find (name);
|
||||||
|
if (hit != _cache.end ())
|
||||||
|
return hit->second;
|
||||||
|
|
||||||
int len = name.length ();
|
int len = name.length ();
|
||||||
Nibbler n (name);
|
Nibbler n (name);
|
||||||
|
|
||||||
// Primitives
|
// Primitives
|
||||||
if (is_primitive (name))
|
if (is_primitive (name))
|
||||||
|
{
|
||||||
|
_cache[name] = name;
|
||||||
return name;
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
// rc. --> context.config
|
// rc. --> context.config
|
||||||
else if (len > 3 &&
|
else if (len > 3 &&
|
||||||
name.substr (0, 3) == "rc.")
|
name.substr (0, 3) == "rc.")
|
||||||
{
|
{
|
||||||
return context.config.get (name.substr (3));
|
return _cache[name] = context.config.get (name.substr (3));
|
||||||
}
|
}
|
||||||
|
|
||||||
// context.*
|
// context.*
|
||||||
|
|
@ -90,25 +98,25 @@ const std::string DOM::get (const std::string& name)
|
||||||
name.substr (0, 8) == "context.")
|
name.substr (0, 8) == "context.")
|
||||||
{
|
{
|
||||||
if (name == "context.program")
|
if (name == "context.program")
|
||||||
return context.args[0].first;
|
return _cache[name] = context.args[0].first;
|
||||||
|
|
||||||
else if (name == "context.args")
|
else if (name == "context.args")
|
||||||
{
|
{
|
||||||
std::string combined;
|
std::string combined;
|
||||||
join (combined, " ", context.args.list ());
|
join (combined, " ", context.args.list ());
|
||||||
return combined;
|
return _cache[name] = combined;
|
||||||
}
|
}
|
||||||
else if (name == "context.width")
|
else if (name == "context.width")
|
||||||
{
|
{
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
s << context.terminal_width;
|
s << context.terminal_width;
|
||||||
return s.str ();
|
return _cache[name] = s.str ();
|
||||||
}
|
}
|
||||||
else if (name == "context.height")
|
else if (name == "context.height")
|
||||||
{
|
{
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
s << context.terminal_height;
|
s << context.terminal_height;
|
||||||
return s.str ();
|
return _cache[name] = s.str ();
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
@ -124,39 +132,39 @@ const std::string DOM::get (const std::string& name)
|
||||||
{
|
{
|
||||||
// Taskwarrior version number.
|
// Taskwarrior version number.
|
||||||
if (name == "system.version")
|
if (name == "system.version")
|
||||||
return VERSION;
|
return _cache[name] = VERSION;
|
||||||
|
|
||||||
#ifdef HAVE_LIBLUA
|
#ifdef HAVE_LIBLUA
|
||||||
// Lua version number.
|
// Lua version number.
|
||||||
else if (name == "system.lua.version")
|
else if (name == "system.lua.version")
|
||||||
return LUA_RELEASE;
|
return _cache[name] = LUA_RELEASE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// OS type.
|
// OS type.
|
||||||
else if (name == "system.os")
|
else if (name == "system.os")
|
||||||
#if defined (DARWIN)
|
#if defined (DARWIN)
|
||||||
return "Darwin";
|
return _cache[name] = "Darwin";
|
||||||
#elif defined (SOLARIS)
|
#elif defined (SOLARIS)
|
||||||
return "Solaris";
|
return _cache[name] = "Solaris";
|
||||||
#elif defined (CYGWIN)
|
#elif defined (CYGWIN)
|
||||||
return "Cygwin";
|
return _cache[name] = "Cygwin";
|
||||||
#elif defined (OPENBSD)
|
#elif defined (OPENBSD)
|
||||||
return "OpenBSD";
|
return _cache[name] = "OpenBSD";
|
||||||
#elif defined (HAIKU)
|
#elif defined (HAIKU)
|
||||||
return "Haiku";
|
return _cache[name] = "Haiku";
|
||||||
#elif defined (FREEBSD)
|
#elif defined (FREEBSD)
|
||||||
return "FreeBSD";
|
return _cache[name] = "FreeBSD";
|
||||||
#elif defined (LINUX)
|
#elif defined (LINUX)
|
||||||
return "Linux";
|
return _cache[name] = "Linux";
|
||||||
#else
|
#else
|
||||||
return STRING_DOM_UNKNOWN
|
return _cache[name] = STRING_DOM_UNKNOWN
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
else
|
else
|
||||||
throw format (STRING_DOM_UNREC, name);
|
throw format (STRING_DOM_UNREC, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return _cache[name] = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
@ -197,13 +205,18 @@ const std::string DOM::get (const std::string& name)
|
||||||
//
|
//
|
||||||
const std::string DOM::get (const std::string& name, Task& task)
|
const std::string DOM::get (const std::string& name, Task& task)
|
||||||
{
|
{
|
||||||
|
// Cache test.
|
||||||
|
std::map <std::string, std::string>::iterator hit = _cache.find (name);
|
||||||
|
if (hit != _cache.end ())
|
||||||
|
return hit->second;
|
||||||
|
|
||||||
Nibbler n (name);
|
Nibbler n (name);
|
||||||
int id;
|
int id;
|
||||||
std::string uuid;
|
std::string uuid;
|
||||||
|
|
||||||
// Primitives
|
// Primitives
|
||||||
if (is_primitive (name))
|
if (is_primitive (name))
|
||||||
return name;
|
return _cache[name] = name;
|
||||||
|
|
||||||
// <id>.<name>
|
// <id>.<name>
|
||||||
else if (n.getInt (id))
|
else if (n.getInt (id))
|
||||||
|
|
@ -291,7 +304,8 @@ void DOM::set (const std::string& name, const std::string& value)
|
||||||
if (len > 3 &&
|
if (len > 3 &&
|
||||||
name.substr (0, 3) == "rc.")
|
name.substr (0, 3) == "rc.")
|
||||||
{
|
{
|
||||||
return context.config.set (name.substr (3), value);
|
_cache[name] = value;
|
||||||
|
context.config.set (name.substr (3), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unrecognized --> error.
|
// Unrecognized --> error.
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,9 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool is_primitive (const std::string&);
|
bool is_primitive (const std::string&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map <std::string, std::string> _cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue