using Esiur.Resource; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Esiur.Core; using Esiur.Data; using Esiur.Data.Types; namespace Esiur.Stores; public class MemoryStore : IStore { public Instance Instance { get; set; } public event DestroyedEvent OnDestroy; KeyList resources = new KeyList(); public void Destroy() { OnDestroy?.Invoke(this); } public string Link(IResource resource) { if (resource.Instance.Store == this) return (string)resource.Instance.Variables["link"]; // return this.Instance.Name + "/$" + resource.Instance.Id; return null; } public AsyncReply Get(string path) { if (path.StartsWith("$")) { uint id; if (uint.TryParse(path.Substring(1), out id)) { foreach (var r in resources) if (r.Value.Instance.Id == id) return new AsyncReply(r.Value); } } else { foreach (var r in resources) if (Link(r.Value) == path)//.Value.Instance.Name == path) return new AsyncReply(r.Value); } return new AsyncReply(null); } public AsyncReply Put(IResource resource, string path) { resources.Add(resource.Instance.Id, resource); resource.Instance.Variables["children"] = new AutoList(resource.Instance); resource.Instance.Variables["parents"] = new AutoList(resource.Instance); resource.Instance.Variables["link"] = path; return new AsyncReply(true); } public AsyncReply Handle(ResourceOperation operation, IResourceContext context = null) { return new AsyncReply(true); } public bool Record(IResource resource, string propertyName, object value, ulong? age, DateTime? dateTime) { throw new NotImplementedException(); } public AsyncReply> GetRecord(IResource resource, DateTime fromDate, DateTime toDate) { throw new NotImplementedException(); } public bool Modify(IResource resource, PropertyDef propertyDef, object value, ulong? age, DateTime? dateTime) { return true; } //public AsyncReply AddChild(IResource parent, IResource child) //{ // if (parent.Instance.Store == this) // { // (parent.Instance.Variables["children"] as AutoList).Add(child); // return new AsyncReply(true); // } // else // return new AsyncReply(false); //} //public AsyncReply AddParent(IResource resource, IResource parent) //{ // if (resource.Instance.Store == this) // { // (resource.Instance.Variables["parents"] as AutoList).Add(parent); // return new AsyncReply(true); // } // else // return new AsyncReply(false); //} public AsyncReply RemoveParent(IResource child, IResource parent) { throw new NotImplementedException(); } public AsyncBag Children(IResource resource, string name) where T : IResource { var parentLink = ReferenceEquals(resource, this) ? string.Empty : (Link(resource) ?? string.Empty).Trim('/'); var prefix = parentLink.Length == 0 ? string.Empty : parentLink + "/"; var children = resources .Where(entry => entry.Value is T) .Select(entry => new { Resource = (T)entry.Value, Link = Link(entry.Value)?.Trim('/') }) .Where(entry => entry.Link is not null && entry.Link.StartsWith(prefix, StringComparison.Ordinal)) .Where(entry => { var relative = entry.Link!.Substring(prefix.Length); return relative.Length > 0 && !relative.Contains('/'); }) .Where(entry => name is null || entry.Resource.Instance.Name == name) .Select(entry => entry.Resource) .ToArray(); return new AsyncBag(children); //var children = (resource.Instance.Variables["children"] as AutoList); //if (name == null) // return new AsyncBag(children.Where(x => x is T).Select(x => (T)x).ToArray()); //else // return new AsyncBag(children.Where(x => x is T && x.Instance.Name == name).Select(x => (T)x).ToArray()); } public AsyncBag Parents(IResource resource, string name) where T : IResource { var parents = (resource.Instance.Variables["parents"] as AutoList); if (name == null) return new AsyncBag(parents.Where(x => x is T).Select(x => (T)x).ToArray()); else return new AsyncBag(parents.Where(x => x is T && x.Instance.Name == name).Select(x => (T)x).ToArray()); } AsyncReply IStore.Remove(IResource resource) { resources.Remove(resource.Instance.Id); return AsyncReply.FromResult(true); } public AsyncReply Remove(string path) { throw new NotImplementedException(); } public AsyncReply Move(IResource resource, string newPath) { throw new NotImplementedException(); } }