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.IndexReader;
31 import org.apache.lucene.index.IndexWriter;
32 import org.apache.lucene.index.IndexWriterConfig;
33 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
34 import org.apache.lucene.search.IndexSearcher;
36 public class TestWindowsMMap extends LuceneTestCase {
38 private final static String alphabet = "abcdefghijklmnopqrstuvwzyz";
41 public void setUp() throws Exception {
45 private String randomToken() {
46 int tl = 1 + random.nextInt(7);
47 StringBuilder sb = new StringBuilder();
48 for(int cx = 0; cx < tl; cx ++) {
49 int c = random.nextInt(25);
50 sb.append(alphabet.substring(c, c+1));
55 private String randomField() {
56 int fl = 1 + random.nextInt(3);
57 StringBuilder fb = new StringBuilder();
58 for(int fx = 0; fx < fl; fx ++) {
59 fb.append(randomToken());
65 private final static String storePathname =
66 _TestUtil.getTempDir("testLuceneMmap").getAbsolutePath();
68 public void testMmapIndex() throws Exception {
69 // sometimes the directory is not cleaned by rmDir, because on Windows it
70 // may take some time until the files are finally dereferenced. So clean the
71 // directory up front, or otherwise new IndexWriter will fail.
72 File dirPath = new File(storePathname);
74 MMapDirectory dir = new MMapDirectory(dirPath, null);
76 // plan to add a set of useful stopwords, consider changing some of the
78 MockAnalyzer analyzer = new MockAnalyzer(random);
79 // TODO: something about lock timeouts and leftover locks.
80 IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
81 TEST_VERSION_CURRENT, analyzer)
82 .setOpenMode(OpenMode.CREATE));
84 IndexReader reader = IndexReader.open(dir);
85 IndexSearcher searcher = new IndexSearcher(reader);
87 int num = atLeast(1000);
88 for (int dx = 0; dx < num; dx++) {
89 String f = randomField();
90 Document doc = new Document();
91 doc.add(newField("data", f, Field.Store.YES, Field.Index.ANALYZED));
92 writer.addDocument(doc);
101 private void rmDir(File dir) {
105 for (File file : dir.listFiles()) {