Element SQLite cache : replace the column mtime by uuid.

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4223 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-10-03 15:41:59 +00:00
parent 4edd1f3f24
commit 3e42c3918b
5 changed files with 170 additions and 88 deletions

View File

@@ -29,8 +29,26 @@ bool ElementDefinition::hasParentCategory() {
} }
/** /**
@return la categorie a laquelle appartient cet element * @brief ElementDefinition::uuid
*/ * @return The uuid of this element definition.
* If uuid can't be found, return a null QUuid.
*/
QUuid ElementDefinition::uuid()
{
if (!m_uuid.isNull()) return m_uuid;
//Get the uuid of element
QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid");
if (!list_.isEmpty())
m_uuid = QUuid(list_.first().attribute("uuid"));
else
qDebug() << "The element : " << filePath() << "haven't got an uuid, please edit and save this element with element editor to create an uuid";
return m_uuid;
}
ElementsCategory *ElementDefinition::parentCategory() { ElementsCategory *ElementDefinition::parentCategory() {
return(parent_category_); return(parent_category_);
} }

View File

@@ -32,17 +32,19 @@ class ElementDefinition : public ElementsCollectionItem {
/** /**
Constructor Constructor
*/ */
ElementDefinition(ElementsCategory *category = 0, ElementsCollection *collection = 0) : ElementsCollectionItem(category), parent_category_(category), parent_collection_(collection) {}; ElementDefinition(ElementsCategory *category = 0, ElementsCollection *collection = 0) :
ElementsCollectionItem(category),
/** parent_category_(category),
Destructor parent_collection_(collection)
*/ {};
virtual ~ElementDefinition() {}; virtual ~ElementDefinition() {};
/** /**
@return the XML definition of a particular element @return the XML definition of a particular element
*/ */
virtual QDomElement xml() = 0; virtual QDomElement xml() = 0;
virtual QUuid uuid();
/** /**
Specify the XML definition of a particular element Specify the XML definition of a particular element
@@ -124,5 +126,6 @@ class ElementDefinition : public ElementsCollectionItem {
private: private:
ElementsCategory *parent_category_; ElementsCategory *parent_category_;
ElementsCollection *parent_collection_; ElementsCollection *parent_collection_;
QUuid m_uuid;
}; };
#endif #endif

View File

@@ -22,6 +22,7 @@
#include "factory/elementfactory.h" #include "factory/elementfactory.h"
#include "element.h" #include "element.h"
#include <QImageWriter> #include <QImageWriter>
#include "qet.h"
/** /**
Construct a cache for elements collections. Construct a cache for elements collections.
@@ -38,28 +39,64 @@ ElementsCollectionCache::ElementsCollectionCache(const QString &database_path, Q
QString connection_name = QString("ElementsCollectionCache-%1").arg(cache_instances++); QString connection_name = QString("ElementsCollectionCache-%1").arg(cache_instances++);
cache_db_ = QSqlDatabase::addDatabase("QSQLITE", connection_name); cache_db_ = QSqlDatabase::addDatabase("QSQLITE", connection_name);
cache_db_.setDatabaseName(database_path); cache_db_.setDatabaseName(database_path);
if (!cache_db_.open()) {
if (!cache_db_.open())
qDebug() << "Unable to open the SQLite database " << database_path << " as " << connection_name << ": " << cache_db_.lastError(); qDebug() << "Unable to open the SQLite database " << database_path << " as " << connection_name << ": " << cache_db_.lastError();
} else { else
{
cache_db_.exec("PRAGMA temp_store = MEMORY"); cache_db_.exec("PRAGMA temp_store = MEMORY");
cache_db_.exec("PRAGMA journal_mode = MEMORY"); cache_db_.exec("PRAGMA journal_mode = MEMORY");
cache_db_.exec("PRAGMA page_size = 4096"); cache_db_.exec("PRAGMA page_size = 4096");
cache_db_.exec("PRAGMA cache_size = 16384"); cache_db_.exec("PRAGMA cache_size = 16384");
cache_db_.exec("PRAGMA locking_mode = EXCLUSIVE"); cache_db_.exec("PRAGMA locking_mode = EXCLUSIVE");
cache_db_.exec("PRAGMA synchronous = OFF"); cache_db_.exec("PRAGMA synchronous = OFF");
/// @todo the tables could already exist, handle that case.
cache_db_.exec("CREATE TABLE names (path VARCHAR(512) NOT NULL, locale VARCHAR(2) NOT NULL, mtime DATETIME NOT NULL, name VARCHAR(128), PRIMARY KEY(path, locale));"); //TODO This code remove old table with mtime for create table with uuid, created at version 0,5
cache_db_.exec("CREATE TABLE pixmaps (path VARCHAR(512) NOT NULL UNIQUE, mtime DATETIME NOT NULL, pixmap BLOB, PRIMARY KEY(path), FOREIGN KEY(path) REFERENCES names (path) ON DELETE CASCADE);"); //see to remove this code at version 0,6 or 0,7 when all users will table with uuid.
QSqlQuery table_name(cache_db_);
// prepare queries if (table_name.exec("PRAGMA table_info(names)"))
{
if (table_name.seek(2))
{
QString str = table_name.value(1).toString();
table_name.finish();
if (str == "mtime")
{
QSqlQuery error;
error = cache_db_.exec("DROP TABLE names");
error = cache_db_.exec("DROP TABLE pixmaps");
}
}
else
table_name.finish();
}
//@TODO the tables could already exist, handle that case.
cache_db_.exec("CREATE TABLE names"
"("
"path VARCHAR(512) NOT NULL,"
"locale VARCHAR(2) NOT NULL,"
"uuid VARCHAR(512) NOT NULL,"
"name VARCHAR(128),"
"PRIMARY KEY(path, locale)"
");");
cache_db_.exec("CREATE TABLE pixmaps"
"("
"path VARCHAR(512) NOT NULL UNIQUE,"
"uuid VARCHAR(512) NOT NULL,"
"pixmap BLOB, PRIMARY KEY(path),"
"FOREIGN KEY(path) REFERENCES names (path) ON DELETE CASCADE);");
// prepare queries
select_name_ = new QSqlQuery(cache_db_); select_name_ = new QSqlQuery(cache_db_);
select_pixmap_ = new QSqlQuery(cache_db_); select_pixmap_ = new QSqlQuery(cache_db_);
insert_name_ = new QSqlQuery(cache_db_); insert_name_ = new QSqlQuery(cache_db_);
insert_pixmap_ = new QSqlQuery(cache_db_); insert_pixmap_ = new QSqlQuery(cache_db_);
select_name_ -> prepare("SELECT name FROM names WHERE path = :path AND locale = :locale AND mtime = :file_mtime"); select_name_ -> prepare("SELECT name FROM names WHERE path = :path AND locale = :locale AND uuid = :uuid");
select_pixmap_ -> prepare("SELECT pixmap FROM pixmaps WHERE path = :path AND mtime = :file_mtime"); select_pixmap_ -> prepare("SELECT pixmap FROM pixmaps WHERE path = :path AND uuid = :uuid");
insert_name_ -> prepare("REPLACE INTO names (path, locale, mtime, name) VALUES (:path, :locale, :mtime, :name)"); insert_name_ -> prepare("REPLACE INTO names (path, locale, uuid, name) VALUES (:path, :locale, :uuid, :name)");
insert_pixmap_ -> prepare("REPLACE INTO pixmaps (path, mtime, pixmap) VALUES (:path, :mtime, :pixmap)"); insert_pixmap_ -> prepare("REPLACE INTO pixmaps (path, uuid, pixmap) VALUES (:path, :uuid, :pixmap)");
} }
} }
@@ -150,24 +187,29 @@ void ElementsCollectionCache::endCollection(ElementsCollection *collection) {
@see name() @see name()
@return True if the retrieval succeeded, false otherwise. @return True if the retrieval succeeded, false otherwise.
*/ */
bool ElementsCollectionCache::fetchElement(ElementDefinition *element) { bool ElementsCollectionCache::fetchElement(ElementDefinition *element)
// can we use the cache with this element? {
// can we use the cache with this element?
bool use_cache = cache_db_.isOpen() && element -> parentCollection() -> isCacheable(); bool use_cache = cache_db_.isOpen() && element -> parentCollection() -> isCacheable();
// attempt to fetch the element name from the cache database // attempt to fetch the element name from the cache database
if (!use_cache) { if (!use_cache)
{
return(fetchData(element -> location())); return(fetchData(element -> location()));
} else { }
else
{
QString element_path = element -> location().toString(); QString element_path = element -> location().toString();
QDateTime mtime = element -> modificationTime(); bool got_name = fetchNameFromCache(element_path, element->uuid());
bool got_name = fetchNameFromCache(element_path, mtime); bool got_pixmap = fetchPixmapFromCache(element_path, element->uuid());
bool got_pixmap = fetchPixmapFromCache(element_path, mtime); if (got_name && got_pixmap)
if (got_name && got_pixmap) { {
return(true); return(true);
} }
if (fetchData(element -> location())) { if (fetchData(element -> location()))
cacheName(element_path, mtime); {
cachePixmap(element_path, mtime); cacheName(element_path, element->uuid());
cachePixmap(element_path, element->uuid());
} }
return(true); return(true);
} }
@@ -208,42 +250,49 @@ bool ElementsCollectionCache::fetchData(const ElementsLocation &location) {
} }
/** /**
Retrieve the name for an element, given its path and last modification * @brief ElementsCollectionCache::fetchNameFromCache
time. The value is then available through the name() method. * Retrieve the name for an element, given its path and uuid
@param path Element path (as obtained using ElementsLocation::toString()) * The value is then available through the name() method.
@param file_mtime Date and time of last modification of this element. Any * @param path : Element path (as obtained using ElementsLocation::toString())
older cached value will be ignored. * @param uuid : Element uuid
@return True if the retrieval succeeded, false otherwise. * @return True if the retrieval succeeded, false otherwise.
*/ */
bool ElementsCollectionCache::fetchNameFromCache(const QString &path, const QDateTime &file_mtime) { bool ElementsCollectionCache::fetchNameFromCache(const QString &path, const QUuid &uuid)
{
select_name_ -> bindValue(":path", path); select_name_ -> bindValue(":path", path);
select_name_ -> bindValue(":locale", locale_); select_name_ -> bindValue(":locale", locale_);
select_name_ -> bindValue(":file_mtime", file_mtime); select_name_ -> bindValue(":uuid", uuid.toString());
if (select_name_ -> exec()) { if (select_name_ -> exec())
if (select_name_ -> first()) { {
if (select_name_ -> first())
{
current_name_ = select_name_ -> value(0).toString(); current_name_ = select_name_ -> value(0).toString();
select_name_ -> finish(); select_name_ -> finish();
return(true); return(true);
} }
} else {
qDebug() << "select_name_->exec() failed";
} }
else
qDebug() << "select_name_->exec() failed";
return(false); return(false);
} }
/** /**
Retrieve the pixmap for an element, given its path and last modification * @brief ElementsCollectionCache::fetchPixmapFromCache
time. It is then available through the pixmap() method. * Retrieve the pixmap for an element, given its path and uuid.
@param path Element path (as obtained using ElementsLocation::toString()) * It is then available through the pixmap() method.
@param file_mtime Date and time of last modification of this element. Any * @param path : Element path (as obtained using ElementsLocation::toString())
older cached pixmap will be ignored. * @param uuid : Element uuid
@return True if the retrieval succeeded, false otherwise. * @return True if the retrieval succeeded, false otherwise.
*/ */
bool ElementsCollectionCache::fetchPixmapFromCache(const QString &path, const QDateTime &file_mtime) { bool ElementsCollectionCache::fetchPixmapFromCache(const QString &path, const QUuid &uuid)
{
select_pixmap_ -> bindValue(":path", path); select_pixmap_ -> bindValue(":path", path);
select_pixmap_ -> bindValue(":file_mtime", file_mtime); select_pixmap_ -> bindValue(":uuid", uuid.toString());
if (select_pixmap_ -> exec()) { if (select_pixmap_ -> exec())
if (select_pixmap_ -> first()) { {
if (select_pixmap_ -> first())
{
QByteArray ba = select_pixmap_ -> value(0).toByteArray(); QByteArray ba = select_pixmap_ -> value(0).toByteArray();
// avoid returning always the same pixmap (i.e. same cacheKey()) // avoid returning always the same pixmap (i.e. same cacheKey())
current_pixmap_.detach(); current_pixmap_.detach();
@@ -251,26 +300,29 @@ bool ElementsCollectionCache::fetchPixmapFromCache(const QString &path, const QD
select_pixmap_ -> finish(); select_pixmap_ -> finish();
} }
return(true); return(true);
} else {
qDebug() << "select_pixmap_->exec() failed";
} }
else
qDebug() << "select_pixmap_->exec() failed";
return(false); return(false);
} }
/** /**
Cache the current (i.e. last retrieved) name. The cache entry will use * @brief ElementsCollectionCache::cacheName
the current date and time and the locale set via setLocale(). * Cache the current (i.e. last retrieved) name The cache entry will use the locale set via setLocale().
@param path Element path (as obtained using ElementsLocation::toString()) * @param path : Element path (as obtained using ElementsLocation::toString())
@param mtime Modification time associated with the cache entry -- defaults to current datetime * @param uuid :Element uuid
@return True if the caching succeeded, false otherwise. * @return True if the caching succeeded, false otherwise.
@see name() * @see name()
*/ */
bool ElementsCollectionCache::cacheName(const QString &path, const QDateTime &mtime) { bool ElementsCollectionCache::cacheName(const QString &path, const QUuid &uuid)
{
insert_name_ -> bindValue(":path", path); insert_name_ -> bindValue(":path", path);
insert_name_ -> bindValue(":locale", locale_); insert_name_ -> bindValue(":locale", locale_);
insert_name_ -> bindValue(":mtime", QVariant(mtime)); insert_name_ -> bindValue(":uuid", uuid.toString());
insert_name_ -> bindValue(":name", current_name_); insert_name_ -> bindValue(":name", current_name_);
if (!insert_name_ -> exec()) { if (!insert_name_ -> exec())
{
qDebug() << cache_db_.lastError(); qDebug() << cache_db_.lastError();
return(false); return(false);
} }
@@ -278,22 +330,24 @@ bool ElementsCollectionCache::cacheName(const QString &path, const QDateTime &mt
} }
/** /**
Cache the current (i.e. last retrieved) pixmap. The cache entry will use * @brief ElementsCollectionCache::cachePixmap
the current date and time. * Cache the current (i.e. last retrieved) pixmap
@param path Element path (as obtained using ElementsLocation::toString()) * @param path : Element path (as obtained using ElementsLocation::toString())
@param mtime Modification time associated with the cache entry -- defaults to current datetime * @param uuid : Element uuid
@return True if the caching succeeded, false otherwise. * @return True if the caching succeeded, false otherwise.
@see pixmap() * @see pixmap()
*/ */
bool ElementsCollectionCache::cachePixmap(const QString &path, const QDateTime &mtime) { bool ElementsCollectionCache::cachePixmap(const QString &path, const QUuid &uuid)
{
QByteArray ba; QByteArray ba;
QBuffer buffer(&ba); QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
current_pixmap_.save(&buffer, qPrintable(pixmap_storage_format_)); current_pixmap_.save(&buffer, qPrintable(pixmap_storage_format_));
insert_pixmap_ -> bindValue(":path", path); insert_pixmap_ -> bindValue(":path", path);
insert_pixmap_ -> bindValue(":mtime", QVariant(mtime)); insert_pixmap_ -> bindValue(":uuid", uuid.toString());
insert_pixmap_ -> bindValue(":pixmap", QVariant(ba)); insert_pixmap_ -> bindValue(":pixmap", QVariant(ba));
if (!insert_pixmap_->exec()) { if (!insert_pixmap_->exec())
{
qDebug() << cache_db_.lastError(); qDebug() << cache_db_.lastError();
return(false); return(false);
} }

View File

@@ -29,7 +29,8 @@ class ElementDefinition;
definitions of elements and building full CustomElement objects when definitions of elements and building full CustomElement objects when
(re)loading the elements panel. (re)loading the elements panel.
*/ */
class ElementsCollectionCache : public QObject { class ElementsCollectionCache : public QObject
{
public: public:
// constructor, destructor // constructor, destructor
ElementsCollectionCache(const QString &database_path, QObject * = 0); ElementsCollectionCache(const QString &database_path, QObject * = 0);
@@ -47,10 +48,10 @@ class ElementsCollectionCache : public QObject {
QString name() const; QString name() const;
QPixmap pixmap() const; QPixmap pixmap() const;
bool fetchData(const ElementsLocation &); bool fetchData(const ElementsLocation &);
bool fetchNameFromCache(const QString &, const QDateTime &); bool fetchNameFromCache(const QString &path, const QUuid &uuid);
bool fetchPixmapFromCache(const QString &, const QDateTime &); bool fetchPixmapFromCache(const QString &path, const QUuid &uuid);
bool cacheName(const QString &, const QDateTime & = QDateTime::currentDateTime()); bool cacheName(const QString &path, const QUuid &uuid = QUuid::createUuid());
bool cachePixmap(const QString &, const QDateTime & = QDateTime::currentDateTime()); bool cachePixmap(const QString &path, const QUuid &uuid = QUuid::createUuid());
// attributes // attributes
private: private:

View File

@@ -110,25 +110,31 @@ QString FileElementDefinition::virtualPath() {
} }
/** /**
Recharge l'element * @brief FileElementDefinition::reload
*/ * Reload this file element definition
void FileElementDefinition::reload() { */
if (file_path.isEmpty()) { void FileElementDefinition::reload()
{
if (file_path.isEmpty())
{
is_null = true; is_null = true;
return; return;
} }
// recupere le chemin du fichier *.elmt correspondant // recupere le chemin du fichier *.elmt correspondant
QFileInfo file_info(file_path); QFileInfo file_info(file_path);
if (!file_info.exists() || !file_info.isReadable()) { if (!file_info.exists() || !file_info.isReadable())
{
is_null = true; is_null = true;
return; return;
} }
file_path = file_info.canonicalFilePath(); file_path = file_info.canonicalFilePath();
if (parentCollection()) { if (parentCollection())
{
ElementsCollectionCache *cache = parentCollection() -> cache(); ElementsCollectionCache *cache = parentCollection() -> cache();
if (cache && cache -> fetchNameFromCache(location().toString(), file_info.lastModified())) { if (cache && cache -> fetchNameFromCache(location().toString(), uuid()))
{
// the element file has not been modified since the last time // the element file has not been modified since the last time
// we put its name in cache: we do not need to load it. // we put its name in cache: we do not need to load it.
is_null = false; is_null = false;