/// <summary> /// Creates a new transaction log manager. /// </summary> /// <param name="engineId">The engine this transaction log manager is used for. Only used for logging purposes.</param> /// <param name="keyValueStore">The key/value store to write transaction logs to.</param> /// <param name="policy">The serialization policy to use when serializing objects.</param> public TransactionLogManager(Uri engineId, IKeyValueStore keyValueStore, ISerializationPolicy policy) { _engineId = engineId; _keyValueStore = keyValueStore; _policy = policy; _lock = new AsyncLock(); _versionedLogs = new Queue <ITransactionLog>(); _metadataTable = new SerializingKeyValueTable <string>( _keyValueStore.GetTable(MetadataTableName), (str, s) => { using var writer = new BinaryWriter(s); writer.Write(str); }, s => { using var reader = new BinaryReader(s); return(reader.ReadString()); } ); using (var t = _keyValueStore.CreateTransaction()) { var tx = _metadataTable.Enter(t); if (!tx.Contains(ActiveCountKey)) { Invariant.Assert(!tx.Contains(LatestKey) && !tx.Contains(HeldCountKey), "Transaction log versioning keys are only partially populated."); _activeCount = _heldCount = _latest = 0; } else { Invariant.Assert(tx.Contains(LatestKey) && tx.Contains(HeldCountKey), "Transaction log versioning keys are only partially populated."); _activeCount = long.Parse(tx[ActiveCountKey], CultureInfo.InvariantCulture); _heldCount = long.Parse(tx[HeldCountKey], CultureInfo.InvariantCulture); _latest = long.Parse(tx[LatestKey], CultureInfo.InvariantCulture); } } Tracing.Transaction_Log_Initialization(null, _engineId, _latest, _activeCount, _heldCount); for (var i = _latest - _heldCount + 1; i <= _latest; i++) { _versionedLogs.Enqueue(new TransactionLog(_keyValueStore, _policy, i)); } }
private static IKeyValueTable <string, ArtifactOperation> GetArtifactTable(IKeyValueStore keyValueStore, ISerializationPolicy policy, long version, string name) { return(new SerializingKeyValueTable <ArtifactOperation>( keyValueStore.GetTable(GetInternalTableName(version, name)), (o, s) => { using var writer = new TransactionLogOperationWriter(s, policy); writer.WriteHeader(); writer.Save(o); }, s => { using var reader = new TransactionLogOperationReader(s, policy); reader.ReadHeader(); var ret = reader.Load(); reader.ReadFooter(); return ret; } )); }