Exemple #1
0
        public DictionaryLoader(string dictionaryName, PortableFS portableFS, bool buildThumbs)
        {
            DictionaryName = dictionaryName;
            FS             = portableFS;
            BuildThumbs    = buildThumbs;

            if (LoadTaskCanceller != null)
            {
                LoadTaskCanceller.Cancel();
            }

            WordDictionary.Loading.IsCompleted = false;
            WordDictionary.Loading.DictionaryLoadedNotifier.Reset();

            LoadTaskCanceller = new CancellationTokenSource();
            LoadTask          = Task.Run(LoadDictionaryTask, LoadTaskCanceller.Token);

            Debug.WriteLine("Loading dictionary " + dictionaryName);
        }
Exemple #2
0
 public BinDictionaryLoader(string dictionaryName, PortableFS portableFS, bool buildThumbs = true) : base(dictionaryName, portableFS, buildThumbs)
 {
 }
Exemple #3
0
        public static async Task WriteDictionary(string dictionaryName, PortableFS portableFS)
        {
            const int sliceSize     = 20000;
            int       jobSliceStart = 0;
            int       jobSliceEnd   = sliceSize;

            List <Task <MemoryStream> > dictionaryWriteJobs = new List <Task <MemoryStream> >();

            while (jobSliceStart < WordDictionary.Words.Count)
            {
                BinDictionaryWriteJob dictionaryWriteJob = new BinDictionaryWriteJob(jobSliceStart, Math.Min(jobSliceEnd, WordDictionary.Words.Count));

                dictionaryWriteJobs.Add(Task.Run((Func <MemoryStream>)dictionaryWriteJob.WriteDictionaryPart));

                jobSliceStart = jobSliceEnd;
                jobSliceEnd  += sliceSize;
            }

            Task.WaitAll(dictionaryWriteJobs.ToArray());

            Stream stream = await portableFS.GetFileWriteStream(Path.Combine("Dictionaries", dictionaryName + ".otakudict"));

            BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);

            // version
            writer.Write((int)3);
            // number of parts
            writer.Write((int)dictionaryWriteJobs.Count);

            // size of each part
            foreach (Task <MemoryStream> dictionaryWriteJob in dictionaryWriteJobs)
            {
                writer.Write((int)dictionaryWriteJob.Result.Length);
            }

            // copy parts
            foreach (Task <MemoryStream> dictionaryWriteJob in dictionaryWriteJobs)
            {
                dictionaryWriteJob.Result.Seek(0, SeekOrigin.Begin);
                dictionaryWriteJob.Result.CopyTo(stream);
                dictionaryWriteJob.Result.Dispose();
            }

            // make sure we're at end
            writer.Seek(0, SeekOrigin.End);

            writer.Write((int)WordDictionary.IndexedWords.Length);
            writer.Write((int)WordDictionary.IndexedWordMatches.Length);

            // copy index
            foreach (IndexedWord indexedWord in WordDictionary.IndexedWords)
            {
                writer.Write(indexedWord.IndexedWordStr);
                writer.Write((ulong)indexedWord.LetterMask);
                writer.Write((int)indexedWord.MatchesCount);
            }

            foreach (int wordMatch in WordDictionary.IndexedWordMatches)
            {
                writer.Write(wordMatch);
            }

            stream.Flush();
            stream.Dispose();
        }