Concurrency logic refactored in Registry; implemented Registry.Delete; IDataObject turned into a class; some access modifiers replaced with a stronger ones

main
Inga 🏳‍🌈 15 years ago
parent b401ed57f6
commit 1c35027e31
  1. 17
      Core/DataObject.cs
  2. 8
      Core/IDataObject.cs
  3. 54
      Core/Registry.cs

@ -12,7 +12,7 @@ namespace FLocal.Core {
private TKey? _id; private TKey? _id;
public TKey id { public override TKey id {
get { get {
if(this.isNewObject) { if(this.isNewObject) {
throw new ObjectDoesntHaveAnIdException(); throw new ObjectDoesntHaveAnIdException();
@ -21,7 +21,7 @@ namespace FLocal.Core {
} }
} }
public bool isNewObject { private bool isNewObject {
get { get {
return !this._id.HasValue; return !this._id.HasValue;
} }
@ -29,23 +29,26 @@ namespace FLocal.Core {
private bool isJustCreated; private bool isJustCreated;
public DataObject() { internal DataObject() {
Debug.Assert(this is T); Debug.Assert(this is T);
this.isJustCreated = true; this.isJustCreated = true;
this._id = null; this._id = null;
} }
public static T LoadById(TKey id) { public static T LoadById(TKey id) {
return registry.Get(id); return registry.Get(id, false);
} }
protected virtual void AfterCreate() { } protected virtual void AfterCreate(bool forLoadingFromHash) { }
public void CreateByIdFromRegistry(TKey id) { internal override void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash) {
if(!this.isJustCreated) throw new CriticalException("Object already has an id"); if(!this.isJustCreated) throw new CriticalException("Object already has an id");
this._id = id; this._id = id;
this.isJustCreated = false; this.isJustCreated = false;
this.AfterCreate(); this.AfterCreate(forLoadingFromHash);
}
internal override void markAsDeletedFromRegistry() {
} }
private static Registry<TKey, T> registry { private static Registry<TKey, T> registry {

@ -5,7 +5,7 @@ using System.Text;
namespace FLocal.Core { namespace FLocal.Core {
interface IDataObject<TKey, TData> /*: IDataObject<TKey>*/ public abstract class IDataObject<TKey, TData>
where TData : IDataObject<TKey, TData>, new() where TData : IDataObject<TKey, TData>, new()
where TKey : struct { where TKey : struct {
@ -13,14 +13,16 @@ namespace FLocal.Core {
//static TData CreateByIdFromRegistry(TKey id); //static TData CreateByIdFromRegistry(TKey id);
void CreateByIdFromRegistry(TKey id); internal abstract void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash);
//TKey GetId(); //TKey GetId();
TKey id { public abstract TKey id {
get; get;
} }
internal abstract void markAsDeletedFromRegistry();
} }
} }

@ -8,41 +8,65 @@ namespace FLocal.Core {
where TData : IDataObject<TKey, TData>, new() where TData : IDataObject<TKey, TData>, new()
where TKey : struct { where TKey : struct {
public static readonly Registry<TKey, TData> instance = new Registry<TKey,TData>(); internal static readonly Registry<TKey, TData> instance = new Registry<TKey,TData>();
private Dictionary<TKey, TData> storage; private Dictionary<TKey, TData> storage;
private HashSet<TKey> locks; private Dictionary<TKey, object> locks;
protected Registry() { private Registry() {
this.storage = new Dictionary<TKey,TData>(); this.storage = new Dictionary<TKey,TData>();
this.locks = new HashSet<TKey>(); this.locks = new Dictionary<TKey, object>();
} }
public TData Get(TKey id) { internal TData Get(TKey id, bool forLoadingFromHash) {
if(this.locks.Contains(id)) throw new CriticalException("Locked"); if(!this.locks.ContainsKey(id)) {
lock(this.locks) {
if(!this.locks.ContainsKey(id)) {
this.locks.Add(id, new object());
}
}
}
lock(this.locks[id]) {
if(!this.storage.ContainsKey(id)) { if(!this.storage.ContainsKey(id)) {
try {
this.locks.Add(id);
//this.storage[id] = IDataObject<TKey, TData>.CreateByIdFromRegistry(id); //this.storage[id] = IDataObject<TKey, TData>.CreateByIdFromRegistry(id);
//this.storage[id] = TData.CreateByIdFromRegistry(id); //this.storage[id] = TData.CreateByIdFromRegistry(id);
TData obj = new TData(); TData obj = new TData();
obj.CreateByIdFromRegistry(id); obj.CreateByIdFromRegistry(id, forLoadingFromHash);
this.storage[id] = obj; this.storage[id] = obj;
this.locks.Remove(id);
} catch(FLocalException e) {
this.locks.Remove(id);
throw e;
} }
} }
lock(this.locks) {
if(this.storage.ContainsKey(id)) {
return this.storage[id]; return this.storage[id];
} }
}
return this.Get(id, forLoadingFromHash);
}
public bool IsCached(TKey id) { /* public bool IsCached(TKey id) {
if(this.locks.Contains(id)) throw new CriticalException("locked"); if(this.locks.Contains(id)) throw new CriticalException("locked");
return this.storage.ContainsKey(id); return this.storage.ContainsKey(id);
}*/
internal void Delete(TKey[] idsToDelete) {
foreach(TKey id in idsToDelete) {
lock(this.locks[id]) {
IDataObject<TKey, TData> obj = null;
lock(this.locks) {
if(this.storage.ContainsKey(id)) {
obj = this.storage[id];
this.storage.Remove(id);
}
}
if(obj != null) {
obj.markAsDeletedFromRegistry();
}
}
}
} }
} }

Loading…
Cancel
Save