using System; using System.Collections.Generic; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Structure; using Swarmops.Database; using Swarmops.Logic.Structure; namespace Swarmops.Logic.Cache { public class OrganizationCache { private static DateTime lastRefresh; private static object loadCacheLock = new object(); public static bool __loadCache = true; private static Dictionary> __organizationCache = null; private static readonly int cacheLifeSpanMinutes = 2; private static Dictionary> __cachedUptakeGeographies = null; static OrganizationCache() { lastRefresh = DateTime.MinValue; } public static bool needsReload { get { lock (loadCacheLock) { return (__loadCache || lastRefresh.AddMinutes (cacheLifeSpanMinutes) < DateTime.Now); } } private set { lock (loadCacheLock) { __loadCache = value; } } } private static Dictionary> GetHashedOrganizations() { if (needsReload) { lock (loadCacheLock) { if (needsReload) { __organizationCache = SwarmDb.GetDatabaseForReading().GetHashedOrganizations(); __cachedUptakeGeographies = null; lastRefresh = DateTime.Now; needsReload = false; } } } return __organizationCache; } public static Organization FromCache (int OrganizationId) { lock (loadCacheLock) { return Organization.FromBasic (GetOrganization (OrganizationId)); } } internal static int CreateOrganization (int ParentOrganizationId, string NameInternational, string Name, string NameShort, string Domain, string MailPrefix, int AnchorGeographyId, bool AcceptsMembers, bool AutoAssignNewMembers, int DefaultCountryId) { int Identity = SwarmDb.GetDatabaseForWriting().CreateOrganization (ParentOrganizationId, NameInternational, Name, NameShort, Domain, MailPrefix, AnchorGeographyId, AcceptsMembers, AutoAssignNewMembers, DefaultCountryId); needsReload = true; return Identity; } public static void UpdateOrganization (int ParentOrganizationId, string NameInternational, string Name, string NameShort, string Domain, string MailPrefix, int AnchorGeographyId, bool AcceptsMembers, bool AutoAssignNewMembers, int DefaultCountryId, int OrganizationId) { SwarmDb.GetDatabaseForWriting() .UpdateOrganization (ParentOrganizationId, NameInternational, Name, NameShort, Domain, MailPrefix, AnchorGeographyId, AcceptsMembers, AutoAssignNewMembers, DefaultCountryId, OrganizationId); needsReload = true; } internal static void Reload (int objectId) { __loadCache = true; // Let this be for the moment, new and old parents need to be loaded as well. Better right now to reload the whole cache. //lock (loadCacheLock) //{ // __organizationCache[objectId][0] = Organization.FromBasic(SwarmDb.GetDatabaseForReading().GetOrganization(objectId)); //} } public static BasicOrganization[] GetAll() { lock (loadCacheLock) { Dictionary> hashedOrganisations = GetHashedOrganizations(); List returnList = new List(); foreach (int id in hashedOrganisations.Keys) { returnList.Add (hashedOrganisations[id][0]); } return returnList.ToArray(); } } public static BasicOrganization GetOrganization (int OrganizationId) { lock (loadCacheLock) { Dictionary> hashedOrganisations = GetHashedOrganizations(); if (hashedOrganisations.ContainsKey (OrganizationId)) return hashedOrganisations[OrganizationId][0]; else { //Didn't find, strange, id's should exist, try reloading the cache, it could be newly added. needsReload = true; hashedOrganisations = GetHashedOrganizations(); if (hashedOrganisations.ContainsKey (OrganizationId)) return hashedOrganisations[OrganizationId][0]; else { throw new ArgumentException ("No such OrganizationId: " + OrganizationId.ToString()); } } } } public static BasicOrganization[] GetOrganizationTree (int startOrganizationId) { lock (loadCacheLock) { Dictionary> organizations = GetHashedOrganizations(); return GetOrganizationTree (organizations, startOrganizationId, 0); } } public static Dictionary GetOrganizationHashtable (int startOrganizationId) { BasicOrganization[] organizations = GetOrganizationTree (startOrganizationId); Dictionary result = new Dictionary(); foreach (BasicOrganization organization in organizations) { result[organization.OrganizationId] = organization; } return result; } private static BasicOrganization[] GetOrganizationTree (Dictionary> organizations, int startOrganizationId, int generation) { List result = new List(); //Prime the cache BasicOrganization start = GetOrganization (startOrganizationId); List thisList = organizations[startOrganizationId]; foreach (BasicOrganization organization in thisList) { if (organization.OrganizationId != startOrganizationId) { result.Add (organization); // new Organization(organization.OrganizationId, organization.ParentOrganizationId, organization.Name, generation + 1)); // Add recursively BasicOrganization[] children = GetOrganizationTree (organizations, organization.OrganizationId, generation + 1); if (children.Length > 0) { foreach (BasicOrganization child in children) { result.Add (child); } } } else if (generation == 0) { // The top parent is special and should be added; the others shouldn't result.Add (organization); // (new Organization(organization.OrganizationId, organization.ParentOrganizationId, organization.Name, generation)); } } return result.ToArray(); } public static BasicOrganization[] GetOrganizationChildren (int parentOrgId) { List result = new List(); lock (loadCacheLock) { BasicOrganization parent = GetOrganization (parentOrgId); //TODO: It is possible to miss a child here if that child was added after the last cache load. Dictionary> hashedOrganizations = GetHashedOrganizations(); foreach (BasicOrganization b in hashedOrganizations[parentOrgId]) { if (b.Identity != parentOrgId) result.Add (b); } return result.ToArray(); } } public static BasicOrganization[] GetOrganizationLine (int leafOrganizationId) { lock (loadCacheLock) { List result = new List(); BasicOrganization currentOrganization = GetOrganization (leafOrganizationId); // This iterates until the zero-parentid root Organization is found while (currentOrganization.OrganizationId > 0) { result.Add (currentOrganization); if (currentOrganization.ParentOrganizationId > 0) { currentOrganization = GetOrganization (currentOrganization.ParentOrganizationId); } else { currentOrganization = new BasicOrganization(); } } result.Reverse(); return result.ToArray(); } } [Obsolete ("Never use this function. Mark the org as unused, deleted. Records are needed for historic reasons.", true)] internal static void DeleteOrganization (int p) { SwarmDb.GetDatabaseForWriting().DeleteOrganization (p); needsReload = true; } /* * Caching of Uptake Geographies */ // this is nulled when a main cache load is done, so it don't need a timing parameter really // since that will be governed by the timeout of organisations. private static Dictionary> GetUptakes() { //to make sure we dont end up loading uptakes, loding organisations, then load uptakes again. lock (loadCacheLock) { GetHashedOrganizations(); if (__cachedUptakeGeographies == null) { __cachedUptakeGeographies = SwarmDb.GetDatabaseForReading().GetAllOrganizationUptakeGeographyIds(); } } return __cachedUptakeGeographies; } internal static int[] GetOrganizationsIdsInGeographies (BasicGeography[] nodes) { if (nodes == null || nodes.Length == 0) { return new int[0]; } List nodeIdList = new List(); foreach (BasicGeography node in nodes) { nodeIdList.Add (node.GeographyId); } return GetOrganizationIdsInGeographies (nodeIdList.ToArray()); } public static int[] GetOrganizationIdsInGeographies (int[] nodeIds) { if (nodeIds == null || nodeIds.Length == 0) { return new int[0]; } Dictionary resultKeys = new Dictionary(); Dictionary idsToFind = new Dictionary(); foreach (int i in nodeIds) idsToFind[i] = true; //fist add those that has the geo as anchor lock (loadCacheLock) { Dictionary> hashedOrgs = GetHashedOrganizations(); foreach (int id in hashedOrgs.Keys) { BasicOrganization bo = hashedOrgs[id][0]; if (idsToFind.ContainsKey (bo.AnchorGeographyId)) { resultKeys[bo.Identity] = true; } } //then add those that has the geo as uptake Dictionary> allUptakes = GetUptakes(); foreach (int orgId in allUptakes.Keys) { //connected to valid org? if (hashedOrgs.ContainsKey (orgId)) { foreach (int geoId in allUptakes[orgId]) { if (idsToFind.ContainsKey (geoId)) { resultKeys[orgId] = true; } } } } } return (new List (resultKeys.Keys)).ToArray(); ; } internal static void AddOrgUptakeGeography (int p, int geoId) { SwarmDb.GetDatabaseForWriting().AddOrgUptakeGeography (p, geoId); lock (loadCacheLock) { __cachedUptakeGeographies = null; } } internal static void DeleteOrgUptakeGeography (int p, int geoId) { SwarmDb.GetDatabaseForWriting().DeleteOrgUptakeGeography (p, geoId); lock (loadCacheLock) { __cachedUptakeGeographies = null; } } } }