Skip to content

Commit

Permalink
Feature/static caching (#26)
Browse files Browse the repository at this point in the history
* Added a decorator to statically cache schema and calculated dependencies

* Upped pre-release version
  • Loading branch information
carl-berg authored Feb 21, 2023
1 parent d3f9624 commit 4ea286d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DataDude/DataDude.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<Version>0.8.0-preview.2</Version>
<Version>0.8.0-preview.3</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
8 changes: 5 additions & 3 deletions DataDude/DataDudeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace DataDude
{
public class DataDudeContext
{
internal const string SchemaKey = "Schema";
private Dictionary<string, object> _store;

public DataDudeContext(ISchemaLoader schemaLoader)
{
_store = new Dictionary<string, object>();
Expand Down Expand Up @@ -60,7 +62,7 @@ public DataDudeContext(ISchemaLoader schemaLoader)

public IEnumerable<string> ContextKeys => _store.Keys;

public SchemaInformation? Schema => Get<SchemaInformation>("Schema");
public SchemaInformation? Schema => Get<SchemaInformation>(SchemaKey);

public static DbType GetDbType(ColumnInformation column)
{
Expand Down Expand Up @@ -96,10 +98,10 @@ public void Set<T>(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);
}
}
}
Expand Down
54 changes: 54 additions & 0 deletions DataDude/Instructions/StaticCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using DataDude.Schema;
using System.Threading.Tasks;

namespace DataDude.Instructions
{
/// <summary>
/// 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
/// </summary>
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<string, IDictionary<TableInformation, IReadOnlyList<TableInformation>>> 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<IDictionary<TableInformation, IReadOnlyList<TableInformation>>>(cacheKey) is { } createdCache)
{
Dependencies[cacheKey] = createdCache;
}
}

return default;
}
}
}

0 comments on commit 4ea286d

Please sign in to comment.