001: package org.apache.lucene.index;
002:
003: /**
004: * Copyright 2006 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.lucene.store.Directory;
020: import org.apache.lucene.store.MockRAMDirectory;
021: import org.apache.lucene.analysis.standard.StandardAnalyzer;
022: import org.apache.lucene.document.Document;
023: import org.apache.lucene.document.Field;
024: import org.apache.lucene.util.LuceneTestCase;
025:
026: import java.io.IOException;
027:
028: public class TestIndexWriterMerging extends LuceneTestCase {
029:
030: /**
031: * Tests that index merging (specifically addIndexes()) doesn't
032: * change the index order of documents.
033: */
034: public void testLucene() throws IOException {
035:
036: int num = 100;
037:
038: Directory indexA = new MockRAMDirectory();
039: Directory indexB = new MockRAMDirectory();
040:
041: fillIndex(indexA, 0, num);
042: boolean fail = verifyIndex(indexA, 0);
043: if (fail) {
044: fail("Index a is invalid");
045: }
046:
047: fillIndex(indexB, num, num);
048: fail = verifyIndex(indexB, num);
049: if (fail) {
050: fail("Index b is invalid");
051: }
052:
053: Directory merged = new MockRAMDirectory();
054:
055: IndexWriter writer = new IndexWriter(merged,
056: new StandardAnalyzer(), true);
057: writer.setMergeFactor(2);
058:
059: writer.addIndexes(new Directory[] { indexA, indexB });
060: writer.close();
061:
062: fail = verifyIndex(merged, 0);
063: merged.close();
064:
065: assertFalse("The merged index is invalid", fail);
066: }
067:
068: private boolean verifyIndex(Directory directory, int startAt)
069: throws IOException {
070: boolean fail = false;
071: IndexReader reader = IndexReader.open(directory);
072:
073: int max = reader.maxDoc();
074: for (int i = 0; i < max; i++) {
075: Document temp = reader.document(i);
076: //System.out.println("doc "+i+"="+temp.getField("count").stringValue());
077: //compare the index doc number to the value that it should be
078: if (!temp.getField("count").stringValue().equals(
079: (i + startAt) + "")) {
080: fail = true;
081: System.out.println("Document " + (i + startAt)
082: + " is returning document "
083: + temp.getField("count").stringValue());
084: }
085: }
086: reader.close();
087: return fail;
088: }
089:
090: private void fillIndex(Directory dir, int start, int numDocs)
091: throws IOException {
092:
093: IndexWriter writer = new IndexWriter(dir,
094: new StandardAnalyzer(), true);
095: writer.setMergeFactor(2);
096: writer.setMaxBufferedDocs(2);
097:
098: for (int i = start; i < (start + numDocs); i++) {
099: Document temp = new Document();
100: temp.add(new Field("count", ("" + i), Field.Store.YES,
101: Field.Index.UN_TOKENIZED));
102:
103: writer.addDocument(temp);
104: }
105: writer.close();
106: }
107: }
|