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.
20 import java.util.Collections;
23 import org.apache.lucene.util.LuceneTestCase;
24 import org.apache.lucene.util._TestUtil;
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.analysis.standard.StandardAnalyzer;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.index.IndexWriter;
31 import org.apache.lucene.index.IndexWriterConfig;
32 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
33 import org.apache.lucene.search.IndexSearcher;
35 public class TestWindowsMMap extends LuceneTestCase {
37 private final static String alphabet = "abcdefghijklmnopqrstuvwzyz";
40 public void setUp() throws Exception {
44 private String randomToken() {
45 int tl = 1 + random.nextInt(7);
46 StringBuilder sb = new StringBuilder();
47 for(int cx = 0; cx < tl; cx ++) {
48 int c = random.nextInt(25);
49 sb.append(alphabet.substring(c, c+1));
54 private String randomField() {
55 int fl = 1 + random.nextInt(3);
56 StringBuilder fb = new StringBuilder();
57 for(int fx = 0; fx < fl; fx ++) {
58 fb.append(randomToken());
64 private final static String storePathname =
65 _TestUtil.getTempDir("testLuceneMmap").getAbsolutePath();
67 public void testMmapIndex() throws Exception {
68 // sometimes the directory is not cleaned by rmDir, because on Windows it
69 // may take some time until the files are finally dereferenced. So clean the
70 // directory up front, or otherwise new IndexWriter will fail.
71 File dirPath = new File(storePathname);
73 MMapDirectory dir = new MMapDirectory(dirPath, null);
75 // plan to add a set of useful stopwords, consider changing some of the
77 MockAnalyzer analyzer = new MockAnalyzer(random);
78 // TODO: something about lock timeouts and leftover locks.
79 IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
80 TEST_VERSION_CURRENT, analyzer)
81 .setOpenMode(OpenMode.CREATE));
83 IndexSearcher searcher = new IndexSearcher(dir, true);
85 int num = atLeast(1000);
86 for (int dx = 0; dx < num; dx++) {
87 String f = randomField();
88 Document doc = new Document();
89 doc.add(newField("data", f, Field.Store.YES, Field.Index.ANALYZED));
90 writer.addDocument(doc);
98 private void rmDir(File dir) {
102 for (File file : dir.listFiles()) {