pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / store / TestMultiMMap.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.util.Random;
22
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.RandomIndexWriter;
28 import org.apache.lucene.util.BytesRef;
29 import org.apache.lucene.util.LuceneTestCase;
30 import org.apache.lucene.util._TestUtil;
31
32 /**
33  * Tests MMapDirectory's MultiMMapIndexInput
34  * <p>
35  * Because Java's ByteBuffer uses an int to address the
36  * values, it's necessary to access a file >
37  * Integer.MAX_VALUE in size using multiple byte buffers.
38  */
39 public class TestMultiMMap extends LuceneTestCase {
40   File workDir;
41   
42   @Override
43   public void setUp() throws Exception {
44     super.setUp();
45     assumeTrue("test requires a jre that supports unmapping", MMapDirectory.UNMAP_SUPPORTED);
46     workDir = _TestUtil.getTempDir("TestMultiMMap");
47     workDir.mkdirs();
48   }
49
50   public void testSeekZero() throws Exception {
51     for (int i = 0; i < 31; i++) {
52       MMapDirectory mmapDir = new MMapDirectory(_TestUtil.getTempDir("testSeekZero"));
53       mmapDir.setMaxChunkSize(1<<i);
54       IndexOutput io = mmapDir.createOutput("zeroBytes");
55       io.close();
56       IndexInput ii = mmapDir.openInput("zeroBytes");
57       ii.seek(0L);
58       ii.close();
59       mmapDir.close();
60     }
61   }
62   
63   public void testSeekEnd() throws Exception {
64     for (int i = 0; i < 17; i++) {
65       MMapDirectory mmapDir = new MMapDirectory(_TestUtil.getTempDir("testSeekEnd"));
66       mmapDir.setMaxChunkSize(1<<i);
67       IndexOutput io = mmapDir.createOutput("bytes");
68       byte bytes[] = new byte[1<<i];
69       random.nextBytes(bytes);
70       io.writeBytes(bytes, bytes.length);
71       io.close();
72       IndexInput ii = mmapDir.openInput("bytes");
73       byte actual[] = new byte[1<<i];
74       ii.readBytes(actual, 0, actual.length);
75       assertEquals(new BytesRef(bytes), new BytesRef(actual));
76       ii.seek(1<<i);
77       ii.close();
78       mmapDir.close();
79     }
80   }
81   
82   public void testSeeking() throws Exception {
83     for (int i = 0; i < 10; i++) {
84       MMapDirectory mmapDir = new MMapDirectory(_TestUtil.getTempDir("testSeeking"));
85       mmapDir.setMaxChunkSize(1<<i);
86       IndexOutput io = mmapDir.createOutput("bytes");
87       byte bytes[] = new byte[1<<(i+1)]; // make sure we switch buffers
88       random.nextBytes(bytes);
89       io.writeBytes(bytes, bytes.length);
90       io.close();
91       IndexInput ii = mmapDir.openInput("bytes");
92       byte actual[] = new byte[1<<(i+1)]; // first read all bytes
93       ii.readBytes(actual, 0, actual.length);
94       assertEquals(new BytesRef(bytes), new BytesRef(actual));
95       for (int sliceStart = 0; sliceStart < bytes.length; sliceStart++) {
96         for (int sliceLength = 0; sliceLength < bytes.length - sliceStart; sliceLength++) {
97           byte slice[] = new byte[sliceLength];
98           ii.seek(sliceStart);
99           ii.readBytes(slice, 0, slice.length);
100           assertEquals(new BytesRef(bytes, sliceStart, sliceLength), new BytesRef(slice));
101         }
102       }
103       ii.close();
104       mmapDir.close();
105     }
106   }
107   
108   public void testRandomChunkSizes() throws Exception {
109     int num = atLeast(10);
110     for (int i = 0; i < num; i++)
111       assertChunking(random, _TestUtil.nextInt(random, 20, 100));
112   }
113   
114   private void assertChunking(Random random, int chunkSize) throws Exception {
115     File path = _TestUtil.createTempFile("mmap" + chunkSize, "tmp", workDir);
116     path.delete();
117     path.mkdirs();
118     MMapDirectory mmapDir = new MMapDirectory(path);
119     mmapDir.setMaxChunkSize(chunkSize);
120     // we will map a lot, try to turn on the unmap hack
121     if (MMapDirectory.UNMAP_SUPPORTED)
122       mmapDir.setUseUnmap(true);
123     MockDirectoryWrapper dir = new MockDirectoryWrapper(random, mmapDir);
124     RandomIndexWriter writer = new RandomIndexWriter(random, dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
125     Document doc = new Document();
126     Field docid = newField("docid", "0", Field.Store.YES, Field.Index.NOT_ANALYZED);
127     Field junk = newField("junk", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
128     doc.add(docid);
129     doc.add(junk);
130     
131     int numDocs = 100;
132     for (int i = 0; i < numDocs; i++) {
133       docid.setValue("" + i);
134       junk.setValue(_TestUtil.randomUnicodeString(random));
135       writer.addDocument(doc);
136     }
137     IndexReader reader = writer.getReader();
138     writer.close();
139     
140     int numAsserts = atLeast(100);
141     for (int i = 0; i < numAsserts; i++) {
142       int docID = random.nextInt(numDocs);
143       assertEquals("" + docID, reader.document(docID).get("docid"));
144     }
145     reader.close();
146     dir.close();
147   }
148 }