experiment creating and writing to two instances of memstate at the same time
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
<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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
running the above a few times in a row, i.e. opening in linqpad and pressing F5 about 20 times produces the following output