pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / 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.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;
35
36 public class TestWindowsMMap extends LuceneTestCase {
37   
38   private final static String alphabet = "abcdefghijklmnopqrstuvwzyz";
39   
40   @Override
41   public void setUp() throws Exception {
42     super.setUp();
43   }
44   
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));
51     }
52     return sb.toString();
53   }
54   
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());
60       fb.append(" ");
61     }
62     return fb.toString();
63   }
64   
65   private final static String storePathname = 
66    _TestUtil.getTempDir("testLuceneMmap").getAbsolutePath();
67
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);
73     rmDir(dirPath);
74     MMapDirectory dir = new MMapDirectory(dirPath, null);
75     
76     // plan to add a set of useful stopwords, consider changing some of the
77     // interior filters.
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));
83     writer.commit();
84     IndexReader reader = IndexReader.open(dir);
85     IndexSearcher searcher = new IndexSearcher(reader);
86     
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);
93     }
94     
95     searcher.close();
96     reader.close();
97     writer.close();
98     rmDir(dirPath);
99   }
100
101   private void rmDir(File dir) {
102     if (!dir.exists()) {
103       return;
104     }
105     for (File file : dir.listFiles()) {
106       file.delete();
107     }
108     dir.delete();
109   }
110 }