using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace StellaOps.Provcache.Postgres;
public sealed partial class PostgresEvidenceChunkRepository
{
///
public async Task StoreChunksAsync(
string proofRoot,
IEnumerable 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);
}
///
public async Task 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;
}
}