From: caballero Date: Fri, 17 Apr 2009 16:51:09 +0000 (+0000) Subject: Added Timestamp database to avoid repetition of files on addition. X-Git-Tag: EED.02Oct2009~98 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=42f7c93a6f8fc2beeb8709be1b3616ec40f4699e;p=creaImageIO.git Added Timestamp database to avoid repetition of files on addition. --- diff --git a/src2/CMakeLists.txt b/src2/CMakeLists.txt index 83bab4a..18228ce 100644 --- a/src2/CMakeLists.txt +++ b/src2/CMakeLists.txt @@ -34,6 +34,7 @@ SET( SRCS # creaImageIOGimmick creaImageIOSynchronizer + creaImageIOTimestampDatabaseHandler # Abstract views creaImageIOGimmickView diff --git a/src2/creaImageIOGimmick.cpp b/src2/creaImageIOGimmick.cpp index 8d1583c..5ae8438 100644 --- a/src2/creaImageIOGimmick.cpp +++ b/src2/creaImageIOGimmick.cpp @@ -35,11 +35,11 @@ namespace creaImageIO // 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 mTreeHandlerMap["Local database"] = mLocalDatabase; - // Create or open local database if (! boost::filesystem::exists( GetLocalDatabasePath() ) ) { @@ -69,6 +69,37 @@ namespace creaImageIO } } + + + // Creates files and directories database + mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath()); + // Create or open local database + if (! boost::filesystem::exists( GetTimestampDatabasePath() ) ) + { + std::string mess = "Timestamp database '"; + mess += GetTimestampDatabasePath(); + mess += "' does not exist : creating it"; + GimmickMessage(1,mess<Create() ) + { + GimmickError("ERROR CREATING '"<Open() ) + { + GimmickError("ERROR OPENING '"<second; } + //======================================================================== + ///Returns the timestamp database handler + TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const + { + return mTimestampDatabase; + } + //======================================================================== /// Add the files to the tree handler @@ -189,6 +244,7 @@ namespace creaImageIO GimmickMessage(2,"Adding files to '"< #include +#include #include namespace creaImageIO @@ -100,6 +101,9 @@ namespace creaImageIO /// Returns the TreeHandler with a given name TreeHandler* GetTreeHandler(const std::string& name) const; + + /// Returns the TimestampDatabase + TimestampDatabaseHandler* GetTimestampDatabase() const; /// SQLiteTreeHandler* GetLocalDatabase() { return mLocalDatabase; } @@ -113,11 +117,13 @@ namespace creaImageIO const std::string& GetUserSettingsDirectory(); void CreateUserSettingsDirectory(); const std::string& GetLocalDatabasePath(); + const std::string& GetTimestampDatabasePath(); //============================================= private: SQLiteTreeHandler* mLocalDatabase; + TimestampDatabaseHandler* mTimestampDatabase; TreeHandlerMapType mTreeHandlerMap; Synchronizer* mSynchronizer; @@ -125,6 +131,7 @@ namespace creaImageIO std::string mHomeDirectory; std::string mUserSettingsDirectory; std::string mLocalDatabasePath; + std::string mTimestampDatabasePath; TreeHandlerImageAdder mImageAdder; }; diff --git a/src2/creaImageIOGimmickView.cpp b/src2/creaImageIOGimmickView.cpp index e076ef7..2c2507f 100644 --- a/src2/creaImageIOGimmickView.cpp +++ b/src2/creaImageIOGimmickView.cpp @@ -109,7 +109,7 @@ namespace creaImageIO i!= mGimmick->GetTreeHandlerMap().end(); ++i) { - this->CreateTreeView(i->second); + this->CreateTreeView(i->second, mGimmick->GetTimestampDatabase()); } } //====================================================================== diff --git a/src2/creaImageIOGimmickView.h b/src2/creaImageIOGimmickView.h index 83b7407..1e9968b 100644 --- a/src2/creaImageIOGimmickView.h +++ b/src2/creaImageIOGimmickView.h @@ -101,7 +101,7 @@ namespace creaImageIO void CreateTreeViews(); /// Create the tree view for TreeHandler provided - virtual void CreateTreeView( TreeHandler*) + virtual void CreateTreeView( TreeHandler*, TimestampDatabaseHandler* ) { GimmickError("INTERNAL ERROR : CreateTreeView not implemented"); } /// Updates the TreeView of given name from level l to bottom diff --git a/src2/creaImageIOSQLiteTreeHandler.cpp b/src2/creaImageIOSQLiteTreeHandler.cpp index 52042a2..6af107d 100644 --- a/src2/creaImageIOSQLiteTreeHandler.cpp +++ b/src2/creaImageIOSQLiteTreeHandler.cpp @@ -915,13 +915,56 @@ namespace creaImageIO <<"' in level "<< GetTree().GetLevelDescriptor(node->GetLevel()).GetName() <GetChildrenList().begin(); - i != node->GetChildrenList().end(); - i++) - { - DBRecursiveRemoveNode((*i)); - } + + if(node->GetNumberOfChildren()!=0) + { + Node::ChildrenListType::iterator i; + for (i = node->GetChildrenList().begin(); + i != node->GetChildrenList().end(); + i++) + { + DBRecursiveRemoveNode((*i)); + } + } + else if(node->GetLevel()GetLevel()+1,node->GetAttribute("ID")); + } + } + + //===================================================================== + void SQLiteTreeHandler::DBRecursiveRemoveNode(int level, std::string parentId) + { + std::stringstream out; + std::stringstream result; + out<<"SELECT ID FROM "< +#include + +#include "CppSQLite3.h" + +#include + +#include + +#include "wx/wx.h" +#include +#include + +#include +using namespace crea; + +#include +#include + +namespace creaImageIO +{ + using namespace tree; + //============================================================= + TimestampDatabaseHandler::TimestampDatabaseHandler(const std::string& filename) + : mFileName(filename) + { + mDB = new CppSQLite3DB; + GimmickMessage(1,"SQLite version : " + <SQLiteVersion())<< std::endl); + } + //============================================================= + + //============================================================= + TimestampDatabaseHandler::~TimestampDatabaseHandler() + { + delete mDB; + } + //============================================================= + //===================================================================== + bool TimestampDatabaseHandler::Open() + { + return DBOpen(); + } + + //===================================================================== + bool TimestampDatabaseHandler::Create() + { + return DBCreate(); + } + //===================================================================== + + + //===================================================================== + bool TimestampDatabaseHandler::Close() + { + return true; + } + //===================================================================== + + + //===================================================================== + bool TimestampDatabaseHandler::Destroy() + { + return false; + } + + + + + + + //===================================================================== + // SQLite DB specific methods + //===================================================================== + //===================================================================== +#define QUERYTIMESTAMPDB(QUER,RES) \ + try \ + { \ + GimmickMessage(2,"SQL query: '"<execQuery(QUER.c_str()); \ + } \ + catch (CppSQLite3Exception& e) \ + { \ + GimmickError("SQLite query '"<execDML(UP.c_str()); \ + } \ + catch (CppSQLite3Exception& e) \ + { \ + GimmickError("SQLite update '"<open(GetFileName().c_str()); + } + catch (CppSQLite3Exception& e) + { + GimmickError("Opening '"<open(GetFileName().c_str()); + } + catch (CppSQLite3Exception& e) + { + GimmickError(e.errorCode() << ":" + << e.errorMessage() <GetNumberOfChildren(); + if(n>0) + { + std::vector children=node->GetChildrenList(); + std::vector::iterator it; + for(it=children.begin();it!=children.end();++it) + { + RemoveNode(searchAtt,(*it)); + } + } + else if(node->GetLevel()==3) + { + RemoveFile(searchAtt,node->GetAttribute("FullFileName")); + } + + + } + //===================================================================== + void TimestampDatabaseHandler::RemoveFile(const std::string& searchAtt, const std::string& searchVal) + { + + std::stringstream result; + std::string sel="SELECT PARENT_ID FROM FILES WHERE "+searchAtt+"='"+searchVal+"'"; + + CppSQLite3Query q; + QUERYTIMESTAMPDB(sel,q); + + while (!q.eof()) + { + for (int fld = 0; fld < q.numFields(); fld++) + { + result< +#include +#include +class CppSQLite3DB; + +namespace creaImageIO +{ + using namespace std; +//======================================================================= + /// Concrete TreeHandler which manages a Tree stored in a sqlite database + class TimestampDatabaseHandler + { + public: + //==================================================================== + /// Ctor with database file name + TimestampDatabaseHandler(const std::string& filename); + /// Dtor + virtual ~TimestampDatabaseHandler(); + //==================================================================== + + //==================================================================== + /// Returns the sqlite db file name + const std::string& GetFileName() const { return mFileName; } + //==================================================================== + + //==================================================================== + // INITIALIZATION / FINALIZATION + //==================================================================== + + //==================================================================== + /// Opens an existing 'source' + bool Open(); + /// Closes the 'source' + bool Close(); + /// Creates a new 'source' + bool Create(); + /// Destroys the 'source' + bool Destroy(); + //==================================================================== + + //==================================================================== + // READ / WRITE + //==================================================================== + //==================================================================== + ///Returns the id of the path if it's indexed, blank otherwise + std::string IsIndexed(const std::string& path); + ///Sets the current path's parent + bool AddDirectory(const std::string& parent, + const std::string& path, + const time_t lastModif, + const time_t lastRead); + ///Adds a new file to the database without a parent + void AddFile(const std::string& path, const time_t lastModif, const time_t lastRead); + ///Adds a new file to the database with a parent + void AddFile(const std::string& parentId,const std::string& path, const time_t lastModif, const time_t lastRead); + ///Sets the attribute to the value passed as parameter where the searchParameter is searchValue + void SetAttribute(const std::string& attName, + const std::string& attValue, + const std::string& searchParam, + const std::string& searchValue); + ///Removes the given node + void RemoveNode(const std::string& searchAtt, tree::Node* node); + ///Removes the filename with the given pathname + void RemoveFile(const std::string& searchAtt, const std::string& searchVal); + ///Cleans the path name + void CleanName(std::string& str) const; + ///Checks the timestamp in the database and compares it with the given one. + //If there is a difference, it will return false, otherwise it will return true. + bool CheckTimestamp(const std::string pathId, const time_t lastModif); + + //==================================================================== + + + protected: + //====================================================================== + /// Open the database + bool DBOpen(); + //====================================================================== + //====================================================================== + // Creation + /// Creates a new database on disk and the tables + bool DBCreate(); + //====================================================================== + //====================================================================== + // Removes a file from the database + void DBRemove(const std::string& searchAtt, const std::string& searchVal); + + private: + /// The DB + CppSQLite3DB* mDB; + /// The physical location associated to the DicomDatabase (directory, db file...) + std::string mFileName; + + }; + // EO class + //======================================================================= + + +} // EO namespace creaImageIO + +// EOF +#endif + diff --git a/src2/creaImageIOTreeHandlerImageAdder.cpp b/src2/creaImageIOTreeHandlerImageAdder.cpp index 800a8b8..f5424b6 100644 --- a/src2/creaImageIOTreeHandlerImageAdder.cpp +++ b/src2/creaImageIOTreeHandlerImageAdder.cpp @@ -1,8 +1,15 @@ #include #include #include "boost/filesystem.hpp" +#include +#include + namespace fs = boost::filesystem; +using boost::filesystem::path; +using boost::next; +using boost::prior; + //using namespace crea; @@ -90,7 +97,8 @@ namespace creaImageIO mProgress.IncNumberScannedDirs(); if ( !fs::exists( dirpath ) ) return; - + time_t lastModif=fs::last_write_time(dirpath); + fs::directory_iterator end_itr; // default construction yields past-the-end for ( fs::directory_iterator itr( dirpath ); itr != end_itr; @@ -99,24 +107,32 @@ namespace creaImageIO // If is directory & recurse : do recurse if ( fs::is_directory(itr->status()) ) { - if (recursive) AddDirectoryRecursor( itr->string(), recursive); + if (recursive) + { + AddDirectoryRecursor( itr->string(), recursive); + } } else { - mProgress.IncNumberScannedFiles(); - if (IsHandledFile(itr->string())) - { - mProgress.IncNumberHandledFiles(); - AddFile( itr->string() ); - } - mProgressSignal(mProgress); - if (mProgress.GetStop()) - { - //itr = end_itr; - break; - } + bool valid=mTimestampHandler->AddDirectory(dirpath, itr->string(), lastModif, time(0)); + if(valid) + { + mProgress.IncNumberScannedFiles(); + if (IsHandledFile(itr->string())) + { + mProgress.IncNumberHandledFiles(); + AddFile( itr->string() ); + } + mProgressSignal(mProgress); + if (mProgress.GetStop()) + { + //itr = end_itr; + break; + } + } } } + } //======================================================================= diff --git a/src2/creaImageIOTreeHandlerImageAdder.h b/src2/creaImageIOTreeHandlerImageAdder.h index bcb74c0..e490509 100644 --- a/src2/creaImageIOTreeHandlerImageAdder.h +++ b/src2/creaImageIOTreeHandlerImageAdder.h @@ -2,6 +2,7 @@ #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__ #include +#include #include #include #include @@ -29,6 +30,8 @@ namespace creaImageIO ~TreeHandlerImageAdder(); /// Sets the TreeHandler void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;} + /// Sets the TimestampDatabaseHandler + void SetTimestampHandler(TimestampDatabaseHandler* tdh) { mTimestampHandler = tdh;} //==================================================================== //==================================================================== @@ -113,6 +116,7 @@ namespace creaImageIO bool recurse ); TreeHandler* mTreeHandler; + TimestampDatabaseHandler* mTimestampHandler; ImageReader mReader; Progress mProgress; diff --git a/src2/creaImageIOTreeView.cpp b/src2/creaImageIOTreeView.cpp index a34f0c6..42d4fd3 100644 --- a/src2/creaImageIOTreeView.cpp +++ b/src2/creaImageIOTreeView.cpp @@ -4,8 +4,9 @@ namespace creaImageIO { // CTor - TreeView::TreeView(TreeHandler* handler, GimmickView* gimmick ) + TreeView::TreeView(TreeHandler* handler, TimestampDatabaseHandler* tdh, GimmickView* gimmick ) : mTreeHandler(handler), + mTimestampDatabaseHandler (tdh), mGimmickView(gimmick) { GimmickDebugMessage(1,"TreeView::TreeView" diff --git a/src2/creaImageIOTreeView.h b/src2/creaImageIOTreeView.h index 886fc38..c5fab53 100644 --- a/src2/creaImageIOTreeView.h +++ b/src2/creaImageIOTreeView.h @@ -2,6 +2,7 @@ #define __creaImageIOTreeView_h_INCLUDED__ #include +#include #include #include @@ -22,7 +23,7 @@ namespace creaImageIO { public: /// Ctor - TreeView(TreeHandler*,GimmickView*); + TreeView(TreeHandler*,TimestampDatabaseHandler*,GimmickView*); /// Virtual destructor virtual ~TreeView(); @@ -52,11 +53,14 @@ namespace creaImageIO protected: TreeHandler* GetTreeHandler() { return mTreeHandler; } + TimestampDatabaseHandler* GetTimestampDatabaseHandler() { return mTimestampDatabaseHandler; } GimmickView* GetGimmickView() { return mGimmickView; } private: /// The TreeHandler with which it corresponds TreeHandler* mTreeHandler; + /// The Timestamp handler with which it corresponds + TimestampDatabaseHandler* mTimestampDatabaseHandler; /// The GimmickView which holds the TreeView GimmickView* mGimmickView; diff --git a/src2/creaImageIOWxGimmickView.cpp b/src2/creaImageIOWxGimmickView.cpp index dd8745f..6bcf489 100644 --- a/src2/creaImageIOWxGimmickView.cpp +++ b/src2/creaImageIOWxGimmickView.cpp @@ -206,13 +206,13 @@ namespace creaImageIO //====================================================================== /// Create the tree view for TreeHandler provided - void WxGimmickView::CreateTreeView( TreeHandler* h) + void WxGimmickView::CreateTreeView( TreeHandler* h, TimestampDatabaseHandler* tdh) { std::string name(h->GetTree().GetAttribute("Name")); GimmickMessage(2,"Creating the tree view for '"<< name<<"'"< #include - const std::string empty_string(""); //===================================================================== @@ -107,11 +106,12 @@ namespace creaImageIO //===================================================================== // CTor WxTreeView::WxTreeView(TreeHandler* handler, + TimestampDatabaseHandler* tdh, GimmickView* gimmick, wxWindow* parent, const wxWindowID id) : wxPanel(parent,id), - TreeView(handler,gimmick) + TreeView(handler, tdh, gimmick) { GimmickDebugMessage(1,"WxTreeView::WxTreeView" <RemoveNode("PATH",(*i)); GetTreeHandler()->Remove(*i); } @@ -377,9 +378,6 @@ namespace creaImageIO } - //===================================================================== - - //===================================================================== /// Updates a level of the view (adds or removes children, etc.) void WxTreeView::UpdateLevel( int level ) diff --git a/src2/creaImageIOWxTreeView.h b/src2/creaImageIOWxTreeView.h index 4b97590..9e2072e 100644 --- a/src2/creaImageIOWxTreeView.h +++ b/src2/creaImageIOWxTreeView.h @@ -24,7 +24,7 @@ namespace creaImageIO { public: /// Ctor - WxTreeView(TreeHandler*, GimmickView*, + WxTreeView(TreeHandler*, TimestampDatabaseHandler*, GimmickView*, wxWindow* parent, const wxWindowID id); /// Virtual destructor virtual ~WxTreeView(); @@ -35,7 +35,7 @@ namespace creaImageIO ///Removes selected nodes on given level virtual void RemoveSelected(); - + /// Callback for item selection void OnItemSelected(wxListEvent& event);