Skip to content

Commit

Permalink
add option to use async friendly datastore
Browse files Browse the repository at this point in the history
this comes as a property because I could not find a way to detect this automagically

Signed-off-by: sriv-e6x <[email protected]>
  • Loading branch information
sriv-e6x committed Sep 6, 2024
1 parent 554021b commit 881ec3e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 10 deletions.
34 changes: 34 additions & 0 deletions Gauge.CSharp.Lib/DataStoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,39 @@ internal static void Clear(this ThreadLocal<ConcurrentDictionary<object, object>
store.Value.Clear();
}
}
internal static object Get(this ConcurrentDictionary<object, object> store, string key)
{
lock (store)
{
object outVal;
var valueExists = store.TryGetValue(key, out outVal);
return valueExists ? outVal : null;
}
}

internal static T Get<T>(this ConcurrentDictionary<object, object> store, string key)
{
lock (store)
{
return (T)store.Get(key);
}
}

internal static void Add(this ConcurrentDictionary<object, object> store, string key, object value)
{
lock (store)
{
store[key] = value;

}
}

internal static void Clear(this ConcurrentDictionary<object, object> store)
{
lock (store)
{
store.Clear();
}
}
}
}
6 changes: 3 additions & 3 deletions Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Description>CSharp bindings for Gauge. Write CSharp step implementation for Gauge specs. https://gauge.org</Description>
<Version>0.10.3</Version>
<AssemblyVersion>0.10.3.0</AssemblyVersion>
<FileVersion>0.10.3.0</FileVersion>
<Version>0.11.0</Version>
<AssemblyVersion>0.11.0.0</AssemblyVersion>
<FileVersion>0.11.0.0</FileVersion>
<Authors>getgauge</Authors>
<Company>ThoughtWorks Inc.</Company>
<Copyright>Copyright © ThoughtWorks Inc. 2018</Copyright>
Expand Down
25 changes: 23 additions & 2 deletions Gauge.CSharp.Lib/ScenarioDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

Expand All @@ -7,24 +8,44 @@ public class ScenarioDataStore
{
private static ThreadLocal<ConcurrentDictionary<object, object>> store = new ThreadLocal<ConcurrentDictionary<object, object>>(() => new ConcurrentDictionary<object, object>());

private static ConcurrentDictionary<object, object> asyncStore = new ConcurrentDictionary<object, object>();

public static object Get(string key)
{
return store.Get(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key);
}

public static T Get<T>(string key)
{
return store.Get<T>(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get<T>(key) : store.Get<T>(key);
}

public static void Add(string key, object value)
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Add(key, value);
return;
}
store.Add(key, value);
}

public static void Clear()
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Clear();
return;
}
store.Clear();
}

private static Boolean UseAsyncFriendlyDatastore()
{
var useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores");
if(String.IsNullOrEmpty(useAsyncFriendlyDatastore))
return false;
return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}
25 changes: 23 additions & 2 deletions Gauge.CSharp.Lib/SpecDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

Expand All @@ -7,24 +8,44 @@ public class SpecDataStore
{
private static ThreadLocal<ConcurrentDictionary<object, object>> store = new ThreadLocal<ConcurrentDictionary<object, object>>(() => new ConcurrentDictionary<object, object>());

private static ConcurrentDictionary<object, object> asyncStore = new ConcurrentDictionary<object, object>();

public static object Get(string key)
{
return store.Get(key);
return useAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key);
}

public static T Get<T>(string key)
{
return store.Get<T>(key);
return useAsyncFriendlyDatastore() ? asyncStore.Get<T>(key) : store.Get<T>(key);
}

public static void Add(string key, object value)
{
if(useAsyncFriendlyDatastore())
{
asyncStore.Add(key, value);
return;
}
store.Add(key, value);
}

public static void Clear()
{
if(useAsyncFriendlyDatastore())
{
asyncStore.Clear();
return;
}
store.Clear();
}

private static Boolean useAsyncFriendlyDatastore()
{
string useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores");
if(String.IsNullOrEmpty(useAsyncFriendlyDatastore))
return false;
return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}
27 changes: 24 additions & 3 deletions Gauge.CSharp.Lib/SuiteDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

Expand All @@ -7,24 +8,44 @@ public class SuiteDataStore
{
private static ThreadLocal<ConcurrentDictionary<object, object>> store = new ThreadLocal<ConcurrentDictionary<object, object>>(() => new ConcurrentDictionary<object, object>());

private static ConcurrentDictionary<object, object> asyncStore = new ConcurrentDictionary<object, object>();

public static object Get(string key)
{
return store.Get(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key);
}

public static T Get<T>(string key)
{
return store.Get<T>(key);
return UseAsyncFriendlyDatastore() ? asyncStore.Get<T>(key) : store.Get<T>(key);
}

public static void Add(string key, object value)
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Add(key, value);
return;
}
store.Add(key, value);
}

public static void Clear()
{
if(UseAsyncFriendlyDatastore())
{
asyncStore.Clear();
return;
}
store.Clear();
}

private static Boolean UseAsyncFriendlyDatastore()
{
var useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores");
if(String.IsNullOrEmpty(useAsyncFriendlyDatastore))
return false;
return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
}
}

0 comments on commit 881ec3e

Please sign in to comment.