void setUpDirs(CuTest *tc, Directory * dir, Directory * aux) { IndexWriter4Test * writer = NULL; WhitespaceAnalyzer analyzer; writer = newWriter(dir, &analyzer, true); writer->setMaxBufferedDocs(1000); // add 1000 documents in 1 segment addDocs(writer, 1000); assertEquals(1000, writer->docCount()); assertEquals(1, writer->getSegmentCount()); writer->close(); _CLLDELETE(writer); writer = newWriter(aux, &analyzer, true); writer->setUseCompoundFile(false); // use one without a compound file writer->setMaxBufferedDocs(100); writer->setMergeFactor(10); // add 30 documents in 3 segments for (int i = 0; i < 3; i++) { addDocs(writer, 10); writer->close(); _CLLDELETE(writer); writer = newWriter(aux, &analyzer, false); writer->setUseCompoundFile(false); // use one without a compound file writer->setMaxBufferedDocs(100); writer->setMergeFactor(10); } assertEquals(30, writer->docCount()); assertEquals(3, writer->getSegmentCount()); writer->close(); _CLLDELETE(writer); }
// case 0: add self or exceed maxMergeDocs, expect exception void testAddSelf(CuTest * tc) { // main directory Directory * dir = _CLNEW RAMDirectory(); // auxiliary directory Directory * aux = _CLNEW RAMDirectory(); IndexWriter4Test * writer = NULL; WhitespaceAnalyzer analyzer; writer = newWriter(dir, &analyzer, true); // add 100 documents addDocs(writer, 100); assertEquals(100, writer->docCount()); writer->close(); _CLLDELETE(writer); writer = newWriter(aux, &analyzer, true); writer->setUseCompoundFile(false); // use one without a compound file writer->setMaxBufferedDocs(1000); // add 140 documents in separate files addDocs(writer, 40); writer->close(); _CLLDELETE(writer); writer = newWriter(aux, &analyzer, true); writer->setUseCompoundFile(false); // use one without a compound file writer->setMaxBufferedDocs(1000); addDocs(writer, 100); writer->close(); _CLLDELETE(writer); writer = newWriter(dir, &analyzer, false); try { // cannot add self ValueArray<Directory*> dirs(2); dirs[0] = aux; dirs[1] = dir; writer->addIndexesNoOptimize( dirs ); assertTrue(false); } catch (CLuceneError&) { assertEquals(100, writer->docCount()); } writer->close(); _CLLDELETE(writer); // make sure the index is correct verifyNumDocs(tc, dir, 100); dir->close(); _CLLDELETE(dir); aux->close(); _CLLDELETE(aux); }
void unpackCB(LONG id, LONG width, LONG height, BYTE *rgbBuf) { struct writer_t *w; struct bmp24_writer_t *bw; FILE *f; char fn[256]; if (rgbBuf == NULL) return; sprintf(fn, "out/%d.bmp", id); f = fopen(fn, "wb"); if (f == NULL) { fprintf(stderr, "Failed to open %s for writing: %s\n", fn, strerror(errno)); exit(1); } w = newWriter(f); bw = newBMP24Writer(w, width, height); writeBMP24Header(bw); writeBMP24Bitmap(bw, rgbBuf); deleteBMP24Writer(bw); deleteWriter(w); fclose(f); if (progress % 10 == 0) fprintf(stderr, "%d..\n", progress); progress++; }
// case 3: tail segments, invariants hold, copy, invariants hold void testNoMergeAfterCopy(CuTest * tc) { // main directory Directory * dir = _CLNEW RAMDirectory(); // auxiliary directory Directory * aux = _CLNEW RAMDirectory(); WhitespaceAnalyzer an; setUpDirs(tc, dir, aux); IndexWriter4Test * writer = newWriter(dir, &an, false); writer->setMaxBufferedDocs(10); writer->setMergeFactor(4); ValueArray<Directory*> dirs(2); dirs[0] = aux; dirs[1] = aux; writer->addIndexesNoOptimize(dirs); assertEquals(1060, writer->docCount()); assertEquals(1000, writer->getDocCount(0)); writer->close(); _CLLDELETE(writer); // make sure the index is correct verifyNumDocs(tc, dir, 1060); dir->close(); _CLLDELETE(dir); aux->close(); _CLLDELETE(aux); }
// case 5: tail segments, invariants not hold void testMoreMerges(CuTest * tc) { // main directory Directory * dir = _CLNEW RAMDirectory(); // auxiliary directory Directory * aux = _CLNEW RAMDirectory(); Directory * aux2 = _CLNEW RAMDirectory(); WhitespaceAnalyzer an; setUpDirs(tc, dir, aux); IndexWriter4Test * writer = newWriter(aux2, &an, true); writer->setMaxBufferedDocs(100); writer->setMergeFactor(10); ValueArray<Directory*> dirs(1); dirs[0] = aux; writer->addIndexesNoOptimize(dirs); assertEquals(30, writer->docCount()); assertEquals(3, writer->getSegmentCount()); writer->close(); _CLLDELETE(writer); IndexReader * reader = IndexReader::open(aux); for (int i = 0; i < 27; i++) { reader->deleteDocument(i); } assertEquals(3, reader->numDocs()); reader->close(); _CLLDELETE(reader); reader = IndexReader::open(aux2); for (int i = 0; i < 8; i++) { reader->deleteDocument(i); } assertEquals(22, reader->numDocs()); reader->close(); _CLLDELETE( reader ); writer = newWriter(dir, &an, false); writer->setMaxBufferedDocs(6); writer->setMergeFactor(4); ValueArray<Directory*> dirs2(2); dirs2[0] = aux; dirs2[1] = aux2; writer->addIndexesNoOptimize(dirs2); assertEquals(1025, writer->docCount()); assertEquals(1000, writer->getDocCount(0)); writer->close(); _CLLDELETE( writer ); // make sure the index is correct verifyNumDocs(tc, dir, 1025); dir->close(); _CLLDELETE(dir); aux->close(); _CLLDELETE(aux); aux2->close(); _CLLDELETE(aux2); }
void testSimpleCase(CuTest *tc) { // main directory Directory * dir = _CLNEW RAMDirectory(); // two auxiliary directories Directory * aux = _CLNEW RAMDirectory(); Directory * aux2 = _CLNEW RAMDirectory(); IndexWriter4Test * writer = NULL; WhitespaceAnalyzer analyzer; writer = newWriter(dir, &analyzer, true); // add 100 documents addDocs(writer, 100); assertEquals(100, writer->docCount()); writer->close(); _CLLDELETE(writer); writer = newWriter(aux, &analyzer, true); writer->setUseCompoundFile(false); // use one without a compound file // add 40 documents in separate files addDocs(writer, 40); assertEquals(40, writer->docCount()); writer->close(); _CLLDELETE(writer); writer = newWriter(aux2, &analyzer, true); // add 40 documents in compound files addDocs2(writer, 50); assertEquals(50, writer->docCount()); writer->close(); _CLLDELETE(writer); // test doc count before segments are merged writer = newWriter(dir, &analyzer, false); assertEquals(100, writer->docCount()); { ValueArray<Directory*> dirs(2); dirs[0] = aux; dirs[1] = aux2; writer->addIndexesNoOptimize( dirs ); } assertEquals(190, writer->docCount()); writer->close(); _CLLDELETE(writer); // make sure the old index is correct verifyNumDocs(tc, aux, 40); // make sure the new index is correct verifyNumDocs(tc, dir, 190); // now add another set in. Directory * aux3 = _CLNEW RAMDirectory(); writer = newWriter(aux3, &analyzer, true); // add 40 documents addDocs(writer, 40); assertEquals(40, writer->docCount()); writer->close(); _CLLDELETE(writer); // test doc count before segments are merged/index is optimized writer = newWriter(dir, &analyzer, false); assertEquals(190, writer->docCount()); { ValueArray<Directory*> dirs(1); dirs[0] = aux3; writer->addIndexesNoOptimize( dirs ); } assertEquals(230, writer->docCount()); writer->close(); _CLLDELETE(writer); // make sure the new index is correct verifyNumDocs(tc, dir, 230); Term t1(_T("content"), _T("aaa")); Term t2(_T("content"), _T("bbb")); verifyTermDocs(tc, dir, &t1, 180); verifyTermDocs(tc, dir, &t2, 50); // now optimize it. writer = newWriter(dir, &analyzer, false); writer->optimize(); writer->close(); _CLLDELETE(writer); // make sure the new index is correct verifyNumDocs(tc, dir, 230); verifyTermDocs(tc, dir, &t1, 180); verifyTermDocs(tc, dir, &t2, 50); // now add a single document Directory * aux4 = _CLNEW RAMDirectory(); writer = newWriter(aux4, &analyzer, true); addDocs2(writer, 1); writer->close(); _CLLDELETE(writer); writer = newWriter(dir, &analyzer, false); assertEquals(230, writer->docCount()); { ValueArray<Directory*> dirs(1); dirs[0] = aux4; writer->addIndexesNoOptimize( dirs ); } assertEquals(231, writer->docCount()); writer->close(); _CLLDELETE(writer); verifyNumDocs(tc, dir, 231); verifyTermDocs(tc, dir, &t2, 51); dir->close(); _CLLDELETE(dir); aux->close(); _CLLDELETE(aux); aux2->close(); _CLLDELETE(aux2); aux3->close(); _CLLDELETE(aux3); aux4->close(); _CLLDELETE(aux4); }
void doSomeStuff(){ printf("Creating account for Dan Brown\n"); struct Writer danBrown = newWriter( "Dan Brown", "danbrown", "password" ); addWriter(&danBrown, &eBookStore); printf("Created account for Dan Brown with ID %d\n\n", danBrown.id); printf("Adding the book Angels and Demons\n"); struct Book angelsAndDemons = newBook( "Angels and Demons", "Dan Brown", "Science and Mythology and Stuff", "In not so distant past...", danBrown.id, 100 ); publishBook(&angelsAndDemons, &eBookStore); printf("Added the book Angels and Demons with ID %d\n\n", angelsAndDemons.id); printf("Adding the book Da Vinci Code\n"); struct Book daVinciCode = newBook( "Da Vinci Code", "Dan Brown", "Its about the hipster Leo", "Once upon a time...", danBrown.id, 100 ); publishBook(&daVinciCode, &eBookStore); printf("Added the book Da Vinci Code with ID %d\n\n", daVinciCode.id); printf("Books published by %s on our platform are,\n", danBrown.name); for(int i=0;i<danBrown.bookCount;i++){ int bookID = danBrown.bookIDs[i]; struct Book *b = eBookStore.bookDataStore.books[bookID]; printf("#%d:\t%s\n", i+1, b->title); } printf("\n"); printf("Finding books similar to %s\n", daVinciCode.title); assignSimilarBooks(&daVinciCode, &eBookStore); for(int i=0;i<daVinciCode.similarBookCount;i++){ int bookID = daVinciCode.similarBooks[i]; struct Book *b = eBookStore.bookDataStore.books[bookID]; printf("#%d:\t%s\n", i+1, b->title); } printf("\n"); printf("Creating customer account for Robert Langdon\n"); struct Customer robertLangdon = newCustomer("robertlangdon", "password"); addCustomerToDataStore(&robertLangdon, &eBookStore.customerDataStore); printf("Created customer account for Robert Langdon with user id %d\n\n", robertLangdon.id); printf("Searching for books with the title 'Angels and Demons'\n"); struct Book* searchResult = searchBooksInDataStore(1, "Angels and Demons", 1, &eBookStore.bookDataStore)[0]; printf("Found one book with the name %s by the author %s\n", searchResult->title, searchResult->author); printf("Its summary is, %s\n\n", searchResult->summary); printf("Submitting an excellent review for %s from user name %s\n", searchResult->title, robertLangdon.username); addReview(searchResult, &robertLangdon, 5, "Awesome read! :)", &eBookStore); printf("Fetching all reviews for %s,\n", searchResult->title); for(int i=0;i<searchResult->reviewCount;i++){ printf("#%d: Rating: %d Comments: %s\n", i+1, searchResult->reviews[i]->rating, searchResult->reviews[i]->body); } printf("Average rating of %s is %f\n\n", searchResult->title, getAverageRating(searchResult)); printf("That's all for this demo folks! :)\n"); }