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

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

@ -12,7 +12,7 @@ namespace FLocal.Core {
private TKey? _id;
public TKey id {
public override TKey id {
get {
if(this.isNewObject) {
throw new ObjectDoesntHaveAnIdException();
@ -21,7 +21,7 @@ namespace FLocal.Core {
}
}
public bool isNewObject {
private bool isNewObject {
get {
return !this._id.HasValue;
}
@ -29,25 +29,28 @@ namespace FLocal.Core {
private bool isJustCreated;
public DataObject() {
internal DataObject() {
Debug.Assert(this is T);
this.isJustCreated = true;
this._id = null;
}
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");
this._id = id;
this.isJustCreated = false;
this.AfterCreate();
this.AfterCreate(forLoadingFromHash);
}
internal override void markAsDeletedFromRegistry() {
}
private static Registry<TKey, T> registry {
get {
return Registry<TKey, T>.instance;

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

@ -8,42 +8,66 @@ namespace FLocal.Core {
where TData : IDataObject<TKey, TData>, new()
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 HashSet<TKey> locks;
private Dictionary<TKey, object> locks;
protected Registry() {
private Registry() {
this.storage = new Dictionary<TKey,TData>();
this.locks = new HashSet<TKey>();
this.locks = new Dictionary<TKey, object>();
}
public TData Get(TKey id) {
if(this.locks.Contains(id)) throw new CriticalException("Locked");
if(!this.storage.ContainsKey(id)) {
try {
this.locks.Add(id);
//this.storage[id] = IDataObject<TKey, TData>.CreateByIdFromRegistry(id);
//this.storage[id] = TData.CreateByIdFromRegistry(id);
TData obj = new TData();
obj.CreateByIdFromRegistry(id);
this.storage[id] = obj;
this.locks.Remove(id);
} catch(FLocalException e) {
this.locks.Remove(id);
throw e;
}
internal TData Get(TKey id, bool forLoadingFromHash) {
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)) {
//this.storage[id] = IDataObject<TKey, TData>.CreateByIdFromRegistry(id);
//this.storage[id] = TData.CreateByIdFromRegistry(id);
TData obj = new TData();
obj.CreateByIdFromRegistry(id, forLoadingFromHash);
this.storage[id] = obj;
}
}
return this.storage[id];
lock(this.locks) {
if(this.storage.ContainsKey(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");
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