// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data;
namespace Spludlow
{
///
/// Simple Persistant varible storage (per host). For thread safty do get() and set() inside using{}, see test methods below
///
public class Variables : IDisposable
{
private Spludlow.Data.LockedTextTable _LockedTextTable;
private static string Filename = Spludlow.Config.ProgramData + @"\Data\Variables.txt";
private static DataTable _EmptyTable = Spludlow.Data.TextTable.ReadText(new string[]
{
"Key Value",
"String* String",
});
private static Spludlow.Data.LockedTextTable MakeLockedTable()
{
return new Data.LockedTextTable(Filename, false, Encoding.UTF8, false, _EmptyTable);
}
public Variables()
{
this._LockedTextTable = MakeLockedTable();
}
public void Set(string key, string value)
{
DataRow row = this._LockedTextTable.Table.Rows.Find(key);
if (row == null)
row = this._LockedTextTable.Table.Rows.Add(key, value);
else
row["Value"] = value;
}
public static void SetValue(string key, string value)
{
using (Variables variables = new Variables())
variables.Set(key, value);
}
public string Get(string key)
{
DataRow row = this._LockedTextTable.Table.Rows.Find(key);
if (row == null)
return null;
else
return (string)row["Value"];
}
public static string GetValue(string key)
{
using (Variables variables = new Variables())
return variables.Get(key);
}
public void Dispose()
{
this._LockedTextTable.Dispose();
}
///
/// Bump up a number on seperate theads. Report is logged showing results with and without using{}.
///
public static void Test()
{
string key = "VariablesTest";
int threads = 8;
int iterations = 8;
int sleepMs = 100;
DataSet dataSet = new DataSet();
Spludlow.Variables.SetValue(key, "0");
dataSet.Tables.Add(Test(key, threads, iterations, sleepMs, false));
Spludlow.Variables.SetValue(key, "0");
dataSet.Tables.Add(Test(key, threads, iterations, sleepMs, true));
Spludlow.Log.Report("Variables Thread Test", dataSet);
}
private static DataTable Test(string key, int threads, int iterations, int sleepMs, bool rightWay)
{
DataTable table = Spludlow.Data.TextTable.ReadText(new string[]
{
"Thread Pass Value",
"Int32 Int32 String",
});
table.TableName = (rightWay == true ? "Right Way" : "Wrong Way");
Task[] tasks = new Task[threads];
for (int index = 0; index < threads; ++index)
{
int threadId = index;
tasks[index] = new Task(() => TestWork(key, iterations, threadId, sleepMs, table, rightWay));
}
foreach (Task task in tasks)
task.Start();
Task.WaitAll(tasks);
return table;
}
private static void TestWork(string key, int iterations, int threadId, int sleepMs, DataTable table, bool rightWay)
{
Random rand = new Random();
for (int pass = 0; pass < iterations; ++pass)
{
string value;
if (rightWay == true)
{
using (Spludlow.Variables variables = new Spludlow.Variables())
{
value = variables.Get(key);
System.Threading.Thread.Sleep(rand.Next(sleepMs));
value = (Int32.Parse(value) + 1).ToString();
variables.Set(key, value);
}
}
else
{
value = Spludlow.Variables.GetValue(key);
System.Threading.Thread.Sleep(rand.Next(sleepMs));
value = (Int32.Parse(value) + 1).ToString();
Spludlow.Variables.SetValue(key, value);
}
table.Rows.Add(threadId, pass, value);
}
}
}
}