Skip to content

Commit

Permalink
Merge pull request #337 from BlythMeister/Caching
Browse files Browse the repository at this point in the history
Update DefaultDatabase to use and trust the script cache
  • Loading branch information
BiggerNoise committed Sep 10, 2018
2 parents 7029e74 + 972bf72 commit 4fb8c69
Showing 1 changed file with 12 additions and 52 deletions.
64 changes: 12 additions & 52 deletions product/roundhouse/databases/DefaultDatabase.cs
Expand Up @@ -238,6 +238,7 @@ public virtual void insert_script_run(string script_name, string sql_to_run, str
try
{
retry_policy.ExecuteAction(() => repository.save_or_update(script_run));
scripts_cache[script_name] = script_run;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -330,13 +331,13 @@ public virtual long insert_version_and_get_version_id(string repository_path, st

public virtual string get_current_script_hash(string script_name)
{
ScriptsRun script = get_from_script_cache(script_name) ?? get_script_run(script_name);
var script = get_script_run(script_name);
return script != null ? script.text_hash : string.Empty;
}

public virtual bool has_run_script_already(string script_name)
{
ScriptsRun script = get_from_script_cache(script_name) ?? get_script_run(script_name);
var script = get_script_run(script_name);
return script != null;
}

Expand All @@ -347,31 +348,19 @@ protected IList<ScriptsRun> get_all_scripts()

protected ScriptsRun get_script_run(string script_name)
{
QueryOver<ScriptsRun> criteria = QueryOver.Of<ScriptsRun>()
.Where(x => x.script_name == script_name)
.OrderBy(x => x.id).Desc
.Take(1);

ScriptsRun script = null;
IList<ScriptsRun> found_items;
try
{
found_items = retry_policy.ExecuteAction(() => repository.get_with_criteria(criteria));
}
catch (Exception ex)
if (scripts_cache == null)
{
Log.bound_to(this).log_an_error_event_containing(
"{0} with provider {1} does not provide a facility for recording scripts run this time.{2}{3}",
GetType(), provider, Environment.NewLine, ex.to_string());
throw;
}
scripts_cache = new Dictionary<string, ScriptsRun>();

if (found_items != null && found_items.Count > 0)
{
script = found_items[0];
// latest id overrides possible old one, just like in queries searching for scripts
foreach (var script in get_all_scripts().OrderBy(x => x.id))
{
scripts_cache[script.script_name] = script;
}
}

return script;
ScriptsRun script_run;
return scripts_cache.TryGetValue(script_name, out script_run) ? script_run : null;
}

public virtual void Dispose()
Expand Down Expand Up @@ -403,34 +392,5 @@ private void dispose_connection(IConnection<DBCONNECTION> connection)
connection.Dispose();
}
}

private ScriptsRun get_from_script_cache(string script_name)
{
ensure_script_cache();

ScriptsRun script;
if (scripts_cache.TryGetValue(script_name, out script))
{
return script;
}

return null;
}

private void ensure_script_cache()
{
if (scripts_cache != null)
{
return;
}

scripts_cache = new Dictionary<string, ScriptsRun>();

// latest id overrides possible old one, just like in queries searching for scripts
foreach (var script in get_all_scripts().OrderBy(x => x.id))
{
scripts_cache[script.script_name] = script;
}
}
}
}

0 comments on commit 4fb8c69

Please sign in to comment.