コード例 #1
0
ファイル: Program.cs プロジェクト: Perf-Org-5KRepos/bion
        private static void Search(string filePath, String8 term)
        {
            int iterations = 1;
            int matchCount = 0;

            string       outputPath = Path.ChangeExtension(filePath, ".search.json");
            BionSearcher searcher   = null;

            try
            {
                using (new ConsoleWatch($"Loading {filePath}..."))
                {
                    searcher = new BionSearcher(filePath, 3);
                }

                using (new ConsoleWatch($"Finding \"{term}\" [{iterations:n0}x]...",
                                        () => $"Done. Wrote {matchCount:n0} matches to {outputPath}"))
                {
                    for (int iteration = 0; iteration < iterations; ++iteration)
                    {
                        ISearchResult result = searcher.Find(term);
                        using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(outputPath)))
                        {
                            writer.Formatting = Formatting.Indented;
                            writer.WriteStartArray();

                            matchCount = searcher.Write(writer, result, 0, -1);

                            writer.WriteEndArray();
                        }
                    }
                }
            }
            finally
            {
                searcher?.Dispose();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Perf-Org-5KRepos/bion
        private static void SearchAll(string bionRootPath)
        {
            List <BionSearcher> searchers = new List <BionSearcher>();

            try
            {
                using (new ConsoleWatch($"Loading Indices under {bionRootPath}...",
                                        () => $"Done; {searchers.Count:n0} loaded"))
                {
                    foreach (string bionFilePath in Directory.GetFiles(bionRootPath, "*.bion", SearchOption.AllDirectories))
                    {
                        BionSearcher searcher = new BionSearcher(bionFilePath, 4);

                        lock (searchers)
                        {
                            searchers.Add(searcher);
                        }
                    }
                }

                int    searchCount = 0;
                int    resultLimit = 100;
                string outputRoot  = "SearchOut";
                byte[] buffer      = null;

                string outputPath = null;
                int    matchCount = 0;

                Directory.CreateDirectory(outputRoot);

                while (true)
                {
                    System.Console.Write("> ");
                    string query = System.Console.ReadLine().Trim();
                    if (String.IsNullOrEmpty(query))
                    {
                        break;
                    }

                    String8 term = String8.Copy(query, ref buffer);

                    matchCount = 0;
                    searchCount++;
                    outputPath = Path.Combine(outputRoot, $"Results.{searchCount}.json");

                    using (new ConsoleWatch("", () => $"Done. {matchCount:n0} matches to {outputPath}"))
                    {
                        using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(outputPath)))
                        {
                            writer.Formatting = Formatting.Indented;
                            writer.WriteStartArray();

                            for (int i = 0; i < searchers.Count; ++i)
                            {
                                ISearchResult result = searchers[i].Find(term);
                                matchCount += searchers[i].Write(writer, result, 0, resultLimit - matchCount);

                                if (matchCount >= resultLimit)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                foreach (BionSearcher searcher in searchers)
                {
                    searcher.Dispose();
                }
            }
        }