Get removing & metaTable removing working

This commit is contained in:
Starbeamrainbowlabs 2018-09-11 16:01:32 +01:00
parent ddd94b3641
commit 7bef5d9b17
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
4 changed files with 42 additions and 14 deletions

View File

@ -6,6 +6,7 @@ using Newtonsoft.Json;
using LibSearchBox; using LibSearchBox;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Newtonsoft.Json.Serialization;
namespace SearchBoxCLI namespace SearchBoxCLI
{ {

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using Stackoverflow.Utilities; using Stackoverflow.Utilities;
@ -8,32 +9,39 @@ 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> MapOut {
public Dictionary<int, string> Map {
get { get {
Dictionary<int, string> result = new Dictionary<int, string>(); Dictionary<int, string> result = new Dictionary<int, string>();
foreach (BiDictionary<int, string>.Pair pair in map) foreach (BiDictionary<int, string>.Pair pair in map)
result.Add(pair.First, pair.Second); result.Add(pair.First, pair.Second);
return result; return result;
} }
set {
map.Clear();
foreach (KeyValuePair<int, string> pair in value)
map.Add(pair.Key, pair.Value);
}
} }
public IdMap() public IdMap()
{ {
} }
public void Import(Dictionary<int, string> inMap, bool clearOld = true) {
// Clear out the old map, if any
if(clearOld) map.Clear();
// Import the new records
foreach (KeyValuePair<int, string> pair in inMap)
map.Add(pair.Key, pair.Value);
// Calculate the next id
// FUTURE: Store & retrieve this via JSON
nextId = map.Max((pair) => pair.First) + 1;
}
public int GetId(string pageName) public int GetId(string pageName)
{ {
// Perform unicode normalization // Perform unicode normalization

View File

@ -29,8 +29,7 @@ namespace LibSearchBox
public bool RemoveIndex(int pageId, Index oldIndex) public bool RemoveIndex(int pageId, Index oldIndex)
{ {
foreach (string token in oldIndex.Tokens()) foreach (string token in oldIndex.Tokens()) {
{
if (!invertedIndex.ContainsKey(token) || !invertedIndex[token].ContainsKey(pageId)) continue; if (!invertedIndex.ContainsKey(token) || !invertedIndex[token].ContainsKey(pageId)) continue;
if (!invertedIndex[token].TryRemove(pageId, out List<int> noop)) if (!invertedIndex[token].TryRemove(pageId, out List<int> noop))
@ -52,6 +51,8 @@ namespace LibSearchBox
if (!pair.Value.ContainsKey(pageId)) continue; if (!pair.Value.ContainsKey(pageId)) continue;
if (!pair.Value.TryRemove(pageId, out List<int> noop)) if (!pair.Value.TryRemove(pageId, out List<int> noop))
return false; return false;
if (pair.Value.Count == 0 && !invertedIndex.TryRemove(pair.Key, out var noopAgain))
return false;
} }
return true; return true;
} }

View File

@ -7,11 +7,28 @@ namespace LibSearchBox
{ {
public class SearchBoxException : Exception { public SearchBoxException(string message) : base(message) { } } public class SearchBoxException : Exception { public SearchBoxException(string message) : base(message) { } }
[JsonObject(MemberSerialization.OptIn)]
public class SearchBox public class SearchBox
{ {
public IdMap idMap = new IdMap(); private IdMap idMap = new IdMap();
public InvertedIndex index = new InvertedIndex();
[JsonProperty("ids")]
public Dictionary<int, string> IdMap {
get {
Dictionary<int, string> result = idMap.MapOut;
if (result.Count == 0) return null;
return result;
}
set {
if (value == null) return;
idMap.Import(value);
}
}
[JsonProperty]
public ConcurrentDictionary<int, DocumentMeta> metaTable = new ConcurrentDictionary<int, DocumentMeta>(); public ConcurrentDictionary<int, DocumentMeta> metaTable = new ConcurrentDictionary<int, DocumentMeta>();
[JsonProperty]
public InvertedIndex index = new InvertedIndex();
public SearchBox() public SearchBox()
{ {
@ -22,6 +39,7 @@ namespace LibSearchBox
{ {
DocumentMeta info = new DocumentMeta(title, tags); DocumentMeta info = new DocumentMeta(title, tags);
int id = idMap.GetId(info.Title); int id = idMap.GetId(info.Title);
metaTable.AddOrUpdate(id, info, (key, oldValue) => info);
Index upsideIndex = new Index(content); Index upsideIndex = new Index(content);
index.AddIndex(id, upsideIndex); index.AddIndex(id, upsideIndex);
} }
@ -34,7 +52,7 @@ namespace LibSearchBox
Index oldIndex = new Index(oldContent), newIndex = new Index(newContent); Index oldIndex = new Index(oldContent), newIndex = new Index(newContent);
if (!index.ReplaceIndex(id, oldIndex, newIndex)) if (!index.ReplaceIndex(id, oldIndex, newIndex))
throw new Exception($"Error: Failed to replace index for document with title {title}."); throw new SearchBoxException($"Error: Failed to replace index for document with title {title}.");
} }
public void RemoveDocument(string title) public void RemoveDocument(string title)