real time indexer: monitor the configuration for changes and reexecute when needed

This commit is contained in:
Jean-Francois Dockes
2012-03-06 09:35:21 +01:00
parent 7ce1a5f2e4
commit 2c6b023a88
8 changed files with 87 additions and 8 deletions
+25 -3
View File
@@ -23,6 +23,7 @@
#include <unistd.h> // for access(2)
#include <ctype.h>
#include <fnmatch.h>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
@@ -145,13 +146,13 @@ void ConfSimple::parseinput(istream &input)
ConfSimple::ConfSimple(int readonly, bool tildexp)
: dotildexpand(tildexp), m_holdWrites(false)
: dotildexpand(tildexp), m_fmtime(0), m_holdWrites(false)
{
status = readonly ? STATUS_RO : STATUS_RW;
}
ConfSimple::ConfSimple(const string& d, int readonly, bool tildexp)
: dotildexpand(tildexp), m_holdWrites(false)
: dotildexpand(tildexp), m_fmtime(0), m_holdWrites(false)
{
status = readonly ? STATUS_RO : STATUS_RW;
@@ -160,7 +161,7 @@ ConfSimple::ConfSimple(const string& d, int readonly, bool tildexp)
}
ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
: dotildexpand(tildexp), m_filename(fname), m_holdWrites(false)
: dotildexpand(tildexp), m_filename(fname), m_fmtime(0), m_holdWrites(false)
{
status = readonly ? STATUS_RO : STATUS_RW;
@@ -193,6 +194,7 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
}
parseinput(input);
i_changed(true);
}
ConfSimple::StatusCode ConfSimple::getStatus() const
@@ -204,6 +206,26 @@ ConfSimple::StatusCode ConfSimple::getStatus() const
}
}
bool ConfSimple::sourceChanged()
{
return i_changed(false);
}
bool ConfSimple::i_changed(bool upd)
{
if (!m_filename.empty()) {
struct stat st;
if (stat(m_filename.c_str(), &st) == 0) {
if (m_fmtime != st.st_mtime) {
if (upd)
m_fmtime = st.st_mtime;
return true;
}
}
}
return false;
}
int ConfSimple::get(const string &nm, string &value, const string &sk) const
{
if (!ok())
+16
View File
@@ -107,6 +107,7 @@ public:
virtual list<string> getSubKeys() = 0;
virtual list<string> getSubKeys(bool) = 0;
virtual bool holdWrites(bool) = 0;
virtual bool sourceChanged() = 0;
};
/**
@@ -140,6 +141,9 @@ public:
virtual ~ConfSimple() {};
/** Origin file changed. Only makes sense if we read the data from a file */
virtual bool sourceChanged();
/**
* Decide if we actually rewrite the backing-store after modifying the
* tree.
@@ -252,6 +256,7 @@ protected:
private:
// Set if we're working with a file
string m_filename;
time_t m_fmtime;
// Configuration data submaps (one per subkey, the main data has a
// null subkey)
map<string, map<string, string> > m_submaps;
@@ -267,6 +272,7 @@ private:
// Internal version of set: no RW checking
virtual int i_set(const string &nm, const string &val,
const string &sk, bool init = false);
bool i_changed(bool upd);
};
/**
@@ -366,6 +372,16 @@ public:
return *this;
}
virtual bool sourceChanged()
{
typename list<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->sourceChanged())
return true;
}
return false;
}
virtual int get(const string &name, string &value, const string &sk) const
{
typename list<T*>::const_iterator it;