1
0
voyager-api/ScrapperAPI/Repositories/ContentRepository.cs

106 lines
3.8 KiB
C#

using System.IO.Compression;
using Dapper;
using ScrapperAPI.Dtos;
using ScrapperAPI.Interfaces;
using ScrapperAPI.Records;
using ScrapperAPI.Utils;
namespace ScrapperAPI.Repositories;
public sealed class ContentRepository : IContentRepository
{
private readonly IDbConnectionFactory _db;
public ContentRepository(IDbConnectionFactory db) => _db = db;
public async Task<int> SaveAsync(int queueId, string content, CancellationToken ct)
{
var compressed = CompressionUtils.GzipCompressUtf8(content, CompressionLevel.Fastest);
const string sql = """
insert into content(queue_id, content_encoding, content_bytes, original_length, compressed_length)
values (@queueId, 'gzip', @bytes, @origLen, @compLen)
returning id;
""";
using var conn = await _db.CreateOpenConnectionAsync(ct);
return await conn.ExecuteScalarAsync<int>(new CommandDefinition(sql, new
{
queueId,
bytes = compressed,
origLen = content.Length, // chars (ok)
compLen = compressed.Length // bytes
}, cancellationToken: ct));
}
public async Task<int> SaveCompressedAsync(
int queueId,
string contentEncoding,
byte[] contentBytes,
int originalLength,
int compressedLength,
CancellationToken ct)
{
const string sql = """
insert into content(queue_id, content_encoding, content_bytes, original_length, compressed_length)
values (@queueId, @contentEncoding, @bytes, @origLen, @compLen)
returning id;
""";
using var conn = await _db.CreateOpenConnectionAsync(ct);
return await conn.ExecuteScalarAsync<int>(new CommandDefinition(sql, new
{
queueId,
contentEncoding,
bytes = contentBytes,
origLen = originalLength,
compLen = compressedLength
}, cancellationToken: ct));
}
public async Task<ContentRow?> GetByQueueIdAsync(int queueId, CancellationToken ct)
{
const string sql = """
select id, queue_id as QueueId, content, created_date as CreatedDate
from content
where queue_id = @queueId
order by id desc
limit 1;
""";
using var conn = await _db.CreateOpenConnectionAsync(ct);
return await conn.QuerySingleOrDefaultAsync<ContentRow>(
new CommandDefinition(sql, new { queueId }, cancellationToken: ct));
}
public async Task<CompressedContent?> GetCompressedByQueueIdAsync(
int queueId,
CancellationToken ct
)
{
const string sql = """
select
id,
queue_id as QueueId,
content_encoding as ContentEncoding,
content_bytes as ContentBytes,
original_length as OriginalLength,
compressed_length as CompressedLength,
created_date as CreatedDate
from content
where queue_id = @queueId
order by id desc
limit 1;
""";
using var conn = await _db.CreateOpenConnectionAsync(ct);
return await conn.QuerySingleOrDefaultAsync<CompressedContent>(
new CommandDefinition(
sql,
new { queueId },
cancellationToken: ct
)
);
}
}