Esempio n. 1
0
        /// <summary>
        /// Reads the operation from the specified position in the log.
        /// </summary>
        /// <param name="resource">The parent <see cref="ITransactedResource" /> responsible for deserializing the operation.</param>
        /// <param name="position">See the <see cref="ILogPosition" />.</param>
        /// <returns>The <see cref="IOperation" /> read from the log.</returns>
        public IOperation Read(ITransactedResource resource, ILogPosition position)
        {
            int        cb;
            long       recEndPos;
            string     description;
            IOperation operation;

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

                file.Position = ((FileLogPosition)position).Position;

                if (file.ReadInt32() != Magic)
                {
                    throw new TransactionException(CorruptMsg);
                }

                cb = file.ReadInt32();
                if (cb <= 0 || cb + file.Position > file.Length)
                {
                    throw new TransactionException(CorruptMsg);
                }

                recEndPos   = file.Position + cb;
                description = file.ReadString32();
                operation   = resource.ReadOperation(file);

                if (file.Position != recEndPos)
                {
                    SysLog.LogWarning("ITransactedResource.ReadOperation() returned with an unexpected stream position.");
                    file.Position = recEndPos;
                }

                return(operation);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the cache index file.
        /// </summary>
        private void LoadIndex()
        {
            var indexPath = Path.Combine(settings.PhraseCacheFolder, IndexFileName);

            try
            {
                if (!File.Exists(indexPath))
                {
                    // No index exists yet, so create an empty one.

                    SaveIndex(false);
                    return;
                }

                using (var fs = new EnhancedFileStream(indexPath, FileMode.Open))
                {
                    int count;

                    if (fs.ReadInt32() != Magic)
                    {
                        throw new SwitchException("[PhraseCache] cannot read index file [{0}] due to an invalid magic number.  The existing cache will be purged.", indexPath);
                    }

                    count = fs.ReadInt32();
                    for (int i = 0; i < count; i++)
                    {
                        string        text;
                        string        voice;
                        string        actualVoice;
                        PhraseType    phraseType;
                        TtsEncoding   encoding;
                        TtsSampleRate rate;
                        DateTime      lastAccessUtc;
                        string        path;
                        Phrase        phrase;

                        phraseType    = (PhraseType)Enum.Parse(typeof(PhraseType), fs.ReadString16());
                        text          = fs.ReadString32();
                        voice         = fs.ReadString16();
                        actualVoice   = fs.ReadString16();
                        encoding      = (TtsEncoding)Enum.Parse(typeof(TtsEncoding), fs.ReadString16());
                        rate          = (TtsSampleRate)Enum.Parse(typeof(TtsSampleRate), fs.ReadString16());
                        lastAccessUtc = new DateTime(fs.ReadInt64());
                        path          = fs.ReadString16();

                        phrase = new Phrase(phraseType, voice, encoding, rate, text)
                        {
                            Path          = path,
                            ActualVoice   = actualVoice,
                            LastAccessUtc = lastAccessUtc,
                        };

                        index[GetCacheKey(phrase)] = phrase;
                    }
                }
            }
            catch (Exception e)
            {
                // We're going to handle all exceptions leaving the loaded phrase
                // table empty.  This will have the effect of starting the cache
                // from scratch.  Eventually, all of the existing files will be
                // purged and the presumably bad index file will be overwritten.

                index.Clear();
                SysLog.LogException(e);
            }
        }