JobTransferPathRepository
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace Relativity.Transfer
{
internal class JobTransferPathRepository
{
private readonly ConcurrentDictionary<long, JobTransferPath> jobPaths = new ConcurrentDictionary<long, JobTransferPath>();
private readonly ConcurrentDictionary<long, long> alternateKeyMap = new ConcurrentDictionary<long, long>();
private readonly ITransferPathKeyGenerator primaryKeyGenerator;
private readonly ITransferPathKeyGenerator alternateKeyGenerator;
private long count;
public long Count => Interlocked.Read(ref count);
public JobTransferPathRepository(ITransferPathKeyGenerator primaryKeyGenerator)
: this(primaryKeyGenerator, null)
{
}
public JobTransferPathRepository(ITransferPathKeyGenerator primaryKeyGenerator, ITransferPathKeyGenerator alternateKeyGenerator)
{
if (primaryKeyGenerator == null)
throw new ArgumentNullException("primaryKeyGenerator");
this.primaryKeyGenerator = primaryKeyGenerator;
this.alternateKeyGenerator = alternateKeyGenerator;
}
public void Clear()
{
jobPaths.Clear();
alternateKeyMap.Clear();
Interlocked.Exchange(ref count, 0);
}
public void Delete(JobTransferPath path)
{
if (path == null)
throw new ArgumentNullException("path");
Delete(path.Path);
}
public void Delete(TransferPath path)
{
if (path == (TransferPath)null)
throw new ArgumentNullException("path");
long value = primaryKeyGenerator.GetKey(path);
if (jobPaths.ContainsKey(value))
Delete(value);
else if (alternateKeyGenerator != null) {
long key = alternateKeyGenerator.GetKey(path);
if (alternateKeyMap.TryGetValue(key, out value) && jobPaths.ContainsKey(value))
Delete(value);
}
}
public JobTransferPath SelectByKey(TransferPath path)
{
if (path == (TransferPath)null)
throw new ArgumentNullException("path");
return SelectByKey(path.SourcePath);
}
public JobTransferPath SelectByKey(string path)
{
long value = primaryKeyGenerator.GetKey(path);
JobTransferPath jobTransferPath = SelectByPrimaryKey(value);
if (jobTransferPath != null)
return jobTransferPath;
if (alternateKeyGenerator == null)
return null;
long key = value;
if (!alternateKeyMap.TryGetValue(key, out value))
return null;
return SelectByPrimaryKey(value);
}
public ICollection<JobTransferPath> SelectAll()
{
return jobPaths.Values;
}
public void Upsert(JobTransferPath path)
{
if (path == null)
throw new ArgumentNullException("path");
long key = primaryKeyGenerator.GetKey(path.Path);
bool exists = false;
jobPaths.AddOrUpdate(key, path, delegate(long i, JobTransferPath jobTransferPath) {
exists = true;
return jobTransferPath;
});
if (!exists) {
path.Index = Convert.ToInt32(Count);
Interlocked.Increment(ref count);
}
if (alternateKeyGenerator != null)
alternateKeyMap.AddOrUpdate(alternateKeyGenerator.GetKey(path.Path), key, (long i, long alternateKey) => alternateKey);
}
private JobTransferPath SelectByPrimaryKey(long key)
{
if (!jobPaths.TryGetValue(key, out JobTransferPath value))
return null;
return value;
}
private void Delete(long key)
{
if (jobPaths.TryRemove(key, out JobTransferPath _))
Interlocked.Decrement(ref count);
}
}
}