Start on the inverted index CLI operating modes

This commit is contained in:
Starbeamrainbowlabs 2018-09-10 00:12:00 +01:00
parent b55fdeae34
commit 39d1d6f90d
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
1 changed files with 24 additions and 2 deletions

View File

@ -41,7 +41,9 @@ namespace SearchBoxCLI
{ {
case "s": case "s":
case "source": case "source":
Source = new StreamReader(args[++i]); string sourceFilename = args[++i];
Source = new StreamReader(sourceFilename);
Name = sourceFilename;
break; break;
case "n": case "n":
@ -98,17 +100,37 @@ namespace SearchBoxCLI
private static int HandleInvIndexAdd() private static int HandleInvIndexAdd()
{ {
if (Name == string.Empty) {
Console.Error.WriteLine("Error: The document name must be specified when reading from stdin!");
return 1;
}
if (InvIndexFilepath == string.Empty) {
Console.Error.WriteLine("Error: No inverted index filepath specified.");
return 1;
}
if (IdMapFilepath == string.Empty) {
Console.Error.WriteLine("Error: No id map filepath specified.");
return 1;
}
// --------------------------------------
if (!File.Exists(InvIndexFilepath)) if (!File.Exists(InvIndexFilepath))
File.WriteAllText(InvIndexFilepath, "[]"); File.WriteAllText(InvIndexFilepath, "[]");
if (!File.Exists(IdMapFilepath)) if (!File.Exists(IdMapFilepath))
File.WriteAllText(InvIndexFilepath, "{}"); File.WriteAllText(InvIndexFilepath, "{}");
IdMap idMap = JsonConvert.DeserializeObject<IdMap>(File.ReadAllText(IdMapFilepath)); IdMap idMap = JsonConvert.DeserializeObject<IdMap>(File.ReadAllText(IdMapFilepath));
int newId = idMap.GetId(Name == string.Empty ? Source);
InvertedIndex invertedIndex = JsonConvert.DeserializeObject<InvertedIndex>( InvertedIndex invertedIndex = JsonConvert.DeserializeObject<InvertedIndex>(
File.ReadAllText(InvIndexFilepath) File.ReadAllText(InvIndexFilepath)
); );
Index newIndex = new Index(Source.ReadToEnd()); Index newIndex = new Index(Source.ReadToEnd());
invertedIndex.AddIndex(newIndex); invertedIndex.AddIndex(newId, newIndex);
File.WriteAllText(InvIndexFilepath, JsonConvert.SerializeObject(invertedIndex));
File.WriteAllText(IdMapFilepath, JsonConvert.SerializeObject(idMap));
return 0; return 0;
} }