/// <summary> /// Invoked for deleting mapping in cache becase it no longer exists in store. /// </summary> /// <param name="mapping">Storage representation of mapping.</param> public virtual void DeleteMapping(IStoreMapping mapping) { using (ReadLockScope rls = _cacheRoot.GetReadLockScope(false)) { CacheShardMap csm = _cacheRoot.LookupById(mapping.ShardMapId); if (csm != null) { using (WriteLockScope wlscsm = csm.GetWriteLockScope()) { csm.Mapper.Remove(mapping); // Update perf counters for remove mapping operation and mappings count. csm.IncrementPerformanceCounter(PerformanceCounterName.MappingsRemovePerSec); csm.SetPerformanceCounter(PerformanceCounterName.MappingsCount, csm.Mapper.GetMappingsCount()); Tracer.TraceVerbose( TraceSourceConstants.ComponentNames.ShardMapManager, "OnDeleteMapping", "Cache delete mapping complete. Mapping Id: {0}", mapping.Id); } } } }
/// <summary> /// Removes shard map from cache given the name. /// </summary> /// <param name="ssm">Storage representation of shard map.</param> internal void Remove(IStoreShardMap ssm) { if (_shardMapsByName.ContainsKey(ssm.Name)) { CacheShardMap csm = _shardMapsByName[ssm.Name]; _shardMapsByName.Remove(ssm.Name); // Dispose off the cached map if (csm != null) { csm.Dispose(); } } if (_shardMapsById.ContainsKey(ssm.Id)) { CacheShardMap csm = _shardMapsById[ssm.Id]; _shardMapsById.Remove(ssm.Id); // Dispose off the cached map if (csm != null) { csm.Dispose(); } } }
/// <summary> /// Invoked for updating specified performance counter for a cached shard map object. /// </summary> /// <param name="shardMap">Storage representation of a shard map.</param> /// <param name="name">Performance counter to increment.</param> public void IncrementPerformanceCounter(IStoreShardMap shardMap, PerformanceCounterName name) { using (ReadLockScope rls = _cacheRoot.GetReadLockScope(false)) { CacheShardMap csm = _cacheRoot.LookupById(shardMap.Id); if (csm != null) { using (ReadLockScope rlsShardMap = csm.GetReadLockScope(false)) { csm.IncrementPerformanceCounter(name); } } } }
/// <summary> /// Looks up a given key in given shard map. /// </summary> /// <param name="shardMap">Storage representation of shard map.</param> /// <param name="key">Key value.</param> /// <returns>Mapping corresponding to <paramref name="key"/> or null.</returns> public virtual ICacheStoreMapping LookupMappingByKey(IStoreShardMap shardMap, ShardKey key) { ICacheStoreMapping sm = null; using (ReadLockScope rls = _cacheRoot.GetReadLockScope(false)) { CacheShardMap csm = _cacheRoot.LookupById(shardMap.Id); if (csm != null) { using (ReadLockScope rlsShardMap = csm.GetReadLockScope(false)) { IStoreMapping smDummy; sm = csm.Mapper.LookupByKey(key, out smDummy); } } } return(sm); }
/// <summary> /// Invoked for deleting mapping in cache becase it no longer exists in store. /// </summary> /// <param name="mapping">Storage representation of mapping.</param> public virtual void DeleteMapping(IStoreMapping mapping) { using (ReadLockScope rls = _cacheRoot.GetReadLockScope(false)) { CacheShardMap csm = _cacheRoot.LookupById(mapping.ShardMapId); if (csm != null) { using (WriteLockScope wlscsm = csm.GetWriteLockScope()) { csm.Mapper.Remove(mapping); Tracer.TraceVerbose( TraceSourceConstants.ComponentNames.ShardMapManager, "OnDeleteMapping", "Cache delete mapping complete. Mapping Id: {0}", mapping.Id); } } } }
internal CacheShardMap AddOrUpdate(IStoreShardMap ssm) { CacheShardMap csm = new CacheShardMap(ssm); CacheShardMap csmOldByName = null; CacheShardMap csmOldById = null; if (_shardMapsByName.TryGetValue(ssm.Name, out csmOldByName)) { _shardMapsByName.Remove(ssm.Name); } if (_shardMapsById.TryGetValue(ssm.Id, out csmOldById)) { _shardMapsById.Remove(ssm.Id); } // Both should be found or none should be found. Debug.Assert((csmOldByName == null && csmOldById == null) || (csmOldByName != null && csmOldById != null)); // Both should point to same cached copy. Debug.Assert(Object.ReferenceEquals(csmOldByName, csmOldById)); if (csmOldByName != null) { csm.TransferStateFrom(csmOldByName); // Dispose off the old cached shard map csmOldByName.Dispose(); } _shardMapsByName.Add(ssm.Name, csm); _shardMapsById.Add(ssm.Id, csm); return csm; }
/// <summary> /// Looks up a given key in given shard map. /// </summary> /// <param name="shardMap">Storage representation of shard map.</param> /// <param name="key">Key value.</param> /// <returns>Mapping corresponding to <paramref name="key"/> or null.</returns> public virtual ICacheStoreMapping LookupMappingByKey(IStoreShardMap shardMap, ShardKey key) { ICacheStoreMapping sm = null; using (ReadLockScope rls = _cacheRoot.GetReadLockScope(false)) { CacheShardMap csm = _cacheRoot.LookupById(shardMap.Id); if (csm != null) { using (ReadLockScope rlsShardMap = csm.GetReadLockScope(false)) { IStoreMapping smDummy; sm = csm.Mapper.LookupByKey(key, out smDummy); // perf counter can not be updated in csm.Mapper.LookupByKey() as this function is also called from csm.Mapper.AddOrUpdate() // so updating perf counter value here instead. csm.IncrementPerformanceCounter(sm == null ? PerformanceCounterName.MappingsLookupFailedPerSec : PerformanceCounterName.MappingsLookupSucceededPerSec); } } } return(sm); }
internal CacheShardMap AddOrUpdate(IStoreShardMap ssm) { CacheShardMap csm = new CacheShardMap(ssm); CacheShardMap csmOldByName = null; CacheShardMap csmOldById = null; if (_shardMapsByName.TryGetValue(ssm.Name, out csmOldByName)) { _shardMapsByName.Remove(ssm.Name); } if (_shardMapsById.TryGetValue(ssm.Id, out csmOldById)) { _shardMapsById.Remove(ssm.Id); } // Both should be found or none should be found. Debug.Assert((csmOldByName == null && csmOldById == null) || (csmOldByName != null && csmOldById != null)); // Both should point to same cached copy. Debug.Assert(Object.ReferenceEquals(csmOldByName, csmOldById)); if (csmOldByName != null) { csm.TransferStateFrom(csmOldByName); // Dispose off the old cached shard map csmOldByName.Dispose(); } _shardMapsByName.Add(ssm.Name, csm); _shardMapsById.Add(ssm.Id, csm); return(csm); }