diff --git a/src/Microsoft.ML.Core/Data/Repository.cs b/src/Microsoft.ML.Core/Data/Repository.cs index 40d7b7cc47..71b6c94d77 100644 --- a/src/Microsoft.ML.Core/Data/Repository.cs +++ b/src/Microsoft.ML.Core/Data/Repository.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -93,7 +94,7 @@ public void Dispose() // more than once. // REVIEW: Should we garbage collect to some degree? Currently we don't delete any // of these temp files until the repository is disposed. - protected readonly Dictionary PathMap; + protected readonly ConcurrentDictionary PathMap; /// /// Exception context. @@ -107,7 +108,7 @@ internal Repository(bool needDir, IExceptionContext ectx) Contracts.AssertValueOrNull(ectx); _ectx = ectx; - PathMap = new Dictionary(); + PathMap = new ConcurrentDictionary(); _open = new List(); if (needDir) DirTemp = GetShortTempDir(); @@ -256,7 +257,7 @@ protected void GetPath(out string pathEnt, out string pathTemp, string dir, stri string root = Path.GetFullPath(DirTemp ?? @"x:\dummy"); string entityPath = Path.Combine(root, dir ?? "", name); entityPath = Path.GetFullPath(entityPath); - string tempPath = Path.Combine(root, PathMap.Count.ToString()); + string tempPath = Path.Combine(root, Path.GetRandomFileName()); tempPath = Path.GetFullPath(tempPath); string parent = Path.GetDirectoryName(entityPath); @@ -333,10 +334,8 @@ public Entry CreateEntry(string dir, string name) string pathEnt; string pathTemp; GetPath(out pathEnt, out pathTemp, dir, name, true); - if (PathMap.ContainsKey(pathEnt)) + if (!PathMap.TryAdd(pathEnt, pathTemp)) throw ExceptionContext.ExceptParam(nameof(name), "Duplicate entry: '{0}'", pathEnt); - else - PathMap.Add(pathEnt, pathTemp); Stream stream; if (pathTemp != null) @@ -525,7 +524,9 @@ public Entry OpenEntryOrNull(string dir, string name) // Extract to a temporary file. Directory.CreateDirectory(Path.GetDirectoryName(pathTemp)); entry.ExtractToFile(pathTemp); - PathMap.Add(pathLower, pathTemp); + if (!PathMap.TryAdd(pathLower, pathTemp)) + throw ExceptionContext.ExceptParam(nameof(name), "Duplicate entry: '{0}'", pathLower); + stream = new FileStream(pathTemp, FileMode.Open, FileAccess.Read); } else