Get adding to a search index working

This commit is contained in:
Starbeamrainbowlabs 2018-09-11 14:47:24 +01:00
parent 959aa219ce
commit ddd94b3641
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
5 changed files with 48 additions and 9 deletions

View file

@ -38,8 +38,12 @@ namespace SearchBoxCLI
List<string> extras = new List<string>();
for (int i = 0; i < args.Length; i++)
{
switch (args[i].TrimStart("-".ToCharArray()))
{
if (!args[i].StartsWith("-")) {
extras.Add(args[i]);
continue;
}
switch (args[i].TrimStart("-".ToCharArray())) {
case "s":
case "source":
string sourceFilename = args[++i];
@ -79,7 +83,7 @@ namespace SearchBoxCLI
}
if (extras.Count < 1) return HandleHelp();
string modeText = extras.First(); extras.RemoveAt(0);
Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText);
Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText, true);
switch (Mode) {
case OperatingModes.Index: return HandleIndex();
@ -126,6 +130,10 @@ namespace SearchBoxCLI
Console.Error.WriteLine("Error: The document name must be specified when reading from stdin!");
return 1;
}
if (SearchIndexFilepath == string.Empty) {
Console.Error.WriteLine("Error: No search index file path specified.");
return 1;
}
// --------------------------------------

View file

@ -6,12 +6,13 @@ namespace LibSearchBox
public class DocumentMeta
{
public string Title { get; set; }
public List<string> Tags { get; private set; }
public List<string> Tags { get; private set; } = new List<string>();
public DocumentMeta(string inTitle, IEnumerable<string> inTags)
{
Title = inTitle;
Tags = new List<string>(inTags);
if (inTags != null)
Tags.AddRange(inTags);
}
public void ReplaceTags(IEnumerable<string> newTags)
@ -19,5 +20,15 @@ namespace LibSearchBox
Tags.Clear();
Tags.AddRange(newTags);
}
#region Overrides
public override string ToString()
{
return $"[DocumentMeta Title={Title}, Tags={string.Join(",", Tags)}]";
}
#endregion
}
}

View file

@ -1,17 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Stackoverflow.Utilities;
namespace LibSearchBox
{
public class IdNotFoundException : Exception { public IdNotFoundException(string message) : base(message) { } }
[JsonObject(MemberSerialization.OptIn)]
public class IdMap
{
[JsonProperty]
private int nextId = 0;
public BiDictionary<int, string> map = new BiDictionary<int, string>();
[JsonProperty]
public Dictionary<int, string> Map {
get {
Dictionary<int, string> result = new Dictionary<int, string>();
foreach (BiDictionary<int, string>.Pair pair in map)
result.Add(pair.First, pair.Second);
return result;
}
set {
map.Clear();
foreach (KeyValuePair<int, string> pair in value)
map.Add(pair.Key, pair.Value);
}
}
public IdMap()
{
}

View file

@ -6,7 +6,8 @@ namespace LibSearchBox
{
public class InvertedIndex
{
private ConcurrentDictionary<string, ConcurrentDictionary<int, List<int>>> invertedIndex = new ConcurrentDictionary<string, ConcurrentDictionary<int, List<int>>>();
// Gotta be public to allow Newtonsoft.JSON to do it's job. Don't interact with it directly!
public ConcurrentDictionary<string, ConcurrentDictionary<int, List<int>>> invertedIndex = new ConcurrentDictionary<string, ConcurrentDictionary<int, List<int>>>();
public InvertedIndex()
{

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace LibSearchBox
{
@ -8,9 +9,9 @@ namespace LibSearchBox
public class SearchBox
{
private IdMap idMap = new IdMap();
private InvertedIndex index = new InvertedIndex();
private ConcurrentDictionary<int, DocumentMeta> metaTable = new ConcurrentDictionary<int, DocumentMeta>();
public IdMap idMap = new IdMap();
public InvertedIndex index = new InvertedIndex();
public ConcurrentDictionary<int, DocumentMeta> metaTable = new ConcurrentDictionary<int, DocumentMeta>();
public SearchBox()
{