KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > IndexTest


1 package org.apache.lucene;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.analysis.SimpleAnalyzer;
20 import org.apache.lucene.index.IndexWriter;
21 import org.apache.lucene.demo.FileDocument;
22
23 import java.io.File JavaDoc;
24 import java.util.Date JavaDoc;
25
26 class IndexTest {
27   public static void main(String JavaDoc[] args) {
28     try {
29       Date JavaDoc start = new Date JavaDoc();
30       // FIXME: OG: what's with this hard-coded dirs??
31
IndexWriter writer = new IndexWriter("F:\\test", new SimpleAnalyzer(),
32                        true);
33
34       writer.mergeFactor = 20;
35
36       // FIXME: OG: what's with this hard-coded dirs??
37
indexDocs(writer, new File JavaDoc("F:\\recipes"));
38
39       writer.optimize();
40       writer.close();
41
42       Date JavaDoc end = new Date JavaDoc();
43
44       System.out.print(end.getTime() - start.getTime());
45       System.out.println(" total milliseconds");
46
47       Runtime JavaDoc runtime = Runtime.getRuntime();
48
49       System.out.print(runtime.freeMemory());
50       System.out.println(" free memory before gc");
51       System.out.print(runtime.totalMemory());
52       System.out.println(" total memory before gc");
53
54       runtime.gc();
55
56       System.out.print(runtime.freeMemory());
57       System.out.println(" free memory after gc");
58       System.out.print(runtime.totalMemory());
59       System.out.println(" total memory after gc");
60
61     } catch (Exception JavaDoc e) {
62       System.out.println(" caught a " + e.getClass() +
63              "\n with message: " + e.getMessage());
64     }
65   }
66
67   public static void indexDocs(IndexWriter writer, File JavaDoc file)
68        throws Exception JavaDoc {
69     if (file.isDirectory()) {
70       String JavaDoc[] files = file.list();
71       for (int i = 0; i < files.length; i++)
72     indexDocs(writer, new File JavaDoc(file, files[i]));
73     } else {
74       System.out.println("adding " + file);
75       writer.addDocument(FileDocument.Document(file));
76     }
77   }
78 }
79
Popular Tags