fix everything storage

pull/1/head
Ogoun 5 years ago
parent fca123e13a
commit 23aa640f3a

@ -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<string, int>();
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<int>("1", 1); return s; })
.Add((s) => { Console.WriteLine("2"); s.Add<int>("2", 2); return s; })
.Add((s) => { Console.WriteLine("3"); s.Add<int>("3", 3); return s; })
.Add((s) => { Console.WriteLine("4"); s.Add<int>("4", 4); return s; })
.Add((s) => { Console.WriteLine("5"); s.Add<int>("5", 5); return s; });
var result = fiber.Run();
Console.WriteLine();
Console.WriteLine("Result");
foreach (var key in result.Keys<int>())
{
Console.WriteLine($"{key}: {result.Get<int>(key)}");
}
Console.ReadKey();
Configuration.Save(Configuration.ReadFromApplicationConfig());
/*Configuration.Save(Configuration.ReadFromApplicationConfig());
Bootstrap.Startup<MyService>(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();*/
}
}
}

@ -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<T>(string key, T entity)
{
_insert.Invoke(_instance, new object[] { key, entity, true });
_insert.Invoke(_instance, new object[] { key, entity });
}
public void InsertOrUpdate<T>(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<string> Keys()
{
return (IEnumerable<string>)_keys_getter.Invoke(_instance);
}
public object Get(string key)
{
return _getter.Invoke(_instance, key);
@ -192,5 +203,10 @@ namespace ZeroLevel.Services.Collections
{
return this[type].Get(key);
}
public IEnumerable<string> Keys<T>()
{
return this[typeof(T)].Keys();
}
}
}

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
namespace ZeroLevel.Services.Collections
{
public interface IEverythingStorage
{
IEnumerable<string> Keys<T>();
#region Generic
bool TryAdd<T>(string key, T value);

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using ZeroLevel.Services.Collections;
namespace ZeroLevel.Services
{
public class Fiber
{
class Step
{
public Func<IEverythingStorage, IEverythingStorage> _handler;
public Step _next;
}
Step _head = null;
Step _tail = null;
public Fiber()
{
}
public Fiber Add(Func<IEverythingStorage, IEverythingStorage> 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<Func<IEverythingStorage, IEverythingStorage>> 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;
}
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.