2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +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> <PackageProjectUrl>http://www.esyur.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl> <RepositoryUrl>https://github.com/esyur/esyur-dotnet/</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.9</Version> <Version>1.3.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -24,21 +24,24 @@ namespace Esyur.Stores.MongoDB
IMongoDatabase database; IMongoDatabase database;
IMongoCollection<BsonDocument> resourcesCollection; 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>(); 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 get
{ {
return resourcesCollection.CountDocuments(x => true); return (int)resourcesCollection.CountDocuments(x => true);
}// resources.Count; } }
} }
public void Destroy() public void Destroy()
@ -73,6 +76,7 @@ namespace Esyur.Stores.MongoDB
return true; return true;
} }
[ResourceFunction]
public bool Remove(IResource resource) public bool Remove(IResource resource)
{ {
var objectId = resource.Instance.Attributes["objectId"].ToString(); var objectId = resource.Instance.Attributes["objectId"].ToString();
@ -81,9 +85,15 @@ namespace Esyur.Stores.MongoDB
this.database.DropCollection("record_" + objectId); this.database.DropCollection("record_" + objectId);
resourcesCollection.DeleteOne(filter); resourcesCollection.DeleteOne(filter);
ResourceRemoved?.Invoke(resource);
Instance.Modified("Count");
return true; return true;
} }
AsyncReply<T> Fetch<T>(string id) where T : IResource AsyncReply<T> Fetch<T>(string id) where T : IResource
{ {
@ -304,6 +314,10 @@ namespace Esyur.Stores.MongoDB
PutResource(resource).Wait(); PutResource(resource).Wait();
ResourceAdded?.Invoke(resource);
Instance.Modified("Count");
return true; return true;
} }
@ -773,6 +787,8 @@ namespace Esyur.Stores.MongoDB
public bool Modify(IResource resource, string propertyName, object value, ulong age, DateTime dateTime) 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(); 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> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
@ -9,7 +9,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Esiur\Esiur.csproj" /> <ProjectReference Include="..\Esyur\Esyur.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

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

View File

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

View File

@ -286,7 +286,7 @@ namespace Esyur.Net.HTTP
//base.Send(fd); //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]; var buffer = new byte[5000];

View File

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

View File

@ -113,18 +113,43 @@ namespace Esyur.Proxy
var getInstance = resourceType.GetTypeInfo().GetProperty("Instance").GetGetMethod(); 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_0);
g.Emit(OpCodes.Ldarg_1); g.Emit(OpCodes.Ldarg_1);
g.Emit(OpCodes.Call, pi.GetSetMethod()); 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.Ldarg_0);
g.Emit(OpCodes.Call, getInstance); 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.Ldstr, pi.Name);
g.Emit(OpCodes.Call, modifyMethod); g.Emit(OpCodes.Call, modifyMethod);
g.Emit(OpCodes.Nop); g.Emit(OpCodes.Nop);
g.MarkLabel(exitMethod);
g.Emit(OpCodes.Ret); g.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(builder); propertyBuilder.SetSetMethod(builder);

View File

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

View File

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

View File

@ -58,6 +58,8 @@ namespace Esyur.Resource
public static KeyList<string, Func<IStore>> Protocols { get; } = getSupportedProtocols(); public static KeyList<string, Func<IStore>> Protocols { get; } = getSupportedProtocols();
private static Regex urlRegex = new Regex(@"^(?:([\S]*)://([^/]*)/?)");
static KeyList<string, Func<IStore>> getSupportedProtocols() static KeyList<string, Func<IStore>> getSupportedProtocols()
{ {
@ -106,7 +108,7 @@ namespace Esyur.Resource
public static async AsyncReply<bool> Open() public static async AsyncReply<bool> Open()
{ {
foreach(var rk in resources) foreach (var rk in resources)
{ {
IResource r; IResource r;
if (rk.Value.TryGetTarget(out r)) if (rk.Value.TryGetTarget(out r))
@ -351,32 +353,39 @@ namespace Esyur.Resource
// Should we create a new store ? // 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]; //if (path.Contains("://"))
var pathname = string.Join("/", url[1].Split(new char[] { '/' }).Skip(1)); //{
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(); 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; warehouseIsOpen = true;
if (pathname.Length > 0 && pathname != "") if (url[3].Length > 0 && url[3] != "")
store.Get(pathname).Then(r => { store.Get(url[3]).Then(r =>
{
rt.Trigger(r); rt.Trigger(r);
}).Error(e => rt.TriggerError(e)); }).Error(e => rt.TriggerError(e));
else else
rt.Trigger(store); rt.Trigger(store);
}).Error(e => { }).Error(e =>
{
rt.TriggerError(e); rt.TriggerError(e);
Warehouse.Remove(store); Warehouse.Remove(store);
}); });
@ -388,7 +397,7 @@ namespace Esyur.Resource
Query(path).Then(rs => Query(path).Then(rs =>
{ {
// rt.TriggerError(new Exception()); // rt.TriggerError(new Exception());
if (rs != null && rs.Length > 0) if (rs != null && rs.Length > 0)
rt.Trigger(rs.First()); rt.Trigger(rs.First());
else else
@ -468,15 +477,59 @@ namespace Esyur.Resource
resources.Add(resource.Instance.Id, new WeakReference<IResource>(resource)); resources.Add(resource.Instance.Id, new WeakReference<IResource>(resource));
if (warehouseIsOpen) 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) 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 where T : IResource
{ {
var type = ResourceProxy.GetProxy<T>(); 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; 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); Put(res, name, store, parent, null, 0, manager, attributes);
return (T)res; return (T)res;
} }
@ -552,7 +605,8 @@ namespace Esyur.Resource
stores.Remove(resource as IStore); stores.Remove(resource as IStore);
// remove all objects associated with the store // remove all objects associated with the store
var toBeRemoved = resources.Values.Where(x => { var toBeRemoved = resources.Values.Where(x =>
{
IResource r; IResource r;
return x.TryGetTarget(out r) && r.Instance.Store == resource; return x.TryGetTarget(out r) && r.Instance.Store == resource;
}).ToArray(); }).ToArray();
@ -573,6 +627,6 @@ namespace Esyur.Resource
resource.Destroy(); resource.Destroy();
return true; return true;
} }
} }
} }