public override void Dispose() { base.Dispose(); SurfaceElevationPatchRequest = null; TRexSpatialMemoryCacheContext = null; }
/// <summary> /// Attempts to read an element from a cache context given the spatial location of the element /// </summary> /// <param name="context">The request, filter and other data specific context for spatial data</param> /// <param name="originX">The origin (bottom left) cell of the spatial data sub grid</param> /// <param name="originY">The origin (bottom left) cell of the spatial data sub grid</param> public ITRexMemoryCacheItem Get(ITRexSpatialMemoryCacheContext context, int originX, int originY) { lock (_contexts) { return(context.Get(originX, originY)); } }
/// <summary> /// Adds an item into a context within the overall memory cache of these items. The context must be obtained from the /// memory cache instance prior to use. This operation is thread safe - all operations are concurrency locked within the /// confines of the context. /// </summary> public CacheContextAdditionResult Add(ITRexSpatialMemoryCacheContext context, ITRexMemoryCacheItem element, long invalidationVersion) { lock (_contexts) { // If the invalidation version of a sub grid derivative being added to the cache does not match the current invalidation version // then prevent it being added to the cache, and return a negative result to indicate the refusal if (invalidationVersion != context.InvalidationVersion) { _log.LogInformation($"Sub grid derivative at {element.CacheOriginX}:{element.CacheOriginY} not added to cache context due to invalidation version mismatch ({invalidationVersion} vs {context.InvalidationVersion})"); return(CacheContextAdditionResult.RejectedDueToInvlidationVersionMismatch); } if (context.MarkedForRemoval) { context.Reanimate(); } var result = context.Add(element); if (result == CacheContextAdditionResult.Added) { // Perform some house keeping to keep the cache size in bounds ItemAddedToContext(element.IndicativeSizeInBytes()); while (_currentSizeInBytes > MaxSizeInBytes && !MRUList.IsEmpty()) { MRUList.EvictOneLRUItem(); } } return(result); } }
public SurfaceElevationPatchRequest(ITRexSpatialMemoryCache cache, ITRexSpatialMemoryCacheContext context) : this() { if (cache != null && context != null) { _cache = new SurfaceElevationPatchRequestCache(cache, context, _clientLeafSubGridFactory); } }
public void Set(T item, ITRexSpatialMemoryCacheContext context, long mruEpochToken, int prev, int next) { Item = item; Context = context; MRUEpochToken = mruEpochToken; ExpiryTime = context == null ? Consts.MIN_DATETIME_AS_UTC : DateTime.UtcNow + context.CacheDurationTime; Prev = prev; Next = next; }
public int Next; // No get/set semantics on purpose as this is a struct public TRexCacheItem(T item, ITRexSpatialMemoryCacheContext context, long mruEpochToken, int prev, int next) { Item = item; Context = context; MRUEpochToken = mruEpochToken; ExpiryTime = DateTime.UtcNow + context.CacheDurationTime; Prev = prev; Next = next; }
/// <summary> /// Removes an item from a context within the overall memory cache of these items. The context must be obtained from the /// memory cache instance prior to use. This operation is thread safe - all operations are concurrency locked within the /// confines of the context. /// </summary> public void Remove(ITRexSpatialMemoryCacheContext context, ITRexMemoryCacheItem element) { lock (_contexts) { context.Remove(element); // Perform some house keeping to keep the cache size in bounds ItemRemovedFromContext(element.IndicativeSizeInBytes()); } }
/// <summary> /// Adds an item into the cache storage. /// </summary> /// <returns>The index of the newly added item</returns> public int Add(T element, ITRexSpatialMemoryCacheContext context) { var token = NextToken(); // Obtain item from free list if (_freeListHead == NULL_INDEX) { // There are no free entries, victimize one to store it EvictOneLRUItem(); } var index = _freeListHead; _freeListHead = _items[index].Next; // Set the parameters for the new item, setting it's prev pointer to point to the oldest member of the MRUList if (MRUHead == NULL_INDEX) { _items[index].Set(element, context, token, index, MRUHead); } else { _items[index].Set(element, context, token, _items[MRUHead].Prev, MRUHead); _items[MRUHead].Prev = index; } if (LRUHead == NULL_INDEX) { LRUHead = index; } MRUHead = index; _tokenCount++; // Return the token to the caller return(index); }
public RequestorUtilitiesTestsLoggingFixture() { // Provide the surveyed surface request mock var surfaceElevationPatchRequest = new Mock <ISurfaceElevationPatchRequest>(); surfaceElevationPatchRequest.Setup(x => x.ExecuteAsync(It.IsAny <ISurfaceElevationPatchArgument>())).Returns(Task.FromResult(new ClientHeightAndTimeLeafSubGrid() as IClientLeafSubGrid)); SurfaceElevationPatchRequest = surfaceElevationPatchRequest.Object; // Provide the mocks for spatial caching var tRexSpatialMemoryCacheContext = new Mock <ITRexSpatialMemoryCacheContext>(); TRexSpatialMemoryCacheContext = tRexSpatialMemoryCacheContext.Object; var tRexSpatialMemoryCache = new Mock <ITRexSpatialMemoryCache>(); tRexSpatialMemoryCache.Setup(x => x.LocateOrCreateContext(It.IsAny <Guid>(), It.IsAny <GridDataType>(), It.IsAny <string>())).Returns(TRexSpatialMemoryCacheContext); tRexSpatialMemoryCache.Setup(x => x.LocateOrCreateContext(It.IsAny <Guid>(), It.IsAny <GridDataType>(), It.IsAny <string>(), It.IsAny <TimeSpan>())).Returns(TRexSpatialMemoryCacheContext); DIBuilder .Continue() .Add(x => x.AddSingleton(ClientLeafSubGridFactoryFactory.CreateClientSubGridFactory())) .Add(x => x.AddSingleton <ISubGridSpatialAffinityKeyFactory>(new SubGridSpatialAffinityKeyFactory())) // Register the mock factory for surface elevation requests .Add(x => x.AddSingleton <Func <ITRexSpatialMemoryCache, ITRexSpatialMemoryCacheContext, ISurfaceElevationPatchRequest> >((cache, context) => SurfaceElevationPatchRequest)) .Add(x => x.AddSingleton <ITRexSpatialMemoryCache>(tRexSpatialMemoryCache.Object)) .Add(x => x.AddTransient <ISurveyedSurfaces>(factory => new TRex.SurveyedSurfaces.SurveyedSurfaces())) .Add(x => x.AddSingleton <ISiteModels>(new TRex.SiteModels.SiteModels(TRex.Storage.Models.StorageMutability.Immutable))) .Add(x => x.AddSingleton <Func <ISubGridRequestor> >(factory => () => new SubGridRequestor())) .Add(x => x.AddSingleton <ISubGridRetrieverFactory>(new SubGridRetrieverFactory())) .Complete(); }
public SurfaceElevationPatchRequestCache(ITRexSpatialMemoryCache cache, ITRexSpatialMemoryCacheContext context, IClientLeafSubGridFactory clientLeafSubGridFactory) { _cache = cache; _context = context; _clientLeafSubGridFactory = clientLeafSubGridFactory; }
/// <summary> /// Removes this item from the context it is associated with by setting the index reference in the MRU list /// held in the sub grid tree to 0 /// Note: This call operates within a write lock obtained from the sub grid tree in an ancestor calling context /// </summary> public void RemoveFromContext() { Context?.RemoveFromContextTokensOnly(Item); Context = null; }