long candy ready to test

This commit is contained in:
meelstorm
2025-09-05 11:11:23 +02:00
parent d52c844b5d
commit e5746ef766
39 changed files with 904 additions and 168 deletions

View File

@@ -1,13 +1,15 @@
using Inspectron.Fastbuffer.Interfaces;
using System.Collections.Concurrent;
using OpenCvSharp;
using Serilog;
namespace Inspectron.Fastbuffer.Filesystems;
public class AsyncFilesystem : IFilesystem
{
private readonly DirectFilesystem _directFilesystem = new();
private readonly ConcurrentDictionary<string, byte[]> _buffer = new();
private readonly BlockingCollection<(string path, byte[] data)> _writeQueue = new();
private readonly List<string> _buffer = new();
private readonly BlockingCollection<(string path, Mat data)> _writeQueue = new();
private readonly Thread _backgroundThread;
private readonly object _lock = new();
@@ -18,27 +20,17 @@ public class AsyncFilesystem : IFilesystem
_backgroundThread.Start();
}
public void Write(string path, byte[] data)
public void Write(string path, Mat data)
{
lock (_lock)
{
_buffer[path] = data;
_buffer.Add(path);
_writeQueue.Add((path, data));
}
}
public byte[] Read(string path)
{
lock (_lock)
{
if (_buffer.TryGetValue(path, out var data))
{
return data;
}
}
return _directFilesystem.Read(path);
}
public void Delete(string path)
{
@@ -46,8 +38,9 @@ public class AsyncFilesystem : IFilesystem
lock (_lock)
{
// pats is in buffer, so it's not written to disk yet
if (_buffer.TryRemove(path, out _))
if (_buffer.Contains(path))
{
_buffer.Remove(path);
return;
}
_directFilesystem.Delete(path);
@@ -61,8 +54,9 @@ public class AsyncFilesystem : IFilesystem
{
_directFilesystem.Write(path, data);
lock (_lock)
_buffer.TryRemove(path, out _);
_buffer.Remove(path);
if (_writeQueue.Count > 5)
Log.Warning($"AsyncFilesystem write queue size: {_writeQueue.Count}");
}
}

View File

@@ -1,15 +1,17 @@
using Inspectron.Fastbuffer.Interfaces;
using OpenCvSharp;
using Serilog;
namespace Inspectron.Fastbuffer.Filesystems;
public class DirectFilesystem:IFilesystem
{
public void Write(string path, byte[] data)
public void Write(string path, Mat data)
{
// ensure path
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllBytes(path, data);
var bytes = data.ToBytes();
File.WriteAllBytes(path, bytes);
}
public byte[] Read(string path)

View File

@@ -1,5 +1,6 @@
using System.Text;
using Inspectron.Fastbuffer.Interfaces;
using OpenCvSharp;
using Serilog;
namespace Inspectron.Fastbuffer;
@@ -112,7 +113,7 @@ public class IndexFile
}
public void WriteNextArtifact(byte[] data, string fileName)
public void WriteNextArtifact(Mat data, string fileName)
{
var id = GetNextId();
Log.Debug("Writing artifact {id}",id);
@@ -132,19 +133,5 @@ public class IndexFile
fileStream.Write(padding, 0, padding.Length);
}
public byte[] ReadArtifact(int id)
{
using var fileStream = File.Open(_indexFilePath, FileMode.Open);
fileStream.Seek(id * RecordSize+ HeaderSize, SeekOrigin.Begin);
var bytes = new byte[RecordSize];
fileStream.Read(bytes, 0, RecordSize);
var fileName = Encoding.UTF8.GetString(bytes).TrimEnd('\0');
var filePath = Path.Combine(_bufferPath, fileName);
return _filesystem.Read(filePath);
}
}

View File

@@ -11,6 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.6.0.20220608" />
<PackageReference Include="Serilog" Version="4.2.0" />
</ItemGroup>

View File

@@ -1,8 +1,9 @@
namespace Inspectron.Fastbuffer.Interfaces;
using OpenCvSharp;
namespace Inspectron.Fastbuffer.Interfaces;
public interface IFilesystem:IDisposable
{
public void Write(string path, byte[] data);
public byte[] Read(string path);
public void Write(string path, Mat data);
public void Delete(string path);
}

View File

@@ -1,4 +1,5 @@
using Inspectron.Fastbuffer.Interfaces;
using OpenCvSharp;
namespace Inspectron.Fastbuffer;
@@ -26,7 +27,7 @@ public class IsolatedRingbuffer: IDisposable
}
public void Write(byte[] data, string fileName)
public void Write(Mat data, string fileName)
{
_index.WriteNextArtifact(data, fileName);
}

View File

@@ -1,52 +0,0 @@
using Inspectron.Fastbuffer.Interfaces;
namespace Inspectron.Fastbuffer
{
public class RingbufferRepository
{
private readonly string _path;
private readonly string _goodIndexPath;
private readonly string _badIndexPath;
private readonly IndexFile _goodIndex;
private readonly IndexFile _badIndex;
public const string BufferDirectory = ".rb";
public const string GoodIndexFile = "good.idx";
public const string BadIndexFile = "bad.idx";
public RingbufferRepository(string path, int goodBufferSize, int badBufferSize, IFilesystem filesystem)
{
_path = path;
_goodIndexPath = Path.Combine(_path, BufferDirectory, GoodIndexFile);
_badIndexPath = Path.Combine(_path, BufferDirectory, BadIndexFile);
Directory.CreateDirectory(Path.Combine(_path,BufferDirectory));
_goodIndex = new IndexFile(_goodIndexPath, goodBufferSize, filesystem);
_badIndex = new IndexFile(_badIndexPath, badBufferSize, filesystem);
var id= _goodIndex.GetCurrentArtifactId();
Console.WriteLine(@"RB size: "+id);
for (int i = 0; i < 3; i++)
{
var artifact = _goodIndex.GetArtifactPath(i);
Console.WriteLine(@"Artifact: "+artifact);
}
}
public IndexFile GoodIndex => _goodIndex;
public IndexFile BadIndex => _badIndex;
public void WriteGood(byte[] data, string fileName)
{
_goodIndex.WriteNextArtifact(data, fileName);
}
public void WriteBad(byte[] data, string fileName)
{
_badIndex.WriteNextArtifact(data, fileName);
}
}
}