stabilizaiton work - projects rework for maintenanceability and ui livening
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresEvidenceChunkRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<EvidenceChunk>> GetChunksAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
var entities = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot)
|
||||
.OrderBy(e => e.ChunkIndex)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Retrieved {Count} chunks for proof root {ProofRoot}", entities.Count, proofRoot);
|
||||
return entities.Select(MapToModel).ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<EvidenceChunk?> GetChunkAsync(
|
||||
string proofRoot,
|
||||
int chunkIndex,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
var entity = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot && e.ChunkIndex == chunkIndex)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entity is null ? null : MapToModel(entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresEvidenceChunkRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ChunkManifest?> GetManifestAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
// Get metadata without loading blobs
|
||||
var chunks = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot)
|
||||
.OrderBy(e => e.ChunkIndex)
|
||||
.Select(e => new
|
||||
{
|
||||
e.ChunkId,
|
||||
e.ChunkIndex,
|
||||
e.ChunkHash,
|
||||
e.BlobSize,
|
||||
e.ContentType,
|
||||
e.CreatedAt
|
||||
})
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (chunks.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var metadata = chunks
|
||||
.Select(c => new ChunkMetadata
|
||||
{
|
||||
ChunkId = c.ChunkId,
|
||||
Index = c.ChunkIndex,
|
||||
Hash = c.ChunkHash,
|
||||
Size = c.BlobSize,
|
||||
ContentType = c.ContentType
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new ChunkManifest
|
||||
{
|
||||
ProofRoot = proofRoot,
|
||||
TotalChunks = chunks.Count,
|
||||
TotalSize = chunks.Sum(c => (long)c.BlobSize),
|
||||
Chunks = metadata,
|
||||
GeneratedAt = _timeProvider.GetUtcNow()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using StellaOps.Provcache.Entities;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresEvidenceChunkRepository
|
||||
{
|
||||
private static EvidenceChunk MapToModel(ProvcacheEvidenceChunkEntity entity)
|
||||
{
|
||||
return new EvidenceChunk
|
||||
{
|
||||
ChunkId = entity.ChunkId,
|
||||
ProofRoot = entity.ProofRoot,
|
||||
ChunkIndex = entity.ChunkIndex,
|
||||
ChunkHash = entity.ChunkHash,
|
||||
Blob = entity.Blob,
|
||||
BlobSize = entity.BlobSize,
|
||||
ContentType = entity.ContentType,
|
||||
CreatedAt = entity.CreatedAt
|
||||
};
|
||||
}
|
||||
|
||||
private ProvcacheEvidenceChunkEntity MapToEntity(EvidenceChunk chunk, string proofRoot)
|
||||
{
|
||||
return new ProvcacheEvidenceChunkEntity
|
||||
{
|
||||
ChunkId = chunk.ChunkId == Guid.Empty ? _guidProvider.NewGuid() : chunk.ChunkId,
|
||||
ProofRoot = proofRoot,
|
||||
ChunkIndex = chunk.ChunkIndex,
|
||||
ChunkHash = chunk.ChunkHash,
|
||||
Blob = chunk.Blob,
|
||||
BlobSize = chunk.BlobSize,
|
||||
ContentType = chunk.ContentType,
|
||||
CreatedAt = chunk.CreatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresEvidenceChunkRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<int> GetChunkCountAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
return await _context.EvidenceChunks
|
||||
.CountAsync(e => e.ProofRoot == proofRoot, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> GetTotalSizeAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
return await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot)
|
||||
.SumAsync(e => (long)e.BlobSize, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets total storage across all proof roots.
|
||||
/// </summary>
|
||||
public async Task<long> GetTotalStorageAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.EvidenceChunks
|
||||
.SumAsync(e => (long)e.BlobSize, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prunes chunks older than the specified date.
|
||||
/// </summary>
|
||||
public async Task<int> PruneOldChunksAsync(
|
||||
DateTimeOffset olderThan,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.EvidenceChunks
|
||||
.Where(e => e.CreatedAt < olderThan)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresEvidenceChunkRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<EvidenceChunk>> GetChunkRangeAsync(
|
||||
string proofRoot,
|
||||
int startIndex,
|
||||
int count,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
if (startIndex < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(startIndex), "Start index must be non-negative.");
|
||||
}
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count), "Count must be positive.");
|
||||
}
|
||||
|
||||
var entities = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot && e.ChunkIndex >= startIndex)
|
||||
.OrderBy(e => e.ChunkIndex)
|
||||
.Take(count)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entities.Select(MapToModel).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Determinism;
|
||||
using StellaOps.Provcache.Entities;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL implementation of <see cref="IEvidenceChunkRepository"/>.
|
||||
/// </summary>
|
||||
public sealed class PostgresEvidenceChunkRepository : IEvidenceChunkRepository
|
||||
public sealed partial class PostgresEvidenceChunkRepository : IEvidenceChunkRepository
|
||||
{
|
||||
private readonly ProvcacheDbContext _context;
|
||||
private readonly ILogger<PostgresEvidenceChunkRepository> _logger;
|
||||
@@ -26,239 +24,4 @@ public sealed class PostgresEvidenceChunkRepository : IEvidenceChunkRepository
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_guidProvider = guidProvider ?? SystemGuidProvider.Instance;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<EvidenceChunk>> GetChunksAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
var entities = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot)
|
||||
.OrderBy(e => e.ChunkIndex)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Retrieved {Count} chunks for proof root {ProofRoot}", entities.Count, proofRoot);
|
||||
return entities.Select(MapToModel).ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<EvidenceChunk?> GetChunkAsync(
|
||||
string proofRoot,
|
||||
int chunkIndex,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
var entity = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot && e.ChunkIndex == chunkIndex)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entity is null ? null : MapToModel(entity);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<EvidenceChunk>> GetChunkRangeAsync(
|
||||
string proofRoot,
|
||||
int startIndex,
|
||||
int count,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
if (startIndex < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(startIndex), "Start index must be non-negative.");
|
||||
}
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count), "Count must be positive.");
|
||||
}
|
||||
|
||||
var entities = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot && e.ChunkIndex >= startIndex)
|
||||
.OrderBy(e => e.ChunkIndex)
|
||||
.Take(count)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entities.Select(MapToModel).ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ChunkManifest?> GetManifestAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
// Get metadata without loading blobs
|
||||
var chunks = await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot)
|
||||
.OrderBy(e => e.ChunkIndex)
|
||||
.Select(e => new
|
||||
{
|
||||
e.ChunkId,
|
||||
e.ChunkIndex,
|
||||
e.ChunkHash,
|
||||
e.BlobSize,
|
||||
e.ContentType,
|
||||
e.CreatedAt
|
||||
})
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (chunks.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var metadata = chunks
|
||||
.Select(c => new ChunkMetadata
|
||||
{
|
||||
ChunkId = c.ChunkId,
|
||||
Index = c.ChunkIndex,
|
||||
Hash = c.ChunkHash,
|
||||
Size = c.BlobSize,
|
||||
ContentType = c.ContentType
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new ChunkManifest
|
||||
{
|
||||
ProofRoot = proofRoot,
|
||||
TotalChunks = chunks.Count,
|
||||
TotalSize = chunks.Sum(c => (long)c.BlobSize),
|
||||
Chunks = metadata,
|
||||
GeneratedAt = _timeProvider.GetUtcNow()
|
||||
};
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<int> GetChunkCountAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
return await _context.EvidenceChunks
|
||||
.CountAsync(e => e.ProofRoot == proofRoot, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> GetTotalSizeAsync(
|
||||
string proofRoot,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(proofRoot);
|
||||
|
||||
return await _context.EvidenceChunks
|
||||
.Where(e => e.ProofRoot == proofRoot)
|
||||
.SumAsync(e => (long)e.BlobSize, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets total storage across all proof roots.
|
||||
/// </summary>
|
||||
public async Task<long> GetTotalStorageAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.EvidenceChunks
|
||||
.SumAsync(e => (long)e.BlobSize, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prunes chunks older than the specified date.
|
||||
/// </summary>
|
||||
public async Task<int> PruneOldChunksAsync(
|
||||
DateTimeOffset olderThan,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.EvidenceChunks
|
||||
.Where(e => e.CreatedAt < olderThan)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static EvidenceChunk MapToModel(ProvcacheEvidenceChunkEntity entity)
|
||||
{
|
||||
return new EvidenceChunk
|
||||
{
|
||||
ChunkId = entity.ChunkId,
|
||||
ProofRoot = entity.ProofRoot,
|
||||
ChunkIndex = entity.ChunkIndex,
|
||||
ChunkHash = entity.ChunkHash,
|
||||
Blob = entity.Blob,
|
||||
BlobSize = entity.BlobSize,
|
||||
ContentType = entity.ContentType,
|
||||
CreatedAt = entity.CreatedAt
|
||||
};
|
||||
}
|
||||
|
||||
private ProvcacheEvidenceChunkEntity MapToEntity(EvidenceChunk chunk, string proofRoot)
|
||||
{
|
||||
return new ProvcacheEvidenceChunkEntity
|
||||
{
|
||||
ChunkId = chunk.ChunkId == Guid.Empty ? _guidProvider.NewGuid() : chunk.ChunkId,
|
||||
ProofRoot = proofRoot,
|
||||
ChunkIndex = chunk.ChunkIndex,
|
||||
ChunkHash = chunk.ChunkHash,
|
||||
Blob = chunk.Blob,
|
||||
BlobSize = chunk.BlobSize,
|
||||
ContentType = chunk.ContentType,
|
||||
CreatedAt = chunk.CreatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> DeleteAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _context.ProvcacheItems
|
||||
.FirstOrDefaultAsync(e => e.VeriKey == veriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (entity is null)
|
||||
return false;
|
||||
|
||||
_context.ProvcacheItems.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Globalization;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteByFeedEpochOlderThanAsync(string feedEpoch, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => string.Compare(e.FeedEpoch, feedEpoch) < 0)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("feed", feedEpoch, "feed-update", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteExpiredAsync(DateTimeOffset asOf, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => e.ExpiresAt <= asOf)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync(
|
||||
"expired",
|
||||
asOf.ToString("O", CultureInfo.InvariantCulture),
|
||||
"ttl-expiry",
|
||||
deleted,
|
||||
cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteByPolicyHashAsync(string policyHash, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => e.PolicyHash == policyHash)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("policy", policyHash, "policy-update", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteBySignerSetHashAsync(string signerSetHash, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => e.SignerSetHash == signerSetHash)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("signer", signerSetHash, "signer-revocation", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using StellaOps.Canonical.Json;
|
||||
using StellaOps.Provcache.Entities;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
private static readonly JsonSerializerOptions _replaySeedJsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
private ProvcacheEntry MapToEntry(ProvcacheItemEntity entity)
|
||||
{
|
||||
var replaySeed = JsonSerializer.Deserialize<ReplaySeed>(entity.ReplaySeed, _replaySeedJsonOptions)
|
||||
?? new ReplaySeed { FeedIds = [], RuleIds = [] };
|
||||
|
||||
return new ProvcacheEntry
|
||||
{
|
||||
VeriKey = entity.VeriKey,
|
||||
Decision = new DecisionDigest
|
||||
{
|
||||
DigestVersion = entity.DigestVersion,
|
||||
VeriKey = entity.VeriKey,
|
||||
VerdictHash = entity.VerdictHash,
|
||||
ProofRoot = entity.ProofRoot,
|
||||
ReplaySeed = replaySeed,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
ExpiresAt = entity.ExpiresAt,
|
||||
TrustScore = entity.TrustScore
|
||||
},
|
||||
PolicyHash = entity.PolicyHash,
|
||||
SignerSetHash = entity.SignerSetHash,
|
||||
FeedEpoch = entity.FeedEpoch,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
ExpiresAt = entity.ExpiresAt,
|
||||
HitCount = entity.HitCount,
|
||||
LastAccessedAt = entity.LastAccessedAt
|
||||
};
|
||||
}
|
||||
|
||||
private ProvcacheItemEntity MapToEntity(ProvcacheEntry entry)
|
||||
{
|
||||
return new ProvcacheItemEntity
|
||||
{
|
||||
VeriKey = entry.VeriKey,
|
||||
DigestVersion = entry.Decision.DigestVersion,
|
||||
VerdictHash = entry.Decision.VerdictHash,
|
||||
ProofRoot = entry.Decision.ProofRoot,
|
||||
ReplaySeed = CanonJson.Serialize(entry.Decision.ReplaySeed, _replaySeedJsonOptions),
|
||||
PolicyHash = entry.PolicyHash,
|
||||
SignerSetHash = entry.SignerSetHash,
|
||||
FeedEpoch = entry.FeedEpoch,
|
||||
TrustScore = entry.Decision.TrustScore,
|
||||
HitCount = entry.HitCount,
|
||||
CreatedAt = entry.CreatedAt,
|
||||
ExpiresAt = entry.ExpiresAt,
|
||||
UpdatedAt = _timeProvider.GetUtcNow(),
|
||||
LastAccessedAt = entry.LastAccessedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task IncrementHitCountAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
await _context.ProvcacheItems
|
||||
.Where(e => e.VeriKey == veriKey)
|
||||
.ExecuteUpdateAsync(
|
||||
setters => setters
|
||||
.SetProperty(e => e.HitCount, e => e.HitCount + 1)
|
||||
.SetProperty(e => e.LastAccessedAt, now),
|
||||
cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ProvcacheStatistics> GetStatisticsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
var hourFromNow = now.AddHours(1);
|
||||
|
||||
var totalEntries = await _context.ProvcacheItems
|
||||
.LongCountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var totalHits = await _context.ProvcacheItems
|
||||
.SumAsync(e => e.HitCount, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var expiringWithinHour = await _context.ProvcacheItems
|
||||
.LongCountAsync(e => e.ExpiresAt <= hourFromNow, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var uniquePolicies = await _context.ProvcacheItems
|
||||
.Select(e => e.PolicyHash)
|
||||
.Distinct()
|
||||
.CountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var uniqueSignerSets = await _context.ProvcacheItems
|
||||
.Select(e => e.SignerSetHash)
|
||||
.Distinct()
|
||||
.CountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var oldest = await _context.ProvcacheItems
|
||||
.OrderBy(e => e.CreatedAt)
|
||||
.Select(e => (DateTimeOffset?)e.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var newest = await _context.ProvcacheItems
|
||||
.OrderByDescending(e => e.CreatedAt)
|
||||
.Select(e => (DateTimeOffset?)e.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return new ProvcacheStatistics
|
||||
{
|
||||
TotalEntries = totalEntries,
|
||||
TotalHits = totalHits,
|
||||
ExpiringWithinHour = expiringWithinHour,
|
||||
UniquePolicies = uniquePolicies,
|
||||
UniqueSignerSets = uniqueSignerSets,
|
||||
OldestEntry = oldest,
|
||||
NewestEntry = newest
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProvcacheEntry?> GetAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _context.ProvcacheItems
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(e => e.VeriKey == veriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entity is null ? null : MapToEntry(entity);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyDictionary<string, ProvcacheEntry>> GetManyAsync(
|
||||
IEnumerable<string> veriKeys,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var keyList = veriKeys.ToList();
|
||||
if (keyList.Count == 0)
|
||||
return new Dictionary<string, ProvcacheEntry>();
|
||||
|
||||
var entities = await _context.ProvcacheItems
|
||||
.AsNoTracking()
|
||||
.Where(e => keyList.Contains(e.VeriKey))
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entities.ToDictionary(e => e.VeriKey, MapToEntry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Provcache.Entities;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
private async Task LogRevocationAsync(
|
||||
string type,
|
||||
string targetHash,
|
||||
string reason,
|
||||
long entriesAffected,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var revocation = new ProvcacheRevocationEntity
|
||||
{
|
||||
RevocationId = _guidProvider.NewGuid(),
|
||||
RevocationType = type,
|
||||
TargetHash = targetHash,
|
||||
Reason = reason,
|
||||
EntriesAffected = entriesAffected,
|
||||
CreatedAt = _timeProvider.GetUtcNow()
|
||||
};
|
||||
|
||||
_context.Revocations.Add(revocation);
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Logged revocation: type={Type}, target={TargetHash}, affected={EntriesAffected}",
|
||||
type,
|
||||
targetHash,
|
||||
entriesAffected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
public sealed partial class PostgresProvcacheRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task UpsertAsync(ProvcacheEntry entry, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = MapToEntity(entry);
|
||||
|
||||
var existing = await _context.ProvcacheItems
|
||||
.FirstOrDefaultAsync(e => e.VeriKey == entry.VeriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
_context.ProvcacheItems.Add(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Entry(existing).CurrentValues.SetValues(entity);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UpsertManyAsync(IEnumerable<ProvcacheEntry> entries, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entryList = entries.ToList();
|
||||
if (entryList.Count == 0)
|
||||
return;
|
||||
|
||||
var veriKeys = entryList.Select(e => e.VeriKey).ToList();
|
||||
var existing = await _context.ProvcacheItems
|
||||
.Where(e => veriKeys.Contains(e.VeriKey))
|
||||
.ToDictionaryAsync(e => e.VeriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
foreach (var entry in entryList)
|
||||
{
|
||||
var entity = MapToEntity(entry);
|
||||
|
||||
if (existing.TryGetValue(entry.VeriKey, out var existingEntity))
|
||||
{
|
||||
_context.Entry(existingEntity).CurrentValues.SetValues(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.ProvcacheItems.Add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,14 @@
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Canonical.Json;
|
||||
using StellaOps.Determinism;
|
||||
using StellaOps.Provcache.Entities;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace StellaOps.Provcache.Postgres;
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL implementation of <see cref="IProvcacheRepository"/>.
|
||||
/// </summary>
|
||||
public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
public sealed partial class PostgresProvcacheRepository : IProvcacheRepository
|
||||
{
|
||||
private readonly ProvcacheDbContext _context;
|
||||
private readonly ILogger<PostgresProvcacheRepository> _logger;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly IGuidProvider _guidProvider;
|
||||
private static readonly JsonSerializerOptions ReplaySeedJsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public PostgresProvcacheRepository(
|
||||
ProvcacheDbContext context,
|
||||
@@ -35,312 +21,4 @@ public sealed class PostgresProvcacheRepository : IProvcacheRepository
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_guidProvider = guidProvider ?? SystemGuidProvider.Instance;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ProvcacheEntry?> GetAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _context.ProvcacheItems
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(e => e.VeriKey == veriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entity is null ? null : MapToEntry(entity);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyDictionary<string, ProvcacheEntry>> GetManyAsync(
|
||||
IEnumerable<string> veriKeys,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var keyList = veriKeys.ToList();
|
||||
if (keyList.Count == 0)
|
||||
return new Dictionary<string, ProvcacheEntry>();
|
||||
|
||||
var entities = await _context.ProvcacheItems
|
||||
.AsNoTracking()
|
||||
.Where(e => keyList.Contains(e.VeriKey))
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return entities.ToDictionary(e => e.VeriKey, MapToEntry);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UpsertAsync(ProvcacheEntry entry, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = MapToEntity(entry);
|
||||
|
||||
var existing = await _context.ProvcacheItems
|
||||
.FirstOrDefaultAsync(e => e.VeriKey == entry.VeriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
_context.ProvcacheItems.Add(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Entry(existing).CurrentValues.SetValues(entity);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UpsertManyAsync(IEnumerable<ProvcacheEntry> entries, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entryList = entries.ToList();
|
||||
if (entryList.Count == 0)
|
||||
return;
|
||||
|
||||
var veriKeys = entryList.Select(e => e.VeriKey).ToList();
|
||||
var existing = await _context.ProvcacheItems
|
||||
.Where(e => veriKeys.Contains(e.VeriKey))
|
||||
.ToDictionaryAsync(e => e.VeriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
foreach (var entry in entryList)
|
||||
{
|
||||
var entity = MapToEntity(entry);
|
||||
|
||||
if (existing.TryGetValue(entry.VeriKey, out var existingEntity))
|
||||
{
|
||||
_context.Entry(existingEntity).CurrentValues.SetValues(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.ProvcacheItems.Add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> DeleteAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = await _context.ProvcacheItems
|
||||
.FirstOrDefaultAsync(e => e.VeriKey == veriKey, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (entity is null)
|
||||
return false;
|
||||
|
||||
_context.ProvcacheItems.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteByPolicyHashAsync(string policyHash, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => e.PolicyHash == policyHash)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("policy", policyHash, "policy-update", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteBySignerSetHashAsync(string signerSetHash, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => e.SignerSetHash == signerSetHash)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("signer", signerSetHash, "signer-revocation", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteByFeedEpochOlderThanAsync(string feedEpoch, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => string.Compare(e.FeedEpoch, feedEpoch, StringComparison.Ordinal) < 0)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("feed", feedEpoch, "feed-update", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<long> DeleteExpiredAsync(DateTimeOffset asOf, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deleted = await _context.ProvcacheItems
|
||||
.Where(e => e.ExpiresAt <= asOf)
|
||||
.ExecuteDeleteAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (deleted > 0)
|
||||
{
|
||||
await LogRevocationAsync("expired", asOf.ToString("O", CultureInfo.InvariantCulture), "ttl-expiry", deleted, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task IncrementHitCountAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
await _context.ProvcacheItems
|
||||
.Where(e => e.VeriKey == veriKey)
|
||||
.ExecuteUpdateAsync(
|
||||
setters => setters
|
||||
.SetProperty(e => e.HitCount, e => e.HitCount + 1)
|
||||
.SetProperty(e => e.LastAccessedAt, now),
|
||||
cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ProvcacheStatistics> GetStatisticsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
var hourFromNow = now.AddHours(1);
|
||||
|
||||
var totalEntries = await _context.ProvcacheItems
|
||||
.LongCountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var totalHits = await _context.ProvcacheItems
|
||||
.SumAsync(e => e.HitCount, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var expiringWithinHour = await _context.ProvcacheItems
|
||||
.LongCountAsync(e => e.ExpiresAt <= hourFromNow, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var uniquePolicies = await _context.ProvcacheItems
|
||||
.Select(e => e.PolicyHash)
|
||||
.Distinct()
|
||||
.CountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var uniqueSignerSets = await _context.ProvcacheItems
|
||||
.Select(e => e.SignerSetHash)
|
||||
.Distinct()
|
||||
.CountAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var oldest = await _context.ProvcacheItems
|
||||
.OrderBy(e => e.CreatedAt)
|
||||
.Select(e => (DateTimeOffset?)e.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var newest = await _context.ProvcacheItems
|
||||
.OrderByDescending(e => e.CreatedAt)
|
||||
.Select(e => (DateTimeOffset?)e.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return new ProvcacheStatistics
|
||||
{
|
||||
TotalEntries = totalEntries,
|
||||
TotalHits = totalHits,
|
||||
ExpiringWithinHour = expiringWithinHour,
|
||||
UniquePolicies = uniquePolicies,
|
||||
UniqueSignerSets = uniqueSignerSets,
|
||||
OldestEntry = oldest,
|
||||
NewestEntry = newest
|
||||
};
|
||||
}
|
||||
|
||||
private async Task LogRevocationAsync(
|
||||
string type,
|
||||
string targetHash,
|
||||
string reason,
|
||||
long entriesAffected,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var revocation = new ProvcacheRevocationEntity
|
||||
{
|
||||
RevocationId = _guidProvider.NewGuid(),
|
||||
RevocationType = type,
|
||||
TargetHash = targetHash,
|
||||
Reason = reason,
|
||||
EntriesAffected = entriesAffected,
|
||||
CreatedAt = _timeProvider.GetUtcNow()
|
||||
};
|
||||
|
||||
_context.Revocations.Add(revocation);
|
||||
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Logged revocation: type={Type}, target={TargetHash}, affected={EntriesAffected}",
|
||||
type,
|
||||
targetHash,
|
||||
entriesAffected);
|
||||
}
|
||||
|
||||
private ProvcacheEntry MapToEntry(ProvcacheItemEntity entity)
|
||||
{
|
||||
var replaySeed = JsonSerializer.Deserialize<ReplaySeed>(entity.ReplaySeed, ReplaySeedJsonOptions)
|
||||
?? new ReplaySeed { FeedIds = [], RuleIds = [] };
|
||||
|
||||
return new ProvcacheEntry
|
||||
{
|
||||
VeriKey = entity.VeriKey,
|
||||
Decision = new DecisionDigest
|
||||
{
|
||||
DigestVersion = entity.DigestVersion,
|
||||
VeriKey = entity.VeriKey,
|
||||
VerdictHash = entity.VerdictHash,
|
||||
ProofRoot = entity.ProofRoot,
|
||||
ReplaySeed = replaySeed,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
ExpiresAt = entity.ExpiresAt,
|
||||
TrustScore = entity.TrustScore
|
||||
},
|
||||
PolicyHash = entity.PolicyHash,
|
||||
SignerSetHash = entity.SignerSetHash,
|
||||
FeedEpoch = entity.FeedEpoch,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
ExpiresAt = entity.ExpiresAt,
|
||||
HitCount = entity.HitCount,
|
||||
LastAccessedAt = entity.LastAccessedAt
|
||||
};
|
||||
}
|
||||
|
||||
private ProvcacheItemEntity MapToEntity(ProvcacheEntry entry)
|
||||
{
|
||||
return new ProvcacheItemEntity
|
||||
{
|
||||
VeriKey = entry.VeriKey,
|
||||
DigestVersion = entry.Decision.DigestVersion,
|
||||
VerdictHash = entry.Decision.VerdictHash,
|
||||
ProofRoot = entry.Decision.ProofRoot,
|
||||
ReplaySeed = CanonJson.Serialize(entry.Decision.ReplaySeed, ReplaySeedJsonOptions),
|
||||
PolicyHash = entry.PolicyHash,
|
||||
SignerSetHash = entry.SignerSetHash,
|
||||
FeedEpoch = entry.FeedEpoch,
|
||||
TrustScore = entry.Decision.TrustScore,
|
||||
HitCount = entry.HitCount,
|
||||
CreatedAt = entry.CreatedAt,
|
||||
ExpiresAt = entry.ExpiresAt,
|
||||
UpdatedAt = _timeProvider.GetUtcNow(),
|
||||
LastAccessedAt = entry.LastAccessedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,3 +9,4 @@ Source of truth: `docs-archived/implplan/2025-12-29-csproj-audit/SPRINT_20251229
|
||||
| AUDIT-0099-T | DONE | Revalidated 2026-01-08; test coverage audit for Provcache.Postgres. |
|
||||
| AUDIT-0099-A | DONE | Applied 2026-01-13 (CanonJson replay seeds; test gaps tracked). |
|
||||
| REMED-06 | DONE | SOLID review notes captured for SPRINT_20260130_002. |
|
||||
| REMED-08 | DONE | Split repositories into partials, fixed private field naming, added Postgres repository tests; `dotnet test src/__Libraries/__Tests/StellaOps.Provcache.Postgres.Tests/StellaOps.Provcache.Postgres.Tests.csproj` passed 2026-02-03. |
|
||||
|
||||
Reference in New Issue
Block a user