Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Draft: implement a draft of transitional tag management design
Dmitry Kazakov31c7d8e2e01c
diff --git a/libs/resources/KisMemoryStorage.h b/libs/resources/KisMemoryStorage.hindex 987d01defc..24403c0fb9 100644--- a/libs/resources/KisMemoryStorage.h+++ b/libs/resources/KisMemoryStorage.h@@ -48,6 +48,7 @@ public:private:friend class TestMemoryStorage;friend class TestResourceLocator;+ friend class TestStorageWrapper;bool testingRemoveResource(const QString &url);bool testingAddTag(const QString &resourceType, KisTagSP tag);bool testingRemoveTag(const QString &resourceType, const QString &tagUrl);diff --git a/libs/resources/KisResourceCacheDb.cpp b/libs/resources/KisResourceCacheDb.cppindex 28765001aa..c0dadddf78 100644--- a/libs/resources/KisResourceCacheDb.cpp+++ b/libs/resources/KisResourceCacheDb.cpp@@ -506,6 +506,43 @@ bool KisResourceCacheDb::initialize(const QString &location)return s_valid;}+std::pair<QVector<int>,QVector<int>> KisResourceCacheDb::tagsForStorage(const QString &resourceType, const QString &storageLocation)+{+ try {+ KisSqlQueryLoader loader(":/sql/storage_tags_ref_count.sql", KisSqlQueryLoader::single_statement_mode);+ loader.query().bindValue(":resource_type", resourceType);+ loader.query().bindValue(":location", changeToEmptyIfNull(storageLocation));+ loader.exec();++ QVector<int> uniqueTags;+ QVector<int> sharedTags;++ while (loader.query().next()) {+ if (loader.query().value("ref_count").toInt() > 1) {+ sharedTags << loader.query().value("tag_id").toInt();+ } else {+ uniqueTags << loader.query().value("tag_id").toInt();+ }+ }++ return {uniqueTags, sharedTags};++ } catch (const KisSqlQueryLoader::FileException &e) {+ qWarning().noquote() << "ERROR: deleteStorage:" << e.message;+ qWarning().noquote() << " file:" << e.filePath;+ qWarning().noquote() << " error:" << e.fileErrorString;+ return {};+ } catch (const KisSqlQueryLoader::SQLException &e) {+ qWarning().noquote() << "ERROR: deleteStorage:" << e.message;+ qWarning().noquote() << " file:" << e.filePath;+ qWarning().noquote() << " statement:" << e.statementIndex;+ qWarning().noquote() << " error:" << e.sqlError.text();+ return {};+ }++ return {};+}+QVector<int> KisResourceCacheDb::resourcesForStorage(const QString &resourceType, const QString &storageLocation){QVector<int> result;@@ -1405,7 +1442,6 @@ bool KisResourceCacheDb::linkTagToStorage(const QString &url, const QString &resbool KisResourceCacheDb::addTag(const QString &resourceType, const QString storageLocation, KisTagSP tag){-if (hasTag(tag->url(), resourceType)) {// Check whether this storage is already registered for this tagQSqlQuery q;@@ -1418,6 +1454,7 @@ bool KisResourceCacheDb::addTag(const QString &resourceType, const QString stora"AND tags.resource_type_id = (SELECT id\n"" FROM resource_types\n"" WHERE name = :resource_type)\n"+ "AND storages.location = :storage_location\n""AND tags.url = :url")){qWarning() << "Could not prepare select tags from tags_storages query" << q.lastError();@@ -1425,6 +1462,7 @@ bool KisResourceCacheDb::addTag(const QString &resourceType, const QString storaq.bindValue(":url", tag->url());q.bindValue(":resource_type", resourceType);+ q.bindValue(":storage_location", changeToEmptyIfNull(KisResourceLocator::instance()->makeStorageLocationRelative(storageLocation)));if (!q.exec()) {qWarning() << "Could not execute tags_storages query" << q.boundValues() << q.lastError();@@ -1753,38 +1791,31 @@ bool KisResourceCacheDb::deleteStorage(QString location)// attempt to cache them. Which means we can never drop any tables while// there is an instance of those models in existence, even just creating// and dropping an empty temporary table fails.- QVariantList tagIdsToDelete;+ QVariantList uniqueTagIdsToDelete;{- KisSqlQueryLoader loader("inline://gather_tag_ids_to_delete",- "SELECT tags_storages.tag_id tag_id\n "- "FROM tags_storages\n"- "WHERE tags_storages.storage_id =\n"- " (SELECT storages.id\n"- " FROM storages\n"- " WHERE storages.location = :location)\n",- KisSqlQueryLoader::single_statement_mode);- loader.query().bindValue(":location", changeToEmptyIfNull(location));- loader.exec();- QSqlQuery &query = loader.query();- while (query.next()) {- tagIdsToDelete.append(query.value(0).toLongLong());- }+ auto [unique, shared] = tagsForStorage(ResourceType::PaintOpPresets, location);+ std::copy(unique.begin(), unique.end(), std::back_inserter(uniqueTagIdsToDelete));}- if (!tagIdsToDelete.isEmpty()) {- {+ {KisSqlQueryLoader loader("inline://delete_tags_storage_links_for_storage",- "DELETE FROM tags_storages WHERE tag_id = ?",+ "WITH storage_id_query AS (\n"+ " SELECT storages.id\n"+ " FROM storages\n"+ " WHERE storages.location = :location)\n"+ "DELETE FROM tags_storages\n"+ "WHERE storage_id IN storage_id_query\n",KisSqlQueryLoader::single_statement_mode);- loader.query().addBindValue(tagIdsToDelete);- loader.execBatch();- }+ loader.query().bindValue(":location", changeToEmptyIfNull(location));+ loader.exec();+ }+ if (!uniqueTagIdsToDelete.isEmpty()) {{KisSqlQueryLoader loader("inline://delete_tags_translations_for_storage","DELETE FROM tag_translations WHERE tag_id = ?",KisSqlQueryLoader::single_statement_mode);- loader.query().addBindValue(tagIdsToDelete);+ loader.query().addBindValue(uniqueTagIdsToDelete);loader.execBatch();}@@ -1792,7 +1823,7 @@ bool KisResourceCacheDb::deleteStorage(QString location)KisSqlQueryLoader loader("inline://delete_resource_tags_for_storage","DELETE FROM resource_tags WHERE tag_id = ?",KisSqlQueryLoader::single_statement_mode);- loader.query().addBindValue(tagIdsToDelete);+ loader.query().addBindValue(uniqueTagIdsToDelete);loader.execBatch();}@@ -1800,7 +1831,7 @@ bool KisResourceCacheDb::deleteStorage(QString location)KisSqlQueryLoader loader("inline://delete_tags_for_storage","DELETE FROM tags WHERE id = ?",KisSqlQueryLoader::single_statement_mode);- loader.query().addBindValue(tagIdsToDelete);+ loader.query().addBindValue(uniqueTagIdsToDelete);loader.execBatch();}}diff --git a/libs/resources/KisResourceCacheDb.h b/libs/resources/KisResourceCacheDb.hindex 3b20a07d40..94206268f0 100644--- a/libs/resources/KisResourceCacheDb.h+++ b/libs/resources/KisResourceCacheDb.h@@ -92,6 +92,17 @@ private:*/static bool registerResourceType(const QString &resourceType);+ /**+ * Returns a list of tags related to the storage+ *+ * The first item of the pair represents the tags that are linked to+ * this very storage uniquely. The second item of the pair lists the+ * tags that are shared with other storages.+ */+ static std::pair<QVector<int>,QVector<int>> tagsForStorage(const QString &resourceType, const QString &storageLocation);+ /**+ * Returns a list of resources owned by the storage+ */static QVector<int> resourcesForStorage(const QString &resourceType, const QString &storageLocation);static int resourceIdForResource(const QString &resourceFileName, const QString &resourceType, const QString &storageLocation);static bool resourceNeedsUpdating(int resourceId, QDateTime timestamp);diff --git a/libs/resources/KisResourceLocator.cpp b/libs/resources/KisResourceLocator.cppindex 3d5c8434e7..a3a19b62b8 100644--- a/libs/resources/KisResourceLocator.cpp+++ b/libs/resources/KisResourceLocator.cpp@@ -822,7 +822,7 @@ void KisResourceLocator::setMetaDataForStorage(const QString &storageLocation, Q}}-void KisResourceLocator::purge(const QString &storageLocation)+void KisResourceLocator::purge(const QString &storageLocation, const QVector<int> &removedTagIds){Q_FOREACH(const auto key, d->resourceCache.keys()) {if (key.first == storageLocation) {@@ -830,6 +830,14 @@ void KisResourceLocator::purge(const QString &storageLocation)KisResourceThumbnailCache::instance()->remove(key);}}++ for (auto it = d->tagCache.begin(); it != d->tagCache.end();) {+ if (removedTagIds.contains(it.value()->id())) {+ it = d->tagCache.erase(it);+ } else {+ ++it;+ }+ }}bool KisResourceLocator::addStorage(const QString &storageLocation, KisResourceStorageSP storage)@@ -897,11 +905,22 @@ bool KisResourceLocator::removeStorage(const QString &storageLocation)}}+ QVector<std::pair<QString, QVector<int>>> removedTags;+ QVector<int> allRemovedTagIds;+ Q_FOREACH(const QString &type, KisResourceLoaderRegistry::instance()->resourceTypes()) {+ auto [uniqueTags, sharedTags] = KisResourceCacheDb::tagsForStorage(type, storageLocation);+ removedTags << std::make_pair(type, uniqueTags);+ allRemovedTagIds << uniqueTags;+ }+Q_FOREACH (const auto &typedResources, removedResources) {Q_EMIT beginExternalResourceRemove(typedResources.first, typedResources.second);}- purge(storageLocation);+ // TODO: add model notification about removed tags+ Q_UNUSED(removedTags);++ purge(storageLocation, allRemovedTagIds);KisResourceStorageSP storage = d->storages.take(storageLocation);diff --git a/libs/resources/KisResourceLocator.h b/libs/resources/KisResourceLocator.hindex f6c5c75440..76daa412f4 100644--- a/libs/resources/KisResourceLocator.h+++ b/libs/resources/KisResourceLocator.h@@ -76,7 +76,7 @@ public:/*** @brief purge purges the local resource cache*/- void purge(const QString &storageLocation);+ void purge(const QString &storageLocation, const QVector<int> &removedTagIds);/*** @brief addStorage Adds a new resource storage to the database. The storage isdiff --git a/libs/resources/KisResourceStorage.h b/libs/resources/KisResourceStorage.hindex 25ab1670ad..4a2a8fad5c 100644--- a/libs/resources/KisResourceStorage.h+++ b/libs/resources/KisResourceStorage.h@@ -274,6 +274,7 @@ private:friend class KisResourceLocator;friend class KisResourceCacheDb;friend class TestResourceLocator;+ friend class TestStorageWrapper;KisStoragePlugin* testingGetStoragePlugin();diff --git a/libs/resources/KisTag.h b/libs/resources/KisTag.hindex 80e6d573f7..e380fb6237 100644--- a/libs/resources/KisTag.h+++ b/libs/resources/KisTag.h@@ -88,6 +88,7 @@ private:friend class AbrTagIterator;friend class TestMemoryStorage;friend class TestResourceLocator;+ friend class TestStorageWrapper;void setId(int id);void setActive(bool active);diff --git a/libs/resources/TagsDesignNotes.md b/libs/resources/TagsDesignNotes.mdnew file mode 100644index 0000000000..f429571908--- /dev/null+++ b/libs/resources/TagsDesignNotes.md@@ -0,0 +1,78 @@+# Tags Design Notes++## Design decisions++1) It is impossible for a tag to be linked to no storage. Any tag should be linked at least to something, e.g. to the _folder_ storage++1) Tags from the folder storage may be saved into the _folder_ storage by the user manually. In this case we should keep all deleted tags in the database to prevent them from appearing in the GUI again and again after restart.++1) Tags are **not** automatically promoted to a "local" tag on resource assignment. I.e. a tag **belong** to the storages it comes from.++1) `tags_storages` table registers "where the tag comes from", so that we could cleanup now-unused tags of the removed storages. There is no one-to-one relation between tags and storages. One tag can come from multiple storages.++1) When multiple storages provide tags with the same URL, all such tags will be merged into one. The `tags_storages` table will have one record for each such storage.++1) [current behavior] The `tags_storages` table works like a reference-countng table. When the **last storage owning a tag** is removed, Krita removes the tag as well, whether or not there are still resources tagged with this tag, i.e.+ * if a tag links the resources from the removed storage only, then the resources and the tag are removed+ * if a tag links the resources from the outside of the removed storage, these external resources will be untagged, and only the tag will be removed++2) [planned behavior] Currently, a tag from one storage can be linked to a resource from another storage, and, when the first storage is removed, the resource from the second storage will be untagged. We plan to change this behavior by indroducing the following rule:++ * if the user manually assigns a tag to a resource from outside of the tag's storage, then the tag becomes "global", i.e. saved in the folder storage++ It will basically mean that all cross-storage tags will be automatically added to the main _folder_ storage, which cannot be removed, which, in turn, guarantees that the tag will never be removed either.++## Requirements++### 1. Tag is added via GUI++* the tag is added into the _folder_ storage+* this type of tags is kept forever, i.e. they can never be deleted from the database, only deactivated++### 2. Tag is removed via GUI++* tag belongs to the _folder_ storage only+ * the tag is deactivated+* tag belongs to the bundle storage only+ * the tag is deactivated+* tag belongs to the _folder_ storage and a bundle as well+ * the tag is deactivated++### 3. Resource is tagged+* resource belongs to the same storage as the tag+ * just tag the resource (add a row in `resource_tags`)+* resource belongs to a different storage than the tag+ * [TO BE IMPLEMENTED] promote the tag to a "local tag" (link it to _folder_ storage in `tags_storages` table)+ * then tag the resource (add a row in `resource_tags`)++### 4. Resource is untagged+* resource belongs to the same storage as the tag+ * just untag (mark the link as "inactive" in `resource_tags` table)+* resource belongs to a different storage than the tag+ * just untag (mark the link as "inactive" in `resource_tags` table)++### 5. Storage is added/sync'ed+* [TO BE IMPLEMENTED] the tags stored in a storage can address **only** the resources from this storage (currently it tries to address the resources globally)+* if two storages provide two tags with the same URL, they should be merged, `tags_storages` table will have two rows, one for each storage++### 6. Storage is removed+* if some other storage still links this tag+ * the tag is preserved+* if the tag links the resources in the removed storage only+ * the tag is removed completely+* if the tag also links resources outside the removed storage+ * [CURRENTLY] all external resources are untagged and the tag is removed+ * [TO BE IMPLEMENTED] the tag is promoted to a local tag and kept in the database+ * [TO BE IMPLEMENTED] ASSERT: if a tag links to resources outside the storage, it **must** also be linked to at least one storage, e.g. _folder_ storage (see the "promote to local on assignment" requirement above)++### 7. Storage is deactivated+* deactivation of a storage implicitly deactivates tags linked to this storage, i.e. a tag is active if at least one of its storages is active+* NOTE: the actual code doing that is placed in `KisTagModel::filterAcceptsRow`++### 8. Storage is reactivated+* reactivation of a storage implicitly activates a tag++## TODO+1) Cleanup/promote orphaned tags on migration+2) Cleanup for orphaned storage-tag records on migration+3) Cleanup for orphaned tag-resource records on migration\ No newline at end of filediff --git a/libs/resources/sql.qrc b/libs/resources/sql.qrcindex 4f7bf33933..62328c81de 100644--- a/libs/resources/sql.qrc+++ b/libs/resources/sql.qrc@@ -30,6 +30,6 @@<file>sql/delete_resource_tags_for_storage_indirect.sql</file><file>sql/delete_versioned_resources_for_storage_direct.sql</file><file>sql/delete_resource_metadata_for_storage.sql</file>-+ <file>sql/storage_tags_ref_count.sql</file></qresource></RCC>diff --git a/libs/resources/sql/storage_tags_ref_count.sql b/libs/resources/sql/storage_tags_ref_count.sqlnew file mode 100644index 0000000000..68478df783--- /dev/null+++ b/libs/resources/sql/storage_tags_ref_count.sql@@ -0,0 +1,12 @@+WITH tags_linked_to_storage AS (+ SELECT tags_storages.tag_id+ FROM tags_storages+ INNER JOIN tags ON tags.id == tags_storages.tag_id+ INNER JOIN resource_types ON resource_types.id == tags.resource_type_id+ INNER JOIN storages ON storages.id == tags_storages.storage_id+ WHERE resource_types.name == :resource_type+ AND storages.location == :location)+SELECT tags_storages.tag_id AS tag_id, COUNT(*) AS ref_count+FROM tags_storages+WHERE tags_storages.tag_id IN tags_linked_to_storage+GROUP BY tags_storages.tag_id\ No newline at end of filediff --git a/libs/resources/tests/TestResourceLocator.cpp b/libs/resources/tests/TestResourceLocator.cppindex 50d8324b83..b788dcecdc 100644--- a/libs/resources/tests/TestResourceLocator.cpp+++ b/libs/resources/tests/TestResourceLocator.cpp@@ -594,9 +594,9 @@ void TestResourceLocator::testLoadTagsFromStorage_data(){QTest::addColumn<TagDeletionType>("tagDeletionType");- QTest::newRow("storage_explicit_delete") << TagDeletionType(StorageExplicitDelete);- QTest::newRow("delete_all_temporary") << TagDeletionType(StorageDeleteAllTemporary);- QTest::newRow("delete_tag_and_resync") << TagDeletionType(StorageDeleteTagAndResync);+ QTest::newRow("storage_explicit_delete") << StorageExplicitDelete;+ QTest::newRow("delete_all_temporary") << StorageDeleteAllTemporary;+ QTest::newRow("delete_tag_and_resync") << StorageDeleteTagAndResync;}void TestResourceLocator::testLoadTagsFromStorage()@@ -686,6 +686,312 @@ void TestResourceLocator::testLoadTagsFromStorage()}}+class TestStorageWrapper {+public:++ const int storageIndex = 0;+ const int resourceIndex = 0;+ const int tagIndex = 0;+ KisResourceStorageSP storage;++ QString storageLocation() const {+ return QString("document_%1").arg(storageIndex);+ }+ QString tagUrl() const {+ return QString("test_tag_url_%1").arg(tagIndex);+ }++ QString resourceType() const {+ return ResourceType::PaintOpPresets;+ }++ QString resourceName() const {+ return QString("metadata_test_%1.kpp").arg(resourceIndex);+ }++ KisMemoryStorage* memoryStorageBackend() const {+ return dynamic_cast<KisMemoryStorage *>(storage->testingGetStoragePlugin());+ }++ TestStorageWrapper(int storageIndexArg, int resourceIndexArg, int tagIndexTag)+ : storageIndex(storageIndexArg)+ , resourceIndex(resourceIndexArg)+ , tagIndex(tagIndexTag)+ {+ storage = QSharedPointer<KisResourceStorage>::create(storageLocation());+ QVERIFY(storage->valid());++ KisMemoryStorage *memoryStorageBackend =+ dynamic_cast<KisMemoryStorage *>(storage->testingGetStoragePlugin());++ QSharedPointer<DummyResource> resource(new DummyResource(resourceName(), resourceType()));+ resource->setSomething(QString("123456789012345678901234567890_%1").arg(resourceIndex));++ // add a resource to the storage+ storage->addResource(resource);++ {+ // add a tag to the storage+ KisTagSP tag(new KisTag());+ tag->setName(QString("Test Tag %1").arg(tagIndex));+ tag->setNames({{"ru", QString("Тестовый тег %1").arg(tagIndex)}});+ tag->setUrl(tagUrl());+ tag->setResourceType(resourceType());+ tag->setDefaultResources({resource->filename()});+ tag->setValid(true);+ memoryStorageBackend->testingAddTag(ResourceType::PaintOpPresets, tag);+ }+ }+};++void TestResourceLocator::testTagsForStorageUnique()+{+ TestStorageWrapper storageWrapper1(/* storage */ 1, /* resource */ 1, /* tag */ 1);+ TestStorageWrapper storageWrapper2(/* storage */ 2, /* resource */ 2, /* tag */ 2);++ m_locator->addStorage(storageWrapper1.storageLocation(), storageWrapper1.storage);+ m_locator->addStorage(storageWrapper2.storageLocation(), storageWrapper2.storage);++ QVERIFY(m_locator->hasStorage(storageWrapper1.storageLocation()));+ QVERIFY(m_locator->hasStorage(storageWrapper2.storageLocation()));++ {+ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ QVERIFY(tag1);+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(storageWrapper1.resourceType(), storageWrapper1.storageLocation());+ QCOMPARE(unique, {tag1->id()});+ QCOMPARE(shared, {});+ }++ {+ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagSP tag2 = tagModel.tagForUrl(storageWrapper2.tagUrl());+ QVERIFY(tag2);+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(storageWrapper2.resourceType(), storageWrapper2.storageLocation());+ QCOMPARE(unique, {tag2->id()});+ QCOMPARE(shared, {});+ }++ {+ // unrelated resource type returns nothing+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(ResourceType::Brushes, storageWrapper1.storageLocation());+ QCOMPARE(unique, {});+ QCOMPARE(shared, {});+ }+}+void TestResourceLocator::testTagsForStorageShared()+{+ TestStorageWrapper storageWrapper1(/* storage */ 1, /* resource */ 1, /* tag */ 1);+ TestStorageWrapper storageWrapper2(/* storage */ 2, /* resource */ 2, /* tag */ 1);++ m_locator->addStorage(storageWrapper1.storageLocation(), storageWrapper1.storage);+ m_locator->addStorage(storageWrapper2.storageLocation(), storageWrapper2.storage);++ QVERIFY(m_locator->hasStorage(storageWrapper1.storageLocation()));+ QVERIFY(m_locator->hasStorage(storageWrapper2.storageLocation()));++ {+ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ QVERIFY(tag1);+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(storageWrapper1.resourceType(), storageWrapper1.storageLocation());+ QCOMPARE(unique, {});+ QCOMPARE(shared, {tag1->id()});+ }++ {+ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagSP tag2 = tagModel.tagForUrl(storageWrapper2.tagUrl());+ QVERIFY(tag2);+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(storageWrapper2.resourceType(), storageWrapper2.storageLocation());+ QCOMPARE(unique, {});+ QCOMPARE(shared, {tag2->id()});+ }++ {+ // unrelated resource type returns nothing+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(ResourceType::Brushes, storageWrapper1.storageLocation());+ QCOMPARE(unique, {});+ QCOMPARE(shared, {});+ }++ m_locator->removeStorage(storageWrapper2.storageLocation());++ {+ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ QVERIFY(tag1);+ auto [unique, shared] = KisResourceCacheDb::tagsForStorage(storageWrapper1.resourceType(), storageWrapper1.storageLocation());+ QCOMPARE(unique, {tag1->id()});+ QCOMPARE(shared, {});+ }+}++void TestResourceLocator::testDeleteStorageWithCrossLinkedTags()+{+ // TODO: rename TagDeletionType -> StorageDeletionType+ const TagDeletionType tagDeletionType = StorageExplicitDelete;++ TestStorageWrapper storageWrapper1(/* storage */ 1, /* resource */ 1, /* tag */ 1);+ TestStorageWrapper storageWrapper2(/* storage */ 2, /* resource */ 2, /* tag */ 2);++ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagResourceModel tagResourceModel(ResourceType::PaintOpPresets);++ m_locator->addStorage(storageWrapper1.storageLocation(), storageWrapper1.storage);+ m_locator->addStorage(storageWrapper2.storageLocation(), storageWrapper2.storage);++ QVERIFY(m_locator->hasStorage(storageWrapper1.storageLocation()));+ QVERIFY(m_locator->hasStorage(storageWrapper2.storageLocation()));++ // now use tag from storageWrapper1 for a resource in storageWrapper2++ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ QVERIFY(tag1);++ KoResourceSP resource2 = m_locator->resource(storageWrapper2.storageLocation(), storageWrapper2.resourceType(), storageWrapper2.resourceName());+ QVERIFY(resource2);++ tagResourceModel.tagResources(tag1, {resource2->resourceId()});++ QCOMPARE(countTagRecordsInTags(tag1->id()), 1);+ QCOMPARE(countTagRecordsInTagTranslations(tag1->id()), 1);+ QCOMPARE(countTagRecordsInResourceTags(tag1->id()), 2); // two resources are tagged with this tag+ QCOMPARE(countTagRecordsInTagStorages(tag1->id()), 1); // one storage is linked as the source of this tag++ tagResourceModel.setResourcesFilter({resource2});+ QCOMPARE(tagResourceModel.rowCount(), 2);++ // now remove the storage++ if (tagDeletionType == StorageExplicitDelete || tagDeletionType == StorageDeleteAllTemporary) {+ const int deletedStorageId = storageWrapper1.storage->storageId();+ const int deletedTagId = [&]() {+ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ KIS_ASSERT(tag1);+ return tag1->id();+ }();++ if (tagDeletionType == StorageExplicitDelete) {+ m_locator->removeStorage(storageWrapper1.storageLocation());+ } else if (tagDeletionType == StorageDeleteAllTemporary) {+ KisResourceCacheDb::deleteTemporaryResources();+ }++ QCOMPARE(countStorageRecordsForStorageId(deletedStorageId), 0);+ QCOMPARE(countStorageRecordsInTagsStoragesForStorageId(deletedStorageId), 0);++ QCOMPARE(countTagRecordsInTags(deletedTagId), 0);+ QCOMPARE(countTagRecordsInTagTranslations(deletedTagId), 0);+ QCOMPARE(countTagRecordsInResourceTags(deletedTagId), 0);+ QCOMPARE(countTagRecordsInTagStorages(deletedTagId), 0);++ {+ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ QVERIFY(!tag1);+ }++ {+ KisTagSP tag2 = tagModel.tagForUrl(storageWrapper2.tagUrl());+ QVERIFY(tag2);+ }++ // the model is automatically updated+ QCOMPARE(tagResourceModel.rowCount(), 1);+ }+}++void TestResourceLocator::testDeleteStorageWithSharedTags()+{+ // TODO: rename TagDeletionType -> StorageDeletionType+ const TagDeletionType tagDeletionType = StorageExplicitDelete;++ // the two storages provide the same tag!+ TestStorageWrapper storageWrapper1(/* storage */ 1, /* resource */ 1, /* tag */ 1);+ TestStorageWrapper storageWrapper2(/* storage */ 2, /* resource */ 2, /* tag */ 1);++ KisTagModel tagModel(ResourceType::PaintOpPresets);+ KisTagResourceModel tagResourceModel(ResourceType::PaintOpPresets);++ m_locator->addStorage(storageWrapper1.storageLocation(), storageWrapper1.storage);+ m_locator->addStorage(storageWrapper2.storageLocation(), storageWrapper2.storage);++ QVERIFY(m_locator->hasStorage(storageWrapper1.storageLocation()));+ QVERIFY(m_locator->hasStorage(storageWrapper2.storageLocation()));++ KisTagSP tag1 = tagModel.tagForUrl(storageWrapper1.tagUrl());+ QVERIFY(tag1);++ QCOMPARE(countTagRecordsInTags(tag1->id()), 1);+ QCOMPARE(countTagRecordsInTagTranslations(tag1->id()), 1);+ QCOMPARE(countTagRecordsInResourceTags(tag1->id()), 2); // two resources are tagged with this tag+ QCOMPARE(countTagRecordsInTagStorages(tag1->id()), 2); // two storages are linked as the source of this tag++ {+ KoResourceSP resource2 = m_locator->resource(storageWrapper2.storageLocation(),+ storageWrapper2.resourceType(),+ storageWrapper2.resourceName());+ QVERIFY(resource2);++ tagResourceModel.setTagsFilter(QVector<KisTagSP>{});+ tagResourceModel.setResourcesFilter({resource2});+ QCOMPARE(tagResourceModel.rowCount(), 1);+ }++ {+ KoResourceSP resource1 = m_locator->resource(storageWrapper2.storageLocation(),+ storageWrapper2.resourceType(),+ storageWrapper2.resourceName());+ QVERIFY(resource1);++ tagResourceModel.setTagsFilter(QVector<KisTagSP>{});+ tagResourceModel.setResourcesFilter({resource1});+ QCOMPARE(tagResourceModel.rowCount(), 1);+ }++ {+ tagResourceModel.setTagsFilter({tag1});+ tagResourceModel.setResourcesFilter(QVector<KoResourceSP>{});+ QCOMPARE(tagResourceModel.rowCount(), 2);+ }++ // now remove the storage++ if (tagDeletionType == StorageExplicitDelete || tagDeletionType == StorageDeleteAllTemporary) {+ const int deletedStorageId = storageWrapper1.storage->storageId();++ if (tagDeletionType == StorageExplicitDelete) {+ m_locator->removeStorage(storageWrapper1.storageLocation());+ } else if (tagDeletionType == StorageDeleteAllTemporary) {+ KisResourceCacheDb::deleteTemporaryResources();+ }++ QCOMPARE(countStorageRecordsForStorageId(deletedStorageId), 0);+ QCOMPARE(countStorageRecordsInTagsStoragesForStorageId(deletedStorageId), 0);++ KisTagSP tag2 = tagModel.tagForUrl(storageWrapper2.tagUrl());+ QVERIFY(tag2);++ QCOMPARE(countTagRecordsInTags(tag2->id()), 1);+ QCOMPARE(countTagRecordsInTagTranslations(tag2->id()), 1);+ QCOMPARE(countTagRecordsInResourceTags(tag2->id()), 1);+ QCOMPARE(countTagRecordsInTagStorages(tag2->id()), 1);++ {+ KisTagResourceModel tagResourceModel(ResourceType::PaintOpPresets);+ KoResourceSP resource2 = m_locator->resource(storageWrapper2.storageLocation(),+ storageWrapper2.resourceType(),+ storageWrapper2.resourceName());+ QVERIFY(resource2);++ tagResourceModel.setTagsFilter(QVector<KisTagSP>{});+ tagResourceModel.setResourcesFilter({resource2});+ QCOMPARE(tagResourceModel.rowCount(), 1);+ }+ }+}+void TestResourceLocator::testSyncVersions(){int resourceId = -1;diff --git a/libs/resources/tests/TestResourceLocator.h b/libs/resources/tests/TestResourceLocator.hindex c2a84a20d6..6336a9c088 100644--- a/libs/resources/tests/TestResourceLocator.h+++ b/libs/resources/tests/TestResourceLocator.h@@ -33,6 +33,12 @@ private Q_SLOTS:void testLoadTagsFromStorage();void testLoadTagsFromStorage_data();+ void testTagsForStorageUnique();+ void testTagsForStorageShared();++ void testDeleteStorageWithCrossLinkedTags();+ void testDeleteStorageWithSharedTags();+void cleanupTestCase();void testSyncVersions();diff --git a/libs/resources/tests/TestResourceModel.cpp b/libs/resources/tests/TestResourceModel.cppindex 12376ab7d4..fbcf6f33df 100644--- a/libs/resources/tests/TestResourceModel.cpp+++ b/libs/resources/tests/TestResourceModel.cpp@@ -306,7 +306,7 @@ void TestResourceModel::testUpdateResource(){// Check the resource itself- KisResourceLocator::instance()->purge("");+ KisResourceLocator::instance()->purge("", {});KoResourceSP resource = KisResourceLocator::instance()->resourceForId(resourceId);QVERIFY(resource);