001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.lucene.store.jdbc;
018:
019: import java.io.IOException;
020: import java.sql.Connection;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: import org.apache.lucene.analysis.SimpleAnalyzer;
026: import org.apache.lucene.document.Document;
027: import org.apache.lucene.document.Field;
028: import org.apache.lucene.index.IndexWriter;
029: import org.apache.lucene.store.Directory;
030: import org.apache.lucene.store.DirectoryTemplate;
031: import org.apache.lucene.store.FSDirectory;
032: import org.apache.lucene.store.RAMDirectory;
033: import org.apache.lucene.store.jdbc.datasource.DataSourceUtils;
034:
035: /**
036: * @author kimchy
037: */
038: public class SimpleVsTests extends AbstractJdbcDirectoryTests {
039:
040: private boolean DISABLE = true;
041:
042: private Directory fsDir;
043: private Directory ramDir;
044: private Directory jdbcDir;
045: private Collection docs = loadDocuments(3000, 5);
046: private boolean useCompoundFile = false;
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050: String fsIndexDir = System.getProperty("java.io.tmpdir", "tmp")
051: + System.getProperty("file.separator") + "fs-index";
052:
053: ramDir = new RAMDirectory();
054: fsDir = FSDirectory.getDirectory(fsIndexDir, true);
055: jdbcDir = new JdbcDirectory(dataSource, createDialect(), "TEST");
056:
057: Connection con = DataSourceUtils.getConnection(dataSource);
058: ((JdbcDirectory) jdbcDir).create();
059: DataSourceUtils.commitConnectionIfPossible(con);
060: DataSourceUtils.releaseConnection(con);
061: }
062:
063: public void testTiming() throws IOException {
064: if (DISABLE) {
065: return;
066: }
067: long ramTiming = timeIndexWriter(ramDir);
068: long fsTiming = timeIndexWriter(fsDir);
069: long jdbcTiming = timeIndexWriter(jdbcDir);
070:
071: assertTrue(fsTiming > ramTiming);
072:
073: System.out.println("RAMDirectory Time: " + (ramTiming) + " ms");
074: System.out.println("FSDirectory Time : " + (fsTiming) + " ms");
075: System.out.println("JdbcDirectory Time : " + (jdbcTiming)
076: + " ms");
077: }
078:
079: private long timeIndexWriter(Directory dir) throws IOException {
080: long start = System.currentTimeMillis();
081: addDocuments(dir);
082: long stop = System.currentTimeMillis();
083: return (stop - start);
084: }
085:
086: private void addDocuments(Directory dir) throws IOException {
087: DirectoryTemplate template = new DirectoryTemplate(dir);
088: template
089: .execute(new DirectoryTemplate.DirectoryCallbackWithoutResult() {
090: protected void doInDirectoryWithoutResult(
091: Directory dir) throws IOException {
092: IndexWriter writer = new IndexWriter(dir,
093: new SimpleAnalyzer(), true);
094: writer.setUseCompoundFile(useCompoundFile);
095:
096: /**
097: // change to adjust performance of indexing with FSDirectory
098: writer.mergeFactor = writer.mergeFactor;
099: writer.maxMergeDocs = writer.maxMergeDocs;
100: writer.minMergeDocs = writer.minMergeDocs;
101: */
102:
103: for (Iterator iter = docs.iterator(); iter
104: .hasNext();) {
105: Document doc = new Document();
106: String word = (String) iter.next();
107: doc.add(new Field("keyword", word,
108: Field.Store.YES,
109: Field.Index.UN_TOKENIZED));
110: doc.add(new Field("unindexed", word,
111: Field.Store.YES, Field.Index.NO));
112: doc.add(new Field("unstored", word,
113: Field.Store.NO,
114: Field.Index.TOKENIZED));
115: doc.add(new Field("text", word,
116: Field.Store.YES,
117: Field.Index.TOKENIZED));
118: writer.addDocument(doc);
119: }
120: writer.optimize();
121: writer.close();
122: }
123: });
124: }
125:
126: private Collection loadDocuments(int numDocs, int wordsPerDoc) {
127: Collection docs = new ArrayList(numDocs);
128: for (int i = 0; i < numDocs; i++) {
129: StringBuffer doc = new StringBuffer(wordsPerDoc);
130: for (int j = 0; j < wordsPerDoc; j++) {
131: doc.append("Bibamus ");
132: }
133: docs.add(doc.toString());
134: }
135: return docs;
136: }
137:
138: }
|