AssetDatabase.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef ASSETPROCESSOR_ASSETDATABASE_H
  2. #define ASSETPROCESSOR_ASSETDATABASE_H
  3. /*
  4. * Copyright (c) Contributors to the Open 3D Engine Project.
  5. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. *
  7. * SPDX-License-Identifier: Apache-2.0 OR MIT
  8. *
  9. */
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzCore/Asset/AssetCommon.h>
  12. #include <AzToolsFramework/AssetDatabase/AssetDatabaseConnection.h>
  13. #include <QtCore/QSet>
  14. #include <QtCore/QString>
  15. #include "AzToolsFramework/API/EditorAssetSystemAPI.h"
  16. #include <native/AssetManager/SourceAssetReference.h>
  17. class QStringList;
  18. namespace AssetProcessor
  19. {
  20. //! the Asset Processor's database manager's job is to create and modify the actual underlying
  21. //! SQL database. All queries to make changes to the database go through here. This includes
  22. //! connecting to existing database and altering or creating database tables, etc.
  23. class AssetDatabaseConnection
  24. : public AzToolsFramework::AssetDatabase::AssetDatabaseConnection
  25. {
  26. public:
  27. AZ_CLASS_ALLOCATOR(AssetDatabaseConnection, AZ::SystemAllocator);
  28. AssetDatabaseConnection();
  29. ~AssetDatabaseConnection();
  30. //////////////////////////////////////////////////////////////////////////
  31. // AzToolsFramework::AssetDatabase::Connection
  32. public:
  33. bool IsReadOnly() const override
  34. {
  35. return false;// return false, we actually curate/write to this database.
  36. }
  37. void VacuumAndAnalyze();
  38. protected:
  39. void CreateStatements() override;
  40. bool PostOpenDatabase(bool ignoreFutureAssetDBVersionError) override;
  41. //////////////////////////////////////////////////////////////////////////
  42. public:
  43. bool DataExists();
  44. void LoadData();
  45. void ClearData();
  46. //////////////////////////////////////////////////////////////////////////
  47. //Queries
  48. //NOTE: When passing in a structure to the Set<> functions, a default constructed structure has -1 for
  49. //the table generated id and is filled in by the query, that is why it is passed by non const reference.
  50. //For instance when SetSource(scanFolderEntry); is called it evaluates the query and fills in the
  51. //main m_sourceID from the query. If you pass in a structure with the id already filled in, i.e. not -1,
  52. //it is interpreted as an update to the database only if the contents differ in any way from whats
  53. //already in he database. Obviously if it is filled in and does not exist it returns false.
  54. //NOTE: The return bool for these queries only return true if both the query succeeded AND you got a result
  55. //////////////////////////////////////////////////////////////////////////
  56. //scan folders
  57. bool GetScanFolders(AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntryContainer& container);
  58. bool GetScanFolderByScanFolderID(AZ::s64 scanFolderID, AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntry& entry);
  59. bool GetScanFolderBySourceID(AZ::s64 sourceID, AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntry& entry);
  60. bool GetScanFolderByJobID(AZ::s64 jobID, AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntry& entry);
  61. bool GetScanFolderByProductID(AZ::s64 productID, AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntry& entry);
  62. bool GetScanFolderByPortableKey(QString portableKey, AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntry& entry);
  63. bool SetScanFolder(AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntry& entry); //on success sets scanfolderID, if already exists updates it
  64. bool RemoveScanFolder(AZ::s64 scanFolderID);
  65. bool RemoveScanFolders(AzToolsFramework::AssetDatabase::ScanFolderDatabaseEntryContainer& container);
  66. //sources
  67. bool GetSources(AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  68. bool GetSourceBySourceGuid(AZ::Uuid sourceGuid, AzToolsFramework::AssetDatabase::SourceDatabaseEntry& entry);
  69. bool GetSourceBySourceID(AZ::s64 sourceID, AzToolsFramework::AssetDatabase::SourceDatabaseEntry& entry);
  70. bool GetSourceBySourceNameScanFolderId(QString exactSourceName, AZ::s64 scanFolderID, AzToolsFramework::AssetDatabase::SourceDatabaseEntry& entry);
  71. bool GetSourcesBySourceName(QString exactSourceName, AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  72. bool GetSourcesLikeSourceName(QString likeSourceName, LikeType likeType, AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  73. bool GetSourcesLikeSourceNameScanFolderId(QString likeSourceName, AZ::s64 scanFolderID, LikeType likeType, AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  74. bool GetSourceByJobID(AZ::s64 jobID, AzToolsFramework::AssetDatabase::SourceDatabaseEntry& entry);
  75. bool GetSourceByProductID(AZ::s64 productID, AzToolsFramework::AssetDatabase::SourceDatabaseEntry& source);
  76. bool GetSourcesByProductName(QString exactProductName, AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  77. bool GetSourcesLikeProductName(QString likeProductName, LikeType likeType, AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  78. bool SetSource(AzToolsFramework::AssetDatabase::SourceDatabaseEntry& entry); //on success sets sourceID, if it already exists updates it
  79. bool RemoveSource(AZ::s64 sourceID);
  80. bool RemoveSources(AzToolsFramework::AssetDatabase::SourceDatabaseEntryContainer& container);
  81. bool RemoveSourcesByScanFolderID(AZ::s64 scanFolderID);
  82. bool InvalidateSourceAnalysisFingerprints();
  83. //jobs
  84. // used to initialize the predictor for job Run Keys
  85. AZ::s64 GetHighestJobRunKey();
  86. bool GetJobs(AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  87. bool GetJobByJobID(AZ::s64 jobID, AzToolsFramework::AssetDatabase::JobDatabaseEntry& entry);
  88. bool GetJobByProductID(AZ::s64 productID, AzToolsFramework::AssetDatabase::JobDatabaseEntry& entry);
  89. bool GetJobsBySourceID(AZ::s64 sourceID, AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  90. bool GetJobsBySourceName(const AssetProcessor::SourceAssetReference& sourceAsset, AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  91. bool GetJobsLikeSourceName(QString likeSourceName, LikeType likeType, AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  92. bool GetJobsByFailureCauseSourceId(AZ::s64 sourceID, AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container);
  93. bool GetJobsByProductName(QString exactProductName, AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  94. bool GetJobsLikeProductName(QString likeProductName, LikeType likeType, AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  95. bool SetJob(AzToolsFramework::AssetDatabase::JobDatabaseEntry& entry); //on success sets jobID, if it already exists updates it
  96. bool SetJobFingerprintsBySourceID(AZ::s64 sourceID, AZ::u64 hash);
  97. bool RemoveJob(AZ::s64 jobID);
  98. bool RemoveJobs(AzToolsFramework::AssetDatabase::JobDatabaseEntryContainer& container);
  99. bool RemoveJobByProductID(AZ::s64 productID);
  100. //products
  101. bool GetProducts(AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  102. bool GetProductsByJobID(AZ::s64 jobID, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container);
  103. // note that the pair of (JobID, SubID) uniquely identifies a single job, and thus the result is always only one entry:
  104. bool GetProductByJobIDSubId(AZ::s64 jobID, AZ::u32 subID, AzToolsFramework::AssetDatabase::ProductDatabaseEntry& result);
  105. bool GetProductBySourceGuidSubId(AZ::Uuid sourceGuid, AZ::u32 subId, AzToolsFramework::AssetDatabase::ProductDatabaseEntry& result);
  106. bool GetProductByProductID(AZ::s64 productID, AzToolsFramework::AssetDatabase::ProductDatabaseEntry& entry);
  107. bool GetProductsByProductName(QString exactProductName, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  108. bool GetProductsLikeProductName(QString likeProductName, LikeType likeType, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  109. bool GetProductsBySourceID(AZ::s64 sourceID, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  110. bool GetProductsBySourceName(QString exactSourceName, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  111. bool GetProductsBySourceNameScanFolderID(QString exactSourceName, AZ::s64 scanFolderId, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  112. bool GetProductsLikeSourceName(QString likeSourceName, LikeType likeType, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  113. bool SetProduct(AzToolsFramework::AssetDatabase::ProductDatabaseEntry& entry); //on success sets productID, if it already exists updates it
  114. bool SetProducts(AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container); //on success sets productID, if it already exists updates it
  115. bool RemoveProducts(AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container);
  116. bool RemoveProduct(AZ::s64 productID);
  117. bool RemoveProductsByJobID(AZ::s64 jobID);
  118. bool RemoveProductsBySourceID(AZ::s64 sourceID, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  119. //jobinfo
  120. bool GetJobInfoByJobID(AZ::s64 jobID, AzToolsFramework::AssetSystem::JobInfo& jobInfo);
  121. bool GetJobInfoByJobKey(AZStd::string jobKey, AzToolsFramework::AssetSystem::JobInfoContainer& container);
  122. bool GetJobInfoByJobRunKey(AZ::u64 jobRunKey, AzToolsFramework::AssetSystem::JobInfoContainer& container);
  123. bool GetJobInfoBySourceNameScanFolderId(QString exactSourceName, AZ::s64 scanfolderId, AzToolsFramework::AssetSystem::JobInfoContainer& container, AZ::Uuid builderGuid = AZ::Uuid::CreateNull(), QString jobKey = QString(), QString platform = QString(), AzToolsFramework::AssetSystem::JobStatus status = AzToolsFramework::AssetSystem::JobStatus::Any);
  124. /* --------------------- Source Dependency Table -------------------
  125. * For example, this table records when a source file depends on another source file either directly (DEP_SourceToSource)
  126. * but also whether then source file depends on another source file indirectly because it depends on a job which processes
  127. * that source file
  128. */
  129. /// Set a row in the table. It is invalid to overwrite existing rows without removing them first.
  130. bool SetSourceFileDependency(AzToolsFramework::AssetDatabase::SourceFileDependencyEntry& entry);
  131. /// Set a batch of rows. It is invalid to overwrite existing rows, so consider using RemoveSourceFileDependencies first.
  132. bool SetSourceFileDependencies(AzToolsFramework::AssetDatabase::SourceFileDependencyEntryContainer& container);
  133. /// Remove a dependency, given a row ID
  134. bool RemoveSourceFileDependency(AZ::s64 sourceFileDependencyId);
  135. /// Batch remove a bunch of rows by container
  136. bool RemoveSourceFileDependencies(const AzToolsFramework::AssetDatabase::SourceFileDependencyEntryContainer& container);
  137. /// Batch remove a bunch of rows by IDs
  138. bool RemoveSourceFileDependencies(const AZStd::unordered_set<AZ::s64>& container);
  139. /// Direct retrieval by ID (does not use any filtering)
  140. bool GetSourceFileDependencyBySourceDependencyId(AZ::s64 sourceDependencyId, AzToolsFramework::AssetDatabase::SourceFileDependencyEntry& sourceDependencyEntry);
  141. // The following functions are all search functions (as opposed to the above functions which fetch or operate on specific rows)
  142. // They tend to take a "Type of Dependency" filter - you can use DEP_Any to query all kinds of dependencies.
  143. /// Given a source file, what does it DEPEND ON?
  144. bool GetDependsOnSourceBySource(AZ::Uuid sourceUuid, AzToolsFramework::AssetDatabase::SourceFileDependencyEntry::TypeOfDependency typeOfDependency, AzToolsFramework::AssetDatabase::SourceFileDependencyEntryContainer& container);
  145. /// Given a source file and a builder UUID, does it DEPEND ON?
  146. bool GetSourceFileDependenciesByBuilderGUIDAndSource(const AZ::Uuid& builderGuid, AZ::Uuid sourceGuid, AzToolsFramework::AssetDatabase::SourceFileDependencyEntry::TypeOfDependency typeOfDependency, AzToolsFramework::AssetDatabase::SourceFileDependencyEntryContainer& container);
  147. /// Given a source file, what depends ON IT? ('reverse dependency')
  148. bool GetSourceFileDependenciesByDependsOnSource(
  149. AZ::Uuid sourceGuid,
  150. const char* sourceName,
  151. const char* absolutePath,
  152. AzToolsFramework::AssetDatabase::SourceFileDependencyEntry::TypeOfDependency typeOfDependency,
  153. AzToolsFramework::AssetDatabase::SourceFileDependencyEntryContainer& container);
  154. // --------------------- Legacy SUBID table -------------------
  155. bool CreateOrUpdateLegacySubID(AzToolsFramework::AssetDatabase::LegacySubIDsEntry& entry); // create or overwrite operation.
  156. bool RemoveLegacySubID(AZ::s64 legacySubIDsEntryID);
  157. bool RemoveLegacySubIDsByProductID(AZ::s64 productID);
  158. //ProductDependencies
  159. bool GetProductDependencies(AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntryContainer& container);
  160. bool GetProductDependencyByProductDependencyID(AZ::s64 productDependencyID, AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntry& productDependencyEntry);
  161. bool GetProductDependenciesByProductID(AZ::s64 productID, AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntryContainer& container);
  162. bool GetDirectProductDependencies(AZ::s64 productID, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container);
  163. bool GetDirectReverseProductDependenciesBySourceGuidSubId(AZ::Uuid dependencySourceGuid, AZ::u32 dependencySubId, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container);
  164. bool GetDirectReverseProductDependenciesBySourceGuidAllPlatforms(
  165. AZ::Uuid dependencySourceGuid,
  166. AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntryContainer& container);
  167. bool GetAllProductDependencies(AZ::s64 productID, AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer& container);
  168. bool GetUnresolvedProductDependencies(AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntryContainer& container);
  169. bool SetProductDependency(AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntry& entry);
  170. // Missing product dependencies
  171. bool SetMissingProductDependency(AzToolsFramework::AssetDatabase::MissingProductDependencyDatabaseEntry& entry);
  172. bool GetMissingProductDependenciesByProductId(AZ::s64 productId, AzToolsFramework::AssetDatabase::MissingProductDependencyDatabaseEntryContainer& container);
  173. bool GetMissingProductDependencyByMissingProductDependencyId(AZ::s64 missingProductDependencyId, AzToolsFramework::AssetDatabase::MissingProductDependencyDatabaseEntry& missingProductDependencyEntry);
  174. // updates or inserts multiple dependencies in a single transaction. Unlike SetProductDependencies, this does *not* delete existing dependencies
  175. bool UpdateProductDependencies(AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntryContainer& container);
  176. // bulk inserts are lighter weight and don't change the input data. Note that this also deletes old dependencies for the products mentioned in the container.
  177. bool SetProductDependencies(const AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntryContainer& container);
  178. bool RemoveProductDependencyByProductId(AZ::s64 productID);
  179. // bulk replace builder info table with new builder info table. Replaces the existing table of data.
  180. // Note: newEntries will have their m_builderInfoID member set to their inserted rowId if this call succeeds.
  181. bool SetBuilderInfoTable(AzToolsFramework::AssetDatabase::BuilderInfoEntryContainer& newEntries);
  182. //Files
  183. bool GetFileByFileID(AZ::s64 fileID, AzToolsFramework::AssetDatabase::FileDatabaseEntry& entry);
  184. bool GetFileByFileNameAndScanFolderId(QString fileName, AZ::s64 scanFolderId, AzToolsFramework::AssetDatabase::FileDatabaseEntry& entry);
  185. bool GetFilesLikeFileNameScanFolderId(QString likeFileName, LikeType likeType, AZ::s64 scanFolderId, AzToolsFramework::AssetDatabase::FileDatabaseEntryContainer& container);
  186. bool InsertFiles(AzToolsFramework::AssetDatabase::FileDatabaseEntryContainer& entry);
  187. bool InsertFile(AzToolsFramework::AssetDatabase::FileDatabaseEntry& entry, bool& entryAlreadyExists);
  188. bool UpdateFile(AzToolsFramework::AssetDatabase::FileDatabaseEntry& entry, bool& entryAlreadyExists);
  189. // updates the modtime and hash for a file if it exists. Only returns true if the row existed and was successfully updated
  190. bool UpdateFileModTimeAndHashByFileNameAndScanFolderId(QString fileName, AZ::s64 scanFolderId, AZ::u64 modTime, AZ::u64 hash);
  191. bool UpdateFileHashByFileNameAndScanFolderId(QString fileName, AZ::s64 scanFolderId, AZ::u64 hash);
  192. bool RemoveFile(AZ::s64 sourceID);
  193. //Stats
  194. bool GetStatByStatName(QString statName, AzToolsFramework::AssetDatabase::StatDatabaseEntryContainer& container);
  195. bool GetStatLikeStatName(QString statName, AzToolsFramework::AssetDatabase::StatDatabaseEntryContainer& container);
  196. bool ReplaceStat(AzToolsFramework::AssetDatabase::StatDatabaseEntry& stat);
  197. protected:
  198. void SetDatabaseVersion(AzToolsFramework::AssetDatabase::DatabaseVersion ver);
  199. void ExecuteCreateStatements();
  200. private:
  201. AZStd::vector<AZStd::string> m_createStatements; // contains all statements required to create the tables
  202. };
  203. }//namespace EditorFramework
  204. #endif // ASSETPROCESSOR_ASSETDATABASE_H