mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
Fixing potential race condition in ListRepository. Now internally implemented as a concurrent dictionary.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
using NLog.Filters;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Infrastructure.Storage
|
||||
{
|
||||
@@ -16,18 +19,19 @@ namespace Wox.Infrastructure.Storage
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class ListRepository<T> : IRepository<T>, IEnumerable<T>
|
||||
{
|
||||
protected IList<T> _items = new List<T>();
|
||||
protected IStorage<IList<T>> _storage;
|
||||
public IList<T> Items { get { return _items.Values.ToList(); } }
|
||||
|
||||
public ListRepository(IStorage<IList<T>> storage)
|
||||
private ConcurrentDictionary<int, T> _items = new ConcurrentDictionary<int, T>();
|
||||
|
||||
public ListRepository()
|
||||
{
|
||||
_storage = storage ?? throw new ArgumentNullException("storage", "StorageRepository requires an initialized storage interface");
|
||||
|
||||
}
|
||||
|
||||
public void Set(IList<T> items)
|
||||
{
|
||||
//enforce that internal representation
|
||||
_items = items.ToList<T>();
|
||||
_items = new ConcurrentDictionary<int, T>(items.ToDictionary( i => i.GetHashCode()));
|
||||
}
|
||||
|
||||
public bool Any()
|
||||
@@ -37,27 +41,35 @@ namespace Wox.Infrastructure.Storage
|
||||
|
||||
public void Add(T insertedItem)
|
||||
{
|
||||
_items.Add(insertedItem);
|
||||
if (!_items.TryAdd(insertedItem.GetHashCode(), insertedItem))
|
||||
{
|
||||
Log.Error($"|ListRepository.Add| Item Already Exists <{insertedItem}>");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Remove(T removedItem)
|
||||
{
|
||||
_items.Remove(removedItem);
|
||||
|
||||
if (!_items.TryRemove(removedItem.GetHashCode(), out _))
|
||||
{
|
||||
Log.Error($"|ListRepository.Remove| Item Not Found <{removedItem}>");
|
||||
}
|
||||
}
|
||||
|
||||
public ParallelQuery<T> AsParallel()
|
||||
{
|
||||
return _items.AsParallel();
|
||||
return _items.Values.AsParallel();
|
||||
}
|
||||
|
||||
public bool Contains(T item)
|
||||
{
|
||||
return _items.Contains(item);
|
||||
return _items.ContainsKey(item.GetHashCode());
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return _items.GetEnumerator();
|
||||
return _items.Values.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
|
||||
Reference in New Issue
Block a user