diff --git a/DataDude/DataDude.csproj b/DataDude/DataDude.csproj index 1e31b01..d86c8ef 100644 --- a/DataDude/DataDude.csproj +++ b/DataDude/DataDude.csproj @@ -4,7 +4,7 @@ netstandard2.1 9.0 enable - 0.8.0-preview.2 + 0.8.0-preview.3 true true snupkg diff --git a/DataDude/DataDudeContext.cs b/DataDude/DataDudeContext.cs index c85dcbb..2f7ab97 100644 --- a/DataDude/DataDudeContext.cs +++ b/DataDude/DataDudeContext.cs @@ -12,7 +12,9 @@ namespace DataDude { public class DataDudeContext { + internal const string SchemaKey = "Schema"; private Dictionary _store; + public DataDudeContext(ISchemaLoader schemaLoader) { _store = new Dictionary(); @@ -60,7 +62,7 @@ public DataDudeContext(ISchemaLoader schemaLoader) public IEnumerable ContextKeys => _store.Keys; - public SchemaInformation? Schema => Get("Schema"); + public SchemaInformation? Schema => Get(SchemaKey); public static DbType GetDbType(ColumnInformation column) { @@ -96,10 +98,10 @@ public void Set(string key, T value) public async ValueTask LoadSchema(DbConnection connection, DbTransaction? transaction = null) { - if (_store.ContainsKey("Schema") is false) + if (_store.ContainsKey(SchemaKey) is false) { var schema = await SchemaLoader.Load(connection, transaction); - Set("Schema", schema); + Set(SchemaKey, schema); } } } diff --git a/DataDude/Instructions/StaticCache.cs b/DataDude/Instructions/StaticCache.cs new file mode 100644 index 0000000..2c51d8d --- /dev/null +++ b/DataDude/Instructions/StaticCache.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using System.Linq; +using DataDude.Schema; +using System.Threading.Tasks; + +namespace DataDude.Instructions +{ + /// + /// Attempting to speed up DataDude by statically caching and pre-loading sql schema and calculated dependencies in between runs + /// Warning: This is an experimental feature + /// + public class StaticCache : IInstructionDecorator + { + public StaticCache(DataDudeContext context) + { + if (Schema is { }) + { + // Pre-load schema + context.Set(DataDudeContext.SchemaKey, Schema); + } + + foreach (var (cacheKey, dependencyCache) in Dependencies) + { + // Pre-load calculated dependencies + context.Set(cacheKey, dependencyCache); + } + } + + protected static SchemaInformation? Schema { get; set; } + protected static Dictionary>> Dependencies { get; set; } = new(); + + public ValueTask PreProcess(DataDudeContext context) => default; + + public ValueTask PostProcess(DataDudeContext context) + { + if (Schema is null) + { + // Cache loaded schema + Schema = context.Schema; + } + + // Cache calculated dependencies + foreach (var cacheKey in context.ContextKeys.Where(key => key.StartsWith(DependencyService.CachePrefix) && Dependencies.ContainsKey(key) is false)) + { + if (context.Get>>(cacheKey) is { } createdCache) + { + Dependencies[cacheKey] = createdCache; + } + } + + return default; + } + } +}