From 4f2a77976965f19640416afca24367d3037820b8 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 24 Apr 2017 20:51:02 -0700 Subject: JSONDB performance improvements, threading fix. --- controller/JSONDB.hpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'controller/JSONDB.hpp') diff --git a/controller/JSONDB.hpp b/controller/JSONDB.hpp index 2d3a5224..a045d1b4 100644 --- a/controller/JSONDB.hpp +++ b/controller/JSONDB.hpp @@ -72,29 +72,28 @@ public: template inline void filter(const std::string &prefix,F func) { - Mutex::Lock _l(_db_m); - while (!_ready) { Thread::sleep(250); - _ready = _reload(_basePath,std::string()); + _reload(_basePath,std::string()); } - - for(std::map::iterator i(_db.lower_bound(prefix));i!=_db.end();) { - if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) { - if (!func(i->first,get(i->first))) { - std::map::iterator i2(i); ++i2; - this->erase(i->first); - i = i2; - } else ++i; - } else break; + { + Mutex::Lock _l(_db_m); + for(std::map::iterator i(_db.lower_bound(prefix));i!=_db.end();) { + if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) { + if (!func(i->first,i->second.obj)) { + this->_erase(i->first); + _db.erase(i++); + } else { + ++i; + } + } else break; + } } } - inline bool operator==(const JSONDB &db) const { return ((_basePath == db._basePath)&&(_db == db._db)); } - inline bool operator!=(const JSONDB &db) const { return (!(*this == db)); } - private: - bool _reload(const std::string &p,const std::string &b); + void _erase(const std::string &n); + void _reload(const std::string &p,const std::string &b); bool _isValidObjectName(const std::string &n); std::string _genPath(const std::string &n,bool create); -- cgit v1.2.3