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>(); List<string> extras = new List<string>();
for (int i = 0; i < args.Length; i++) 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 "s":
case "source": case "source":
string sourceFilename = args[++i]; string sourceFilename = args[++i];
@ -79,7 +83,7 @@ namespace SearchBoxCLI
} }
if (extras.Count < 1) return HandleHelp(); if (extras.Count < 1) return HandleHelp();
string modeText = extras.First(); extras.RemoveAt(0); string modeText = extras.First(); extras.RemoveAt(0);
Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText); Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText, true);
switch (Mode) { switch (Mode) {
case OperatingModes.Index: return HandleIndex(); 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!"); Console.Error.WriteLine("Error: The document name must be specified when reading from stdin!");
return 1; 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 class DocumentMeta
{ {
public string Title { get; set; } 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) public DocumentMeta(string inTitle, IEnumerable<string> inTags)
{ {
Title = inTitle; Title = inTitle;
Tags = new List<string>(inTags); if (inTags != null)
Tags.AddRange(inTags);
} }
public void ReplaceTags(IEnumerable<string> newTags) public void ReplaceTags(IEnumerable<string> newTags)
@ -19,5 +20,15 @@ namespace LibSearchBox
Tags.Clear(); Tags.Clear();
Tags.AddRange(newTags); 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Newtonsoft.Json;
using Stackoverflow.Utilities; using Stackoverflow.Utilities;
namespace LibSearchBox namespace LibSearchBox
{ {
public class IdNotFoundException : Exception { public IdNotFoundException(string message) : base(message) { } } public class IdNotFoundException : Exception { public IdNotFoundException(string message) : base(message) { } }
[JsonObject(MemberSerialization.OptIn)]
public class IdMap public class IdMap
{ {
[JsonProperty]
private int nextId = 0; private int nextId = 0;
public BiDictionary<int, string> map = new BiDictionary<int, string>(); 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() public IdMap()
{ {
} }

View file

@ -6,7 +6,8 @@ namespace LibSearchBox
{ {
public class InvertedIndex 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() public InvertedIndex()
{ {

View file

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