//
// Copyright (c) PlaceholderCompany. All rights reserved.
//
namespace Jellyfin.Database.Providers.Postgres;
using System;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations;
///
/// Extension methods for managing bulk operation transactions.
///
public static class BulkTransactionExtensions
{
///
/// Executes a bulk operation within a transaction.
///
/// The database context.
/// The bulk operation to execute.
/// Cancellation token.
/// A task representing the asynchronous operation.
public static async Task ExecuteBulkOperationAsync(
this JellyfinDbContext context,
Func operation,
CancellationToken cancellationToken = default)
{
using var transaction = new BulkOperationTransaction(context);
try
{
await transaction.BeginAsync(cancellationToken).ConfigureAwait(false);
await operation().ConfigureAwait(false);
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
}
catch
{
await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
throw;
}
}
///
/// Executes a bulk operation within a transaction and returns a result.
///
/// The type of result returned by the operation.
/// The database context.
/// The bulk operation to execute.
/// Cancellation token.
/// The result of the operation.
public static async Task ExecuteBulkOperationAsync(
this JellyfinDbContext context,
Func> operation,
CancellationToken cancellationToken = default)
{
using var transaction = new BulkOperationTransaction(context);
try
{
await transaction.BeginAsync(cancellationToken).ConfigureAwait(false);
var result = await operation().ConfigureAwait(false);
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
return result;
}
catch
{
await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
throw;
}
}
}