2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00
This commit is contained in:
Ahmed Zamil 2019-12-23 11:57:16 +03:00
parent 128972bb74
commit a96ddf602f
12 changed files with 183 additions and 57 deletions

View File

@ -11,7 +11,7 @@
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.9</Version>
<Version>1.3.0</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -24,21 +24,24 @@ namespace Esyur.Stores.MongoDB
IMongoDatabase database;
IMongoCollection<BsonDocument> resourcesCollection;
//List<IResource> storeParents = new List<IResource>();
//List<IResource> storeChildren = new List<IResource>();
//string collectionName;
//string dbName;
Dictionary<string, WeakReference> resources = new Dictionary<string, WeakReference>();
public long Count
[ResourceEvent]
public event ResourceEventHanlder ResourceAdded;
[ResourceEvent]
public event ResourceEventHanlder ResourceRemoved;
[ResourceProperty]
public virtual int Count
{
get
{
return resourcesCollection.CountDocuments(x => true);
}// resources.Count; }
return (int)resourcesCollection.CountDocuments(x => true);
}
}
public void Destroy()
@ -73,6 +76,7 @@ namespace Esyur.Stores.MongoDB
return true;
}
[ResourceFunction]
public bool Remove(IResource resource)
{
var objectId = resource.Instance.Attributes["objectId"].ToString();
@ -81,9 +85,15 @@ namespace Esyur.Stores.MongoDB
this.database.DropCollection("record_" + objectId);
resourcesCollection.DeleteOne(filter);
ResourceRemoved?.Invoke(resource);
Instance.Modified("Count");
return true;
}
AsyncReply<T> Fetch<T>(string id) where T : IResource
{
@ -304,6 +314,10 @@ namespace Esyur.Stores.MongoDB
PutResource(resource).Wait();
ResourceAdded?.Invoke(resource);
Instance.Modified("Count");
return true;
}
@ -773,6 +787,8 @@ namespace Esyur.Stores.MongoDB
public bool Modify(IResource resource, string propertyName, object value, ulong age, DateTime dateTime)
{
if (resource == this)
return true;
var objectId = resource.Instance.Attributes["objectId"].ToString();

View File

@ -0,0 +1,28 @@
using Esyur.Core;
using Esyur.Data;
using Esyur.Proxy;
using Esyur.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Esyur.Stores.MongoDB
{
public class MongoDBStore<T> : MongoDBStore where T:IResource
{
[ResourceFunction]
public T Create(string name, Structure values)
{
return Warehouse.New<T>(name, this, null, null, null, null, values);
}
[ResourceFunction]
public async AsyncReply<IResource[]> Slice(int index, int limit)
{
var list = await this.Instance.Children<IResource>();
return list.Skip(index).Take(limit).ToArray();
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
@ -9,7 +9,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Esiur\Esiur.csproj" />
<ProjectReference Include="..\Esyur\Esyur.csproj" />
</ItemGroup>
</Project>

View File

@ -90,6 +90,8 @@ namespace Esyur.Core
Add(r);
}
public AsyncBag()
{

View File

@ -7,7 +7,7 @@
<PackageLicenseUrl>https://github.com/Esyur/Esyur-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.4.3</Version>
<Version>1.4.5</Version>
<RepositoryUrl>https://github.com/esyur/esyur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion>1.3.1.0</AssemblyVersion>

View File

@ -286,7 +286,7 @@ namespace Esyur.Net.HTTP
//base.Send(fd);
using (var fs = new FileStream(filename, FileMode.Open))
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var buffer = new byte[5000];

View File

@ -177,8 +177,9 @@ namespace Esyur.Net
public void Close()
{
//if (!connected)
// return;
// return;
try
{
if (sock != null)

View File

@ -111,20 +111,45 @@ namespace Esyur.Proxy
ILGenerator g = builder.GetILGenerator();
var getInstance = resourceType.GetTypeInfo().GetProperty("Instance").GetGetMethod();
//g.Emit(OpCodes.Ldarg_0);
//g.Emit(OpCodes.Ldarg_1);
//g.Emit(OpCodes.Call, pi.GetSetMethod());
//g.Emit(OpCodes.Nop);
//g.Emit(OpCodes.Ldarg_0);
//g.Emit(OpCodes.Call, getInstance);
//g.Emit(OpCodes.Ldstr, pi.Name);
//g.Emit(OpCodes.Call, modifyMethod);
//g.Emit(OpCodes.Nop);
//g.Emit(OpCodes.Ret);
Label exitMethod = g.DefineLabel();
Label callModified = g.DefineLabel();
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldarg_1);
g.Emit(OpCodes.Call, pi.GetSetMethod());
g.Emit(OpCodes.Nop);
//g.Emit(OpCodes.Nop);
g.Emit(OpCodes.Nop);
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Call, getInstance);
g.Emit(OpCodes.Dup);
g.Emit(OpCodes.Brtrue_S, callModified);
g.Emit(OpCodes.Pop);
g.Emit(OpCodes.Br_S, exitMethod);
g.MarkLabel(callModified);
g.Emit(OpCodes.Ldstr, pi.Name);
g.Emit(OpCodes.Call, modifyMethod);
g.Emit(OpCodes.Nop);
g.MarkLabel(exitMethod);
g.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(builder);

View File

@ -816,10 +816,10 @@ namespace Esyur.Resource
Type t = ResourceProxy.GetBaseType(resource);
#if NETSTANDARD
var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#else
var events = t.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
var events = t.GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#endif
foreach (var evt in events)

View File

@ -140,14 +140,14 @@ namespace Esyur.Resource.Template
#if NETSTANDARD
PropertyInfo[] propsInfo = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
EventInfo[] eventsInfo = type.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] methodsInfo = type.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
PropertyInfo[] propsInfo = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
EventInfo[] eventsInfo = type.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
MethodInfo[] methodsInfo = type.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Instance); // | BindingFlags.DeclaredOnly);
#else
PropertyInfo[] propsInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
EventInfo[] eventsInfo = type.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MethodInfo[] methodsInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
PropertyInfo[] propsInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
EventInfo[] eventsInfo = type.GetEvents(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
MethodInfo[] methodsInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.DeclaredOnly);
#endif
//byte currentIndex = 0;

View File

@ -52,12 +52,14 @@ namespace Esyur.Resource
public delegate void StoreConnectedEvent(IStore store, string name);
public delegate void StoreDisconnectedEvent(IStore store);
public static event StoreConnectedEvent StoreConnected;
public static event StoreDisconnectedEvent StoreDisconnected;
public static KeyList<string, Func<IStore>> Protocols { get; } = getSupportedProtocols();
private static Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)");
static KeyList<string, Func<IStore>> getSupportedProtocols()
{
@ -92,7 +94,7 @@ namespace Esyur.Resource
if (resources[id].TryGetTarget(out r))
return new AsyncReply<IResource>(r);
else
return new AsyncReply<IResource>(null);
return new AsyncReply<IResource>(null);
}
else
return new AsyncReply<IResource>(null);
@ -106,7 +108,7 @@ namespace Esyur.Resource
public static async AsyncReply<bool> Open()
{
foreach(var rk in resources)
foreach (var rk in resources)
{
IResource r;
if (rk.Value.TryGetTarget(out r))
@ -296,7 +298,7 @@ namespace Esyur.Resource
*/
public static async AsyncReply<IResource[]> Query(string path)
{
var rt = new AsyncReply<IResource[]>();
@ -314,8 +316,8 @@ namespace Esyur.Resource
var res = await store.Get(String.Join("/", p.Skip(1).ToArray()));
if (res != null)
return new IResource[] { res };
resource = store;
for (var i = 1; i < p.Length; i++)
{
@ -348,35 +350,42 @@ namespace Esyur.Resource
public static AsyncReply<IResource> Get(string path, object attributes = null, IResource parent = null, IPermissionsManager manager = null)
{
var rt = new AsyncReply<IResource>();
// Should we create a new store ?
if (path.Contains("://"))
if (urlRegex.IsMatch(path))
{
var url = path.Split(new string[] { "://" }, 2, StringSplitOptions.None);
var hostname = url[1].Split(new char[] { '/' }, 2)[0];
var pathname = string.Join("/", url[1].Split(new char[] { '/' }).Skip(1));
//if (path.Contains("://"))
//{
var url = urlRegex.Split(path);
//var url = path.Split(new string[] { "://" }, 2, StringSplitOptions.None);
//var hostname = url[1].Split(new char[] { '/' }, 2)[0];
//var pathname = string.Join("/", url[1].Split(new char[] { '/' }).Skip(1));
if (Protocols.ContainsKey(url[0]))
if (Protocols.ContainsKey(url[1]))
{
var handler = Protocols[url[0]];
var handler = Protocols[url[1]];
var store = handler();
Put(store, hostname, null, parent, null, 0, manager, attributes);
Put(store, url[2], null, parent, null, 0, manager, attributes);
store.Trigger(ResourceTrigger.Open).Then(x => {
store.Trigger(ResourceTrigger.Open).Then(x =>
{
warehouseIsOpen = true;
if (pathname.Length > 0 && pathname != "")
store.Get(pathname).Then(r => {
if (url[3].Length > 0 && url[3] != "")
store.Get(url[3]).Then(r =>
{
rt.Trigger(r);
}).Error(e => rt.TriggerError(e));
else
rt.Trigger(store);
}).Error(e => {
}).Error(e =>
{
rt.TriggerError(e);
Warehouse.Remove(store);
});
@ -384,19 +393,19 @@ namespace Esyur.Resource
return rt;
}
}
Query(path).Then(rs =>
{
// rt.TriggerError(new Exception());
// rt.TriggerError(new Exception());
if (rs != null && rs.Length > 0)
rt.Trigger(rs.First());
else
rt.Trigger(null);
});
return rt;
}
@ -449,9 +458,9 @@ namespace Esyur.Resource
else
parent.Instance.Children.Add(resource);
*/
if (resource is IStore)
if (resource is IStore)
StoreConnected?.Invoke(resource as IStore, name);
//else
store.Put(resource);
@ -468,15 +477,59 @@ namespace Esyur.Resource
resources.Add(resource.Instance.Id, new WeakReference<IResource>(resource));
if (warehouseIsOpen)
resource.Trigger(ResourceTrigger.Initialize);
resource.Trigger(ResourceTrigger.Initialize);
}
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null)
where T:IResource
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, Structure attributes = null, Structure arguments = null, Structure properties = null)
where T : IResource
{
var type = ResourceProxy.GetProxy<T>();
/*
if (arguments != null)
{
var constructors = type.GetConstructors(System.Reflection.BindingFlags.Public);
foreach(var constructor in constructors)
{
var pi = constructor.GetParameters();
if (pi.Length == constructor.le)
}
// cast arguments
ParameterInfo[] pi = fi.GetParameters();
object[] args = new object[pi.Length];
for (var i = 0; i < pi.Length; i++)
{
if (pi[i].ParameterType == typeof(DistributedConnection))
{
args[i] = this;
}
else if (namedArgs.ContainsKey(pi[i].Name))
{
args[i] = DC.CastConvert(namedArgs[pi[i].Name], pi[i].ParameterType);
}
}
constructors[0].
}
*/
var res = Activator.CreateInstance(type) as IResource;
if (properties != null)
{
foreach (var p in properties)
{
var pi = typeof(T).GetProperty(p.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
if (pi != null)
pi.SetValue(res, p.Value);
}
}
Put(res, name, store, parent, null, 0, manager, attributes);
return (T)res;
}
@ -533,7 +586,7 @@ namespace Esyur.Resource
if (t.ClassName == className)
return new AsyncReply<ResourceTemplate>(t);
return null;
return null;
}
public static bool Remove(IResource resource)
@ -542,7 +595,7 @@ namespace Esyur.Resource
if (resource.Instance == null)
return false;
if (resources.ContainsKey(resource.Instance.Id))
if (resources.ContainsKey(resource.Instance.Id))
resources.Remove(resource.Instance.Id);
else
return false;
@ -552,7 +605,8 @@ namespace Esyur.Resource
stores.Remove(resource as IStore);
// remove all objects associated with the store
var toBeRemoved = resources.Values.Where(x => {
var toBeRemoved = resources.Values.Where(x =>
{
IResource r;
return x.TryGetTarget(out r) && r.Instance.Store == resource;
}).ToArray();
@ -566,13 +620,13 @@ namespace Esyur.Resource
StoreDisconnected?.Invoke(resource as IStore);
}
if (resource.Instance.Store != null)
resource.Instance.Store.Remove(resource);
resource.Destroy();
return true;
}
}
}
}