From 3ec7fb833fc17ca83a5aaa7922d8a771fec3a34b Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 28 Nov 2006 19:27:58 +0000 Subject: [PATCH] LUCENE-727: adding unit test for MMapDirectory git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@480163 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 1 + .../apache/lucene/store/TestWindowsMMap.java | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/test/org/apache/lucene/store/TestWindowsMMap.java diff --git a/CHANGES.txt b/CHANGES.txt index 9d7928f995c..53c1d3bfd27 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -283,6 +283,7 @@ Optimizations Test Cases 1. Added TestTermScorer.java (Grant Ingersoll) + 2. Added TestWindowsMMap.java (Benson Margulies via Mike McCandless) Documentation diff --git a/src/test/org/apache/lucene/store/TestWindowsMMap.java b/src/test/org/apache/lucene/store/TestWindowsMMap.java new file mode 100644 index 00000000000..1aad333cf3f --- /dev/null +++ b/src/test/org/apache/lucene/store/TestWindowsMMap.java @@ -0,0 +1,78 @@ +package org.apache.lucene.store; + +import java.util.HashSet; +import java.util.Random; +import java.io.File; + +import junit.framework.TestCase; + +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.store.FSDirectory; + +public class TestWindowsMMap extends TestCase { + + private final static String alphabet = "abcdefghijklmnopqrstuvwzyz"; + private Random random; + + public void setUp() { + random = new Random(); + System.setProperty("org.apache.lucene.FSDirectory.class", "org.apache.lucene.store.MMapDirectory"); + } + + private String randomToken() { + int tl = 1 + random.nextInt(7); + StringBuffer sb = new StringBuffer(); + for(int cx = 0; cx < tl; cx ++) { + int c = random.nextInt(25); + sb.append(alphabet.substring(c, c+1)); + } + return sb.toString(); + } + + private String randomField() { + int fl = 1 + random.nextInt(3); + StringBuffer fb = new StringBuffer(); + for(int fx = 0; fx < fl; fx ++) { + fb.append(randomToken()); + fb.append(" "); + } + return fb.toString(); + } + + private final static String storePathname = "testLuceneMmap"; + + public void testMmapIndex() throws Exception { + FSDirectory storeDirectory; + storeDirectory = FSDirectory.getDirectory(storePathname, true); + + // plan to add a set of useful stopwords, consider changing some of the + // interior filters. + StandardAnalyzer analyzer = new StandardAnalyzer(new HashSet()); + // TODO: something about lock timeouts and leftover locks. + IndexWriter writer = new IndexWriter(storeDirectory, analyzer, true); + IndexSearcher searcher = new IndexSearcher(storePathname); + + for(int dx = 0; dx < 1000; dx ++) { + String f = randomField(); + Document doc = new Document(); + doc.add(new Field("data", f, Field.Store.YES, Field.Index.TOKENIZED)); + writer.addDocument(doc); + } + + searcher.close(); + writer.close(); + rmDir(new File(storePathname)); + } + + private void rmDir(File dir) { + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) { + files[i].delete(); + } + dir.delete(); + } +}