From 23aa640f3aa269b748ba8342a251654a871a496c Mon Sep 17 00:00:00 2001 From: Ogoun Date: Tue, 12 Nov 2019 04:39:41 +0300 Subject: [PATCH] fix everything storage --- TestApp/Program.cs | 34 ++++++++++- .../Services/Collections/EverythingStorage.cs | 28 +++++++-- .../Collections/IEverythingStorage.cs | 3 + ZeroLevel/Services/Fiber.cs | 58 +++++++++++++++++++ 4 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 ZeroLevel/Services/Fiber.cs diff --git a/TestApp/Program.cs b/TestApp/Program.cs index 46e21db..3699281 100644 --- a/TestApp/Program.cs +++ b/TestApp/Program.cs @@ -1,4 +1,8 @@ -using ZeroLevel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ZeroLevel.Services; namespace TestApp { @@ -6,10 +10,34 @@ namespace TestApp { private static void Main(string[] args) { + var dict = new Dictionary(); + var methods = dict.GetType().GetMethods(BindingFlags.Static | + BindingFlags.Instance | + BindingFlags.Public | + BindingFlags.NonPublic | + BindingFlags.FlattenHierarchy) + .Select(s => s.Name).OrderBy(s => s); + var fiber = new Fiber(); + fiber + .Add((s) => { Console.WriteLine("1"); s.Add("1", 1); return s; }) + .Add((s) => { Console.WriteLine("2"); s.Add("2", 2); return s; }) + .Add((s) => { Console.WriteLine("3"); s.Add("3", 3); return s; }) + .Add((s) => { Console.WriteLine("4"); s.Add("4", 4); return s; }) + .Add((s) => { Console.WriteLine("5"); s.Add("5", 5); return s; }); + var result = fiber.Run(); + Console.WriteLine(); + Console.WriteLine("Result"); + foreach (var key in result.Keys()) + { + Console.WriteLine($"{key}: {result.Get(key)}"); + } - Configuration.Save(Configuration.ReadFromApplicationConfig()); + Console.ReadKey(); + + + /*Configuration.Save(Configuration.ReadFromApplicationConfig()); Bootstrap.Startup(args, () => Configuration.ReadSetFromIniFile("config.ini")) .EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug) @@ -17,7 +45,7 @@ namespace TestApp .Run() .WaitWhileStatus(ZeroServiceStatus.Running) .Stop(); - Bootstrap.Shutdown(); + Bootstrap.Shutdown();*/ } } } \ No newline at end of file diff --git a/ZeroLevel/Services/Collections/EverythingStorage.cs b/ZeroLevel/Services/Collections/EverythingStorage.cs index 08da9d0..1f991d8 100644 --- a/ZeroLevel/Services/Collections/EverythingStorage.cs +++ b/ZeroLevel/Services/Collections/EverythingStorage.cs @@ -22,6 +22,7 @@ namespace ZeroLevel.Services.Collections private readonly Invoker _containsKey; private readonly Invoker _remove; private readonly Invoker _getter; + private readonly Invoker _keys_getter; private readonly object _instance; public ConcreteTypeRepository(Type entityType) @@ -31,31 +32,36 @@ namespace ZeroLevel.Services.Collections var instanceType = genericType.MakeGenericType(new Type[] { typeof(string), entityType }); _instance = Activator.CreateInstance(instanceType); - var insert_key = _wrapper.Configure(instanceType, "Insert").Single(); + var insert_key = _wrapper.Configure(instanceType, "Add").Single(); _insert = _wrapper.GetInvoker(insert_key); - var contains_key = _wrapper.Configure(instanceType, "ContainsKey").Single(); + var contains_key = _wrapper.Configure(instanceType, mi => mi.Name.Equals("ContainsKey") && mi.GetParameters()?.Length == 1).Single(); _containsKey = _wrapper.GetInvoker(contains_key); - var remove_key = _wrapper.Configure(instanceType, "Remove").Single(); + var remove_key = _wrapper.Configure(instanceType, mi => mi.Name.Equals("Remove") && mi.GetParameters()?.Length == 1).Single(); _remove = _wrapper.GetInvoker(remove_key); var p = instanceType.GetProperty("Item", entityType); var getter = p.GetGetMethod(); var get_key = _wrapper.Configure(getter); _getter = _wrapper.GetInvoker(get_key); + + var k = instanceType.GetProperty("Keys", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.NonPublic); + var keys_getter = k.GetGetMethod(); + var get_keys = _wrapper.Configure(keys_getter); + _keys_getter = _wrapper.GetInvoker(get_keys); } public void Insert(string key, T entity) { - _insert.Invoke(_instance, new object[] { key, entity, true }); + _insert.Invoke(_instance, new object[] { key, entity }); } public void InsertOrUpdate(string key, T entity) { if ((bool)_containsKey.Invoke(_instance, key)) _remove.Invoke(_instance, key); - _insert.Invoke(_instance, new object[] { key, entity, true }); + _insert.Invoke(_instance, new object[] { key, entity }); } public bool ContainsKey(string key) @@ -73,6 +79,11 @@ namespace ZeroLevel.Services.Collections return (T)_getter.Invoke(_instance, key); } + public IEnumerable Keys() + { + return (IEnumerable)_keys_getter.Invoke(_instance); + } + public object Get(string key) { return _getter.Invoke(_instance, key); @@ -137,7 +148,7 @@ namespace ZeroLevel.Services.Collections public T Get(string key) { return this[typeof(T)].Get(key); - } + } public void AddOrUpdate(string key, T value) { @@ -192,5 +203,10 @@ namespace ZeroLevel.Services.Collections { return this[type].Get(key); } + + public IEnumerable Keys() + { + return this[typeof(T)].Keys(); + } } } \ No newline at end of file diff --git a/ZeroLevel/Services/Collections/IEverythingStorage.cs b/ZeroLevel/Services/Collections/IEverythingStorage.cs index 5104e79..607a015 100644 --- a/ZeroLevel/Services/Collections/IEverythingStorage.cs +++ b/ZeroLevel/Services/Collections/IEverythingStorage.cs @@ -1,9 +1,12 @@ using System; +using System.Collections.Generic; namespace ZeroLevel.Services.Collections { public interface IEverythingStorage { + IEnumerable Keys(); + #region Generic bool TryAdd(string key, T value); diff --git a/ZeroLevel/Services/Fiber.cs b/ZeroLevel/Services/Fiber.cs new file mode 100644 index 0000000..fa29ff8 --- /dev/null +++ b/ZeroLevel/Services/Fiber.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using ZeroLevel.Services.Collections; + +namespace ZeroLevel.Services +{ + public class Fiber + { + class Step + { + public Func _handler; + public Step _next; + } + + Step _head = null; + Step _tail = null; + + public Fiber() + { + } + + public Fiber Add(Func action) + { + if (_head == null) + { + _head = _tail = new Step { _handler = action, _next = null }; + } + else + { + var s = new Step { _handler = action, _next = null }; + _tail._next = s; + _tail = s; + } + return this; + } + + public IEnumerable> Iterate() + { + if (_head == null) yield break; + var current = _head; + while (current != null) + { + yield return current._handler; + current = current._next; + } + } + + public IEverythingStorage Run(IEverythingStorage buffer = null) + { + var storage = buffer; + foreach (var a in Iterate()) + { + storage = a.Invoke(storage ?? new EverythingStorage()); + } + return storage; + } + } +}