In charge of the synchronization of the database and the disk state. More...
#include <creaImageIOSynchron.h>
Public Member Functions | |
Synchronizer (const std::string &path) | |
Ctor. | |
virtual | ~Synchronizer () |
Dtor. | |
void | Initialize () |
Initializes the database. | |
void | InsertAddOp (const std::string &path, const std::string &recursive, const std::string &nChildren, const std::string &refdb) |
Inserts an add operation to the database. | |
void | InsertIgnoreFile (const std::string &addKey, const std::string &path, const std::string &remove, const std::string &time, const std::string &refdb) |
Inserts a file to be ignored. | |
void | RemoveEntry (const std::string i_table, const std::string i_key) |
Removes an entry that matches the given parameter. | |
void | RemoveEntries (const std::string i_table, const std::string i_attribute, const std::string i_operand, const std::string i_key) |
Removes several entries. | |
void | GetFileList (std::vector< AddList > &files, const std::string &refdb) |
Gets the list of AddFiles. | |
void | GetIgnoredFiles (const std::string &key, std::vector< std::string > &ignoreList) |
Gets the list of ignored files. | |
std::string | GetAttribute (const std::string &attribute, const std::string &table, const std::string &searchParam, const std::string &searchValue, const std::string &refdb) |
Gets the attribute that matches the parameters. | |
void | SetAttribute (const std::string &attribute, const std::string &table, const std::string &value, const std::string &searchParam, const std::string &searchValue, const std::string &refdb) |
Sets an attribute to an entry that matches the given parameters. | |
void | GetList (const std::string i_db) |
bool | isIndexed (const std::string filename) |
Synchronizer (TreeHandler *th) | |
Ctor. | |
~Synchronizer () | |
Dtor. | |
void | SetTreeHandler (TreeHandler *handler) |
Sets the tree handler to use in order to synchronize with a given database. | |
std::string | Synchronize (bool update) |
bool | FileExists (std::string file) |
Checks if the file given as a parameter exists in the drive. | |
void | AttributesMatch (bool update, std::string file, std::stringstream &mess) |
Public Attributes | |
std::map< std::string, bool > | mList |
std::vector< AddList > | mAddList |
The current AddList. | |
std::vector< RemoveList > | mIgnoreList |
The current RemoveList. | |
Private Member Functions | |
void | CreateDB () |
Creates a new database. | |
void | UpdateAddList (const std::string &refdb) |
Updates the AddList. | |
void | CleanList (const std::string &refdb) |
Cleans the list in case operations are no longer useful (0 added files). | |
void | CleanName (std::string &str) const |
Cleans the name (changes slashes and backslashes according to the system). | |
std::vector< std::string > | GetIgnoreList (const std::string &i_key) |
Gets the ignore list. | |
const std::string | convert (const std::string &i_word) |
void | SynchronizeFile (bool update, std::string file, std::stringstream &message) |
Synchronizes the given file, doing the action required and returning the result on the string supplied. | |
Private Attributes | |
CppSQLite3DB * | mDB |
The DB. | |
std::string | pathDB |
Path of the current database. | |
TreeHandler * | mHandler |
The tree handler. | |
ImageReader | mReader |
The image reader. |
In charge of the synchronization of the database and the disk state.
Synchronizes a given database with disk.
Definition at line 61 of file creaImageIOSynchron.h.
creaImageIO::Synchronizer::Synchronizer | ( | const std::string & | path | ) |
Ctor.
Definition at line 41 of file creaImageIOSynchron.cpp.
References Initialize(), mDB, and pathDB.
{ pathDB = path + "maintenance_database.db3"; mDB = new CppSQLite3DB; Initialize(); }
creaImageIO::Synchronizer::~Synchronizer | ( | ) | [virtual] |
creaImageIO::Synchronizer::Synchronizer | ( | TreeHandler * | th | ) |
creaImageIO::Synchronizer::~Synchronizer | ( | ) |
Dtor.
void creaImageIO::Synchronizer::AttributesMatch | ( | bool | update, | |
std::string | file, | |||
std::stringstream & | mess | |||
) |
Checks if the attributes of the node given as a parameter matchwith it correspondent file in disk NB: This method doesn't check the existence of the file, so FileExists should be called before.
Definition at line 86 of file creaImageIOSynchronizer.cpp.
References creaImageIO::tree::Descriptor::BuildAttributeMap(), creaImageIO::TreeHandler::GetAttribute(), creaImageIO::tree::Tree::GetAttributeDescriptorList(), creaImageIO::tree::Tree::GetDescriptor(), creaImageIO::tree::Tree::GetNumberOfLevels(), creaImageIO::TreeHandler::GetTree(), mHandler, mReader, creaImageIO::ImageReader::ReadAttributes(), and creaImageIO::TreeHandler::SetAttribute().
Referenced by SynchronizeFile().
{ std::map< std::string, std::string> attr; mHandler->GetTree().GetDescriptor().BuildAttributeMap(attr); mReader.ReadAttributes(file,attr); tree::LevelDescriptor::AttributeDescriptorListType adl= mHandler->GetTree().GetAttributeDescriptorList(mHandler->GetTree().GetNumberOfLevels()-1); tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a; for (a = adl.begin();a!=adl.end();++a) { std::string databaseVal; mHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal); std::string fileVal=attr.find(a->GetKey())->second; if ( a->GetFlags()==0 && databaseVal.compare(fileVal)!=0 ) { if(update) { mHandler->SetAttribute("Image",a->GetKey(),fileVal,"FullFileName", file); mess<<file<<" has been updated in the DB"<<std::endl; } else { mess<<file<<" State: Attributes differ"<<std::endl; } } } }
void creaImageIO::Synchronizer::CleanList | ( | const std::string & | refdb | ) | [private] |
Cleans the list in case operations are no longer useful (0 added files).
Definition at line 184 of file creaImageIOSynchron.cpp.
References mAddList, RemoveEntry(), and UpdateAddList().
Referenced by GetFileList().
{ mAddList.clear(); UpdateAddList(refdb); std::vector<AddList>::iterator it_add = mAddList.begin(); for(;it_add <mAddList.end(); ++it_add) { if(it_add->nbFiles == "0") { RemoveEntry("ADD_OPS", it_add->key); RemoveEntry("IGNORED_FILES", it_add->key); } } mAddList.clear(); UpdateAddList(refdb); }
void creaImageIO::Synchronizer::CleanName | ( | std::string & | str | ) | const [private] |
Cleans the name (changes slashes and backslashes according to the system).
Definition at line 108 of file creaImageIOSynchron.cpp.
Referenced by GetAttribute(), InsertAddOp(), InsertIgnoreFile(), and SetAttribute().
{ size_t pos; do { pos = str.find('\\'); if (pos!=-1) { str.replace(pos, 1, "/"); } } while (pos!=-1); }
const std::string creaImageIO::Synchronizer::convert | ( | const std::string & | i_word | ) | [private] |
Definition at line 449 of file creaImageIOSynchron.cpp.
Referenced by GetAttribute(), InsertAddOp(), InsertIgnoreFile(), and SetAttribute().
{ std::string temp = i_word; boost::algorithm::replace_all(temp,"'","''"); return temp.c_str(); }
void creaImageIO::Synchronizer::CreateDB | ( | ) | [private] |
Creates a new database.
Definition at line 81 of file creaImageIOSynchron.cpp.
References mDB, CppSQLite3DB::open(), pathDB, and UPDATESYNCDB.
Referenced by Initialize().
{ mDB->open(pathDB.c_str()); // CREATING TABLES std::string command; command = "CREATE TABLE "; command += "ADD_OPS"; command += "\n(\nADD_KEY INTEGER PRIMARY KEY"; command += ",\nPATH text"; command += ",\nRECURSIVE boolean"; command += ",\nFILES_ADDED int"; command += ",\nREFERENCEDDB text"; command += "\n)"; UPDATESYNCDB(command); command = "CREATE TABLE "; command += "IGNORED_FILES"; command += "\n(\nID INTEGER PRIMARY KEY"; command += ",\nADD_KEY integer"; command += ",\nPATH text"; command += ",\nREMOVE boolean"; command += ",\nTIME datetext"; command += "\n)"; UPDATESYNCDB(command); }
bool creaImageIO::Synchronizer::FileExists | ( | std::string | file | ) |
Checks if the file given as a parameter exists in the drive.
Definition at line 73 of file creaImageIOSynchronizer.cpp.
References GimmickDebugMessage.
Referenced by SynchronizeFile().
{ GimmickDebugMessage(4,"Verifying if file "<<file<<" exists"<<std::endl); bool exists=true; if ( !fs::exists( file ) ) { exists=false; } return exists; }
std::string creaImageIO::Synchronizer::GetAttribute | ( | const std::string & | attribute, | |
const std::string & | table, | |||
const std::string & | searchParam, | |||
const std::string & | searchValue, | |||
const std::string & | refdb | |||
) |
Gets the attribute that matches the parameters.
Definition at line 315 of file creaImageIOSynchron.cpp.
References CleanName(), convert(), CppSQLite3Query::eof(), CppSQLite3Query::getStringField(), CppSQLite3Query::nextRow(), and QUERYSYNCDB.
Referenced by creaImageIO::TreeHandlerImageAdder::AddDirectory(), creaImageIO::TreeHandlerImageAdder::AddFiles(), creaImageIO::TreeHandlerImageAdder::CopyFiles(), InsertIgnoreFile(), creaImageIO::TreeHandlerImageAdder::remove(), and creaImageIO::TreeHandlerImageAdder::Synchronize().
{ std::stringstream query; std::string result; std::string sVal=convert(searchValue.c_str()); CleanName(sVal); query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal; if(table.compare("ADD_OPS")==0) { query<<"' AND REFERENCEDDB = '"<<refdb<<"';"; } else { query<<"';"; } CppSQLite3Query res; QUERYSYNCDB(query.str(), res); while (!res.eof()) { result=res.getStringField(0); res.nextRow(); } return result; }
void creaImageIO::Synchronizer::GetFileList | ( | std::vector< AddList > & | files, | |
const std::string & | refdb | |||
) |
Gets the list of AddFiles.
Definition at line 123 of file creaImageIOSynchron.cpp.
References CleanList(), and mAddList.
Referenced by creaImageIO::TreeHandlerImageAdder::Synchronize().
void creaImageIO::Synchronizer::GetIgnoredFiles | ( | const std::string & | key, | |
std::vector< std::string > & | ignoreList | |||
) |
Gets the list of ignored files.
Definition at line 130 of file creaImageIOSynchron.cpp.
References GetIgnoreList().
Referenced by creaImageIO::TreeHandlerImageAdder::Synchronize().
{ ignoreList=GetIgnoreList(key); }
std::vector< std::string > creaImageIO::Synchronizer::GetIgnoreList | ( | const std::string & | i_key | ) | [private] |
Gets the ignore list.
Definition at line 281 of file creaImageIOSynchron.cpp.
References CppSQLite3Query::eof(), mIgnoreList, CppSQLite3Query::nextRow(), QUERYSYNCDB, and creaImageIO::RemoveList::remove.
Referenced by GetIgnoredFiles().
{ mIgnoreList.clear(); std::vector<std::string> i_names; std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = "; query+=i_key; CppSQLite3Query res; QUERYSYNCDB(query, res); while (!res.eof()) { RemoveList temp = RemoveList(res); if(temp.remove.compare("0")==0) { mIgnoreList.push_back(temp); } res.nextRow(); } std::vector<RemoveList>::iterator it; for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it) { i_names.push_back((*it).path); } return i_names; }
void creaImageIO::Synchronizer::GetList | ( | const std::string | i_db | ) |
Definition at line 390 of file creaImageIOSynchron.cpp.
References CppSQLite3Query::eof(), CppSQLite3Query::getStringField(), mList, CppSQLite3Query::nextRow(), and QUERYSYNCDB.
Referenced by creaImageIO::TreeHandlerImageAdder::AddDirectory(), and creaImageIO::TreeHandlerImageAdder::AddFiles().
{ mList.clear(); std::vector<std::string> i_names; std::vector<std::string> keys; CppSQLite3Query res; std::string query ="SELECT ADD_KEY, REFERENCEDDB FROM ADD_OPS"; QUERYSYNCDB(query, res); keys.clear(); while (!res.eof()) { std::string key(res.getStringField(0)); std::string db(res.getStringField(1)); if (db == i_db) { keys.push_back(key); } res.nextRow(); } query = "SELECT PATH, REMOVE FROM IGNORED_FILES WHERE"; if(keys.size() > 0) { for (int i=0; i < keys.size(); i++) { query += " ADD_KEY = " + keys[i]; query += " AND"; } query = query.substr(0,query.size() - 4); } else { query += " ADD_KEY = -1"; } QUERYSYNCDB(query, res); while (!res.eof()) { std::string file(res.getStringField(0)); std::string ignore(res.getStringField(1)); mList[file] = ignore == "0"? true : false; res.nextRow(); } }
void creaImageIO::Synchronizer::Initialize | ( | ) |
Initializes the database.
Definition at line 55 of file creaImageIOSynchron.cpp.
References CreateDB(), CppSQLite3Exception::errorCode(), CppSQLite3Exception::errorMessage(), GimmickError, mDB, CppSQLite3DB::open(), and pathDB.
Referenced by Synchronizer().
{ if (!boost::filesystem::exists(pathDB)) { CreateDB(); } // OPENING else { try { mDB->open(pathDB.c_str()); } catch (CppSQLite3Exception& e) { GimmickError("Opening '"<<pathDB<<"' : " << e.errorCode() << ":" << e.errorMessage()); } } // get the ADD operations List //UpdateAddList(pathDB); }
void creaImageIO::Synchronizer::InsertAddOp | ( | const std::string & | path, | |
const std::string & | recursive, | |||
const std::string & | nChildren, | |||
const std::string & | refdb | |||
) |
Inserts an add operation to the database.
Definition at line 210 of file creaImageIOSynchron.cpp.
References CleanName(), convert(), and UPDATESYNCDB.
Referenced by creaImageIO::TreeHandlerImageAdder::AddDirectory(), creaImageIO::TreeHandlerImageAdder::AddFiles(), and creaImageIO::TreeHandlerImageAdder::CopyFiles().
{ std::string insert; std::string pat=path.c_str(); CleanName(pat); insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('"; insert+=convert(pat)+"','"; insert+=recursive+"',"; insert+=nChildren+",'"; insert+=refdb+"');"; UPDATESYNCDB(insert); }
void creaImageIO::Synchronizer::InsertIgnoreFile | ( | const std::string & | addKey, | |
const std::string & | path, | |||
const std::string & | remove, | |||
const std::string & | time, | |||
const std::string & | refdb | |||
) |
Inserts a file to be ignored.
Definition at line 232 of file creaImageIOSynchron.cpp.
References CleanName(), convert(), GetAttribute(), SetAttribute(), and UPDATESYNCDB.
Referenced by creaImageIO::TreeHandlerImageAdder::AddDirectoryRecursor(), creaImageIO::TreeHandlerImageAdder::AddFiles(), creaImageIO::TreeHandlerImageAdder::CopyFiles(), and creaImageIO::TreeHandlerImageAdder::Synchronize().
{ std::string pat=path.c_str(); CleanName(pat); std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb); if(id.compare("")==0) { std::string insert; insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('"; insert+=addKey+"','"; insert+=convert(pat)+"','"; insert+=remove+"',"; insert+=time+");"; UPDATESYNCDB(insert); } else { //Gets the add key std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb); //gets the parent database to check if the file has been added to the current database std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb); //If there is no such entry, add it if(parentDB.compare("")==0) { std::string insert; insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('"; insert+=addKey+"','"; insert+=convert(pat)+"','"; insert+=remove+"',"; insert+=time+");"; UPDATESYNCDB(insert); } else { //Sets the new add key attribute for the file SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb); //Sets the new remove attribute for the file SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb); //Sets the new time attribute for the file SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb); } } }
bool creaImageIO::Synchronizer::isIndexed | ( | const std::string | filename | ) |
Definition at line 433 of file creaImageIOSynchron.cpp.
References mList.
Referenced by creaImageIO::TreeHandlerImageAdder::AddDirectoryRecursor(), and creaImageIO::TreeHandlerImageAdder::AddFiles().
{ bool valid = true; std::string name(filename); boost::algorithm::replace_all( name,"\\" , "/"); std::map <std::string, bool>::iterator it_list = mList.begin(); for(;it_list != mList.end(); it_list++) { if(it_list->first == name) { valid = false; break; } } return valid; }
void creaImageIO::Synchronizer::RemoveEntries | ( | const std::string | i_table, | |
const std::string | i_attribute, | |||
const std::string | i_operand, | |||
const std::string | i_key | |||
) |
Removes several entries.
Definition at line 169 of file creaImageIOSynchron.cpp.
References UPDATESYNCDB.
Referenced by creaImageIO::TreeHandlerImageAdder::DeleteDriveFromOtherDB().
{ std::stringstream query; query<<"DELETE FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'"; UPDATESYNCDB(query.str()); }
void creaImageIO::Synchronizer::RemoveEntry | ( | const std::string | i_table, | |
const std::string | i_key | |||
) |
Removes an entry that matches the given parameter.
Definition at line 155 of file creaImageIOSynchron.cpp.
References UPDATESYNCDB.
Referenced by CleanList().
{ std::string query = "DELETE FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'"; UPDATESYNCDB(query); }
void creaImageIO::Synchronizer::SetAttribute | ( | const std::string & | attribute, | |
const std::string & | table, | |||
const std::string & | value, | |||
const std::string & | searchParam, | |||
const std::string & | searchValue, | |||
const std::string & | refdb | |||
) |
Sets an attribute to an entry that matches the given parameters.
Definition at line 353 of file creaImageIOSynchron.cpp.
References CleanName(), convert(), and UPDATESYNCDB.
Referenced by creaImageIO::TreeHandlerImageAdder::AddDirectory(), creaImageIO::TreeHandlerImageAdder::CopyFiles(), InsertIgnoreFile(), creaImageIO::TreeHandlerImageAdder::remove(), and creaImageIO::TreeHandlerImageAdder::Synchronize().
{ std::string val=value.c_str(); std::string sVal=convert(searchValue.c_str()); CleanName(val); CleanName(sVal); std::string sql = "UPDATE "; sql+=table; sql+=" SET "; sql += attribute; sql += " = '"; sql += val; sql += "' WHERE "; sql += searchParam; sql += " = '"; sql += sVal; if(table.compare("ADD_OPS")==0) { sql += "' AND REFERENCEDDB = '"; sql += refdb; } sql += "';"; UPDATESYNCDB(sql); }
void creaImageIO::Synchronizer::SetTreeHandler | ( | TreeHandler * | handler | ) | [inline] |
Sets the tree handler to use in order to synchronize with a given database.
Definition at line 20 of file creaImageIOSynchronizer.h.
References mHandler.
{mHandler=handler;}
std::string creaImageIO::Synchronizer::Synchronize | ( | bool | update | ) |
Synchronizes the database in the current tree handler with disk by doing the passed action. If it is true, the database will be updated, otherwise a warning message will be returned.
Definition at line 26 of file creaImageIOSynchronizer.cpp.
References creaImageIO::TreeHandler::GetAttribute(), GimmickMessage, mHandler, and SynchronizeFile().
{ GimmickMessage(1,"Synchronizing "<<std::endl); int id=1; std::stringstream mess; std::string file; mHandler->GetAttribute("Image","","","FullFileName",file); size_t ini=0; size_t fin=0; while(fin<file.size()-1) { fin=file.find('#',ini); SynchronizeFile(update,file.substr(ini,fin-ini),mess); ini=fin+1; } if(mess.str()=="") { mess<<"Database up to date"<<std::endl; } GimmickMessage(1,mess.str()); return mess.str(); }
void creaImageIO::Synchronizer::SynchronizeFile | ( | bool | update, | |
std::string | file, | |||
std::stringstream & | message | |||
) | [private] |
Synchronizes the given file, doing the action required and returning the result on the string supplied.
Definition at line 51 of file creaImageIOSynchronizer.cpp.
References AttributesMatch(), creaImageIO::TreeHandler::DeleteTuple(), FileExists(), and mHandler.
Referenced by Synchronize().
{ if(!FileExists(file)) { if(update) { mHandler->DeleteTuple("Image","FullFileName",file); mess<<file<<" has been removed from the DB"<<std::endl; } else { mess<<file<<" State: Non existant"<<std::endl; } } else { AttributesMatch(update,file,mess); } }
void creaImageIO::Synchronizer::UpdateAddList | ( | const std::string & | refdb | ) | [private] |
Updates the AddList.
Definition at line 136 of file creaImageIOSynchron.cpp.
References CppSQLite3Query::eof(), mAddList, CppSQLite3Query::nextRow(), and QUERYSYNCDB.
Referenced by CleanList().
{ std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';"; CppSQLite3Query res; QUERYSYNCDB(query, res); while (!res.eof()) { AddList temp = AddList(res); mAddList.push_back(temp); res.nextRow(); } }
std::vector<AddList> creaImageIO::Synchronizer::mAddList |
The current AddList.
Definition at line 112 of file creaImageIOSynchron.h.
Referenced by CleanList(), GetFileList(), and UpdateAddList().
CppSQLite3DB* creaImageIO::Synchronizer::mDB [private] |
The DB.
Definition at line 118 of file creaImageIOSynchron.h.
Referenced by CreateDB(), Initialize(), Synchronizer(), and ~Synchronizer().
TreeHandler* creaImageIO::Synchronizer::mHandler [private] |
The tree handler.
Definition at line 33 of file creaImageIOSynchronizer.h.
Referenced by AttributesMatch(), SetTreeHandler(), Synchronize(), and SynchronizeFile().
std::vector<RemoveList> creaImageIO::Synchronizer::mIgnoreList |
The current RemoveList.
Definition at line 114 of file creaImageIOSynchron.h.
Referenced by GetIgnoreList().
std::map<std::string, bool> creaImageIO::Synchronizer::mList |
Definition at line 110 of file creaImageIOSynchron.h.
Referenced by GetList(), and isIndexed().
The image reader.
Definition at line 35 of file creaImageIOSynchronizer.h.
Referenced by AttributesMatch().
std::string creaImageIO::Synchronizer::pathDB [private] |
Path of the current database.
Definition at line 120 of file creaImageIOSynchron.h.
Referenced by CreateDB(), Initialize(), and Synchronizer().