using System; using System.Collections.Concurrent; using System.Collections.Generic; namespace LibSearchBox { public class SearchBoxException : Exception { public SearchBoxException(string message) : base(message) { } } public class SearchBox { private IdMap idMap = new IdMap(); private InvertedIndex index = new InvertedIndex(); private ConcurrentDictionary metaTable = new ConcurrentDictionary(); public SearchBox() { } public void AddDocument(string title, IEnumerable 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 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."); } } }