001: package org.apache.lucene.store;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.HashSet;
021: import java.util.Random;
022: import java.io.File;
023:
024: import org.apache.lucene.util.LuceneTestCase;
025:
026: import org.apache.lucene.analysis.standard.StandardAnalyzer;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.document.Field;
029: import org.apache.lucene.index.IndexWriter;
030: import org.apache.lucene.search.IndexSearcher;
031: import org.apache.lucene.store.FSDirectory;
032:
033: public class TestWindowsMMap extends LuceneTestCase {
034:
035: private final static String alphabet = "abcdefghijklmnopqrstuvwzyz";
036: private Random random;
037:
038: public void setUp() throws Exception {
039: super .setUp();
040: random = new Random();
041: System.setProperty("org.apache.lucene.FSDirectory.class",
042: "org.apache.lucene.store.MMapDirectory");
043: }
044:
045: private String randomToken() {
046: int tl = 1 + random.nextInt(7);
047: StringBuffer sb = new StringBuffer();
048: for (int cx = 0; cx < tl; cx++) {
049: int c = random.nextInt(25);
050: sb.append(alphabet.substring(c, c + 1));
051: }
052: return sb.toString();
053: }
054:
055: private String randomField() {
056: int fl = 1 + random.nextInt(3);
057: StringBuffer fb = new StringBuffer();
058: for (int fx = 0; fx < fl; fx++) {
059: fb.append(randomToken());
060: fb.append(" ");
061: }
062: return fb.toString();
063: }
064:
065: private final static String storePathname = new File(System
066: .getProperty("tempDir"), "testLuceneMmap")
067: .getAbsolutePath();
068:
069: public void testMmapIndex() throws Exception {
070: FSDirectory storeDirectory;
071: storeDirectory = FSDirectory.getDirectory(storePathname);
072:
073: // plan to add a set of useful stopwords, consider changing some of the
074: // interior filters.
075: StandardAnalyzer analyzer = new StandardAnalyzer(new HashSet());
076: // TODO: something about lock timeouts and leftover locks.
077: IndexWriter writer = new IndexWriter(storeDirectory, analyzer,
078: true);
079: IndexSearcher searcher = new IndexSearcher(storePathname);
080:
081: for (int dx = 0; dx < 1000; dx++) {
082: String f = randomField();
083: Document doc = new Document();
084: doc.add(new Field("data", f, Field.Store.YES,
085: Field.Index.TOKENIZED));
086: writer.addDocument(doc);
087: }
088:
089: searcher.close();
090: writer.close();
091: rmDir(new File(storePathname));
092: }
093:
094: private void rmDir(File dir) {
095: File[] files = dir.listFiles();
096: for (int i = 0; i < files.length; i++) {
097: files[i].delete();
098: }
099: dir.delete();
100: }
101: }
|