Path: redb/redb.Core/Providers/Base/ObjectDeleteByIds.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Providers/Base/ObjectDeleteByIds.cs
----
using System.Threading;
using System.Threading.Tasks;
using redb.Core.Data;
using redb.Core.Query;
namespace redb.Core.Providers.Base;
///
/// The delete of objects by ids () and the number of the given objects
/// it deleted. Where the delete's rows-affected misses some (SQLite: a child the _id_parent cascade removed with
/// its parent before the statement reached it), the dialect's
/// counts them in the same transaction, before the delete.
///
internal static class ObjectDeleteByIds
{
///
/// Run again on a deadlock when the delete is the caller's whole unit (no transaction on the context); inside the
/// caller's transaction the deadlock leaves to the caller ().
///
public static Task RunAsync(IRedbContext context, ISqlDialect sql, long[] ids, CancellationToken cancellationToken)
=> DeadlockRetryHelper.ExecuteWithRetryAsync(context, () => RunOnceAsync(context, sql, ids, cancellationToken),
cancellationToken: cancellationToken);
private static async Task RunOnceAsync(IRedbContext context, ISqlDialect sql, long[] ids, CancellationToken cancellationToken)
{
var countSql = sql.ObjectStorage_CountObjectsByIds();
if (countSql == null)
return await context.ExecuteAsync(sql.ObjectStorage_DeleteByIds(), new object[] { ids }, cancellationToken);
var counted = 0;
await context.ExecuteAtomicAsync(async () =>
{
counted = checked((int)await context.ExecuteScalarAsync(countSql, new object[] { ids }, cancellationToken));
await context.ExecuteAsync(sql.ObjectStorage_DeleteByIds(), new object[] { ids }, cancellationToken);
}, cancellationToken);
return counted;
}
}