pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / store / TestRAMDirectory.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.ObjectOutput;
23 import java.io.ObjectOutputStream;
24 import java.io.ByteArrayOutputStream;
25
26 import org.apache.lucene.util.LuceneTestCase;
27 import org.apache.lucene.util._TestUtil;
28 import org.apache.lucene.analysis.MockAnalyzer;
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.document.Field;
31 import org.apache.lucene.index.IndexReader;
32 import org.apache.lucene.index.IndexWriter;
33 import org.apache.lucene.index.IndexWriterConfig;
34 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
35 import org.apache.lucene.search.IndexSearcher;
36 import org.apache.lucene.util.English;
37
38 /**
39  * JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases,
40  * but not one of them uses an different constructor other than the default constructor.
41  */
42 public class TestRAMDirectory extends LuceneTestCase {
43   
44   private File indexDir = null;
45   
46   // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
47   private final int docsToAdd = 500;
48   
49   // setup the index
50   @Override
51   public void setUp() throws Exception {
52     super.setUp();
53     indexDir = _TestUtil.getTempDir("RAMDirIndex");
54     
55     Directory dir = newFSDirectory(indexDir);
56     IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
57         TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
58     // add some documents
59     Document doc = null;
60     for (int i = 0; i < docsToAdd; i++) {
61       doc = new Document();
62       doc.add(newField("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED));
63       writer.addDocument(doc);
64     }
65     assertEquals(docsToAdd, writer.maxDoc());
66     writer.close();
67     dir.close();
68   }
69   
70   public void testRAMDirectory () throws IOException {
71     
72     Directory dir = newFSDirectory(indexDir);
73     MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
74     
75     // close the underlaying directory
76     dir.close();
77     
78     // Check size
79     assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
80     
81     // open reader to test document count
82     IndexReader reader = IndexReader.open(ramDir, true);
83     assertEquals(docsToAdd, reader.numDocs());
84     
85     // open search zo check if all doc's are there
86     IndexSearcher searcher = newSearcher(reader);
87     
88     // search for all documents
89     for (int i = 0; i < docsToAdd; i++) {
90       Document doc = searcher.doc(i);
91       assertTrue(doc.getField("content") != null);
92     }
93
94     // cleanup
95     reader.close();
96     searcher.close();
97   }
98   
99   private final int numThreads = 10;
100   private final int docsPerThread = 40;
101   
102   public void testRAMDirectorySize() throws IOException, InterruptedException {
103       
104     Directory dir = newFSDirectory(indexDir);
105     final MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
106     dir.close();
107     
108     final IndexWriter writer = new IndexWriter(ramDir, new IndexWriterConfig(
109         TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
110     writer.forceMerge(1);
111     
112     assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
113     
114     Thread[] threads = new Thread[numThreads];
115     for (int i=0; i<numThreads; i++) {
116       final int num = i;
117       threads[i] = new Thread(){
118         @Override
119         public void run() {
120           for (int j=1; j<docsPerThread; j++) {
121             Document doc = new Document();
122             doc.add(newField("sizeContent", English.intToEnglish(num*docsPerThread+j).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED));
123             try {
124               writer.addDocument(doc);
125             } catch (IOException e) {
126               throw new RuntimeException(e);
127             }
128           }
129         }
130       };
131     }
132     for (int i=0; i<numThreads; i++)
133       threads[i].start();
134     for (int i=0; i<numThreads; i++)
135       threads[i].join();
136
137     writer.forceMerge(1);
138     assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
139     
140     writer.close();
141   }
142
143
144   public void testSerializable() throws IOException {
145     Directory dir = new RAMDirectory();
146     ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
147     assertEquals("initially empty", 0, bos.size());
148     ObjectOutput out = new ObjectOutputStream(bos);
149     int headerSize = bos.size();
150     out.writeObject(dir);
151     out.close();
152     assertTrue("contains more then just header", headerSize < bos.size());
153   } 
154
155   @Override
156   public void tearDown() throws Exception {
157     // cleanup 
158     if (indexDir != null && indexDir.exists()) {
159       rmDir (indexDir);
160     }
161     super.tearDown();
162   }
163
164   // LUCENE-1196
165   public void testIllegalEOF() throws Exception {
166     RAMDirectory dir = new RAMDirectory();
167     IndexOutput o = dir.createOutput("out");
168     byte[] b = new byte[1024];
169     o.writeBytes(b, 0, 1024);
170     o.close();
171     IndexInput i = dir.openInput("out");
172     i.seek(1024);
173     i.close();
174     dir.close();
175   }
176   
177   private void rmDir(File dir) {
178     File[] files = dir.listFiles();
179     for (int i = 0; i < files.length; i++) {
180       files[i].delete();
181     }
182     dir.delete();
183   }
184
185   // LUCENE-2852
186   public void testSeekToEOFThenBack() throws Exception {
187     RAMDirectory dir = new RAMDirectory();
188
189     IndexOutput o = dir.createOutput("out");
190     byte[] bytes = new byte[3*RAMInputStream.BUFFER_SIZE];
191     o.writeBytes(bytes, 0, bytes.length);
192     o.close();
193
194     IndexInput i = dir.openInput("out");
195     i.seek(2*RAMInputStream.BUFFER_SIZE-1);
196     i.seek(3*RAMInputStream.BUFFER_SIZE);
197     i.seek(RAMInputStream.BUFFER_SIZE);
198     i.readBytes(bytes, 0, 2*RAMInputStream.BUFFER_SIZE);
199     i.close();
200     dir.close();
201   }
202 }