pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / store / TestWindowsMMap.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.util.Collections;
21 import java.io.File;
22
23 import org.apache.lucene.util.LuceneTestCase;
24 import org.apache.lucene.util._TestUtil;
25
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;
34
35 public class TestWindowsMMap extends LuceneTestCase {
36   
37   private final static String alphabet = "abcdefghijklmnopqrstuvwzyz";
38   
39   @Override
40   public void setUp() throws Exception {
41     super.setUp();
42   }
43   
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));
50     }
51     return sb.toString();
52   }
53   
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());
59       fb.append(" ");
60     }
61     return fb.toString();
62   }
63   
64   private final static String storePathname = 
65    _TestUtil.getTempDir("testLuceneMmap").getAbsolutePath();
66
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);
72     rmDir(dirPath);
73     MMapDirectory dir = new MMapDirectory(dirPath, null);
74     
75     // plan to add a set of useful stopwords, consider changing some of the
76     // interior filters.
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));
82     writer.commit();
83     IndexSearcher searcher = new IndexSearcher(dir, true);
84     
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);
91     }
92     
93     searcher.close();
94     writer.close();
95     rmDir(dirPath);
96   }
97
98   private void rmDir(File dir) {
99     if (!dir.exists()) {
100       return;
101     }
102     for (File file : dir.listFiles()) {
103       file.delete();
104     }
105     dir.delete();
106   }
107 }