Beispiel #1
0
        private byte[] CombineIndexes(Settings settings, IndexArtifact[] artifacts, Task task)
        {
            var completed      = 0;
            var combineIndexer = new SearchIndexer();
            var indexName      = settings.name.ToLowerInvariant();

            task.Report("Combining indexes...");

            combineIndexer.Start();
            foreach (var a in artifacts)
            {
                if (task.Canceled())
                {
                    return(null);
                }

                task.Report(completed++);

                if (a == null || a.path == null)
                {
                    continue;
                }

                var si = new SearchIndexer();
                if (!si.ReadIndexFromDisk(a.path))
                {
                    continue;
                }

                if (task.Canceled())
                {
                    return(null);
                }

                combineIndexer.CombineIndexes(si, baseScore: settings.baseScore,
                                              (di, indexer) => indexer.AddProperty("a", indexName, di, saveKeyword: true, exact: true));
            }

            if (task.Canceled())
            {
                return(null);
            }

            task.Report($"Sorting {combineIndexer.indexCount} indexes...");

            if (task.async)
            {
                combineIndexer.Finish((bytes) => task.Resolve(new TaskData(bytes, combineIndexer)), null, saveBytes: true);
            }
            else
            {
                combineIndexer.Finish(removedDocuments: null);
                return(combineIndexer.SaveBytes());
            }

            return(null);
        }
        private void CombineIndexes(Settings settings, IndexArtifact[] artifacts, Task task, bool autoResolve)
        {
            task.Report("Combining indexes...", -1f);

            var completed       = 0;
            var searchArtifacts = new ConcurrentBag <SearchIndexer>();
            var tloop           = Parallel.ForEach(artifacts, (a, state) =>
            {
                if (task.Canceled())
                {
                    state.Stop();
                    return;
                }

                if (a == null || a.path == null)
                {
                    return;
                }

                var si = new SearchIndexer(Path.GetFileName(a.source));
                task.Report($"Reading {si.name}...");
                if (!si.ReadIndexFromDisk(a.path))
                {
                    return;
                }

                if (task.Canceled())
                {
                    return;
                }

                searchArtifacts.Add(si);
                task.Report(completed++, artifacts.Length);
            });

            if (!tloop.IsCompleted)
            {
                task.Cancel();
            }

            if (task.Canceled())
            {
                return;
            }

            // Combine all search index artifacts into one large binary stream.
            var combineIndexer = new SearchIndexer();
            var artifactCount  = searchArtifacts.Count;
            var indexName      = settings.name.ToLowerInvariant();

            task.Report($"Combining...");
            combineIndexer.Start();
            combineIndexer.CombineIndexes(searchArtifacts, settings.baseScore, indexName, (progress, name) =>
            {
                task.Report($"Combining {name}...");
                task.Report(progress, artifactCount);
            });

            task.Report($"Sorting {combineIndexer.indexCount} indexes...", -1f);
            byte[] bytes = autoResolve ? combineIndexer.SaveBytes() : null;
            Dispatcher.Enqueue(() => task.Resolve(new TaskData(bytes, combineIndexer), completed: autoResolve));
        }