[qet] [1366] The application now avoids reading and keeping in memory every element file in the collection .

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1366
Author:   xavier
Date:     2011-10-08 23:54:53 +0200 (Sat, 08 Oct 2011)
Log Message:
-----------
The application now avoids reading and keeping in memory every element file in the collection.

Modified Paths:
--------------
    branches/0.3/sources/elementscollection.cpp
    branches/0.3/sources/elementscollection.h
    branches/0.3/sources/elementscollectioncache.h
    branches/0.3/sources/fileelementdefinition.cpp
    branches/0.3/sources/fileelementscollection.cpp
    branches/0.3/sources/qetapp.cpp
    branches/0.3/sources/qetapp.h

Modified: branches/0.3/sources/elementscollection.cpp
===================================================================
--- branches/0.3/sources/elementscollection.cpp	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/elementscollection.cpp	2011-10-08 21:54:53 UTC (rev 1366)
@@ -25,7 +25,8 @@
 	@param parent Item parent
 */
 ElementsCollection::ElementsCollection(ElementsCollectionItem *parent) :
-	ElementsCollectionItem(parent)
+	ElementsCollectionItem(parent),
+	cache_(0)
 {
 }
 
@@ -414,3 +415,16 @@
 	return(result);
 }
 
+/**
+	@return The cache used by this collection, or 0 if this collection does not have any
+*/
+ElementsCollectionCache *ElementsCollection::cache() const {
+	return(cache_);
+}
+
+/**
+	@param cache The cache to be used by this collection
+*/
+void ElementsCollection::setCache(ElementsCollectionCache *cache) {
+	cache_ = cache;
+}

Modified: branches/0.3/sources/elementscollection.h
===================================================================
--- branches/0.3/sources/elementscollection.h	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/elementscollection.h	2011-10-08 21:54:53 UTC (rev 1366)
@@ -21,6 +21,7 @@
 #include "elementscollectionitem.h"
 class QETProject;
 class ElementsCategory;
+class ElementsCollectionCache;
 class ElementDefinition;
 class MoveElementsHandler;
 /**
@@ -74,7 +75,6 @@
 	virtual ElementDefinition *createElement(const QString &);
 	virtual bool isEmpty();
 	virtual int count();
-	virtual bool isCacheable() const = 0;
 	
 	// Methodes propres a la classe ElementsCollection
 	public:
@@ -83,6 +83,9 @@
 	*/
 	virtual ElementsCategory *rootCategory() = 0;
 	virtual ElementsCollectionItem *item(const QString &, bool = true);
+	virtual bool isCacheable() const = 0;
+	virtual ElementsCollectionCache *cache() const;
+	virtual void setCache(ElementsCollectionCache *);
 	
 	// attributs
 	protected:
@@ -90,5 +93,7 @@
 	QString protocol_;
 	/// Projet auquel appartient cette collection
 	QETProject *project_;
+	/// Optional cache used to improve performance
+	ElementsCollectionCache *cache_;
 };
 #endif

Modified: branches/0.3/sources/elementscollectioncache.h
===================================================================
--- branches/0.3/sources/elementscollectioncache.h	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/elementscollectioncache.h	2011-10-08 21:54:53 UTC (rev 1366)
@@ -46,8 +46,6 @@
 	bool fetchElement(ElementDefinition *);
 	QString name() const;
 	QPixmap pixmap() const;
-	
-	private:
 	bool fetchData(const ElementsLocation &);
 	bool fetchNameFromCache(const QString &, const QDateTime &);
 	bool fetchPixmapFromCache(const QString &, const QDateTime &);

Modified: branches/0.3/sources/fileelementdefinition.cpp
===================================================================
--- branches/0.3/sources/fileelementdefinition.cpp	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/fileelementdefinition.cpp	2011-10-08 21:54:53 UTC (rev 1366)
@@ -15,6 +15,7 @@
 	You should have received a copy of the GNU General Public License
 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
 */
+#include "elementscollectioncache.h"
 #include "fileelementdefinition.h"
 #include "fileelementscategory.h"
 #include "fileelementscollection.h"
@@ -43,7 +44,17 @@
 	@return la definition XML de l'element
 */
 QDomElement FileElementDefinition::xml() {
-	return(xml_element_.documentElement());
+	// ouvre le fichier
+	QFile file(file_path);
+	
+	// charge le contenu du fichier en s'attendant a du XML
+	is_null = !xml_element_.setContent(&file);
+	if (is_null) {
+		return(QDomElement());
+	} else {
+		// l'ouverture de la definition a reussi
+		return(xml_element_.documentElement());
+	}
 }
 
 /**
@@ -122,18 +133,21 @@
 	}
 	file_path = file_info.canonicalFilePath();
 	
-	// ouvre le fichier
-	QFile file(file_path);
-	
-	// charge le contenu du fichier en s'attendant a du XML
-	bool read_xml = xml_element_.setContent(&file);
-	if (!read_xml) {
-		is_null = true;
-		return;
+	if (parentCollection()) {
+		ElementsCollectionCache *cache = parentCollection() -> cache();
+		if (cache && cache -> fetchNameFromCache(location().toString(), file_info.lastModified())) {
+			// the element file has not been modified since the last time
+			// we put its name in cache: we do not need to load it.
+			is_null = false;
+			return;
+		}
 	}
 	
-	// l'ouverture de la definition a reussi
-	is_null = false;
+	// we need to ensure this is a valid XML document
+	QFile file(file_path);
+	QDomDocument xml_document;
+	is_null = !xml_document.setContent(&file);
+	xml_document.clear();
 }
 
 /**

Modified: branches/0.3/sources/fileelementscollection.cpp
===================================================================
--- branches/0.3/sources/fileelementscollection.cpp	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/fileelementscollection.cpp	2011-10-08 21:54:53 UTC (rev 1366)
@@ -30,7 +30,6 @@
 	protocol_ = "unknown";
 	project_ = 0;
 	root = 0;
-	reload();
 }
 
 /**

Modified: branches/0.3/sources/qetapp.cpp
===================================================================
--- branches/0.3/sources/qetapp.cpp	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/qetapp.cpp	2011-10-08 21:54:53 UTC (rev 1366)
@@ -21,6 +21,7 @@
 #include "qetdiagrameditor.h"
 #include "qetelementeditor.h"
 #include "elementscollectionitem.h"
+#include "elementscollectioncache.h"
 #include "fileelementscollection.h"
 #include "titleblocktemplate.h"
 #include "templateeditor.h"
@@ -42,6 +43,7 @@
 QString QETApp::lang_dir = QString();
 FileElementsCollection *QETApp::common_collection = 0;
 FileElementsCollection *QETApp::custom_collection = 0;
+ElementsCollectionCache *QETApp::collections_cache_ = 0;
 QMap<uint, QETProject *> QETApp::registered_projects_ = QMap<uint, QETProject *>();
 uint QETApp::next_project_id = 0;
 RecentFiles *QETApp::projects_recent_files_ = 0;
@@ -92,6 +94,13 @@
 	setQuitOnLastWindowClosed(false);
 	connect(this, SIGNAL(lastWindowClosed()), this, SLOT(checkRemainingWindows()));
 	
+	setSplashScreenStep(tr("Chargement... Initialisation du cache des collections d'\351l\351ments", "splash screen caption"));
+	if (!collections_cache_) {
+		QString cache_path = QETApp::configDir() + "/elements_cache.sqlite";
+		collections_cache_ = new ElementsCollectionCache(cache_path, this);
+		collections_cache_ -> setLocale(QLocale::system().name().left(2)); // @todo we need a unique function to get the good language
+	}
+	
 	// loads known collections into memory (this does not include items rendering made in elements panels)
 	setSplashScreenStep(tr("Chargement... Lecture des collections d'\351l\351ments", "splash screen caption"));
 	foreach(ElementsCollection *collection, availableCollections()) {
@@ -228,6 +237,7 @@
 	if (!common_collection) {
 		common_collection = new FileElementsCollection(QETApp::commonElementsDir());
 		common_collection -> setProtocol("common");
+		common_collection -> setCache(collections_cache_);
 	}
 	return(common_collection);
 }
@@ -239,6 +249,7 @@
 	if (!custom_collection) {
 		custom_collection = new FileElementsCollection(QETApp::customElementsDir());
 		custom_collection -> setProtocol("custom");
+		custom_collection -> setCache(collections_cache_);
 	}
 	return(custom_collection);
 }

Modified: branches/0.3/sources/qetapp.h
===================================================================
--- branches/0.3/sources/qetapp.h	2011-10-08 21:51:38 UTC (rev 1365)
+++ branches/0.3/sources/qetapp.h	2011-10-08 21:54:53 UTC (rev 1366)
@@ -26,6 +26,7 @@
 class QETDiagramEditor;
 class QETElementEditor;
 class ElementsCollection;
+class ElementsCollectionCache;
 class ElementsCollectionItem;
 class FileElementsCollection;
 class ElementsCategory;
@@ -137,6 +138,7 @@
 	
 	static FileElementsCollection *common_collection;
 	static FileElementsCollection *custom_collection;
+	static ElementsCollectionCache *collections_cache_;
 	static QMap<uint, QETProject *> registered_projects_;
 	static uint next_project_id;
 	static RecentFiles *projects_recent_files_;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/