Reputation: 121
I am processing zip files using queue. So whenever a new zip file arrives to the specified path, file_info will be added to queue using
fileQueue.Enqueue(IFile);
Now I should add the File to queue only if it does not exist. I tried to implement IEqualityComparer<T>
interface and the
public bool Equals(T x, T y)
{
object xValue = _propertyInfo.GetValue(x, null);
object yValue = _propertyInfo.GetValue(y, null);
return xValue.Equals(yValue);
}
and
public int GetHashCode(T obj)
{
object propertyValue = _propertyInfo.GetValue(obj, null);
if (propertyValue == null)
return 0;
else
return propertyValue.GetHashCode();
}
I have an interface to get the fileInfo
public interface IFile
{
string FilePath { get; set; }
}
Another queue object
public Queue<IFile> fileQueue = new Queue<IFile>();
Can anyone please suggest how to check if the file already exists in the queue before adding it to the queue again. Thanks you very much in advance.
Upvotes: 2
Views: 2541
Reputation: 3555
If You want a fast performing solution (and want to avoid iterating over the whole queue each time You add smth) You'll need to implement Your own queue.
smth like this
public class MyQueue
{
private Queue<IFile> _queue;
private HashSet<int> hashes;
public void Add(IFile file)
{
var hash = GetHash(file);
if (hashes.Add(hash))
{
_queue.Enqueue(file);
}
}
}
Basically have a "table of contents" - that is implemented as a hashset (that is used to hold a list of unique values)
NOTE: once You start using queues, and messages - the whole idea behind such architecture - is that messages should be idempotent. Which means - it doesn't matter it You add the same message to the queue multiple times.
Upvotes: 3
Reputation: 46947
if(!fileQueue.Any(x => x.FilePath == file.FilePath))
fileQueue.Enqueue(file)
Upvotes: 1