/// <summary> /// Gets a <see cref="IEnumerable{CultureInfo}"/> containing <see cref="CultureInfo"/> instances from provided <code>requiredTranslations</code> /// which translations are not found in the provided <see cref="MarketDescriptionCacheItem"/> /// </summary> /// <param name="item">The <see cref="MarketDescriptionCacheItem"/> instance, or a null reference</param> /// <param name="requiredTranslations">The <see cref="IEnumerable{CultureInfo}"/> specifying the required languages</param> /// <returns>A <see cref="IEnumerable{CultureInfo}"/> containing missing translations or a null reference if none of the translations are missing</returns> private static IEnumerable <CultureInfo> GetMissingTranslations(MarketDescriptionCacheItem item, IEnumerable <CultureInfo> requiredTranslations) { Guard.Argument(requiredTranslations, nameof(requiredTranslations)).NotNull().NotEmpty(); if (item == null) { return(requiredTranslations); } var missingCultures = requiredTranslations.Where(c => !item.HasTranslationsFor(c)).ToList(); return(missingCultures.Any() ? missingCultures : null); }
/// <summary> /// Gets a <see cref="IEnumerable{CultureInfo}"/> containing <see cref="CultureInfo"/> instances from provided <code>requiredTranslations</code> /// which translations are not found in the provided <see cref="MarketDescriptionCacheItem"/> /// </summary> /// <param name="item">The <see cref="MarketDescriptionCacheItem"/> instance, or a null reference</param> /// <param name="requiredTranslations">The <see cref="IEnumerable{CultureInfo}"/> specifying the required languages</param> /// <returns>A <see cref="IEnumerable{CultureInfo}"/> containing missing translations or a null reference if none of the translations are missing</returns> private static IEnumerable <CultureInfo> GetMissingTranslations(MarketDescriptionCacheItem item, IEnumerable <CultureInfo> requiredTranslations) { Contract.Requires(requiredTranslations != null && requiredTranslations.Any()); if (item == null) { return(requiredTranslations); } var missingCultures = requiredTranslations.Where(c => !item.HasTranslationsFor(c)).ToList(); return(missingCultures.Any() ? missingCultures : null); }
/// <summary> /// Merges the provided descriptions with those found in cache /// </summary> /// <param name="culture">A <see cref="CultureInfo" /> specifying the language of the <code>descriptions</code></param> /// <param name="description">A <see cref="MarketDescriptionDTO" /> containing market description in specified language</param> private void Merge(CultureInfo culture, MarketDescriptionDTO description) { Contract.Requires(culture != null); Contract.Requires(description != null); if (_isDisposed) { return; } try { _semaphoreCacheMerge.Wait(); var cachedItem = _cache.GetCacheItem(GetCacheKey(description.Id, description.Variant)); if (cachedItem == null) { cachedItem = new CacheItem(GetCacheKey(description.Id, description.Variant), MarketDescriptionCacheItem.Build(description, _mappingValidatorFactory, culture, CacheName)); _cache.Add(cachedItem, _cacheItemPolicy); } else { ((MarketDescriptionCacheItem)cachedItem.Value).Merge(description, culture); } } catch (Exception e) { if (!(e is InvalidOperationException)) { throw; } ExecutionLog.Warn("Mapping validation for MarketDescriptionCacheItem failed.", e); } finally { if (!_isDisposed) { _semaphoreCacheMerge.Release(); } } }
/// <summary> /// Merges the provided descriptions with those found in cache /// </summary> /// <param name="culture">A <see cref="CultureInfo"/> specifying the language of the <code>descriptions</code></param> /// <param name="description">A <see cref="MarketDescriptionDTO"/> containing market description in specified language</param> private async Task MergeAsync(CultureInfo culture, MarketDescriptionDTO description) { Guard.Argument(culture, nameof(culture)).NotNull(); Guard.Argument(description, nameof(description)).NotNull(); if (_isDisposed) { return; } try { await _semaphoreCacheMerge.WaitAsync().ConfigureAwait(false); var cacheId = GetCacheKey(description.Id, description.Variant); var cachedItem = _cache.GetCacheItem(cacheId); if (cachedItem == null) { cachedItem = new CacheItem(cacheId, MarketDescriptionCacheItem.Build(description, _mappingValidatorFactory, culture, CacheName)); _cache.Add(cachedItem, new CacheItemPolicy { SlidingExpiration = OperationManager.VariantMarketDescriptionCacheTimeout }); } else { ((MarketDescriptionCacheItem)cachedItem.Value).Merge(description, culture); } } catch (Exception e) { if (!(e is InvalidOperationException)) { throw; } ExecutionLog.LogWarning(e, "Mapping validation for MarketDescriptionCacheItem failed."); } finally { _semaphoreCacheMerge.ReleaseSafe(); } }
/// <summary> /// Merges the provided descriptions with those found in cache /// </summary> /// <param name="culture">A <see cref="CultureInfo"/> specifying the language of the <code>descriptions</code></param> /// <param name="descriptions">A <see cref="IEnumerable{MarketDescriptionDTO}"/> containing market descriptions in specified language</param> private void Merge(CultureInfo culture, IEnumerable <MarketDescriptionDTO> descriptions) { Guard.Argument(culture, nameof(culture)).NotNull(); Guard.Argument(descriptions, nameof(descriptions)).NotNull().NotEmpty(); var descriptionList = descriptions as List <MarketDescriptionDTO> ?? descriptions.ToList(); try { _semaphoreCacheMerge.Wait(); foreach (var marketDescription in descriptionList) { try { var cachedItem = _cache.GetCacheItem(marketDescription.Id.ToString()); if (cachedItem == null) { cachedItem = new CacheItem(marketDescription.Id.ToString(), MarketDescriptionCacheItem.Build(marketDescription, _mappingValidatorFactory, culture, CacheName)); _cache.Add(cachedItem, _cacheItemPolicy); } else { ((MarketDescriptionCacheItem)cachedItem.Value).Merge(marketDescription, culture); } } catch (Exception e) { if (!(e is InvalidOperationException)) { throw; } ExecutionLog.LogWarning($"Mapping validation for MarketDescriptionCacheItem failed. Id={marketDescription.Id}", e); } } _fetchedLanguages.Add(culture); } finally { if (!_isDisposed) { _semaphoreCacheMerge.Release(); } } }
/// <summary> /// Merges the provided descriptions with those found in cache /// </summary> /// <param name="culture">A <see cref="CultureInfo" /> specifying the language of the <code>descriptions</code></param> /// <param name="descriptions"> /// A <see cref="IEnumerable{MarketDescriptionDTO}" /> containing market descriptions in /// specified language /// </param> private void Merge(CultureInfo culture, IEnumerable <MarketDescriptionDTO> descriptions) { Contract.Requires(culture != null); Contract.Requires(descriptions != null && descriptions.Any()); var descriptionList = descriptions as List <MarketDescriptionDTO> ?? descriptions.ToList(); try { _semaphoreCacheMerge.Wait(); foreach (var marketDescription in descriptionList) { try { var cachedItem = _cache.GetCacheItem(marketDescription.Id.ToString()); if (cachedItem == null) { cachedItem = new CacheItem(marketDescription.Id.ToString(), MarketDescriptionCacheItem.Build(marketDescription, _mappingValidatorFactory, culture, CacheName)); _cache.Add(cachedItem, _cacheItemPolicy); } else { ((MarketDescriptionCacheItem)cachedItem.Value).Merge(marketDescription, culture); } } catch (Exception e) { if (!(e is InvalidOperationException)) { throw; } ExecutionLog.Warn( $"Mapping validation for MarketDescriptionCacheItem failed. Id={marketDescription.Id}", e); } } _fetchedLanguages.Add(culture); } finally { if (!_isDisposed) { _semaphoreCacheMerge.Release(); } } //var c = _cache.Count(); //ExecutionLog.Debug($"InvariantMarketDescriptionCache count: {c}."); //foreach (var keyValue in _cache.Where(s=>s.Key != null)) //{ // var ci = (MarketDescriptionCacheItem)keyValue.Value; // if (ci.Mappings != null) // { // foreach (var mapping in ci.Mappings) // { // if (mapping.OutcomeMappings != null) // { // foreach (var outcomeMapping in mapping.OutcomeMappings) // { // if (outcomeMapping.ProducerOutcomeNames.Count != ci.FetchedLanguages.Count) // { // ExecutionLog.Error($"Market {ci.Id}: problem with outcome mapping {outcomeMapping.OutcomeId} and mapped marketId {outcomeMapping.MarketId}"); // } // } // } // } // } //} //var cacheItem = _cache.First(); }