Start on the inverted index CLI operating modes
This commit is contained in:
parent
b55fdeae34
commit
39d1d6f90d
1 changed files with 24 additions and 2 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue