Ejemplo n.º 1
0
        public void CommitTransaction(ICollection <PersistentQueueOperation> operations)
        {
            if (operations.Count == 0)
            {
                return;
            }

            byte[] transactionBuffer = GenerateTransactionBuffer(operations);

            lock (_transactionLogLock)
            {
                long txLogSize;
                using (FileStream stream = WaitForTransactionLog(transactionBuffer))
                {
                    stream.Write(transactionBuffer, 0, transactionBuffer.Length);
                    txLogSize = stream.Position;
                    stream.Flush();
                }

                ApplyTransactionOperations(operations);
                TrimTransactionLogIfNeeded(txLogSize);

                PersistentQueueUtils.Write(Meta, stream =>
                {
                    byte[] bytes = BitConverter.GetBytes(CurrentFileNumber);
                    stream.Write(bytes, 0, bytes.Length);
                    bytes = BitConverter.GetBytes(CurrentFilePosition);
                    stream.Write(bytes, 0, bytes.Length);
                });

                FlushTrimmedTransactionLog();
            }
        }
Ejemplo n.º 2
0
        private void FlushTrimmedTransactionLog()
        {
            byte[] transactionBuffer;
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(PersistentQueueUtils.StartTransactionSeparator, 0, PersistentQueueUtils.StartTransactionSeparator.Length);

                byte[] count = BitConverter.GetBytes(CurrentCountOfItemsInQueue);
                ms.Write(count, 0, count.Length);

                PersistentQueueEntry[] checkedOut;
                lock (_checkedOutEntries)
                {
                    checkedOut = _checkedOutEntries.ToArray();
                }

                foreach (var entry in checkedOut)
                {
                    WriteEntryToTransactionLog(ms, entry, PersistentQueueOperationTypes.ENQUEUE);
                }

                PersistentQueueEntry[] listedEntries;
                lock (_entries)
                {
                    listedEntries = _entries.ToArray();
                }

                foreach (var entry in listedEntries)
                {
                    // TODO Use Jobs here
                    WriteEntryToTransactionLog(ms, entry, PersistentQueueOperationTypes.ENQUEUE);
                }

                ms.Write(PersistentQueueUtils.EndTransactionSeparator, 0, PersistentQueueUtils.EndTransactionSeparator.Length);
                ms.Flush();
                transactionBuffer = ms.ToArray();
            }

            PersistentQueueUtils.Write(TransactionLog, stream =>
            {
                stream.SetLength(transactionBuffer.Length);
                stream.Write(transactionBuffer, 0, transactionBuffer.Length);
            });
        }