<commit_before>using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Application.Threading;
using JetBrains.Collections;
using JetBrains.Diagnostics;
using JetBrains.DocumentManagers;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Plugins.Unity.Core.Psi.Modules;
using JetBrains.ReSharper.Plugins.Unity.Feature.Caches;
using JetBrains.ReSharper.Plugins.Unity.Utils;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.AssetHierarchy;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.AssetHierarchy.Elements;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.Utils;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.Parsing;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.Text;
using JetBrains.Util;
using JetBrains.Util.Caches;
using JetBrains.Util.PersistentMap;

namespace JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches
{
    [SolutionComponent]
    public class UnityAssetsCache : DeferredCacheBase<UnityAssetData> , IUnityAssetDataElementPointer
    {
        private readonly AssetDocumentHierarchyElementContainer myHierarchyElementContainer;
        private readonly DocumentToProjectFileMappingStorage myDocumentToProjectFileMappingStorage;
        private readonly AssetIndexingSupport myAssetIndexingSupport;
        private readonly PrefabImportCache myPrefabImportCache;
        private readonly IShellLocks myShellLocks;
        private readonly ILogger myLogger;
        private readonly List<IUnityAssetDataElementContainer> myOrderedContainers;
        private readonly List<IUnityAssetDataElementContainer> myOrderedIncreasingContainers;
        private readonly ConcurrentDictionary<IPsiSourceFile, (UnityAssetData, int)> myDocumentNumber = new ConcurrentDictionary<IPsiSourceFile, (UnityAssetData, int)>();
        private readonly ConcurrentDictionary<IPsiSourceFile, long> myCurrentTimeStamp = new ConcurrentDictionary<IPsiSourceFile, long>();
        public UnityAssetsCache(Lifetime lifetime, DocumentToProjectFileMappingStorage documentToProjectFileMappingStorage, AssetIndexingSupport assetIndexingSupport,
            PrefabImportCache prefabImportCache, IPersistentIndexManager persistentIndexManager, IEnumerable<IUnityAssetDataElementContainer> unityAssetDataElementContainers,
            IShellLocks shellLocks, ILogger logger)
            : base(lifetime, persistentIndexManager, new UniversalMarshaller<UnityAssetData>(UnityAssetData.ReadDelegate, UnityAssetData.WriteDelegate))
        {
            myDocumentToProjectFileMappingStorage = documentToProjectFileMappingStorage;
            myAssetIndexingSupport = assetIndexingSupport;
            myPrefabImportCache = prefabImportCache;
            myShellLocks = shellLocks;
            myLogger = logger;
            myOrderedContainers = unityAssetDataElementContainers.OrderByDescending(t => t.Order).ToList();
            myOrderedIncreasingContainers = myOrderedContainers.OrderBy(t => t.Order).ToList();

            myHierarchyElementContainer = myOrderedContainers.First(t => t is AssetDocumentHierarchyElementContainer) as AssetDocumentHierarchyElementContainer;
            
            Map.Cache = new DirectMappedCache<IPsiSourceFile, UnityAssetData>(10);
        }

        public override bool IsApplicable(IPsiSourceFile sourceFile)
        {
            if (!myAssetIndexingSupport.IsEnabled.Value)
                return false;
            
            return sourceFile.PsiModule is UnityExternalFilesPsiModule;
        }

        protected override void MergeData(IPsiSourceFile sourceFile, UnityAssetData data)
        {
            myDocumentNumber.TryRemove(sourceFile, out _);
            myCurrentTimeStamp.TryRemove(sourceFile, out _);

            if (!data.UnityAssetDataElements.TryGetValue(myHierarchyElementContainer.Id, out var hierarchyElement))
                return;
            
            foreach (var container in myOrderedContainers)
            {
                if (data.UnityAssetDataElements.TryGetValue(container.Id, out var element))
                {
                    Assertion.Assert(container != null, "container != null");
                    try
                    {
                        container.Merge(sourceFile, hierarchyElement as AssetDocumentHierarchyElement,this, element);<commit_msg>add temporary fix for RIDER-71633<commit_after>using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Application.Threading;
using JetBrains.Collections;
using JetBrains.Diagnostics;
using JetBrains.DocumentManagers;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Plugins.Unity.Core.Psi.Modules;
using JetBrains.ReSharper.Plugins.Unity.Feature.Caches;
using JetBrains.ReSharper.Plugins.Unity.Utils;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.AssetHierarchy;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.AssetHierarchy.Elements;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.Utils;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.Parsing;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.Text;
using JetBrains.Util;
using JetBrains.Util.Caches;
using JetBrains.Util.PersistentMap;

namespace JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches
{
    [SolutionComponent]
    public class UnityAssetsCache : DeferredCacheBase<UnityAssetData> , IUnityAssetDataElementPointer
    {
        private readonly AssetDocumentHierarchyElementContainer myHierarchyElementContainer;
        private readonly DocumentToProjectFileMappingStorage myDocumentToProjectFileMappingStorage;
        private readonly AssetIndexingSupport myAssetIndexingSupport;
        private readonly PrefabImportCache myPrefabImportCache;
        private readonly IShellLocks myShellLocks;
        private readonly ILogger myLogger;
        private readonly List<IUnityAssetDataElementContainer> myOrderedContainers;
        private readonly List<IUnityAssetDataElementContainer> myOrderedIncreasingContainers;
        private readonly ConcurrentDictionary<IPsiSourceFile, (UnityAssetData, int)> myDocumentNumber = new ConcurrentDictionary<IPsiSourceFile, (UnityAssetData, int)>();
        private readonly ConcurrentDictionary<IPsiSourceFile, long> myCurrentTimeStamp = new ConcurrentDictionary<IPsiSourceFile, long>();
        public UnityAssetsCache(Lifetime lifetime, DocumentToProjectFileMappingStorage documentToProjectFileMappingStorage, AssetIndexingSupport assetIndexingSupport,
            PrefabImportCache prefabImportCache, IPersistentIndexManager persistentIndexManager, IEnumerable<IUnityAssetDataElementContainer> unityAssetDataElementContainers,
            IShellLocks shellLocks, ILogger logger)
            : base(lifetime, persistentIndexManager, new UniversalMarshaller<UnityAssetData>(UnityAssetData.ReadDelegate, UnityAssetData.WriteDelegate))
        {
            myDocumentToProjectFileMappingStorage = documentToProjectFileMappingStorage;
            myAssetIndexingSupport = assetIndexingSupport;
            myPrefabImportCache = prefabImportCache;
            myShellLocks = shellLocks;
            myLogger = logger;
            myOrderedContainers = unityAssetDataElementContainers.OrderByDescending(t => t.Order).ToList();
            myOrderedIncreasingContainers = myOrderedContainers.OrderBy(t => t.Order).ToList();

            myHierarchyElementContainer = myOrderedContainers.First(t => t is AssetDocumentHierarchyElementContainer) as AssetDocumentHierarchyElementContainer;
            
            Map.Cache = new DirectMappedCache<IPsiSourceFile, UnityAssetData>(10);
        }

        public override bool IsApplicable(IPsiSourceFile sourceFile)
        {
            if (!myAssetIndexingSupport.IsEnabled.Value)
                return false;

            if (sourceFile.PsiModule is UnityExternalFilesPsiModule && sourceFile.IsYamlDataFile())
                return ValidateSize(sourceFile);
            
            return false;
        }

        private const int AnimMaxSize = 50 * 1024 * 1024; //50mb
        private bool ValidateSize(IPsiSourceFile sourceFile)
        {
            var location = sourceFile.GetLocation();
            if (!location.IsAnim() && !location.IsController())
                return true;
            
            return myLogger.CatchSilent(() =>
            {
                return location.GetFileLength() <= AnimMaxSize;
            });
        }

        protected override void MergeData(IPsiSourceFile sourceFile, UnityAssetData data)
        {
            myDocumentNumber.TryRemove(sourceFile, out _);
            myCurrentTimeStamp.TryRemove(sourceFile, out _);

            if (!data.UnityAssetDataElements.TryGetValue(myHierarchyElementContainer.Id, out var hierarchyElement))
                return;
            
            foreach (var container in myOrderedContainers)
            {
                if (data.UnityAssetDataElements.TryGetValue(container.Id, out var element))
                {
                    Assertion.Assert(container != null, "container != null");
                    try
                    {
                        container.Merge(sourceFile, hierarchyElement as AssetDocumentHierarchyElement,this, element);
{'fraction_non_alphanumeric': 0.06621052631578947, 'fraction_numerical': 0.0022105263157894735, 'mean_word_length': 3.6505139500734214, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630473', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2455, 'n_words': 447}