50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace StellaOps.Provcache.Postgres;
|
|
|
|
public sealed partial class PostgresEvidenceChunkRepository
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task StoreChunksAsync(
|
|
string proofRoot,
|
|
IEnumerable<EvidenceChunk> chunks,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
|
ArgumentNullException.ThrowIfNull(chunks);
|
|
|
|
var chunkList = chunks.ToList();
|
|
|
|
if (chunkList.Count == 0)
|
|
{
|
|
_logger.LogDebug("No chunks to store for proof root {ProofRoot}", proofRoot);
|
|
return;
|
|
}
|
|
|
|
// Update proof root in chunks if not set
|
|
var entities = chunkList.Select(c => MapToEntity(c, proofRoot)).ToList();
|
|
|
|
_context.EvidenceChunks.AddRange(entities);
|
|
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
_logger.LogDebug("Stored {Count} chunks for proof root {ProofRoot}", chunkList.Count, proofRoot);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<int> DeleteChunksAsync(
|
|
string proofRoot,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
|
|
|
var deleted = await _context.EvidenceChunks
|
|
.Where(e => e.ProofRoot == proofRoot)
|
|
.ExecuteDeleteAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
_logger.LogDebug("Deleted {Count} chunks for proof root {ProofRoot}", deleted, proofRoot);
|
|
return deleted;
|
|
}
|
|
}
|