check point
This commit is contained in:
92
framework/Inspectron.Ringbuffer/Index/GroupIndex.cs
Normal file
92
framework/Inspectron.Ringbuffer/Index/GroupIndex.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Inspectron.Ringbuffer.Interfaces;
|
||||
|
||||
namespace Inspectron.Ringbuffer
|
||||
{
|
||||
public class GroupIndex:IIndex
|
||||
{
|
||||
private readonly int _groupPathPart;
|
||||
private readonly int _goodBadPathPart;
|
||||
|
||||
public GroupIndex(int groupPathPart,int goodBadPathPart)
|
||||
{
|
||||
_groupPathPart = groupPathPart;
|
||||
_goodBadPathPart = goodBadPathPart;
|
||||
}
|
||||
|
||||
|
||||
private readonly object _lock = new object();
|
||||
private readonly Dictionary<string, ProductResults> _groups = new Dictionary<string, ProductResults>();
|
||||
|
||||
|
||||
public void Track(string filePath)
|
||||
{
|
||||
|
||||
var parts = Path.GetDirectoryName(filePath).Split(Path.DirectorySeparatorChar);
|
||||
|
||||
string groupKey;
|
||||
try
|
||||
{
|
||||
groupKey = Path.Combine(Enumerable.Range(0, _groupPathPart + 1).Select(x => parts[x]).ToArray());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Tracking {filePath}");
|
||||
Console.WriteLine(e);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string badGoodPart;
|
||||
if (_goodBadPathPart == -1) badGoodPart = RingbufferPath.GOOD_DIR;
|
||||
else
|
||||
badGoodPart = parts[_goodBadPathPart];
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_groups.ContainsKey(groupKey))
|
||||
{
|
||||
_groups[groupKey] = new ProductResults();
|
||||
}
|
||||
|
||||
if (badGoodPart == RingbufferPath.GOOD_DIR)
|
||||
{
|
||||
_groups[groupKey].Good.Insert(0,filePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
_groups[groupKey].Bad.Insert(0, filePath);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var filesToDelete = new List<string>();
|
||||
foreach (var p in _groups)
|
||||
{
|
||||
var goodToDelete = p.Value.Good.Skip(allowedAmountGood).ToList();
|
||||
var badToDelete = p.Value.Bad.Skip(allowedAmountBad).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<string> Good { get; set; }=new List<string>();
|
||||
public List<string> Bad { get; set; }=new List<string>();
|
||||
}
|
||||
}
|
||||
}
|
||||
70
framework/Inspectron.Ringbuffer/Index/ProductIndex.cs
Normal file
70
framework/Inspectron.Ringbuffer/Index/ProductIndex.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Inspectron.Ringbuffer.Interfaces;
|
||||
|
||||
namespace Inspectron.Ringbuffer
|
||||
{
|
||||
/// <summary>
|
||||
/// Ringbuffer\Product\Good\File
|
||||
/// </summary>
|
||||
public class ProductIndex : IIndex
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
private readonly Dictionary<string, ProductResults> _products = new Dictionary<string, ProductResults>();
|
||||
|
||||
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<string>(),
|
||||
Bad = new List<string>()
|
||||
};
|
||||
|
||||
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<string> GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var filesToDelete = new List<string>();
|
||||
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<string> Good { get; set; }
|
||||
public List<string> Bad { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
56
framework/Inspectron.Ringbuffer/Index/RootIndex.cs
Normal file
56
framework/Inspectron.Ringbuffer/Index/RootIndex.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Inspectron.Ringbuffer.Interfaces;
|
||||
|
||||
namespace Inspectron.Ringbuffer
|
||||
{
|
||||
/// <summary>
|
||||
/// Ringbuffer\Good\Product\File
|
||||
/// </summary>
|
||||
public class RootIndex : IIndex
|
||||
{
|
||||
private readonly List<string> _trackedGoodFiles = new List<string>();
|
||||
private readonly List<string> _trackedBadFiles = new List<string>();
|
||||
private readonly object _lock = new object();
|
||||
|
||||
public void Track(string filePath)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (IsGood(filePath))
|
||||
{
|
||||
if (!_trackedGoodFiles.Contains(filePath))
|
||||
_trackedGoodFiles.Insert(0, filePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_trackedBadFiles.Contains(filePath))
|
||||
_trackedBadFiles.Insert(0, filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<string> GetFilesToDelete(int allowedAmountGood, int allowedAmountBad)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var resGood = _trackedGoodFiles.Skip(allowedAmountGood).ToList();
|
||||
var resBad = _trackedBadFiles.Skip(allowedAmountBad).ToList();
|
||||
|
||||
resGood.ForEach(x => { _trackedGoodFiles.Remove(x); });
|
||||
resBad.ForEach(x => { _trackedBadFiles.Remove(x); });
|
||||
|
||||
return resGood.Concat(resBad).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsGood(string filePath)
|
||||
{
|
||||
var folder = Path.GetDirectoryName(Path.GetDirectoryName(filePath)).Split(Path.DirectorySeparatorChar)
|
||||
.Last();
|
||||
return folder == RingbufferFolder.GOOD_DIR;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user