Реализовано универсальное хранилище

This commit is contained in:
2025-03-10 23:28:41 +03:00
parent 5e21d941df
commit d6423136d2
9 changed files with 132 additions and 22 deletions
@@ -69,7 +69,7 @@ namespace mROA.Implementation.Backend
IContextRepository repository;
var context = command.ObjectId.ContextId != -1
? contextRepository.GetObject<object>(command.ObjectId.ContextId)
? contextRepository.GetObject<object>(command.ObjectId)
: contextRepository.GetSingleObject(invoker.SuitableType);
if (context == null)
@@ -104,7 +104,7 @@ namespace mROA.Implementation.Backend
#if TRACE
Console.WriteLine("Disposing object");
#endif
contextRepository.ClearObject(command.ObjectId.ContextId);
contextRepository.ClearObject(command.ObjectId);
}
return result;
@@ -31,6 +31,8 @@ namespace mROA.Implementation.Backend
_storage = new object[StartupSize];
}
public int HostId { get; set; }
public int ResisterObject<T>(object o, IEndPointContext context)
{
if (!_lastIndexFinder.IsCompleted)
@@ -47,10 +49,10 @@ namespace mROA.Implementation.Backend
return last;
}
public void ClearObject(int id)
public void ClearObject(ComplexObjectIdentifier id)
{
_storage[id] = null;
_lastIndexFinder = Task.FromResult(id);
_storage[id.ContextId] = null;
_lastIndexFinder = Task.FromResult(id.ContextId);
}
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
@@ -58,11 +60,11 @@ namespace mROA.Implementation.Backend
return (T)GetObject(sharedObjectShellShell.Identifier.ContextId);
}
public T GetObject<T>(int id)
public T? GetObject<T>(ComplexObjectIdentifier id)
{
return id == -1 || _storage.Length <= id
return id.ContextId == -1 || _storage.Length <= id.ContextId
? throw new NullReferenceException("Cannot find that object. It is null")
: (T)_storage[id]!;
: (T)_storage[id.ContextId]!;
}
public object GetSingleObject(Type type)
@@ -18,13 +18,15 @@ namespace mROA.Implementation.Backend
{
}
public int HostId { get; set; }
public int ResisterObject<T>(object o, IEndPointContext context)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.ResisterObject<T>(o, context);
}
public void ClearObject(int id)
public void ClearObject(ComplexObjectIdentifier id)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
repository.ClearObject(id);
@@ -33,10 +35,10 @@ namespace mROA.Implementation.Backend
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId);
return repository.GetObject<T>(sharedObjectShellShell.Identifier.ContextId);
return repository.GetObject<T>(sharedObjectShellShell.Identifier)!;
}
public T? GetObject<T>(int id)
public T? GetObject<T>(ComplexObjectIdentifier id)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<T>(id);
@@ -69,11 +71,5 @@ namespace mROA.Implementation.Backend
_repositories.Add(clientId, created);
return created;
}
public object GetObject(int id)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<object>(id);
}
}
}
+44
View File
@@ -0,0 +1,44 @@
using System;
using mROA.Abstract;
namespace mROA.Implementation
{
public class ComplexRepository : IContextRepository
{
public void Inject<T>(T dependency)
{
throw new NotImplementedException();
}
public int HostId { get; set; }
public int ResisterObject<T>(object o, IEndPointContext context)
{
throw new NotImplementedException();
}
public void ClearObject(ComplexObjectIdentifier id)
{
throw new NotImplementedException();
}
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
throw new NotImplementedException();
}
public T? GetObject<T>(ComplexObjectIdentifier id)
{
throw new NotImplementedException();
}
public object GetSingleObject(Type type)
{
throw new NotImplementedException();
}
public int GetObjectIndex<T>(object o, IEndPointContext context)
{
throw new NotImplementedException();
}
}
}
+55
View File
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using mROA.Abstract;
namespace mROA.Implementation
{
public class ExtensibleStorage<T> : IStorage<T>
{
private const int StartupSize = 1024;
private const int GrowSize = 128;
private T?[] _array = new T?[StartupSize];
private readonly LinkedList<int> _freePlaces = new(Enumerable.Range(0, StartupSize));
public T GetValue(int index)
{
return _array[index]!;
}
public int GetIndex(T value)
{
return Array.IndexOf(_array, value);
}
public int Place(T value)
{
if (_freePlaces.Count == 0)
{
Grow();
}
var index = _freePlaces.First.Value;
_array[index] = value;
return index;
}
private void Grow()
{
foreach (var index in Enumerable.Range(_array.Length, GrowSize))
_freePlaces.AddLast(index);
T?[] nextStorage = new T[_array.Length + GrowSize];
Array.Copy(_array, nextStorage, _array.Length);
_array = nextStorage;
}
public void Free(int index)
{
_freePlaces.AddFirst(index);
_array[index] = default;
}
}
}
@@ -9,12 +9,14 @@ namespace mROA.Implementation
public static Dictionary<Type, Type> RemoteTypes = new();
private IRepresentationModuleProducer? _representationProducer;
public int HostId { get; set; }
public int ResisterObject<T>(object o, IEndPointContext context)
{
throw new NotSupportedException();
}
public void ClearObject(int id)
public void ClearObject(ComplexObjectIdentifier id)
{
throw new NotSupportedException();
}
@@ -31,7 +33,7 @@ namespace mROA.Implementation
return remote;
}
public T GetObject<T>(int id)
public T? GetObject<T>(ComplexObjectIdentifier id)
{
if (_representationProducer == null)
throw new NullReferenceException("representation producer is not initialized");