X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/store/TestRAMDirectory.java diff --git a/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/store/TestRAMDirectory.java b/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/store/TestRAMDirectory.java new file mode 100644 index 0000000..b0f599c --- /dev/null +++ b/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/store/TestRAMDirectory.java @@ -0,0 +1,203 @@ +package org.apache.lucene.store; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.io.ByteArrayOutputStream; + +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; +import org.apache.lucene.analysis.MockAnalyzer; +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.IndexWriterConfig.OpenMode; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.util.English; + +/** + * JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases, + * but not one of them uses an different constructor other than the default constructor. + */ +public class TestRAMDirectory extends LuceneTestCase { + + private File indexDir = null; + + // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE + private final int docsToAdd = 500; + + // setup the index + @Override + public void setUp() throws Exception { + super.setUp(); + indexDir = _TestUtil.getTempDir("RAMDirIndex"); + + Directory dir = newFSDirectory(indexDir); + IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( + TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE)); + // add some documents + Document doc = null; + for (int i = 0; i < docsToAdd; i++) { + doc = new Document(); + doc.add(newField("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.NOT_ANALYZED)); + writer.addDocument(doc); + } + assertEquals(docsToAdd, writer.maxDoc()); + writer.close(); + dir.close(); + } + + public void testRAMDirectory () throws IOException { + + Directory dir = newFSDirectory(indexDir); + MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir)); + + // close the underlaying directory + dir.close(); + + // Check size + assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes()); + + // open reader to test document count + IndexReader reader = IndexReader.open(ramDir, true); + assertEquals(docsToAdd, reader.numDocs()); + + // open search zo check if all doc's are there + IndexSearcher searcher = newSearcher(reader); + + // search for all documents + for (int i = 0; i < docsToAdd; i++) { + Document doc = searcher.doc(i); + assertTrue(doc.getField("content") != null); + } + + // cleanup + reader.close(); + searcher.close(); + } + + private final int numThreads = 10; + private final int docsPerThread = 40; + + public void testRAMDirectorySize() throws IOException, InterruptedException { + + Directory dir = newFSDirectory(indexDir); + final MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir)); + dir.close(); + + final IndexWriter writer = new IndexWriter(ramDir, new IndexWriterConfig( + TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND)); + writer.optimize(); + + assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes()); + + Thread[] threads = new Thread[numThreads]; + for (int i=0; i