50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace LibSearchBox
|
|
{
|
|
public class SearchBoxException : Exception { public SearchBoxException(string message) : base(message) { } }
|
|
|
|
public class SearchBox
|
|
{
|
|
public IdMap idMap = new IdMap();
|
|
public InvertedIndex index = new InvertedIndex();
|
|
public ConcurrentDictionary<int, DocumentMeta> metaTable = new ConcurrentDictionary<int, DocumentMeta>();
|
|
|
|
public SearchBox()
|
|
{
|
|
|
|
}
|
|
|
|
public void AddDocument(string title, IEnumerable<string> tags, string content)
|
|
{
|
|
DocumentMeta info = new DocumentMeta(title, tags);
|
|
int id = idMap.GetId(info.Title);
|
|
Index upsideIndex = new Index(content);
|
|
index.AddIndex(id, upsideIndex);
|
|
}
|
|
|
|
public void UpdateDocument(string title, IEnumerable<string> newTags, string oldContent, string newContent)
|
|
{
|
|
int id = idMap.GetId(title);
|
|
DocumentMeta info = metaTable[id];
|
|
info.ReplaceTags(newTags);
|
|
|
|
Index oldIndex = new Index(oldContent), newIndex = new Index(newContent);
|
|
if (!index.ReplaceIndex(id, oldIndex, newIndex))
|
|
throw new Exception($"Error: Failed to replace index for document with title {title}.");
|
|
}
|
|
|
|
public void RemoveDocument(string title)
|
|
{
|
|
int id = idMap.DeletePageName(title);
|
|
metaTable.TryRemove(id, out DocumentMeta noop);
|
|
if (!index.RemoveById(id))
|
|
throw new SearchBoxException($"Failed to remove page with title '{title}' from inverted index.");
|
|
|
|
}
|
|
}
|
|
}
|