Example #1
0
        /// <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));
            }
        }
 /// <summary>
 /// Creates a new serializing key value table using the specified underlying table and the specified serialization and deserialization functions.
 /// </summary>
 /// <param name="table">The underlying table, storing values of type <c>byte[]</c>.</param>
 /// <param name="serialize">The function to serialize a value of type <typeparamref name="TValue"/>.</param>
 /// <param name="deserialize">The function to deserialize a value of type <typeparamref name="TValue"/>.</param>
 public SerializingKeyValueTable(IKeyValueTable <string, byte[]> table, Action <TValue, Stream> serialize, Func <Stream, TValue> deserialize)
 {
     _table       = table;
     _serialize   = serialize;
     _deserialize = deserialize;
 }