Path: redb/redb.Core/Data/RedbKeyGeneratorBase.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/Data/RedbKeyGeneratorBase.cs
----
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace redb.Core.Data
{
///
/// Domain-isolated key cache data.
///
internal class KeyCacheDomain
{
public readonly ConcurrentQueue Keys = new();
public readonly SemaphoreSlim Lock = new(1, 1);
public volatile bool IsRefilling = false;
// The highest id this process has handed out for the domain, whatever the outcome of the transaction that
// took it. A provider whose sequence rolls back with the caller's transaction (SQLite) never hands out an id
// at or below it again.
public long IssuedHighWater;
}
///
/// Base class for key generation with domain-isolated caching.
/// Each domain (typically one per database connection) has isolated cache.
/// Instance is bound to specific domain, static data is shared.
///
public abstract class RedbKeyGeneratorBase : IKeyGenerator
{
// === STATIC: shared data across all instances, isolated by domain ===
private static readonly ConcurrentDictionary _domains = new();
private static int _cacheSize = 10000;
private const double REFILL_THRESHOLD = 0.1; // 10% of cache size
// === INSTANCE: domain binding ===
private readonly string _domain;
///
/// Cache domain this instance is bound to.
///
public string Domain => _domain;
protected RedbKeyGeneratorBase(string? domain = null)
{
_domain = domain ?? "default";
}
private KeyCacheDomain GetCache() => _domains.GetOrAdd(_domain, _ => new KeyCacheDomain());
// === ABSTRACT METHODS (DB-specific) ===
///
/// Generate batch of keys from database sequence.
/// Only batch generation - no single key methods!
///
protected abstract Task> GenerateKeysAsync(int count);
///
/// Provider hook for a refill that must not use a SEPARATE connection, bypassing the shared cache:
///
/// - SQLite single-writer trap: when the calling scope already holds a write transaction
/// (BEGIN IMMEDIATE), a refill on another connection waits for the file's only write lock while
/// the lock holder waits for the refill. Keys come from the scope's transaction instead; a
/// rollback takes the sequence bump back, while the application may still hold the ids, so the
/// provider takes every later range above .
/// - MSSQL and PostgreSQL inside a TransactionScope: another connection would be a second
/// connection in the transaction (a distributed transaction). Keys come through the scope's
/// wrapper on the transaction's connection; sequences are not transactional, so a rollback leaves
/// a gap, never a duplicate.
///
/// Default: null - the refill uses its own connection.
///
protected virtual Task?> TryGenerateKeysInAmbientTransactionAsync(int count)
=> Task.FromResult?>(null);
///
/// The highest id this process has handed out for this domain (), 0 before the first.
/// A provider whose sequence bump can roll back with the caller's transaction takes every next range above it:
/// an id a rolled-back transaction handed out may still be held by the application, and a second owner of the
/// same id would overwrite the first one's row.
///
protected long IssuedHighWater => Interlocked.Read(ref GetCache().IssuedHighWater);
/// Raises to , the highest id of a range just handed out.
protected void RecordIssued(long top)
{
var cache = GetCache();
var current = Interlocked.Read(ref cache.IssuedHighWater);
while (top > current)
{
var seen = Interlocked.CompareExchange(ref cache.IssuedHighWater, top, current);
if (seen == current)
return;
current = seen;
}
}
// === PUBLIC API ===
///
/// Get next object ID (uses shared static cache).
///
// ct is honoured at the entry only: the refill below feeds a SHARED static cache, and a
// caller's token must not abort a refill other callers are waiting on. The refill query
// itself is a single short statement.
public async Task NextObjectIdAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return await GetNextKeyAsync();
}
///
/// Get next value ID (uses shared static cache).
///
public async Task NextValueIdAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return await GetNextKeyAsync();
}
///
/// Get batch of object IDs.
///
public async Task NextObjectIdBatchAsync(int count, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return await GetNextKeyBatchAsync(count);
}
///
/// Get batch of value IDs.
///
public async Task NextValueIdBatchAsync(int count, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return await GetNextKeyBatchAsync(count);
}
// === CORE CACHE LOGIC ===
private async Task GetNextKeyAsync()
{
var cache = GetCache();
// Try to get from cache
if (cache.Keys.TryDequeue(out long key))
{
// Check if background refill needed (< threshold)
int currentCount = cache.Keys.Count;
int threshold = (int)(_cacheSize * REFILL_THRESHOLD);
if (currentCount <= threshold && !cache.IsRefilling)
{
// Fire-and-forget: background thread with separate connection
StartBackgroundRefill();
}
return key;
}
// Cache empty. If this scope sits inside its own write transaction, a refill on a
// separate connection would self-deadlock (SQLite single writer) - take the key
// through the ambient transaction instead, bypassing the shared cache.
var ambient = await TryGenerateKeysInAmbientTransactionAsync(1);
if (ambient is { Count: > 0 })
return ambient[0];
// Cache empty - WAIT until fully refilled
await RefillCacheBlockingAsync();
// Must have keys now
cache.Keys.TryDequeue(out key);
return key;
}
private async Task GetNextKeyBatchAsync(int count)
{
if (count <= 0) return Array.Empty();
var cache = GetCache();
var result = new long[count];
int collected = 0;
while (collected < count)
{
// Try to get from cache
if (cache.Keys.TryDequeue(out long key))
{
result[collected++] = key;
// Check if background refill needed
int currentCount = cache.Keys.Count;
int threshold = (int)(_cacheSize * REFILL_THRESHOLD);
if (currentCount <= threshold && !cache.IsRefilling)
{
StartBackgroundRefill();
}
}
else
{
// Same single-writer trap as in GetNextKeyAsync: inside the scope's own
// transaction the remainder comes through it, bypassing the shared cache.
var ambient = await TryGenerateKeysInAmbientTransactionAsync(count - collected);
if (ambient is { Count: > 0 })
{
foreach (var ambientKey in ambient)
result[collected++] = ambientKey;
continue;
}
// Cache empty - wait for refill
await RefillCacheBlockingAsync();
}
}
return result;
}
// === CACHE REFILL ===
///
/// Starts the background refill WITHOUT the caller's ExecutionContext. An ambient TransactionScope of the
/// caller would otherwise flow into it: the refill would open its connection inside the caller's
/// transaction (a second connection - a distributed transaction) or after that scope completed.
///
private void StartBackgroundRefill()
{
using (System.Threading.ExecutionContext.SuppressFlow())
{
_ = Task.Run(async () => await RefillCacheBackgroundAsync());
}
}
///
/// Background refill - runs in separate thread with separate DB connection.
/// Called when cache is below threshold but not empty.
///
private async Task RefillCacheBackgroundAsync()
{
var cache = GetCache();
// Non-blocking check
if (cache.IsRefilling)
return;
if (!await cache.Lock.WaitAsync(0))
return;
try
{
cache.IsRefilling = true;
int currentCount = cache.Keys.Count;
int keysToGenerate = _cacheSize - currentCount;
if (keysToGenerate <= 0)
return;
// Separate DB connection in background thread
var keys = await GenerateKeysAsync(keysToGenerate);
foreach (var newKey in keys)
{
cache.Keys.Enqueue(newKey);
}
}
finally
{
cache.IsRefilling = false;
cache.Lock.Release();
}
}
///
/// Blocking refill - waits until cache is fully filled.
/// Called when cache is EMPTY - must wait for keys.
///
private async Task RefillCacheBlockingAsync()
{
var cache = GetCache();
// Wait for semaphore (blocks until we can refill)
await cache.Lock.WaitAsync();
try
{
// Double-check if another thread filled while we waited
if (cache.Keys.Count > 0)
return;
cache.IsRefilling = true;
// Generate full batch
var keys = await GenerateKeysAsync(_cacheSize);
foreach (var newKey in keys)
{
cache.Keys.Enqueue(newKey);
}
}
finally
{
cache.IsRefilling = false;
cache.Lock.Release();
}
}
// === CONFIGURATION ===
///
/// Set cache size (default 10000).
///
public static void SetCacheSize(int size)
{
_cacheSize = size;
}
///
/// Clear cache for this domain.
///
public void ClearCache()
{
var cache = GetCache();
while (cache.Keys.TryDequeue(out _)) { }
}
///
/// Clear cache for all domains (for testing/reset).
///
public static void ClearAllCaches()
{
foreach (var domain in _domains.Values)
{
while (domain.Keys.TryDequeue(out _)) { }
}
}
}
}