From f0dbf1200b67d9fd620754b144fb026cc0d85493 Mon Sep 17 00:00:00 2001 From: Andrew Skowronski Date: Tue, 3 Nov 2026 11:31:22 -0500 Subject: [PATCH] [#83] Call handler finalization so their indexes are created --- Analyzer/Resources/Init.sql | 2 +- .../SQLite/Handlers/AnimationClipHandler.cs | 4 ---- Analyzer/SQLite/Handlers/AssetBundleHandler.cs | 2 +- Analyzer/SQLite/Handlers/AudioClipHandler.cs | 4 ---- Analyzer/SQLite/Handlers/BuildReportHandler.cs | 4 ---- .../SQLite/Handlers/ContentSummaryHandler.cs | 4 ---- Analyzer/SQLite/Handlers/ISQLiteHandler.cs | 4 ++++ Analyzer/SQLite/Handlers/MeshHandler.cs | 4 ---- Analyzer/SQLite/Handlers/MonoScriptHandler.cs | 4 ---- .../SQLite/Handlers/PackedAssetsHandler.cs | 4 ---- Analyzer/SQLite/Handlers/PreloadDataHandler.cs | 4 ---- Analyzer/SQLite/Handlers/ShaderHandler.cs | 2 +- Analyzer/SQLite/Handlers/Texture2DHandler.cs | 4 ---- .../Writers/SerializedFileSQLiteWriter.cs | 13 ++++++++++--- Documentation/analyzer-schema.md | 1 + .../UnityDataToolAssetBundleTests.cs | 18 ++++++++++++++++++ 16 files changed, 36 insertions(+), 42 deletions(-) diff --git a/Analyzer/Resources/Init.sql b/Analyzer/Resources/Init.sql index 2414e18..9648222 100644 --- a/Analyzer/Resources/Init.sql +++ b/Analyzer/Resources/Init.sql @@ -209,7 +209,7 @@ WHERE m.type = 'Material'; INSERT INTO types (id, name) VALUES (-1, 'Scene'); -PRAGMA user_version = 10; +PRAGMA user_version = 11; PRAGMA synchronous = OFF; PRAGMA journal_mode = MEMORY; diff --git a/Analyzer/SQLite/Handlers/AnimationClipHandler.cs b/Analyzer/SQLite/Handlers/AnimationClipHandler.cs index 04cf62f..bdf26e9 100644 --- a/Analyzer/SQLite/Handlers/AnimationClipHandler.cs +++ b/Analyzer/SQLite/Handlers/AnimationClipHandler.cs @@ -36,10 +36,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s streamDataSize = 0; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/AssetBundleHandler.cs b/Analyzer/SQLite/Handlers/AssetBundleHandler.cs index 6cadcf8..c08ec0f 100644 --- a/Analyzer/SQLite/Handlers/AssetBundleHandler.cs +++ b/Analyzer/SQLite/Handlers/AssetBundleHandler.cs @@ -140,7 +140,7 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s streamDataSize = 0; } - public void Finalize(SqliteConnection db) + public void FinalizeDatabase(SqliteConnection db) { using var command = new SqliteCommand(); command.Connection = db; diff --git a/Analyzer/SQLite/Handlers/AudioClipHandler.cs b/Analyzer/SQLite/Handlers/AudioClipHandler.cs index a4119ec..f000405 100644 --- a/Analyzer/SQLite/Handlers/AudioClipHandler.cs +++ b/Analyzer/SQLite/Handlers/AudioClipHandler.cs @@ -43,10 +43,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s name = audioClip.Name; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/BuildReportHandler.cs b/Analyzer/SQLite/Handlers/BuildReportHandler.cs index c50b10f..c2bfcfa 100644 --- a/Analyzer/SQLite/Handlers/BuildReportHandler.cs +++ b/Analyzer/SQLite/Handlers/BuildReportHandler.cs @@ -155,10 +155,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s name = buildReport.Name; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/ContentSummaryHandler.cs b/Analyzer/SQLite/Handlers/ContentSummaryHandler.cs index 2229e2f..e9c4c42 100644 --- a/Analyzer/SQLite/Handlers/ContentSummaryHandler.cs +++ b/Analyzer/SQLite/Handlers/ContentSummaryHandler.cs @@ -144,10 +144,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s name = string.Empty; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertSummaryCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/ISQLiteHandler.cs b/Analyzer/SQLite/Handlers/ISQLiteHandler.cs index a7a1836..d751b81 100644 --- a/Analyzer/SQLite/Handlers/ISQLiteHandler.cs +++ b/Analyzer/SQLite/Handlers/ISQLiteHandler.cs @@ -20,6 +20,10 @@ public interface ISQLiteHandler : IDisposable { void Init(Microsoft.Data.Sqlite.SqliteConnection db); void Process(Context ctx, long objectId, RandomAccessReader reader, out string name, out long streamDataSize); + + // Called once after all files are processed. Indexes are created here rather than in Init so + // that bulk inserts do not have to maintain them. + void FinalizeDatabase(SqliteConnection db) { } } public interface ISQLiteFileParser : IDisposable diff --git a/Analyzer/SQLite/Handlers/MeshHandler.cs b/Analyzer/SQLite/Handlers/MeshHandler.cs index 20b22ee..d7c774d 100644 --- a/Analyzer/SQLite/Handlers/MeshHandler.cs +++ b/Analyzer/SQLite/Handlers/MeshHandler.cs @@ -63,10 +63,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s name = mesh.Name; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/MonoScriptHandler.cs b/Analyzer/SQLite/Handlers/MonoScriptHandler.cs index 6d22587..359f8ee 100644 --- a/Analyzer/SQLite/Handlers/MonoScriptHandler.cs +++ b/Analyzer/SQLite/Handlers/MonoScriptHandler.cs @@ -38,10 +38,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s streamDataSize = 0; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/PackedAssetsHandler.cs b/Analyzer/SQLite/Handlers/PackedAssetsHandler.cs index de3a21e..28d796a 100644 --- a/Analyzer/SQLite/Handlers/PackedAssetsHandler.cs +++ b/Analyzer/SQLite/Handlers/PackedAssetsHandler.cs @@ -164,10 +164,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s name = packedAssets.Path; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertPackedAssetsCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/PreloadDataHandler.cs b/Analyzer/SQLite/Handlers/PreloadDataHandler.cs index 716267f..270f79d 100644 --- a/Analyzer/SQLite/Handlers/PreloadDataHandler.cs +++ b/Analyzer/SQLite/Handlers/PreloadDataHandler.cs @@ -51,10 +51,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s streamDataSize = 0; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertDepCommand?.Dispose(); diff --git a/Analyzer/SQLite/Handlers/ShaderHandler.cs b/Analyzer/SQLite/Handlers/ShaderHandler.cs index b1eb520..4de7110 100644 --- a/Analyzer/SQLite/Handlers/ShaderHandler.cs +++ b/Analyzer/SQLite/Handlers/ShaderHandler.cs @@ -143,7 +143,7 @@ private int GetKeywordId(string keyword, SqliteTransaction ctxTransaction) return id; } - public void Finalize(SqliteConnection db) + public void FinalizeDatabase(SqliteConnection db) { using var command = new SqliteCommand(); command.Connection = db; diff --git a/Analyzer/SQLite/Handlers/Texture2DHandler.cs b/Analyzer/SQLite/Handlers/Texture2DHandler.cs index 5cdb6d9..5a62f09 100644 --- a/Analyzer/SQLite/Handlers/Texture2DHandler.cs +++ b/Analyzer/SQLite/Handlers/Texture2DHandler.cs @@ -41,10 +41,6 @@ public void Process(Context ctx, long objectId, RandomAccessReader reader, out s streamDataSize = texture2d.StreamDataSize; } - public void Finalize(SqliteConnection db) - { - } - void IDisposable.Dispose() { m_InsertCommand?.Dispose(); diff --git a/Analyzer/SQLite/Writers/SerializedFileSQLiteWriter.cs b/Analyzer/SQLite/Writers/SerializedFileSQLiteWriter.cs index 547447a..c332ba6 100644 --- a/Analyzer/SQLite/Writers/SerializedFileSQLiteWriter.cs +++ b/Analyzer/SQLite/Writers/SerializedFileSQLiteWriter.cs @@ -424,16 +424,23 @@ public void WriteSerializedFile(string relativePath, string fullPath, string con } } - // Records every referenced object that was assigned an id but never written to the objects - // table (its serialized file was not part of the analyzed input). Must run after all files are + // Lets the handlers create their indexes, then records every referenced object that was assigned + // an id but never written to the objects table (its serialized file was not part of the analyzed + // input). Must run after all files are // processed, since an id is only known to be dangling once nothing has claimed it as an object. // The set of written objects/files is read back from the database so this is robust to every // object write site (regular objects, synthetic Scene objects, etc.) without instrumenting them. public void FinalizeDatabase() { + if (!m_Initialized) + return; + + foreach (var handler in m_Handlers.Values) + handler.FinalizeDatabase(m_Database); + // Dangling refs are part of reference tracking, so honor --skip-references (the view that // makes them useful joins the refs table, which is empty in that mode anyway). - if (!m_Initialized || m_SkipReferences) + if (m_SkipReferences) return; var writtenObjectIds = ReadIntSet("SELECT id FROM objects"); diff --git a/Documentation/analyzer-schema.md b/Documentation/analyzer-schema.md index 53294bc..5076dc5 100644 --- a/Documentation/analyzer-schema.md +++ b/Documentation/analyzer-schema.md @@ -597,6 +597,7 @@ Any schema change - a new or changed table, view or column - must bump the pragm | 8 | `archives.name` is the path relative to the scanned directory, not the bare file name ([#149](https://github.com/Unity-Technologies/UnityDataTools/issues/149)) | | 9 | `content_layout*` tables track the version 3 layout schema of Unity 6.7: loadables keyed by `loadable_index`, `stable_id` / `artifact_index` columns replace `cfid` / `content_hash`, `is_root_asset` records the root position, and the v2-only columns (`asset_path`, `source_lfid`) exist only in databases imported from a version 2 layout ([#131](https://github.com/Unity-Technologies/UnityDataTools/issues/131)) | | 10 | Added the `managed_references` table, `managed_reference_view` and `managed_reference_stats_view` ([#53](https://github.com/Unity-Technologies/UnityDataTools/issues/53)) | +| 11 | Added indexes on `preload_dependencies(object)`, `preload_dependencies(dependency)`, `shader_subprograms(shader)` and `shader_subprogram_keywords(subprogram_id)` ([#83](https://github.com/Unity-Technologies/UnityDataTools/issues/83)) | ## Related documentation diff --git a/UnityDataTool.Tests/UnityDataToolAssetBundleTests.cs b/UnityDataTool.Tests/UnityDataToolAssetBundleTests.cs index 6178f7d..8c53653 100644 --- a/UnityDataTool.Tests/UnityDataToolAssetBundleTests.cs +++ b/UnityDataTool.Tests/UnityDataToolAssetBundleTests.cs @@ -335,6 +335,24 @@ public async Task Analyze_MonoScripts_DatabaseContainsExpectedContent() "Expected to find exactly one MonoBehaviour instance of SerializeReferencePolymorphismExample"); } + [Test] + public async Task Analyze_HandlerIndexes_AreCreated( + [Values("", "--skip-references")] string options) + { + var databasePath = SQLTestHelper.GetDatabasePath(m_TestOutputFolder); + var analyzePath = Path.Combine(Context.UnityDataFolder); + + Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray())); + + using var db = SQLTestHelper.OpenDatabase(databasePath); + SQLTestHelper.AssertQueryInt(db, + @"SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name IN ( + 'preload_dependencies_object', 'preload_dependencies_dependency', + 'shader_subprograms_shader_index', 'shader_subprogram_keywords_subprogram_id_index')", + 4, + "Expected the indexes created by the handlers"); + } + // The 2019.4 and 2020.3 bundles have a version 1 registry, where the rid is the entry's position; // the later ones have version 2. The instance data is the same in both. [Test]