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.analysis.WhitespaceAnalyzer;
30
import org.apache.lucene.document.Document;
31
import org.apache.lucene.document.Field;
32
import org.apache.lucene.index.IndexReader;
33
import org.apache.lucene.index.IndexWriter;
34
import org.apache.lucene.index.IndexWriterConfig;
35
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
36
import org.apache.lucene.search.IndexSearcher;
37
import org.apache.lucene.util.English;
40
* JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases,
41
* but not one of them uses an different constructor other than the default constructor.
43
public class TestRAMDirectory extends LuceneTestCase {
45
private File indexDir = null;
47
// add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
48
private final int docsToAdd = 500;
52
public void setUp() throws Exception {
54
indexDir = _TestUtil.getTempDir("RAMDirIndex");
56
Directory dir = newFSDirectory(indexDir);
57
IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
58
TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
61
for (int i = 0; i < docsToAdd; i++) {
63
doc.add(newField("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED));
64
writer.addDocument(doc);
66
assertEquals(docsToAdd, writer.maxDoc());
71
public void testRAMDirectory () throws IOException {
73
Directory dir = newFSDirectory(indexDir);
74
MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
76
// close the underlaying directory
80
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
82
// open reader to test document count
83
IndexReader reader = IndexReader.open(ramDir, true);
84
assertEquals(docsToAdd, reader.numDocs());
86
// open search zo check if all doc's are there
87
IndexSearcher searcher = newSearcher(reader);
89
// search for all documents
90
for (int i = 0; i < docsToAdd; i++) {
91
Document doc = searcher.doc(i);
92
assertTrue(doc.getField("content") != null);
100
private final int numThreads = 10;
101
private final int docsPerThread = 40;
103
public void testRAMDirectorySize() throws IOException, InterruptedException {
105
Directory dir = newFSDirectory(indexDir);
106
final MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
109
final IndexWriter writer = new IndexWriter(ramDir, new IndexWriterConfig(
110
TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
113
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
115
Thread[] threads = new Thread[numThreads];
116
for (int i=0; i<numThreads; i++) {
118
threads[i] = new Thread(){
121
for (int j=1; j<docsPerThread; j++) {
122
Document doc = new Document();
123
doc.add(newField("sizeContent", English.intToEnglish(num*docsPerThread+j).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED));
125
writer.addDocument(doc);
126
} catch (IOException e) {
127
throw new RuntimeException(e);
133
for (int i=0; i<numThreads; i++)
135
for (int i=0; i<numThreads; i++)
139
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
145
public void testSerializable() throws IOException {
146
Directory dir = new RAMDirectory();
147
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
148
assertEquals("initially empty", 0, bos.size());
149
ObjectOutput out = new ObjectOutputStream(bos);
150
int headerSize = bos.size();
151
out.writeObject(dir);
153
assertTrue("contains more then just header", headerSize < bos.size());
157
public void tearDown() throws Exception {
159
if (indexDir != null && indexDir.exists()) {
166
public void testIllegalEOF() throws Exception {
167
RAMDirectory dir = new RAMDirectory();
168
IndexOutput o = dir.createOutput("out");
169
byte[] b = new byte[1024];
170
o.writeBytes(b, 0, 1024);
172
IndexInput i = dir.openInput("out");
178
private void rmDir(File dir) {
179
File[] files = dir.listFiles();
180
for (int i = 0; i < files.length; i++) {
187
public void testSeekToEOFThenBack() throws Exception {
188
RAMDirectory dir = new RAMDirectory();
190
IndexOutput o = dir.createOutput("out");
191
byte[] bytes = new byte[3*RAMInputStream.BUFFER_SIZE];
192
o.writeBytes(bytes, 0, bytes.length);
195
IndexInput i = dir.openInput("out");
196
i.seek(2*RAMInputStream.BUFFER_SIZE-1);
197
i.seek(3*RAMInputStream.BUFFER_SIZE);
198
i.seek(RAMInputStream.BUFFER_SIZE);
199
i.readBytes(bytes, 0, 2*RAMInputStream.BUFFER_SIZE);