1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import java.io.IOException;
22 import java.io.ObjectOutput;
23 import java.io.ObjectOutputStream;
24 import java.io.ByteArrayOutputStream;
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;
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.
42 public class TestRAMDirectory extends LuceneTestCase {
44 private File indexDir = null;
46 // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
47 private final int docsToAdd = 500;
51 public void setUp() throws Exception {
53 indexDir = _TestUtil.getTempDir("RAMDirIndex");
55 Directory dir = newFSDirectory(indexDir);
56 IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
57 TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
60 for (int i = 0; i < docsToAdd; i++) {
62 doc.add(newField("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED));
63 writer.addDocument(doc);
65 assertEquals(docsToAdd, writer.maxDoc());
70 public void testRAMDirectory () throws IOException {
72 Directory dir = newFSDirectory(indexDir);
73 MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
75 // close the underlaying directory
79 assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
81 // open reader to test document count
82 IndexReader reader = IndexReader.open(ramDir, true);
83 assertEquals(docsToAdd, reader.numDocs());
85 // open search zo check if all doc's are there
86 IndexSearcher searcher = newSearcher(reader);
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);
99 private final int numThreads = 10;
100 private final int docsPerThread = 40;
102 public void testRAMDirectorySize() throws IOException, InterruptedException {
104 Directory dir = newFSDirectory(indexDir);
105 final MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
108 final IndexWriter writer = new IndexWriter(ramDir, new IndexWriterConfig(
109 TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
110 writer.forceMerge(1);
112 assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
114 Thread[] threads = new Thread[numThreads];
115 for (int i=0; i<numThreads; i++) {
117 threads[i] = new Thread(){
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));
124 writer.addDocument(doc);
125 } catch (IOException e) {
126 throw new RuntimeException(e);
132 for (int i=0; i<numThreads; i++)
134 for (int i=0; i<numThreads; i++)
137 writer.forceMerge(1);
138 assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
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);
152 assertTrue("contains more then just header", headerSize < bos.size());
156 public void tearDown() throws Exception {
158 if (indexDir != null && indexDir.exists()) {
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);
171 IndexInput i = dir.openInput("out");
177 private void rmDir(File dir) {
178 File[] files = dir.listFiles();
179 for (int i = 0; i < files.length; i++) {
186 public void testSeekToEOFThenBack() throws Exception {
187 RAMDirectory dir = new RAMDirectory();
189 IndexOutput o = dir.createOutput("out");
190 byte[] bytes = new byte[3*RAMInputStream.BUFFER_SIZE];
191 o.writeBytes(bytes, 0, bytes.length);
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);