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.util.Random;
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;
33
* Tests MMapDirectory's MultiMMapIndexInput
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.
39
public class TestMultiMMap extends LuceneTestCase {
43
public void setUp() throws Exception {
45
assumeTrue("test requires a jre that supports unmapping", MMapDirectory.UNMAP_SUPPORTED);
46
workDir = _TestUtil.getTempDir("TestMultiMMap");
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");
56
IndexInput ii = mmapDir.openInput("zeroBytes");
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);
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));
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);
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];
99
ii.readBytes(slice, 0, slice.length);
100
assertEquals(new BytesRef(bytes, sliceStart, sliceLength), new BytesRef(slice));
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));
114
private void assertChunking(Random random, int chunkSize) throws Exception {
115
File path = _TestUtil.createTempFile("mmap" + chunkSize, "tmp", workDir);
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);
132
for (int i = 0; i < numDocs; i++) {
133
docid.setValue("" + i);
134
junk.setValue(_TestUtil.randomUnicodeString(random));
135
writer.addDocument(doc);
137
IndexReader reader = writer.getReader();
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"));