using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FLocal.Core { class Registry where TData : IDataObject, new() where TKey : struct { internal static readonly Registry instance = new Registry(); private Dictionary storage; private Dictionary locks; private Registry() { this.storage = new Dictionary(); this.locks = new Dictionary(); } 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.CreateByIdFromRegistry(id); //this.storage[id] = TData.CreateByIdFromRegistry(id); TData obj = new TData(); obj.CreateByIdFromRegistry(id, forLoadingFromHash); this.storage[id] = obj; } } lock(this.locks) { if(this.storage.ContainsKey(id)) { return this.storage[id]; } } return this.Get(id, forLoadingFromHash); } /* 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 obj = null; lock(this.locks) { if(this.storage.ContainsKey(id)) { obj = this.storage[id]; this.storage.Remove(id); } } if(obj != null) { obj.markAsDeletedFromRegistry(); } } } } } }