-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added a decorator to statically cache schema and calculated dependencies * Upped pre-release version
- Loading branch information
Showing
3 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |