~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/store/TestRAMDirectory.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.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;
38
 
 
39
 
/**
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.
42
 
 */
43
 
public class TestRAMDirectory extends LuceneTestCase {
44
 
  
45
 
  private File indexDir = null;
46
 
  
47
 
  // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
48
 
  private final int docsToAdd = 500;
49
 
  
50
 
  // setup the index
51
 
  @Override
52
 
  public void setUp() throws Exception {
53
 
    super.setUp();
54
 
    indexDir = _TestUtil.getTempDir("RAMDirIndex");
55
 
    
56
 
    Directory dir = newFSDirectory(indexDir);
57
 
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
58
 
        TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
59
 
    // add some documents
60
 
    Document doc = null;
61
 
    for (int i = 0; i < docsToAdd; i++) {
62
 
      doc = new Document();
63
 
      doc.add(newField("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED));
64
 
      writer.addDocument(doc);
65
 
    }
66
 
    assertEquals(docsToAdd, writer.maxDoc());
67
 
    writer.close();
68
 
    dir.close();
69
 
  }
70
 
  
71
 
  public void testRAMDirectory () throws IOException {
72
 
    
73
 
    Directory dir = newFSDirectory(indexDir);
74
 
    MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
75
 
    
76
 
    // close the underlaying directory
77
 
    dir.close();
78
 
    
79
 
    // Check size
80
 
    assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
81
 
    
82
 
    // open reader to test document count
83
 
    IndexReader reader = IndexReader.open(ramDir, true);
84
 
    assertEquals(docsToAdd, reader.numDocs());
85
 
    
86
 
    // open search zo check if all doc's are there
87
 
    IndexSearcher searcher = newSearcher(reader);
88
 
    
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);
93
 
    }
94
 
 
95
 
    // cleanup
96
 
    reader.close();
97
 
    searcher.close();
98
 
  }
99
 
  
100
 
  private final int numThreads = 10;
101
 
  private final int docsPerThread = 40;
102
 
  
103
 
  public void testRAMDirectorySize() throws IOException, InterruptedException {
104
 
      
105
 
    Directory dir = newFSDirectory(indexDir);
106
 
    final MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));
107
 
    dir.close();
108
 
    
109
 
    final IndexWriter writer = new IndexWriter(ramDir, new IndexWriterConfig(
110
 
        TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
111
 
    writer.optimize();
112
 
    
113
 
    assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
114
 
    
115
 
    Thread[] threads = new Thread[numThreads];
116
 
    for (int i=0; i<numThreads; i++) {
117
 
      final int num = i;
118
 
      threads[i] = new Thread(){
119
 
        @Override
120
 
        public void run() {
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));
124
 
            try {
125
 
              writer.addDocument(doc);
126
 
            } catch (IOException e) {
127
 
              throw new RuntimeException(e);
128
 
            }
129
 
          }
130
 
        }
131
 
      };
132
 
    }
133
 
    for (int i=0; i<numThreads; i++)
134
 
      threads[i].start();
135
 
    for (int i=0; i<numThreads; i++)
136
 
      threads[i].join();
137
 
 
138
 
    writer.optimize();
139
 
    assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
140
 
    
141
 
    writer.close();
142
 
  }
143
 
 
144
 
 
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);
152
 
    out.close();
153
 
    assertTrue("contains more then just header", headerSize < bos.size());
154
 
  } 
155
 
 
156
 
  @Override
157
 
  public void tearDown() throws Exception {
158
 
    // cleanup 
159
 
    if (indexDir != null && indexDir.exists()) {
160
 
      rmDir (indexDir);
161
 
    }
162
 
    super.tearDown();
163
 
  }
164
 
 
165
 
  // LUCENE-1196
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);
171
 
    o.close();
172
 
    IndexInput i = dir.openInput("out");
173
 
    i.seek(1024);
174
 
    i.close();
175
 
    dir.close();
176
 
  }
177
 
  
178
 
  private void rmDir(File dir) {
179
 
    File[] files = dir.listFiles();
180
 
    for (int i = 0; i < files.length; i++) {
181
 
      files[i].delete();
182
 
    }
183
 
    dir.delete();
184
 
  }
185
 
 
186
 
  // LUCENE-2852
187
 
  public void testSeekToEOFThenBack() throws Exception {
188
 
    RAMDirectory dir = new RAMDirectory();
189
 
 
190
 
    IndexOutput o = dir.createOutput("out");
191
 
    byte[] bytes = new byte[3*RAMInputStream.BUFFER_SIZE];
192
 
    o.writeBytes(bytes, 0, bytes.length);
193
 
    o.close();
194
 
 
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);
200
 
    i.close();
201
 
    dir.close();
202
 
  }
203
 
}