using System.Collections.Generic;
using System.IO;
using System.Linq;
using Inspectron.Ringbuffer.Interfaces;
namespace Inspectron.Ringbuffer
{
///
/// Ringbuffer\Product\Good\File
///
public class ProductIndex : IIndex
{
private readonly object _lock = new object();
private readonly Dictionary _products = new Dictionary();
public void Track(string filePath)
{
var product = Path.GetDirectoryName(Path.GetDirectoryName(filePath)).Split(Path.DirectorySeparatorChar)
.Last();
var result = Path.GetDirectoryName(filePath).Split(Path.DirectorySeparatorChar).Last();
lock (_lock)
{
if (!_products.ContainsKey(product))
_products[product] = new ProductResults
{
Good = new List(),
Bad = new List()
};
if (result == RingbufferFolder.GOOD_DIR)
{
if (!_products[product].Good.Contains(filePath))
_products[product].Good.Insert(0, filePath);
}
else
{
if (!_products[product].Bad.Contains(filePath))
_products[product].Bad.Insert(0, filePath);
}
}
}
public List GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
{
lock (_lock)
{
var filesToDelete = new List();
foreach (var p in _products)
{
var goodToDelete = p.Value.Good.Skip(allowedAmountGood).ToList();
var badToDelete = p.Value.Bad.Skip(allowedAmountGood).ToList();
goodToDelete.ForEach(x => p.Value.Good.Remove(x));
badToDelete.ForEach(x => p.Value.Bad.Remove(x));
filesToDelete.AddRange(goodToDelete);
filesToDelete.AddRange(badToDelete);
}
return filesToDelete;
}
}
private class ProductResults
{
public List Good { get; set; }
public List Bad { get; set; }
}
}
}