Ejemplo n.º 1
0
        /// <summary>
        /// Writes an <see cref="IOperation" /> to the log.
        /// </summary>
        /// <param name="resource">The parent <see cref="ITransactedResource" /> responsible for serializing the operation.</param>
        /// <param name="operation">The operation to be written.</param>
        /// <remarks>
        /// <note>
        /// This property can only be called if the operation log is in <see cref="OperationLogMode.Undo" />
        /// mode.
        /// </note>
        /// </remarks>
        /// <exception cref="TransactionException">Thrown if the log isn't open or if the mode isn't <see cref="OperationLogMode.Undo" />.</exception>
        public void Write(ITransactedResource resource, IOperation operation)
        {
            long cbPos;
            long cb;

            using (TimedLock.Lock(this))
            {
                if (file == null)
                {
                    throw new TransactionException(ClosedMsg);
                }

                if (mode != OperationLogMode.Undo)
                {
                    throw new TransactionException("Write is available only when the log is in UNDO mode.");
                }

                file.WriteInt32(Magic);                     // Magic number
                cbPos = file.Position;                      // Length place holder
                file.WriteInt32(0);
                file.WriteString32(operation.Description);  // Description
                resource.WriteOperation(file, operation);   // Serialized operation

                cb = file.Position - cbPos - 4;
                if (cb < 0 || cb > int.MaxValue)
                {
                    throw new TransactionException("ITransactedResource.WriteOperation() returned with an unexpected stream position.");
                }

                file.Position = cbPos;
                file.WriteInt32((int)cb);
                file.Position = file.Length;
                file.Flush();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes the index file.
        /// </summary>
        /// <param name="path">The target file path.</param>
        /// <param name="phrases">The phrases to be written.</param>
        private void SaveIndexTo(string path, Phrase[] phrases)
        {
            using (var fs = new EnhancedFileStream(path, FileMode.Create))
            {
                fs.WriteInt32(Magic);
                fs.WriteInt32(phrases.Length);

                foreach (var phrase in phrases)
                {
                    fs.WriteString16(phrase.PhraseType.ToString());
                    fs.WriteString32(phrase.Text);
                    fs.WriteString16(phrase.Voice);
                    fs.WriteString16(phrase.ActualVoice);
                    fs.WriteString16(phrase.Encoding.ToString());
                    fs.WriteString16(phrase.SampleRate.ToString());
                    fs.WriteInt64(phrase.LastAccessUtc.Ticks);
                    fs.WriteString16(phrase.Path);
                }
            }
        }