001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021:
022: import org.apache.lucene.analysis.standard.StandardAnalyzer;
023: import org.apache.lucene.document.Document;
024: import org.apache.lucene.document.Field;
025: import org.apache.lucene.store.Directory;
026: import org.apache.lucene.store.RAMDirectory;
027:
028: import java.io.IOException;
029:
030: public class TestMultiSegmentReader extends LuceneTestCase {
031: protected Directory dir;
032: private Document doc1;
033: private Document doc2;
034: protected SegmentReader[] readers = new SegmentReader[2];
035: protected SegmentInfos sis;
036:
037: public TestMultiSegmentReader(String s) {
038: super (s);
039: }
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: dir = new RAMDirectory();
044: doc1 = new Document();
045: doc2 = new Document();
046: DocHelper.setupDoc(doc1);
047: DocHelper.setupDoc(doc2);
048: SegmentInfo info1 = DocHelper.writeDoc(dir, doc1);
049: SegmentInfo info2 = DocHelper.writeDoc(dir, doc2);
050: sis = new SegmentInfos();
051: sis.read(dir);
052: }
053:
054: protected IndexReader openReader() throws IOException {
055: IndexReader reader;
056: reader = IndexReader.open(dir);
057: assertTrue(reader instanceof MultiSegmentReader);
058:
059: assertTrue(dir != null);
060: assertTrue(sis != null);
061: assertTrue(reader != null);
062:
063: return reader;
064: }
065:
066: public void test() throws Exception {
067: setUp();
068: doTestDocument();
069: doTestUndeleteAll();
070: }
071:
072: public void doTestDocument() throws IOException {
073: sis.read(dir);
074: IndexReader reader = openReader();
075: assertTrue(reader != null);
076: Document newDoc1 = reader.document(0);
077: assertTrue(newDoc1 != null);
078: assertTrue(DocHelper.numFields(newDoc1) == DocHelper
079: .numFields(doc1)
080: - DocHelper.unstored.size());
081: Document newDoc2 = reader.document(1);
082: assertTrue(newDoc2 != null);
083: assertTrue(DocHelper.numFields(newDoc2) == DocHelper
084: .numFields(doc2)
085: - DocHelper.unstored.size());
086: TermFreqVector vector = reader.getTermFreqVector(0,
087: DocHelper.TEXT_FIELD_2_KEY);
088: assertTrue(vector != null);
089: TestSegmentReader.checkNorms(reader);
090: }
091:
092: public void doTestUndeleteAll() throws IOException {
093: sis.read(dir);
094: IndexReader reader = openReader();
095: assertTrue(reader != null);
096: assertEquals(2, reader.numDocs());
097: reader.deleteDocument(0);
098: assertEquals(1, reader.numDocs());
099: reader.undeleteAll();
100: assertEquals(2, reader.numDocs());
101:
102: // Ensure undeleteAll survives commit/close/reopen:
103: reader.commit();
104: reader.close();
105:
106: if (reader instanceof MultiReader)
107: // MultiReader does not "own" the directory so it does
108: // not write the changes to sis on commit:
109: sis.write(dir);
110:
111: sis.read(dir);
112: reader = openReader();
113: assertEquals(2, reader.numDocs());
114:
115: reader.deleteDocument(0);
116: assertEquals(1, reader.numDocs());
117: reader.commit();
118: reader.close();
119: if (reader instanceof MultiReader)
120: // MultiReader does not "own" the directory so it does
121: // not write the changes to sis on commit:
122: sis.write(dir);
123: sis.read(dir);
124: reader = openReader();
125: assertEquals(1, reader.numDocs());
126: }
127:
128: public void _testTermVectors() {
129: MultiReader reader = new MultiReader(readers);
130: assertTrue(reader != null);
131: }
132:
133: public void testIsCurrent() throws IOException {
134: RAMDirectory ramDir1 = new RAMDirectory();
135: addDoc(ramDir1, "test foo", true);
136: RAMDirectory ramDir2 = new RAMDirectory();
137: addDoc(ramDir2, "test blah", true);
138: IndexReader[] readers = new IndexReader[] {
139: IndexReader.open(ramDir1), IndexReader.open(ramDir2) };
140: MultiReader mr = new MultiReader(readers);
141: assertTrue(mr.isCurrent()); // just opened, must be current
142: addDoc(ramDir1, "more text", false);
143: assertFalse(mr.isCurrent()); // has been modified, not current anymore
144: addDoc(ramDir2, "even more text", false);
145: assertFalse(mr.isCurrent()); // has been modified even more, not current anymore
146: try {
147: mr.getVersion();
148: fail();
149: } catch (UnsupportedOperationException e) {
150: // expected exception
151: }
152: mr.close();
153: }
154:
155: private void addDoc(RAMDirectory ramDir1, String s, boolean create)
156: throws IOException {
157: IndexWriter iw = new IndexWriter(ramDir1,
158: new StandardAnalyzer(), create);
159: Document doc = new Document();
160: doc.add(new Field("body", s, Field.Store.YES,
161: Field.Index.TOKENIZED));
162: iw.addDocument(doc);
163: iw.close();
164: }
165: }
|