A standalone full-text search engine written in C#.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.6 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using SearchBox;
namespace SearchBoxCLI
{
enum OperatingModes
{
Query,
Index,
InvertedIndexAdd,
InvertedIndexRemove,
InvertedIndexUpdate
}
enum OutputModes
{
Json,
Text
}
class MainClass {
private static OperatingModes Mode = OperatingModes.Query;
private static string Name = string.Empty;
private static string InvIndexFilepath = string.Empty;
private static string IdMapFilepath = string.Empty;
private static TextReader Source = Console.In;
private static OutputModes OutputMode = OutputModes.Json;
public static int Main(string[] args)
{
List<string> extras = new List<string>();
for (int i = 0; i < args.Length; i++)
{
switch (args[i].TrimStart("-".ToCharArray()))
{
case "s":
case "source":
Source = new StreamReader(args[++i]);
break;
case "n":
case "name":
Name = args[++i];
break;
case "invindex":
InvIndexFilepath = args[++i];
break;
case "idmap":
break;
case "help":
Console.WriteLine("SearchBox");
Console.WriteLine("---------");
Console.WriteLine("A standalone full-text search engine.");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" ./SearchBox.exe {mode} [options]");
Console.WriteLine();
Console.WriteLine("Modes:");
Console.WriteLine(" query Query a pre-existing inverted search index");
Console.WriteLine(" index Generate a raw index of the source document.");
Console.WriteLine(" add Add a named document to an inverted search index.");
Console.WriteLine(" remove Remove a named document from an inverted search index.");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --source, -s Specifies the source document {index, add}");
Console.WriteLine(" --name, -n Sets the name of the source document {add, remove}");
Console.WriteLine(" --idmap, -i Specifies the location of the id map, which is used to map document names onto their nuemical ids {add, remove}");
Console.WriteLine(" --invindex Specifies the location of the inverted search index to use {add, remove}");
Console.WriteLine(" ");
break;
default:
Console.Error.WriteLine($"Error: Unknown property {args[i]}.");
return 1;
}
}
string modeText = extras.First(); extras.RemoveAt(0);
Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText);
switch (Mode) {
case OperatingModes.Index: return HandleIndex();
case OperatingModes.InvertedIndexAdd: return HandleInvIndexAdd();
}
return 0;
}
private static int HandleInvIndexAdd()
{
if (!File.Exists(InvIndexFilepath))
File.WriteAllText(InvIndexFilepath, "[]");
if (!File.Exists(IdMapFilepath))
File.WriteAllText(InvIndexFilepath, "{}");
IdMap idMap = JsonConvert.DeserializeObject<IdMap>(File.ReadAllText(IdMapFilepath));
InvertedIndex invertedIndex = JsonConvert.DeserializeObject<InvertedIndex>(
File.ReadAllText(InvIndexFilepath)
);
Index newIndex = new Index(Source.ReadToEnd());
invertedIndex.AddIndex(newIndex);
return 0;
}
public static int HandleIndex()
{
Index index = new Index(Source.ReadToEnd());
switch (OutputMode)
{
case OutputModes.Json:
Console.WriteLine(JsonConvert.SerializeObject(index));
break;
case OutputModes.Text:
Console.WriteLine(index);
break;
}
return 0;
}
}
}