TEST(IndexInfoTest, WriteIntoDir) { RAMDirectory dir; IndexInfo infos; infos.addSegment(SegmentInfo(0, 42, 100, 123)); infos.incLastSegmentId(); infos.addSegment(SegmentInfo(1, 66, 200, 456)); infos.incLastSegmentId(); infos.setAttribute("foo", "bar"); infos.save(&dir); ScopedPtr<InputStream> input(dir.openFile("info_0")); ASSERT_EQ(2, input->readVInt32()); ASSERT_EQ(2, input->readVInt32()); ASSERT_EQ(0, input->readVInt32()); ASSERT_EQ(42, input->readVInt32()); ASSERT_EQ(100, input->readVInt32()); ASSERT_EQ(123, input->readVInt32()); ASSERT_EQ(1, input->readVInt32()); ASSERT_EQ(66, input->readVInt32()); ASSERT_EQ(200, input->readVInt32()); ASSERT_EQ(456, input->readVInt32()); ASSERT_EQ(1, input->readVInt32()); ASSERT_EQ("foo", input->readString()); ASSERT_EQ("bar", input->readString()); ASSERT_EQ(3656423981u, input->readInt32()); }
TEST(SegmentInfoListTest, Read) { RAMDirectory dir; ScopedPtr<OutputStream> output(dir.createFile("segments_0")); output->writeVInt32(3); output->writeVInt32(2); output->writeString("segment_0"); output->writeVInt32(42); output->writeString("segment_2"); output->writeVInt32(66); output.reset(); SegmentInfoList infos; ScopedPtr<InputStream> input(dir.openFile("segments_0")); infos.read(input.get()); input.reset(); ASSERT_EQ(3, infos.lastSegmetNum()); ASSERT_EQ(2, infos.segmentCount()); ASSERT_EQ("segment_0", infos.info(0).name()); ASSERT_EQ(42, infos.info(0).numDocs()); ASSERT_EQ("segment_2", infos.info(1).name()); ASSERT_EQ(66, infos.info(1).numDocs()); }
TEST(IndexWriterTest, OpenEmpty) { RAMDirectory dir; ASSERT_FALSE(dir.fileExists("segments_0")); ASSERT_THROW(IndexWriter writer(&dir), IOException); ASSERT_FALSE(dir.fileExists("segments_0")); }
TEST(IndexWriterTest, OpenEmptyCreate) { RAMDirectory dir; ASSERT_FALSE(dir.fileExists("segments_0")); IndexWriter writer(&dir, true); ASSERT_TRUE(dir.fileExists("segments_0")); ASSERT_EQ(0, writer.revision()); }
void testEndThreadException(CuTest *tc) { const int MAX_DOCS=1500; RAMDirectory ram; WhitespaceAnalyzer an; IndexWriter* writer = _CLNEW IndexWriter(&ram, &an, true); // add some documents Document doc; for (int i = 0; i < MAX_DOCS; i++) { TCHAR * tmp = English::IntToEnglish(i); doc.add(* new Field(_T("content"), tmp, Field::STORE_YES | Field::INDEX_UNTOKENIZED)); writer->addDocument(&doc); doc.clear(); _CLDELETE_ARRAY( tmp ); } CuAssertEquals(tc, MAX_DOCS, writer->docCount()); writer->close(); _CLLDELETE(writer); // this sequence is OK: delete searcher after search thread finish { IndexSearcher * searcher = _CLNEW IndexSearcher(&ram); _LUCENE_THREADID_TYPE thread = _LUCENE_THREAD_CREATE(&searchDocs, searcher); SCOPED_LOCK_MUTEX(searchMutex); CONDITION_WAIT(searchMutex, searchCondition); // _LUCENE_SLEEP(9999); //make sure that deleteMutex is being waited on... CONDITION_NOTIFYALL(deleteCondition); _LUCENE_THREAD_JOIN(thread); searcher->close(); _CLLDELETE(searcher); } // this produces memory exception: delete searcher after search finish but before thread finish { IndexSearcher * searcher = _CLNEW IndexSearcher(&ram); _LUCENE_THREADID_TYPE thread = _LUCENE_THREAD_CREATE(&searchDocs, searcher); SCOPED_LOCK_MUTEX(searchMutex); CONDITION_WAIT(searchMutex, searchCondition); searcher->close(); _CLLDELETE(searcher); CONDITION_NOTIFYALL(deleteCondition); _LUCENE_THREAD_JOIN(thread); } ram.close(); }
void testIncludeLowerTrue(CuTest* tc) { WhitespaceAnalyzer a; RAMDirectory* index = _CLNEW RAMDirectory(); IndexWriter* writer = _CLNEW IndexWriter(index, &a, true); Document doc; doc.add(*_CLNEW Field(_T("Category"), _T("a 1"), Field::STORE_YES | Field::INDEX_TOKENIZED)); writer->addDocument(&doc); doc.clear(); doc.add(*_CLNEW Field(_T("Category"), _T("a 2"), Field::STORE_YES | Field::INDEX_TOKENIZED)); writer->addDocument(&doc); doc.clear(); doc.add(*_CLNEW Field(_T("Category"), _T("a 3"), Field::STORE_YES | Field::INDEX_TOKENIZED)); writer->addDocument(&doc); doc.clear(); writer->close(); _CLLDELETE(writer); IndexSearcher* s = _CLNEW IndexSearcher(index); Filter* f = _CLNEW RangeFilter(_T("Category"), _T("3"), _T("3"), true, true); Term* t = _CLNEW Term(_T("Category"), _T("a")); Query* q1 = _CLNEW TermQuery(t); _CLLDECDELETE(t); t = _CLNEW Term(_T("Category"), _T("3")); Query* q2 = _CLNEW TermQuery(t); _CLLDECDELETE(t); Hits* h = s->search(q1); assertTrue(h->length() == 3); _CLLDELETE(h); h = s->search(q2); assertTrue(h->length() == 1); _CLLDELETE(h); h = s->search(q1, f); assertTrue(h->length() == 1); _CLLDELETE(h); s->close(); _CLLDELETE(s); _CLLDELETE(q1); _CLLDELETE(q2); _CLLDELETE(f); index->close(); _CLLDECDELETE(index); }
TEST(IndexInfoTest, ReadFromDirCorruptRecover2) { RAMDirectory dir; { ScopedPtr<OutputStream> output(dir.createFile("info_0")); output->writeVInt32(2); output->writeVInt32(2); output->writeVInt32(0); output->writeVInt32(42); output->writeVInt32(100); output->writeVInt32(123); output->writeVInt32(1); output->writeVInt32(66); output->writeVInt32(200); output->writeVInt32(456); output->writeVInt32(1); output->writeString("foo"); output->writeString("bar"); output->writeInt32(3656423981u); } { ScopedPtr<OutputStream> output(dir.createFile("info_1")); output->writeVInt32(3); } { ScopedPtr<OutputStream> output(dir.createFile("info_2")); output->writeVInt32(4); } IndexInfo infos; infos.load(&dir); ASSERT_EQ(0, infos.revision()); ASSERT_EQ(2, infos.lastSegmentId()); ASSERT_EQ(2, infos.segmentCount()); ASSERT_EQ("segment_0", infos.segment(0).name()); ASSERT_EQ(42, infos.segment(0).blockCount()); ASSERT_EQ(100, infos.segment(0).lastKey()); ASSERT_EQ(123, infos.segment(0).checksum()); ASSERT_EQ("segment_1", infos.segment(1).name()); ASSERT_EQ(66, infos.segment(1).blockCount()); ASSERT_EQ(200, infos.segment(1).lastKey()); ASSERT_EQ(456, infos.segment(1).checksum()); ASSERT_EQ(1, infos.attributes().size()); ASSERT_EQ("bar", infos.attribute("foo")); }
TEST(IndexInfoTest, ReadFromDirCorruptFail) { RAMDirectory dir; { ScopedPtr<OutputStream> output(dir.createFile("info_0")); output->writeVInt32(2); } { ScopedPtr<OutputStream> output(dir.createFile("info_1")); output->writeVInt32(3); } IndexInfo infos; ASSERT_THROW(infos.load(&dir), CorruptIndexException); }
TEST(IndexInfoTest, FindCurrentRevision) { RAMDirectory dir; int rev = IndexInfo::findCurrentRevision(&dir); ASSERT_EQ(-1, rev); delete dir.createFile("info_0"); rev = IndexInfo::findCurrentRevision(&dir); ASSERT_EQ(0, rev); delete dir.createFile("info_1"); rev = IndexInfo::findCurrentRevision(&dir); ASSERT_EQ(1, rev); delete dir.createFile("info_8"); rev = IndexInfo::findCurrentRevision(&dir); ASSERT_EQ(8, rev); }
TEST(SegmentInfoListTest, FindCurrentRevision) { RAMDirectory dir; int rev = SegmentInfoList::findCurrentRevision(&dir); ASSERT_EQ(-1, rev); delete dir.createFile("segments_0"); rev = SegmentInfoList::findCurrentRevision(&dir); ASSERT_EQ(0, rev); delete dir.createFile("segments_1"); rev = SegmentInfoList::findCurrentRevision(&dir); ASSERT_EQ(1, rev); delete dir.createFile("segments_8"); rev = SegmentInfoList::findCurrentRevision(&dir); ASSERT_EQ(8, rev); }
TEST(SegmentInfoListTest, Write) { RAMDirectory dir; SegmentInfoList infos; infos.add(SegmentInfo("segment_0", 42)); infos.incNextSegmentNum(); infos.add(SegmentInfo("segment_1", 66)); infos.incNextSegmentNum(); ScopedPtr<OutputStream> output(dir.createFile("segments_0")); infos.write(output.get()); output.reset(); ScopedPtr<InputStream> input(dir.openFile("segments_0")); ASSERT_EQ(2, input->readVInt32()); ASSERT_EQ(2, input->readVInt32()); ASSERT_EQ("segment_0", input->readString()); ASSERT_EQ(42, input->readVInt32()); ASSERT_EQ("segment_1", input->readString()); ASSERT_EQ(66, input->readVInt32()); }
TEST(IndexWriterTest, AddDocument) { RAMDirectory dir; IndexWriter writer(&dir, true); ASSERT_TRUE(dir.fileExists("segments_0")); ASSERT_EQ(0, writer.revision()); ASSERT_EQ(0, writer.segmentInfoList().segmentCount()); uint32_t fp[] = { 7, 9, 12 }; writer.addDocument(1, fp, 3); writer.commit(); ASSERT_TRUE(dir.fileExists("segments_1")); ASSERT_TRUE(dir.fileExists("segment_0.fii")); ASSERT_TRUE(dir.fileExists("segment_0.fid")); ASSERT_EQ(1, writer.revision()); ASSERT_EQ(1, writer.segmentInfoList().segmentCount()); ASSERT_EQ("segment_0", writer.segmentInfoList().info(0).name()); ASSERT_EQ(1, writer.segmentInfoList().info(0).numDocs()); { ScopedPtr<InputStream> input(dir.openFile("segment_0.fii")); ASSERT_EQ(512, input->readInt32()); ASSERT_EQ(1, input->readInt32()); ASSERT_EQ(7, input->readVInt32()); } { ScopedPtr<InputStream> input(dir.openFile("segment_0.fid")); ASSERT_EQ(3, input->readVInt32()); ASSERT_EQ(1, input->readVInt32()); ASSERT_EQ(2, input->readVInt32()); ASSERT_EQ(1, input->readVInt32()); ASSERT_EQ(3, input->readVInt32()); ASSERT_EQ(1, input->readVInt32()); } }
/* Run above stress test against RAMDirectory and then FSDirectory. */ void testRAMThreading(CuTest *tc){ // First in a RAM directory: RAMDirectory directory; //todo: use MockRAMDirectory? runThreadingTests(tc,directory); directory.close(); }