001: package org.apache.lucene.index.store;
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 java.io.File;
021: import java.io.IOException;
022: import java.io.ObjectOutput;
023: import java.io.ObjectOutputStream;
024: import java.io.ByteArrayOutputStream;
025:
026: import org.apache.lucene.util.LuceneTestCase;
027:
028: import org.apache.lucene.analysis.WhitespaceAnalyzer;
029: import org.apache.lucene.document.Document;
030: import org.apache.lucene.document.Field;
031: import org.apache.lucene.index.IndexReader;
032: import org.apache.lucene.index.IndexWriter;
033: import org.apache.lucene.search.IndexSearcher;
034: import org.apache.lucene.store.Directory;
035: import org.apache.lucene.store.FSDirectory;
036: import org.apache.lucene.store.RAMDirectory;
037: import org.apache.lucene.util.English;
038:
039: import org.apache.lucene.store.MockRAMDirectory;
040:
041: /**
042: * JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases,
043: * but not one of them uses an different constructor other than the default constructor.
044: *
045: * @author Bernhard Messer
046: *
047: * @version $Id: RAMDirectory.java 150537 2004-09-28 22:45:26 +0200 (Di, 28 Sep 2004) cutting $
048: */
049: public class TestRAMDirectory extends LuceneTestCase {
050:
051: private File indexDir = null;
052:
053: // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
054: private final int docsToAdd = 500;
055:
056: // setup the index
057: public void setUp() throws Exception {
058: super .setUp();
059: String tempDir = System.getProperty("java.io.tmpdir");
060: if (tempDir == null)
061: throw new IOException(
062: "java.io.tmpdir undefined, cannot run test");
063: indexDir = new File(tempDir, "RAMDirIndex");
064:
065: IndexWriter writer = new IndexWriter(indexDir,
066: new WhitespaceAnalyzer(), true);
067: // add some documents
068: Document doc = null;
069: for (int i = 0; i < docsToAdd; i++) {
070: doc = new Document();
071: doc.add(new Field("content",
072: English.intToEnglish(i).trim(), Field.Store.YES,
073: Field.Index.UN_TOKENIZED));
074: writer.addDocument(doc);
075: }
076: assertEquals(docsToAdd, writer.docCount());
077: writer.close();
078: }
079:
080: public void testRAMDirectory() throws IOException {
081:
082: Directory dir = FSDirectory.getDirectory(indexDir);
083: MockRAMDirectory ramDir = new MockRAMDirectory(dir);
084:
085: // close the underlaying directory
086: dir.close();
087:
088: // Check size
089: assertEquals(ramDir.sizeInBytes(), ramDir
090: .getRecomputedSizeInBytes());
091:
092: // open reader to test document count
093: IndexReader reader = IndexReader.open(ramDir);
094: assertEquals(docsToAdd, reader.numDocs());
095:
096: // open search zo check if all doc's are there
097: IndexSearcher searcher = new IndexSearcher(reader);
098:
099: // search for all documents
100: for (int i = 0; i < docsToAdd; i++) {
101: Document doc = searcher.doc(i);
102: assertTrue(doc.getField("content") != null);
103: }
104:
105: // cleanup
106: reader.close();
107: searcher.close();
108: }
109:
110: public void testRAMDirectoryFile() throws IOException {
111:
112: MockRAMDirectory ramDir = new MockRAMDirectory(indexDir);
113:
114: // Check size
115: assertEquals(ramDir.sizeInBytes(), ramDir
116: .getRecomputedSizeInBytes());
117:
118: // open reader to test document count
119: IndexReader reader = IndexReader.open(ramDir);
120: assertEquals(docsToAdd, reader.numDocs());
121:
122: // open search zo check if all doc's are there
123: IndexSearcher searcher = new IndexSearcher(reader);
124:
125: // search for all documents
126: for (int i = 0; i < docsToAdd; i++) {
127: Document doc = searcher.doc(i);
128: assertTrue(doc.getField("content") != null);
129: }
130:
131: // cleanup
132: reader.close();
133: searcher.close();
134: }
135:
136: public void testRAMDirectoryString() throws IOException {
137:
138: MockRAMDirectory ramDir = new MockRAMDirectory(indexDir
139: .getCanonicalPath());
140:
141: // Check size
142: assertEquals(ramDir.sizeInBytes(), ramDir
143: .getRecomputedSizeInBytes());
144:
145: // open reader to test document count
146: IndexReader reader = IndexReader.open(ramDir);
147: assertEquals(docsToAdd, reader.numDocs());
148:
149: // open search zo check if all doc's are there
150: IndexSearcher searcher = new IndexSearcher(reader);
151:
152: // search for all documents
153: for (int i = 0; i < docsToAdd; i++) {
154: Document doc = searcher.doc(i);
155: assertTrue(doc.getField("content") != null);
156: }
157:
158: // cleanup
159: reader.close();
160: searcher.close();
161: }
162:
163: private final int numThreads = 50;
164: private final int docsPerThread = 40;
165:
166: public void testRAMDirectorySize() throws IOException,
167: InterruptedException {
168:
169: final MockRAMDirectory ramDir = new MockRAMDirectory(indexDir
170: .getCanonicalPath());
171: final IndexWriter writer = new IndexWriter(ramDir,
172: new WhitespaceAnalyzer(), false);
173: writer.optimize();
174:
175: assertEquals(ramDir.sizeInBytes(), ramDir
176: .getRecomputedSizeInBytes());
177:
178: Thread[] threads = new Thread[numThreads];
179: for (int i = 0; i < numThreads; i++) {
180: final int num = i;
181: threads[i] = new Thread() {
182: public void run() {
183: for (int j = 1; j < docsPerThread; j++) {
184: Document doc = new Document();
185: doc.add(new Field("sizeContent", English
186: .intToEnglish(num * docsPerThread + j)
187: .trim(), Field.Store.YES,
188: Field.Index.UN_TOKENIZED));
189: try {
190: writer.addDocument(doc);
191: } catch (IOException e) {
192: throw new RuntimeException(e);
193: }
194: synchronized (ramDir) {
195: assertEquals(ramDir.sizeInBytes(), ramDir
196: .getRecomputedSizeInBytes());
197: }
198: }
199: }
200: };
201: }
202: for (int i = 0; i < numThreads; i++)
203: threads[i].start();
204: for (int i = 0; i < numThreads; i++)
205: threads[i].join();
206:
207: writer.optimize();
208: assertEquals(ramDir.sizeInBytes(), ramDir
209: .getRecomputedSizeInBytes());
210:
211: writer.close();
212: }
213:
214: public void testSerializable() throws IOException {
215: Directory dir = new RAMDirectory();
216: ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
217: assertEquals("initially empty", 0, bos.size());
218: ObjectOutput out = new ObjectOutputStream(bos);
219: int headerSize = bos.size();
220: out.writeObject(dir);
221: out.close();
222: assertTrue("contains more then just header", headerSize < bos
223: .size());
224: }
225:
226: public void tearDown() throws Exception {
227: super .tearDown();
228: // cleanup
229: if (indexDir != null && indexDir.exists()) {
230: rmDir(indexDir);
231: }
232: }
233:
234: private void rmDir(File dir) {
235: File[] files = dir.listFiles();
236: for (int i = 0; i < files.length; i++) {
237: files[i].delete();
238: }
239: dir.delete();
240: }
241: }
|