2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-09-13 12:43:17 +00:00
This commit is contained in:
2025-08-24 22:10:35 +03:00
parent 490f3eeebd
commit 3ea7adaebc
13 changed files with 106 additions and 123 deletions

View File

@@ -1570,14 +1570,13 @@ public partial class DistributedConnection : NetworkConnection, IStore
return true;
}
// AsyncReply<bool> connect({ISocket socket, String hostname, int port, String username, DC password, String domain})
/// <summary>
/// Store interface.
/// </summary>
/// <param name="resource">Resource.</param>
/// <returns></returns>
public AsyncReply<bool> Put(IResource resource)
public AsyncReply<bool> Put(IResource resource, string path)
{
if (Codec.IsLocalResource(resource, this))
neededResources.Add((resource as DistributedResource).DistributedResourceInstanceId, (DistributedResource)resource);

View File

@@ -272,7 +272,7 @@ partial class DistributedConnection
return;
}
var (_, parsed) = Codec.ParseAsync(data, 0, this, null);
var (_, parsed) = Codec.ParseAsync(data, 0, this, null, dataType);
if (parsed is AsyncReply reply)
{
reply.Then(result =>
@@ -2054,7 +2054,7 @@ partial class DistributedConnection
dataType.ContentLength, Instance.Warehouse);
var peerTime = (DateTime)args[0];
var interval = (uint)args[0];
var interval = (uint)args[1];
uint jitter = 0;

View File

@@ -37,7 +37,7 @@ namespace Esiur.Resource;
public interface IStore : IResource
{
AsyncReply<IResource> Get(string path);
AsyncReply<bool> Put(IResource resource);
AsyncReply<bool> Put(IResource resource, string path);
string Link(IResource resource);
bool Record(IResource resource, string propertyName, object value, ulong? age, DateTime? dateTime);
bool Modify(IResource resource, string propertyName, object value, ulong? age, DateTime? dateTime);

View File

@@ -764,7 +764,7 @@ public class Instance
if (res == res.Instance.store)
return name; // root store
else
return store.Link(res);
return store.Instance.name + "/" + store.Link(res);
}
else
return null;

View File

@@ -31,7 +31,7 @@ public abstract class Store<T> : IStore where T : IResource
public abstract bool Modify(IResource resource, string propertyName, object value, ulong? age, DateTime? dateTime);
public abstract AsyncReply<bool> Put(IResource resource);
public abstract AsyncReply<bool> Put(IResource resource, string path);
public abstract bool Record(IResource resource, string propertyName, object value, ulong? age, DateTime? dateTime);

View File

@@ -346,78 +346,78 @@ public class Warehouse
/// <param name="resource">Resource instance.</param>
/// <param name="store">IStore that manages the resource. Can be null if the resource is a store.</param>
/// <param name="parent">Parent resource. if not presented the store becomes the parent for the resource.</param>
public async AsyncReply<T> Put<T>(string instanceName, T resource, IStore store, TypeTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null) where T : IResource
{
if (resource.Instance != null)
throw new Exception("Resource has a store.");
//public async AsyncReply<T> Put<T>(string instanceName, T resource, IStore store, TypeTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null) where T : IResource
//{
// if (resource.Instance != null)
// throw new Exception("Resource has a store.");
var resourceReference = new WeakReference<IResource>(resource);
// var resourceReference = new WeakReference<IResource>(resource);
if (resource is IStore && store == null)
store = (IStore)resource;
// if (resource is IStore && store == null)
// store = (IStore)resource;
if (store == null)
throw new Exception("Resource store is not set.");
// if (store == null)
// throw new Exception("Resource store is not set.");
resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age);
// resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age);
if (attributes != null)
if (attributes is Map<string, object> attrs)
resource.Instance.SetAttributes(attrs);
else
resource.Instance.SetAttributes(Map<string, object>.FromObject(attributes));
// if (attributes != null)
// if (attributes is Map<string, object> attrs)
// resource.Instance.SetAttributes(attrs);
// else
// resource.Instance.SetAttributes(Map<string, object>.FromObject(attributes));
if (manager != null)
resource.Instance.Managers.Add(manager);
// if (manager != null)
// resource.Instance.Managers.Add(manager);
//if (store == parent)
// parent = null;
// //if (store == parent)
// // parent = null;
try
{
if (resource is IStore)
stores.TryAdd(resource as IStore, new List<WeakReference<IResource>>());
// try
// {
// if (resource is IStore)
// stores.TryAdd(resource as IStore, new List<WeakReference<IResource>>());
if (!await store.Put(resource))
throw new Exception("Store failed to put the resource");
//return default(T);
// if (!await store.Put(resource))
// throw new Exception("Store failed to put the resource");
// //return default(T);
//if (parent != null)
//{
// await parent.Instance.Store.AddChild(parent, resource);
// await store.AddParent(resource, parent);
//}
// //if (parent != null)
// //{
// // await parent.Instance.Store.AddChild(parent, resource);
// // await store.AddParent(resource, parent);
// //}
var t = resource.GetType();
Global.Counters["T-" + t.Namespace + "." + t.Name]++;
// var t = resource.GetType();
// Global.Counters["T-" + t.Namespace + "." + t.Name]++;
resources.TryAdd(resource.Instance.Id, resourceReference);
// resources.TryAdd(resource.Instance.Id, resourceReference);
if (warehouseIsOpen)
{
await resource.Trigger(ResourceTrigger.Initialize);
if (resource is IStore)
await resource.Trigger(ResourceTrigger.Open);
}
// if (warehouseIsOpen)
// {
// await resource.Trigger(ResourceTrigger.Initialize);
// if (resource is IStore)
// await resource.Trigger(ResourceTrigger.Open);
// }
if (resource is IStore)
StoreConnected?.Invoke(resource as IStore);
}
catch (Exception ex)
{
Remove(resource);
throw ex;
}
// if (resource is IStore)
// StoreConnected?.Invoke(resource as IStore);
// }
// catch (Exception ex)
// {
// Remove(resource);
// throw ex;
// }
return resource;
// return resource;
}
//}
/// <summary>
/// Put a resource in the warehouse.
@@ -429,7 +429,7 @@ public class Warehouse
public async AsyncReply<T> Put<T>(string path, T resource, TypeTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null) where T : IResource
{
if (resource.Instance != null)
throw new Exception("Resource has a store.");
throw new Exception("Resource already initialized.");
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("Invalid path.");
@@ -437,6 +437,7 @@ public class Warehouse
var location = path.TrimStart('/').Split('/');
IStore store = null;
//IResource parent = null;
var instanceName = location.Last();
@@ -447,73 +448,46 @@ public class Warehouse
store = (IStore)resource;
}
else
{
if (location.Length == 1 &&
// get parent
var parent = await Get<IResource>(string.Join("/", location.Take(location.Length - 1)));
if (parent == null)
throw new Exception("Can't find parent");
if (location.Length == 1)
return await Put<T>(path, resource, null, customTemplate, age, manager, attributes);
else
{
// get parent
parent = await Get<IResource>(string.Join("/", location.Take(location.Length - 1)));
if (parent == null)
throw new Exception("Can't find parent");
return await Put<T>(instanceName, resource, parent.Instance.Store, customTemplate, age, manager, attributes);
}
store = parent.Instance.Store;// GetStore(location[0]);
//if (store == null)
// throw new Exception("Store not found.");
}
var resourceReference = new WeakReference<IResource>(resource);
if (resource is IStore && store == null)
store = (IStore)resource;
if (store == null)
throw new Exception("Resource store is not set.");
resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age);
if (attributes != null)
if (attributes is Map<string, object> attrs)
resource.Instance.SetAttributes(attrs);
else
resource.Instance.SetAttributes(Map<string, object>.FromObject(attributes));
//if (manager != null)
// resource.Instance.Managers.Add(manager);
//if (store == parent)
// parent = null;
try
{
if (resource is IStore)
stores.TryAdd(resource as IStore, new List<WeakReference<IResource>>());
if (!await store.Put(resource))
throw new Exception("Store failed to put the resource");
//return default(T);
//if (parent != null)
//{
// await parent.Instance.Store.AddChild(parent, resource);
// await store.AddParent(resource, parent);
//}
else if ((IResource)resource != store)
{
if (!await store.Put(resource, string.Join("/", location.Skip(1).ToArray())))
throw new Exception("Store failed to put the resource.");
}
var t = resource.GetType();
Global.Counters["T-" + t.Namespace + "." + t.Name]++;
resources.TryAdd(resource.Instance.Id, resourceReference);
if (warehouseIsOpen)
{
await resource.Trigger(ResourceTrigger.Initialize);
@@ -523,6 +497,7 @@ public class Warehouse
if (resource is IStore)
StoreConnected?.Invoke(resource as IStore);
}
catch (Exception ex)
{
@@ -599,7 +574,7 @@ public class Warehouse
public async AsyncReply<IResource> New(Type type, string path, IPermissionsManager manager = null, object attributes = null, object properties = null)
{
var res = CreateInstance(type, properties);
var res = Create(type, properties);
return await Put(path, res, null, 0, manager, attributes);
}

View File

@@ -15,7 +15,7 @@ public class MemoryStore : IStore
public Instance Instance { get; set; }
public event DestroyedEvent OnDestroy;
KeyList<uint, IResource> resources = new KeyList<uint, IResource>();
@@ -27,8 +27,9 @@ public class MemoryStore : IStore
public string Link(IResource resource)
{
if (resource.Instance.Store == this)
return this.Instance.Name + "/$" + resource.Instance.Id;
return (string)resource.Instance.Variables["link"];
// return this.Instance.Name + "/$" + resource.Instance.Id;
return null;
}
@@ -48,24 +49,25 @@ public class MemoryStore : IStore
else
{
foreach (var r in resources)
if (r.Value.Instance.Name == path)
if (Link(r.Value) == path)//.Value.Instance.Name == path)
return new AsyncReply<IResource>(r.Value);
}
return new AsyncReply<IResource>(null);
}
public AsyncReply<bool> Put(IResource resource)
public AsyncReply<bool> Put(IResource resource, string path)
{
resources.Add(resource.Instance.Id, resource);// new WeakReference<IResource>(resource));
resources.Add(resource.Instance.Id, resource);
resource.Instance.Variables["children"] = new AutoList<IResource, Instance>(resource.Instance);
resource.Instance.Variables["parents"] = new AutoList<IResource, Instance>(resource.Instance);
resource.Instance.Variables["link"] = path;
return new AsyncReply<bool>(true);
}
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
{
return new AsyncReply<bool>(true);
@@ -102,7 +104,7 @@ public class MemoryStore : IStore
// else
// return new AsyncReply<bool>(false);
//}
//public AsyncReply<bool> AddParent(IResource resource, IResource parent)
//{
@@ -123,12 +125,19 @@ public class MemoryStore : IStore
public AsyncBag<T> Children<T>(IResource resource, string name) where T : IResource
{
var children = (resource.Instance.Variables["children"] as AutoList<IResource, Instance>);
if (name == null)
return new AsyncBag<T>(children.Where(x => x is T).Select(x => (T)x).ToArray());
else
return new AsyncBag<T>(children.Where(x => x is T && x.Instance.Name == name).Select(x => (T)x).ToArray());
var rt = new AsyncBag<T>();
var location = Link(resource) + "/";
return new AsyncBag<T>(resources.Where(r => r.Value is T && Link(r.Value).StartsWith(location)).Select(r => (T)r.Value).ToArray());
//var children = (resource.Instance.Variables["children"] as AutoList<IResource, Instance>);
//if (name == null)
// return new AsyncBag<T>(children.Where(x => x is T).Select(x => (T)x).ToArray());
//else
// return new AsyncBag<T>(children.Where(x => x is T && x.Instance.Name == name).Select(x => (T)x).ToArray());
}

View File

@@ -40,7 +40,7 @@ public class TemporaryStore : IStore
return new AsyncReply<IResource>(null);
}
public AsyncReply<bool> Put(IResource resource)
public AsyncReply<bool> Put(IResource resource, string path)
{
resources.Add(resource.Instance.Id, new WeakReference(resource));
return new AsyncReply<bool>(true);