public void ReplaceDocument() { // This test checks that IndexStorer properly discards the old document when a new copy is stored, // even when the title and date/time are different IInMemoryIndex index = MockInMemoryIndex(); DateTime dt1 = DateTime.Now.AddDays(-1); IDocument doc1 = MockDocument("doc1", "Document", "doc", dt1); IDocument doc2 = MockDocument2("doc1", "Article", "doc", DateTime.Now); index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(d.DateTime == dt1 ? doc1 : doc2); }); IndexStorer storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); index.StoreDocument(doc1, null, "", null); Assert.AreEqual(1, index.TotalDocuments, "Wrong document count"); index.StoreDocument(doc2, null, "", null); Assert.AreEqual(1, index.TotalDocuments, "Wrong document count"); storer.Dispose(); storer = null; index = MockInMemoryIndex(); index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(d.DateTime == dt1 ? doc1 : doc2); }); storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); Assert.AreEqual(1, index.TotalDocuments, "Wrong document count"); }
public void DataCorrupted() { IInMemoryIndex index = MockInMemoryIndex(); IDocument doc1 = MockDocument("doc1", "Document", "doc", DateTime.Now); IDocument doc2 = MockDocument2("doc2", "Article", "doc", DateTime.Now); index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(d.Name == doc1.Name ? doc1 : doc2); }); IndexStorer storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); index.StoreDocument(doc1, null, "", null); index.StoreDocument(doc2, null, "", null); Assert.AreEqual(2, index.TotalDocuments, "Wrong document count"); Assert.AreEqual(12, index.TotalWords, "Wrong word count"); Assert.AreEqual(12, index.TotalOccurrences, "Wrong occurrence count"); storer.Dispose(); storer = null; File.Create(documentsFile).Close(); index = MockInMemoryIndex(); storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); Assert.IsTrue(storer.DataCorrupted, "DataCorrupted should be true"); Assert.AreEqual(0, index.TotalDocuments, "Wrong document count"); }
public void InitializeData_DocumentNotAvailable() { IInMemoryIndex index = (IInMemoryIndex)GetIndex(); IDocument doc = MockDocument("doc", "Document", "doc", DateTime.Now); IDocument inexistent = MockDocument2("inexistent", "Inexistent", "doc", DateTime.Now); DumpedDocument[] documents = new DumpedDocument[] { new DumpedDocument(doc), new DumpedDocument(inexistent) }; DumpedWord[] words = new DumpedWord[] { new DumpedWord(new Word(1, "document")), new DumpedWord(new Word(2, "this")), new DumpedWord(new Word(3, "is")), new DumpedWord(new Word(4, "some")), new DumpedWord(new Word(5, "content")), new DumpedWord(new Word(6, "inexistent")), new DumpedWord(new Word(7, "dummy")), new DumpedWord(new Word(8, "text")), new DumpedWord(new Word(9, "used")), new DumpedWord(new Word(10, "for")), new DumpedWord(new Word(11, "testing")), new DumpedWord(new Word(12, "purposes")) }; DumpedWordMapping[] mappings = new DumpedWordMapping[] { new DumpedWordMapping(words[0].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Title)), new DumpedWordMapping(words[1].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Content)), new DumpedWordMapping(words[2].ID, documents[0].ID, new BasicWordInfo(5, 1, WordLocation.Content)), new DumpedWordMapping(words[3].ID, documents[0].ID, new BasicWordInfo(8, 2, WordLocation.Content)), new DumpedWordMapping(words[4].ID, documents[0].ID, new BasicWordInfo(13, 3, WordLocation.Content)), new DumpedWordMapping(words[5].ID, documents[1].ID, new BasicWordInfo(0, 0, WordLocation.Title)), new DumpedWordMapping(words[6].ID, documents[1].ID, new BasicWordInfo(0, 0, WordLocation.Content)), new DumpedWordMapping(words[7].ID, documents[1].ID, new BasicWordInfo(6, 1, WordLocation.Content)), new DumpedWordMapping(words[8].ID, documents[1].ID, new BasicWordInfo(11, 2, WordLocation.Content)), new DumpedWordMapping(words[9].ID, documents[1].ID, new BasicWordInfo(16, 3, WordLocation.Content)), new DumpedWordMapping(words[10].ID, documents[1].ID, new BasicWordInfo(20, 4, WordLocation.Content)), new DumpedWordMapping(words[11].ID, documents[1].ID, new BasicWordInfo(28, 5, WordLocation.Content)) }; index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { if (d.Name == "doc") { return(doc); } else { return(null); } }); index.InitializeData(documents, words, mappings); Assert.AreEqual(1, index.Search(new SearchParameters("this")).Count, "Wrong result count"); Assert.AreEqual(0, index.Search(new SearchParameters("dummy")).Count, "Wrong result count"); }
public void InitializeData_NullMappings() { IInMemoryIndex index = (IInMemoryIndex)GetIndex(); index.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(null); }); index.InitializeData(new DumpedDocument[0], new DumpedWord[0], null); }
public void InitializeData_NullMappings() { IInMemoryIndex index = (IInMemoryIndex)GetIndex(); index.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(null); }); Assert.That(() => index.InitializeData(new DumpedDocument[0], new DumpedWord[0], null), Throws.ArgumentNullException); }
public void InitializeData_Mappings_Null() { IInMemoryIndex sut = (IInMemoryIndex)GetIndex(); sut.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(null); }); var ex = Assert.Throws <ArgumentNullException>(() => sut.InitializeData(new DumpedDocument[0], new DumpedWord[0], null)); Assert.Equal("Value cannot be null.\r\nParameter name: mappings", ex.Message); }
public void Size() { IInMemoryIndex index = MockInMemoryIndex(); index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(null); }); IndexStorer storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); Assert.IsTrue(storer.Size > 0, "Size should be greater than zero"); }
public void InitializeData() { IInMemoryIndex index = (IInMemoryIndex)GetIndex(); IDocument d = MockDocument("doc", "Document", "doc", DateTime.Now); DumpedDocument[] documents = new DumpedDocument[] { new DumpedDocument(d) }; DumpedWord[] words = new DumpedWord[] { new DumpedWord(new Word(1, "document")), new DumpedWord(new Word(2, "this")), new DumpedWord(new Word(3, "is")), new DumpedWord(new Word(4, "some")), new DumpedWord(new Word(5, "content")) }; DumpedWordMapping[] mappings = new DumpedWordMapping[] { new DumpedWordMapping(words[0].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Title)), new DumpedWordMapping(words[1].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Content)), new DumpedWordMapping(words[2].ID, documents[0].ID, new BasicWordInfo(5, 1, WordLocation.Content)), new DumpedWordMapping(words[3].ID, documents[0].ID, new BasicWordInfo(8, 2, WordLocation.Content)), new DumpedWordMapping(words[4].ID, documents[0].ID, new BasicWordInfo(13, 3, WordLocation.Content)) }; index.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(d); }); index.InitializeData(documents, words, mappings); Assert.AreEqual(1, index.TotalDocuments, "Wrong document count"); Assert.AreEqual(5, index.TotalWords, "Wrong word count"); Assert.AreEqual(5, index.TotalOccurrences, "Wrong occurrence count"); SearchResultCollection res = index.Search(new SearchParameters("document content")); Assert.AreEqual(1, res.Count, "Wrong result count"); Assert.AreEqual(2, res[0].Matches.Count, "Wrong matches count"); Assert.AreEqual("document", res[0].Matches[0].Text, "Wrong match text"); Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong match first char index"); Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong match word index"); Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong match location"); Assert.AreEqual("content", res[0].Matches[1].Text, "Wrong match text"); Assert.AreEqual(13, res[0].Matches[1].FirstCharIndex, "Wrong match first char index"); Assert.AreEqual(3, res[0].Matches[1].WordIndex, "Wrong match word index"); Assert.AreEqual(WordLocation.Content, res[0].Matches[1].Location, "Wrong match location"); }
public void InitializeData() { IInMemoryIndex sut = (IInMemoryIndex)GetIndex(); IDocument d = MockDocument("doc", "Document", "doc", DateTime.Now); DumpedDocument[] documents = new DumpedDocument[] { new DumpedDocument(d) }; DumpedWord[] words = new DumpedWord[] { new DumpedWord(new Word(1, "document")), new DumpedWord(new Word(2, "this")), new DumpedWord(new Word(3, "is")), new DumpedWord(new Word(4, "some")), new DumpedWord(new Word(5, "content")) }; DumpedWordMapping[] mappings = new DumpedWordMapping[] { new DumpedWordMapping(words[0].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Title)), new DumpedWordMapping(words[1].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Content)), new DumpedWordMapping(words[2].ID, documents[0].ID, new BasicWordInfo(5, 1, WordLocation.Content)), new DumpedWordMapping(words[3].ID, documents[0].ID, new BasicWordInfo(8, 2, WordLocation.Content)), new DumpedWordMapping(words[4].ID, documents[0].ID, new BasicWordInfo(13, 3, WordLocation.Content)) }; sut.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(d); }); sut.InitializeData(documents, words, mappings); Assert.Equal(1, sut.TotalDocuments); Assert.Equal(5, sut.TotalWords); Assert.Equal(5, sut.TotalOccurrences); SearchResultCollection res = sut.Search(new SearchParameters("document content")); Assert.Single(res); Assert.Equal(2, res[0].Matches.Count); Assert.Equal("document", res[0].Matches[0].Text); Assert.Equal(0, res[0].Matches[0].FirstCharIndex); Assert.Equal(0, res[0].Matches[0].WordIndex); Assert.Equal(WordLocation.Title, res[0].Matches[0].Location); Assert.Equal("content", res[0].Matches[1].Text); Assert.Equal(13, res[0].Matches[1].FirstCharIndex); Assert.Equal(3, res[0].Matches[1].WordIndex); Assert.Equal(WordLocation.Content, res[0].Matches[1].Location); }
/// <summary> /// Initializes the Provider. /// </summary> /// <param name="host">The Host of the Provider.</param> /// <param name="config">The Configuration data, if any.</param> /// <exception cref="ArgumentNullException">If <b>host</b> or <b>config</b> are <c>null</c>.</exception> /// <exception cref="InvalidConfigurationException">If <b>config</b> is not valid or is incorrect.</exception> public void Init(IHostV30 host, string config) { if(host == null) throw new ArgumentNullException("host"); if(config == null) throw new ArgumentNullException("config"); this.host = host; if(!LocalProvidersTools.CheckWritePermissions(host.GetSettingValue(SettingName.PublicDirectory))) { throw new InvalidConfigurationException("Cannot write into the public directory - check permissions"); } if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), PagesDirectory))) { Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), PagesDirectory)); } if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), MessagesDirectory))) { Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), MessagesDirectory)); } if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), SnippetsDirectory))) { Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), SnippetsDirectory)); } if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), ContentTemplatesDirectory))) { Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), ContentTemplatesDirectory)); } if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), DraftsDirectory))) { Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), DraftsDirectory)); } bool upgradeNeeded = false; if(!File.Exists(GetFullPath(NamespacesFile))) { File.Create(GetFullPath(NamespacesFile)).Close(); } upgradeNeeded = VerifyIfPagesFileNeedsAnUpgrade(); if(!File.Exists(GetFullPath(PagesFile))) { File.Create(GetFullPath(PagesFile)).Close(); } else if(upgradeNeeded) { VerifyAndPerformUpgradeForPages(); } if(!File.Exists(GetFullPath(CategoriesFile))) { File.Create(GetFullPath(CategoriesFile)).Close(); } else if(upgradeNeeded) { VerifyAndPerformUpgradeForCategories(); } if(!File.Exists(GetFullPath(NavigationPathsFile))) { File.Create(GetFullPath(NavigationPathsFile)).Close(); } else if(upgradeNeeded) { VerifyAndPerformUpgradeForNavigationPaths(); } // Prepare search index index = new StandardIndex(); index.SetBuildDocumentDelegate(BuildDocumentHandler); indexStorer = new IndexStorer(GetFullPath(IndexDocumentsFile), GetFullPath(IndexWordsFile), GetFullPath(IndexMappingsFile), index); indexStorer.LoadIndex(); if(indexStorer.DataCorrupted) { host.LogEntry("Search Engine Index is corrupted and needs to be rebuilt\r\n" + indexStorer.ReasonForDataCorruption.ToString(), LogEntryType.Warning, null, this); } }
public void LoadIndexAndModifications() { IInMemoryIndex index = MockInMemoryIndex(); IDocument doc1 = MockDocument("doc1", "Document", "doc", DateTime.Now); IDocument doc2 = MockDocument2("doc2", "Article", "doc", DateTime.Now); BuildDocument buildDoc = new BuildDocument(delegate(DumpedDocument d) { return(d.Name == doc1.Name ? doc1 : doc2); }); index.SetBuildDocumentDelegate(buildDoc); IndexStorer storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); index.StoreDocument(doc1, null, "", null); index.StoreDocument(doc2, null, "", null); Assert.AreEqual(2, index.TotalDocuments, "Wrong document count"); Assert.AreEqual(12, index.TotalWords, "Wrong word count"); Assert.AreEqual(12, index.TotalOccurrences, "Wrong occurrence count"); storer.Dispose(); storer = null; index = MockInMemoryIndex(); index.SetBuildDocumentDelegate(buildDoc); storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); Assert.AreEqual(2, index.TotalDocuments, "Wrong document count"); Assert.AreEqual(12, index.TotalWords, "Wrong word count"); Assert.AreEqual(12, index.TotalOccurrences, "Wrong occurrence count"); SearchResultCollection res = index.Search(new SearchParameters("document content article")); Assert.AreEqual(2, res.Count, "Wrong result count"); Assert.AreEqual(2, res[0].Matches.Count, "Wrong matches count"); Assert.AreEqual(1, res[1].Matches.Count, "Wrong matches count"); Assert.AreEqual("document", res[0].Matches[0].Text, "Wrong match text"); Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong match first char index"); Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong match word index"); Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong match location"); Assert.AreEqual("content", res[0].Matches[1].Text, "Wrong match text"); Assert.AreEqual(13, res[0].Matches[1].FirstCharIndex, "Wrong match first char index"); Assert.AreEqual(3, res[0].Matches[1].WordIndex, "Wrong match word index"); Assert.AreEqual(WordLocation.Content, res[0].Matches[1].Location, "Wrong match location"); Assert.AreEqual("article", res[1].Matches[0].Text, "Wrong match text"); Assert.AreEqual(0, res[1].Matches[0].FirstCharIndex, "Wrong match first char index"); Assert.AreEqual(0, res[1].Matches[0].WordIndex, "Wrong match word index"); Assert.AreEqual(WordLocation.Title, res[1].Matches[0].Location, "Wrong match location"); index.RemoveDocument(doc1, null); storer.Dispose(); storer = null; index = MockInMemoryIndex(); index.SetBuildDocumentDelegate(buildDoc); storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); Assert.AreEqual(1, index.TotalDocuments, "Wrong document count"); Assert.AreEqual(7, index.TotalWords, "Wrong word count"); Assert.AreEqual(7, index.TotalOccurrences, "Wrong occurrence count"); res = index.Search(new SearchParameters("document content article")); Assert.AreEqual(1, res.Count, "Wrong result count"); Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count"); Assert.AreEqual("article", res[0].Matches[0].Text, "Wrong match text"); Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong match first char index"); Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong match word index"); Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong match location"); index.Clear(null); storer.Dispose(); storer = null; index = MockInMemoryIndex(); index.SetBuildDocumentDelegate(buildDoc); storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index); storer.LoadIndex(); Assert.AreEqual(0, index.TotalDocuments, "Wrong document count"); Assert.AreEqual(0, index.TotalWords, "Wrong word count"); Assert.AreEqual(0, index.TotalOccurrences, "Wrong occurrence count"); Assert.AreEqual(0, index.Search(new SearchParameters("document")).Count, "Wrong result count"); }
public void SetBuildDocumentDelegate_NullDelegate() { IInMemoryIndex index = (IInMemoryIndex)GetIndex(); index.SetBuildDocumentDelegate(null); }
public void SetBuildDocumentDelegate_NullDelegate() { IInMemoryIndex index = (IInMemoryIndex)GetIndex(); Assert.That(() => index.SetBuildDocumentDelegate(null), Throws.ArgumentNullException); }
public void SetBuildDocumentDelegate_BuildDocument_Null() { IInMemoryIndex sut = (IInMemoryIndex)GetIndex(); var ex = Assert.Throws <ArgumentNullException>("buildDocument", () => sut.SetBuildDocumentDelegate(null)); }