From 66f2d68a504d06b45b4047bd7a445c11889b9fc0 Mon Sep 17 00:00:00 2001 From: caballero Date: Fri, 10 Apr 2009 14:34:15 +0000 Subject: [PATCH] Added synchronization classes and methods. --- src2/CMakeLists.txt | 3 +- src2/creaImageIOGimmick.cpp | 23 +++++- src2/creaImageIOGimmick.h | 6 ++ src2/creaImageIOGimmickView.cpp | 3 + src2/creaImageIOSQLiteTreeHandler.cpp | 88 +++++++++++++++++++- src2/creaImageIOSQLiteTreeHandler.h | 36 +++++++- src2/creaImageIOSynchronizer.cpp | 114 ++++++++++++++++++++++++++ src2/creaImageIOSynchronizer.h | 43 ++++++++++ src2/creaImageIOTreeHandler.h | 18 ++++ 9 files changed, 328 insertions(+), 6 deletions(-) create mode 100644 src2/creaImageIOSynchronizer.cpp create mode 100644 src2/creaImageIOSynchronizer.h diff --git a/src2/CMakeLists.txt b/src2/CMakeLists.txt index b3c1c0a..83bab4a 100644 --- a/src2/CMakeLists.txt +++ b/src2/CMakeLists.txt @@ -33,6 +33,7 @@ SET( SRCS # creaImageIOGimmick + creaImageIOSynchronizer # Abstract views creaImageIOGimmickView @@ -50,7 +51,7 @@ SET( SRCS # Viewer creaImageIOWxViewer -creaImageIOImagePointerHolder.h + creaImageIOImagePointerHolder.h ) diff --git a/src2/creaImageIOGimmick.cpp b/src2/creaImageIOGimmick.cpp index ced62fa..8d1583c 100644 --- a/src2/creaImageIOGimmick.cpp +++ b/src2/creaImageIOGimmick.cpp @@ -34,7 +34,7 @@ namespace creaImageIO CreateUserSettingsDirectory(); // Sets the current directory to the home dir mCurrentDirectory = GetHomeDirectory(); - + mSynchronizer=0; // Create local database handler mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath()); // Add it to the TreeHandlerMap @@ -202,13 +202,32 @@ namespace creaImageIO GimmickMessage(2,"Adding dir '"<SetTreeHandler(handler); + } + mSynchronizer->Synchronize(update); + + } + //======================================================================== /// void Gimmick::Print(const std::string& d) diff --git a/src2/creaImageIOGimmick.h b/src2/creaImageIOGimmick.h index cdb7676..93d982a 100644 --- a/src2/creaImageIOGimmick.h +++ b/src2/creaImageIOGimmick.h @@ -3,6 +3,7 @@ #include #include +#include namespace creaImageIO { @@ -89,6 +90,10 @@ namespace creaImageIO void AddDir(const std::string& handler, const std::string& path, bool recurse); + ///Synchronizes the loaded data with the database. If remove is true the database will be updated, otherwise + ///only a warning sign will be issued + void Synchronize(bool update, TreeHandler* handler); + /// Prints the tree handled by the handler void Print(const std::string& handler); @@ -114,6 +119,7 @@ namespace creaImageIO private: SQLiteTreeHandler* mLocalDatabase; TreeHandlerMapType mTreeHandlerMap; + Synchronizer* mSynchronizer; std::string mCurrentDirectory; std::string mHomeDirectory; diff --git a/src2/creaImageIOGimmickView.cpp b/src2/creaImageIOGimmickView.cpp index 8c0884c..40bd219 100644 --- a/src2/creaImageIOGimmickView.cpp +++ b/src2/creaImageIOGimmickView.cpp @@ -162,7 +162,10 @@ namespace creaImageIO { ImageExtent * extent= (ImageExtent*)&ie; mExtent[2]+=(*extent).Get(2); + if(mExtent[2]>1) + { SetDimension(3); + } } //====================================================================== diff --git a/src2/creaImageIOSQLiteTreeHandler.cpp b/src2/creaImageIOSQLiteTreeHandler.cpp index 17025b4..8dcdd17 100644 --- a/src2/creaImageIOSQLiteTreeHandler.cpp +++ b/src2/creaImageIOSQLiteTreeHandler.cpp @@ -188,6 +188,24 @@ namespace creaImageIO return DBSetAttribute(n,key,value); } //===================================================================== + //===================================================================== + /// Sets an attribute + void SQLiteTreeHandler::SetAttribute(const std::string& levelDescriptor, + const std::string& key, + const std::string& value, + const std::string& searchParam, + const std::string& searchVal) + { + DBSetAttribute(levelDescriptor,key,value,searchParam, searchVal); + } + //===================================================================== + /// Deletes a tuple + void SQLiteTreeHandler::DeleteTuple(std::string levelDescriptor, + std::string key, std::string value) + { + DBDelete(levelDescriptor,key,value); + } + //===================================================================== @@ -858,10 +876,30 @@ namespace creaImageIO // sql += " LIMIT 1"; UPDATEDB(sql); } - //===================================================================== + //===================================================================== + /// Sets an attribute of a Node + void SQLiteTreeHandler::DBSetAttribute(const std::string& levelDescriptor, + const std::string& key, + const std::string& value, + const std::string& searchParam, + const std::string& searchVal) + { - //===================================================================== + std::string sql = "UPDATE "; + sql += levelDescriptor; + sql += " SET "; + sql += key; + sql += " = '"; + sql += value; + sql += "' WHERE "; + sql += searchParam; + sql += " = '"; + sql += searchVal; + sql += "'"; + UPDATEDB(sql); + } + //===================================================================== void SQLiteTreeHandler::DBRecursiveRemoveNode(Node* node) { @@ -887,6 +925,52 @@ namespace creaImageIO } } + //===================================================================== + void SQLiteTreeHandler::DBDelete(std::string levelDescriptor, std::string key, std::string value) + { + + std::stringstream query; + query<<"DELETE FROM "< +#include +#include "boost/filesystem.hpp" + +namespace fs = boost::filesystem; + +namespace creaImageIO +{ + + //============================================================== + Synchronizer::Synchronizer(TreeHandler * th) + : mHandler(th) + { + + } + //============================================================== + + //============================================================== + Synchronizer::~Synchronizer() + { + + } + //============================================================== + + //============================================================== + std::string Synchronizer::Synchronize(bool update) + { + GimmickMessage(1,"Synchronizing "<GetAttribute("Image","","","FullFileName",file); + size_t ini=0; + size_t fin=0; + while(finDeleteTuple("Image","FullFileName",file); + mess< 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< +#include + +namespace creaImageIO +{ + +//======================================================================= +/// Synchronizes a given database with disk +class Synchronizer + { + public: + /// Ctor + Synchronizer(TreeHandler* th); + /// Dtor + ~Synchronizer(); + ///Sets the tree handler to use in order to synchronize with a given database + void SetTreeHandler(TreeHandler * handler){mHandler=handler;} + ///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. + std::string Synchronize(bool update); + ///Checks if the file given as a parameter exists in the drive + bool FileExists(std::string file); + ///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. + void AttributesMatch(bool update, std::string file, std::stringstream& mess); + + + private: + ///The tree handler + TreeHandler* mHandler; + ///The image reader + ImageReader mReader; + ///Synchronizes the given file, doing the action required and returning the result on the string supplied + void SynchronizeFile(bool update, std::string file, std::stringstream& message); + + }; +} // EO namespace creaImageIO + +// EOF +#endif \ No newline at end of file diff --git a/src2/creaImageIOTreeHandler.h b/src2/creaImageIOTreeHandler.h index 16f599c..5a7cecd 100644 --- a/src2/creaImageIOTreeHandler.h +++ b/src2/creaImageIOTreeHandler.h @@ -79,6 +79,15 @@ namespace creaImageIO virtual unsigned int GetNumberOfChildren(tree::Node* n) { return 0; } //==================================================================== + //==================================================================== + /// Returns the attribute requested. Useful for synchronization. + virtual void GetAttribute(std::string levelDescriptor, + std::string searchParam, + std::string searchVal, + std::string key, + std::string& result){} + //==================================================================== + //==================================================================== /// Recursively loads the children of node 'parent' until maxlevel // is reached. @@ -112,6 +121,15 @@ namespace creaImageIO virtual bool SetAttribute(tree::Node*, const std::string& key, const std::string& value) { return false; } + // Sets an attribute + virtual void SetAttribute(const std::string& levelDescriptor, + const std::string& key, + const std::string& value, + const std::string& searchParam, + const std::string& searchVal){} + //Deletes the tuple that matches the parameters given + virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value){} + //==================================================================== -- 2.46.1