Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
experiment creating and writing to two instances of memstate at the same time
<Query Kind="Program">
<NuGetReference>Memstate.Core</NuGetReference>
<NuGetReference>Memstate.JsonNet</NuGetReference>
<Namespace>Memstate.Configuration</Namespace>
<Namespace>Memstate</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
public class Cat {
public string Name { get; set;}
}
public class CatDB {
public IDictionary<string, Cat> Cats { get; } = new Dictionary<string, Cat>();
}
public class Getcats : Query<CatDB, Cat[]>
{
public override Cat[] Execute(CatDB model)
{
return model.Cats.Select(c => c.Value).ToArray();
}
}
public class AddCat : Command<CatDB, Cat> {
public string Name { get; set;}
public override Cat Execute(CatDB model)
{
var cat = new Cat() { Name = Name };
model.Cats.Add(cat.Name, cat);
return cat;
}
}
static async Task Main()
{
var dir = new FileInfo(Util.CurrentQueryPath).Directory.FullName;
var config = Config.Current;
config.SerializerName = "Newtonsoft.Json";
var settings = config.GetSettings<EngineSettings>();
settings.MaxBatchSize = 1;
settings.StreamName = Path.Combine(dir, "catdatabase");
var catdb = Engine.Start<CatDB>().GetAwaiter().GetResult();
Console.WriteLine("cat engine started");
var cats =await catdb.Execute(new Getcats());
Console.WriteLine("cats\n----\n");
foreach(var c in cats) Console.WriteLine(c.Name);
Console.WriteLine();
var cat = await catdb.Execute(new AddCat() { Name = $"Frodor-{Guid.NewGuid().ToString().Substring(10, 5)}" });
Console.WriteLine($"cat created: {cat.Name}");
settings.StreamName = Path.Combine(dir, "dogdatabase");
var dogdb = Engine.Start<CatDB>().GetAwaiter().GetResult();
Console.WriteLine("dog engine started");
var dogs = await dogdb.Execute(new Getcats());
Console.WriteLine("dogs\n----\n");
foreach (var d in dogs) Console.WriteLine(d.Name);
Console.WriteLine();
var cat2 = await catdb.Execute(new AddCat() { Name = $"Frodor-{Guid.NewGuid().ToString().Substring(10, 5)}" });
Console.WriteLine($"cat created: {cat2.Name}");
var dog1 = await dogdb.Execute(new AddCat() { Name = $"doggy-GANDALF-{Guid.NewGuid().ToString().Substring(10, 5)}" });
Console.WriteLine($"dog created: {dog1.Name}");
await catdb.DisposeAsync();
await dogdb.DisposeAsync();
Console.WriteLine("finished");
}
// Define other methods, classes and namespaces here
@goblinfactory
Copy link
Author

goblinfactory commented Sep 29, 2020

running the above a few times in a row, i.e. opening in linqpad and pressing F5 about 20 times produces the following output

cat engine started
cats
----

Frodor-f44-4
Frodor-1fa-4
Frodor-d49-4
Frodor-648-4
Frodor-ce3-4
Frodor-2fb-4
Frodor-ece-4
Frodor-9f6-4
Frodor-72f-4
Frodor-1cb-4
Frodor-172-4
Frodor-2a2-4
Frodor-abb-4
Frodor-b2b-4
Frodor-7fa-4
Frodor-dcb-4
Frodor-122-4

cat created: Frodor-d18-4
dog engine started
dogs
----

doggy-GANDALF-ba0-4
doggy-GANDALF-beb-4
doggy-GANDALF-93d-4
doggy-GANDALF-932-4
doggy-GANDALF-3b3-4
doggy-GANDALF-d78-4
doggy-GANDALF-ca6-4
doggy-GANDALF-3ae-4
doggy-GANDALF-575-4

cat created: Frodor-d78-4
dog created: doggy-GANDALF-495-4
finished

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment