Path: redb/redb.Core/Providers/Base/ObjectStorageProviderBase.StaleKeyCache.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/ObjectStorageProviderBase.StaleKeyCache.cs ---- using System; using System.Collections.Generic; using redb.Core.Data; using redb.Core.Models.Contracts; namespace redb.Core.Providers.Base { public abstract partial class ObjectStorageProviderBase { /// /// An insert of a save failed on the primary key of _objects or _values with an id the key cache /// handed out: value rows always take theirs from the cache, objects only when the save gave them their id. The /// cached ids predate rows the database got since, typically by a restore or an import while this application /// kept running. /// private static bool IsStaleKeyCache(Exception ex, bool createdObjects) { if (!DbErrorClassifier.IsObjectOrValueKeyViolation(ex)) return false; var text = DbErrorClassifier.DescribeUniqueViolation(ex).ConstraintName ?? ""; var onValues = text.Contains("values", StringComparison.OrdinalIgnoreCase); return onValues || createdObjects; } /// /// Drops the cached key block of this database, so the next save takes a fresh range from the sequence, and /// gives the objects of the failed save their id 0 back, so saving them again creates them (an id kept from /// the failed save would address the row that has it). No retry: the error leaves, the caller saves again. /// private Exception StaleKeyCacheFailure(Exception ex, IEnumerable created) { (_context.Keys as RedbKeyGeneratorBase)?.ClearCache(); foreach (var obj in created) obj.Id = 0; return new InvalidOperationException( "The save was not written: an id this application took from its key cache is already used in the " + "database. The cached ids predate rows the database got since, typically by a restore or an import " + "into a database a running application uses. The key cache of this database was reset; save again. " + "Restart applications that use a database after restoring or importing into it.", ex); } } }