<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Data;

namespace Sitecore.FakeDb.Serialization
{
    public static class SerializedIdToPathResolver
    {
        private static readonly Dictionary<string, SerializedIdToPathSet> _pathSets
            = new Dictionary<string, SerializedIdToPathSet>();

        public static string FindFilePath(this ID id, string serializationFolderName)
        {
            // find or create the set of paths for the serialization folder
            SerializedIdToPathSet pathSet;
            if (_pathSets.ContainsKey(serializationFolderName))
            {
                pathSet = _pathSets[serializationFolderName];
            }
            else
            {
                pathSet = new SerializedIdToPathSet();
                pathSet.FilePaths.Push(Deserializer.GetSerializationFolder(serializationFolderName).FullName);
                _pathSets.Add(serializationFolderName, pathSet);
            }

            // get the individual item if already found
            if (pathSet.Paths.ContainsKey(id))
            {
                return pathSet.Paths[id];
            }

            
            while (pathSet.FilePaths.Any())
            {
                string filePath = pathSet.FilePaths.Pop();
                foreach (string subdirectory in Directory.GetDirectories(filePath))
                {
                    pathSet.FilePaths.Push(subdirectory);
                }

                string foundFile = null;
                foreach (string file in Directory.GetFiles(filePath, "*.item"))
                {
                    using (StreamReader sr = new StreamReader(file))
                    {
                        sr.ReadLine();
                        sr.ReadLine();
                        string itemIdStr = sr.ReadLine().Substring(4);
                        if (ID.IsID(itemIdStr))
                        {
                            ID itemId = ID.Parse(itemIdStr);
                            pathSet.Paths.Add(itemId, file);
                            if (itemId == id)
                            {
                                foundFile = file;
                            }
                        }
                    }
                }

                if (foundFile != null)
                {
                    return foundFile;
                }
            }

            return null;
        }
    }
}<commit_msg>Added a lock for resolving path by id, to ensure correct handling if tests happen to run in parallel<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Data;

namespace Sitecore.FakeDb.Serialization
{
    public static class SerializedIdToPathResolver
    {
        private static readonly Dictionary<string, SerializedIdToPathSet> _pathSets
            = new Dictionary<string, SerializedIdToPathSet>();

        public static string FindFilePath(this ID id, string serializationFolderName)
        {
            lock (_pathSets)
            {
                // find or create the set of paths for the serialization folder
                SerializedIdToPathSet pathSet;
                if (_pathSets.ContainsKey(serializationFolderName))
                {
                    pathSet = _pathSets[serializationFolderName];
                }
                else
                {
                    pathSet = new SerializedIdToPathSet();
                    pathSet.FilePaths.Push(Deserializer.GetSerializationFolder(serializationFolderName).FullName);
                    _pathSets.Add(serializationFolderName, pathSet);
                }

                // get the individual item if already found
                if (pathSet.Paths.ContainsKey(id))
                {
                    return pathSet.Paths[id];
                }


                while (pathSet.FilePaths.Any())
                {
                    string filePath = pathSet.FilePaths.Pop();
                    foreach (string subdirectory in Directory.GetDirectories(filePath))
                    {
                        pathSet.FilePaths.Push(subdirectory);
                    }

                    string foundFile = null;
                    foreach (string file in Directory.GetFiles(filePath, "*.item"))
                    {
                        using (StreamReader sr = new StreamReader(file))
                        {
                            sr.ReadLine();
                            sr.ReadLine();
                            string itemIdStr = sr.ReadLine().Substring(4);
                            if (ID.IsID(itemIdStr))
                            {
                                ID itemId = ID.Parse(itemIdStr);
                                pathSet.Paths.Add(itemId, file);
                                if (itemId == id)
                                {
                                    foundFile = file;
                                }
                            }
                        }
                    }

                    if (foundFile != null)
                    {
                        return foundFile;
                    }
                }

                return null;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06967670011148272, 'fraction_numerical': 0.0003716090672612412, 'mean_word_length': 1.135263784212614, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3140801', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1167, 'n_words': 262}