提交代码
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
using LiteDB;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace HardwareControl.Core.Scanning
|
||||
{
|
||||
/// <summary>
|
||||
/// 扫描数据持久化(独立 LiteDB 文件)。
|
||||
/// 扫描期间数据只保存在内存,流程正常结束后一次性写入。
|
||||
/// </summary>
|
||||
public sealed class ScanDataStore : IDisposable
|
||||
{
|
||||
private const string RunCollection = "scan_run";
|
||||
private const string PointCollection = "scan_point";
|
||||
|
||||
private readonly LiteDatabase _database;
|
||||
private readonly ILiteCollection<ScanRunRecord> _runs;
|
||||
private readonly ILiteCollection<ScanPointRecord> _points;
|
||||
private readonly ILogger<ScanDataStore> _logger;
|
||||
private readonly object _gate = new();
|
||||
|
||||
public ScanDataStore(string? databasePath, ILogger<ScanDataStore> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
|
||||
var path = string.IsNullOrWhiteSpace(databasePath)
|
||||
? Path.Combine(AppContext.BaseDirectory, "Data", "scan-data.db")
|
||||
: databasePath;
|
||||
|
||||
var fullPath = Path.GetFullPath(path);
|
||||
var directory = Path.GetDirectoryName(fullPath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
_database = new LiteDatabase(new ConnectionString
|
||||
{
|
||||
Filename = fullPath,
|
||||
Connection = ConnectionType.Shared,
|
||||
});
|
||||
|
||||
_runs = _database.GetCollection<ScanRunRecord>(RunCollection);
|
||||
_points = _database.GetCollection<ScanPointRecord>(PointCollection);
|
||||
_points.EnsureIndex(point => point.ScanId);
|
||||
|
||||
_logger.LogInformation("扫描数据库:{Path}", fullPath);
|
||||
}
|
||||
|
||||
/// <summary>一次性写入一次扫描及其所有采样点。</summary>
|
||||
public void SaveCompleted(
|
||||
ScanRunRecord run,
|
||||
IReadOnlyList<ScanPointRecord> points)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_database.BeginTrans();
|
||||
try
|
||||
{
|
||||
_runs.Upsert(run);
|
||||
for (var i = 0; i < points.Count; i++)
|
||||
{
|
||||
points[i].Id = 0;
|
||||
}
|
||||
|
||||
_points.InsertBulk(points);
|
||||
_database.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_database.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"扫描数据已写入数据库:{ScanId},采样点 {Count} 个",
|
||||
run.ScanId,
|
||||
points.Count);
|
||||
}
|
||||
|
||||
public ScanRunRecord? GetRun(string scanId)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _runs.FindById(scanId);
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<ScanPointRecord> GetPoints(string scanId)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _points
|
||||
.Find(point => point.ScanId == scanId)
|
||||
.OrderBy(point => point.Index)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public ScanRunRecord? GetLatestRun()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _runs
|
||||
.FindAll()
|
||||
.OrderByDescending(run => run.StartedAt)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>按开始时间倒序读取最近的扫描记录,用于历史列表。</summary>
|
||||
public IReadOnlyList<ScanRunRecord> GetRecentRuns(int limit)
|
||||
{
|
||||
var count = Math.Clamp(limit, 1, 200);
|
||||
lock (_gate)
|
||||
{
|
||||
return _runs
|
||||
.Find(Query.All("StartedAt", Query.Descending), 0, count)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>删除一条扫描记录及其全部采样点;记录不存在时返回 false。</summary>
|
||||
public bool DeleteRun(string scanId)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_database.BeginTrans();
|
||||
try
|
||||
{
|
||||
_points.DeleteMany(point => point.ScanId == scanId);
|
||||
var removed = _runs.Delete(scanId);
|
||||
_database.Commit();
|
||||
|
||||
if (removed)
|
||||
{
|
||||
_logger.LogInformation("扫描记录已删除:{ScanId}", scanId);
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
catch
|
||||
{
|
||||
_database.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() => _database.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user