Bladeren bron

Performance improving database startup time

Kenric Nugteren 1 jaar geleden
bovenliggende
commit
030648852e
2 gewijzigde bestanden met toevoegingen van 26 en 49 verwijderingen
  1. 9 4
      InABox.Core/DatabaseSchema/DatabaseSchema.cs
  2. 17 45
      inabox.database.sqlite/SQLiteProvider.cs

+ 9 - 4
InABox.Core/DatabaseSchema/DatabaseSchema.cs

@@ -37,7 +37,7 @@ namespace InABox.Core
 
         private static IReadOnlyCollection<SubObject>? GetSubObjectDefs(Type t)
         {
-            CheckProperties(t);
+            CheckPropertiesInternal(t);
             return _subObjects.GetValueOrDefault(t);
         }
 
@@ -289,7 +289,7 @@ namespace InABox.Core
             }
         }
 
-        private static ImmutableDictionary<string, IProperty>? CheckProperties(Type type)
+        private static ImmutableDictionary<string, IProperty>? CheckPropertiesInternal(Type type)
         {
             try
             {
@@ -315,8 +315,13 @@ namespace InABox.Core
 
         }
 
+        public static void CheckProperties(Type type)
+        {
+            CheckPropertiesInternal(type);
+        }
+
         private static IEnumerable<IProperty> PropertiesInternal(Type type)
-            => CheckProperties(type)?.Select(x => x.Value) ?? Enumerable.Empty<IProperty>();
+            => CheckPropertiesInternal(type)?.Values ?? Enumerable.Empty<IProperty>();
 
         /// <summary>
         /// Return the standard property list for <paramref name="type"/>; this includes nested properties.
@@ -344,7 +349,7 @@ namespace InABox.Core
         
         public static IProperty? Property(Type type, string name)
         {
-            var prop = CheckProperties(type)?.GetValueOrDefault(name);
+            var prop = CheckPropertiesInternal(type)?.GetValueOrDefault(name);
 
             // Walk up the inheritance tree, see if an ancestor has this property
             if (prop == null && type.BaseType != null)

+ 17 - 45
inabox.database.sqlite/SQLiteProvider.cs

@@ -128,11 +128,9 @@ namespace InABox.Database.SQLite
                 File.Copy(URL, Path.ChangeExtension(URL, string.Format("{0:D3}", i)));
             }
 
+            using var access = GetWriteAccess();
 
-            using (var access = GetWriteAccess())
-            {
-                ExecuteSQL(access, "PRAGMA journal_mode=WAL;");
-            }
+            ExecuteSQL(access, "PRAGMA journal_mode=WAL;");
 
             //using (var access = GetReadAccess())
             //{
@@ -150,7 +148,10 @@ namespace InABox.Database.SQLite
 
             //ExecuteSQL("PRAGMA foreign_keys = on;");
 
-            //using var profiler = new Profiler(true);
+            foreach(var type in Types)
+            {
+                DatabaseSchema.CheckProperties(type);
+            }
 
             // Need to arrange the typelist to ensure that foreign keys
             // refer to tables that already exist
@@ -158,8 +159,6 @@ namespace InABox.Database.SQLite
             foreach (var type in Types)
                 LoadType(type, ordered);
 
-            //profiler.Log("Ordered");
-
             //Load up the metadata
             var metadata = LoadMetaData();
             
@@ -168,20 +167,15 @@ namespace InABox.Database.SQLite
             {
                 OnLog?.Invoke(LogType.Information, $"Creating Table: {nameof(CustomProperty)}");
 
-                using var access = GetWriteAccess();
                 CreateTable(access, typeof(CustomProperty), true, []);
             }
             else
             {
-                using var access = GetWriteAccess();
                 CheckFields(access, typeof(CustomProperty), value.Item1, []);
             }
 
-            //profiler.Log("Ordered");
-
             var customproperties = Load<CustomProperty>(); // new Filter<CustomProperty>(x => x.Class).IsEqualTo(type.EntityName()))
 
-
             metadata = LoadMetaData();
 
             foreach (var type in ordered)
@@ -191,41 +185,31 @@ namespace InABox.Database.SQLite
                     table = type.EntityName().Split('.').Last();
                     if (!metadata.ContainsKey(table))
                     {
-                        OnLog?.Invoke(LogType.Information, "Creating Table: " + type.EntityName().Split('.').Last());
-                        using (var access = GetWriteAccess())
-                        {
-                            CreateTable(access, type, true, customproperties);
-                        }
+                        OnLog?.Invoke(LogType.Information, "Creating Table: " + type.Name);
+                        CreateTable(access, type, true, customproperties);
                     }
                 }
             }
 
             metadata = LoadMetaData();
-
+            
             foreach (var type in ordered)
             {
                 if (type.GetCustomAttribute<AutoEntity>() == null)
                 {
                     table = type.EntityName().Split('.').Last();
-                    using (var access = GetWriteAccess())
-                    {
-                        CheckFields(access, type, metadata[table].Item1, customproperties);
-                    }
+                    CheckFields(access, type, metadata[table].Item1, customproperties);
                 }
             }
 
             metadata = LoadMetaData();
-
+            
             foreach (var type in ordered)
             {
                 if (type.GetCustomAttribute<AutoEntity>() == null)
-
                 {
-                    table = type.EntityName().Split('.').Last();
-                    using (var access = GetWriteAccess())
-                    {
-                        CheckTriggers(access, type, metadata[table].Item2);
-                    }
+                    table = type.Name;
+                    CheckTriggers(access, type, metadata[table].Item2);
                 }
             }
 
@@ -237,10 +221,7 @@ namespace InABox.Database.SQLite
                 {
 
                     table = type.EntityName().Split('.').Last();
-                    using (var access = GetWriteAccess())
-                    {
-                        CheckIndexes(access, type, metadata[table].Item3);
-                    }
+                    CheckIndexes(access, type, metadata[table].Item3);
                 }
             }
 
@@ -254,27 +235,18 @@ namespace InABox.Database.SQLite
                     if (!metadata.ContainsKey(table))
                     {
                         OnLog?.Invoke(LogType.Information, "Creating Table: " + type.EntityName().Split('.').Last());
-                        using (var access = GetWriteAccess())
-                        {
-                            CreateTable(access, type, true, customproperties);
-                        }
+                        CreateTable(access, type, true, customproperties);
                     }
                     else
                     {
-                        using (var access = GetWriteAccess())
-                        {
-                            CheckFields(access, type, metadata[table].Item1, customproperties);
-                        }
+                        CheckFields(access, type, metadata[table].Item1, customproperties);
                     }
                 }
             }
 
             if (bForceRebuild)
             {
-                using (var access = GetWriteAccess())
-                {
-                    ExecuteSQL(access, "VACUUM;");
-                }
+                ExecuteSQL(access, "VACUUM;");
 
                 File.Delete(chkfile);
             }