Esempio n. 1
0
        public Directory CreateIndex()
        {
            if (IsLoaded)
            {
                throw new InvalidOperationException();
            }

            IsLoading = true;

            var index = new RAMDirectory();

            IReadOnlyList <(string UserField, string Language)> tasks =
                _adapter.GetUserFields()
                .Where(_adapter.IsIndexedInSpellchecker)
                .SelectMany(f => _adapter.GetFieldLanguages(f).Select(l => (f, l)))
                .ToReadOnlyList();

            TotalFields = tasks.Count;
            var indexedWordsByField = new Dictionary <string, HashSet <string> >(Str.Comparer);

            void getContentToIndex((string UserField, string Language) task)
            {
                var values = _adapter.IsStoredInSpellchecker(task.UserField, task.Language)
                                        ? GetObjectsToIndex().SelectMany(c =>
                                                                         _adapter.GetSpellcheckerValues(c, task.UserField, task.Language))
                                        : GetValuesCache(task.UserField, task.Language);

                var spellcheckerField = _adapter.GetSpellcheckerFieldIn(task.UserField, task.Language);

                HashSet <string> indexedWords;

                lock (indexedWordsByField)
                {
                    if (!indexedWordsByField.TryGetValue(spellcheckerField, out indexedWords))
                    {
                        indexedWords = new HashSet <string>(Str.Comparer);
                        indexedWordsByField.Add(spellcheckerField, indexedWords);
                    }
                }

                lock (indexedWords)
                    foreach (string value in values)
                    {
                        indexedWords.Add(value);
                    }
            }

            IndexUtils.ForEach(tasks, getContentToIndex);

            TotalFields    = indexedWordsByField.Count;
            _indexedFields = 0;

            _spellchecker.BeginIndex(index);

            IndexUtils.ForEach(
                indexedWordsByField,
                indexField,
                suppressParallelism: true);

            if (_abort)
            {
                return(null);
            }

            _spellchecker.EndIndex();

            IsLoading = false;
            IndexingProgress?.Invoke();

            IsLoaded = true;

            return(index);
        }