KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > TermInfosTest


1 package org.apache.lucene.index;
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.store.Directory;
20 import org.apache.lucene.store.FSDirectory;
21
22 import java.util.Date JavaDoc;
23 import java.util.Random JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29
30 class TermInfosTest {
31   public static void main(String JavaDoc[] args) {
32     try {
33       test();
34     } catch (Exception JavaDoc e) {
35       System.out.println(" caught a " + e.getClass() +
36              "\n with message: " + e.getMessage());
37     }
38   }
39
40   // FIXME: OG: remove hard-coded file names
41
public static void test()
42        throws Exception JavaDoc {
43
44     File JavaDoc file = new File JavaDoc("words.txt");
45     System.out.println(" reading word file containing " +
46                file.length() + " bytes");
47
48     Date JavaDoc start = new Date JavaDoc();
49
50     Vector JavaDoc keys = new Vector JavaDoc();
51     FileInputStream JavaDoc ws = new FileInputStream JavaDoc(file);
52     BufferedReader JavaDoc wr = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(ws));
53
54     for (String JavaDoc key = wr.readLine(); key!=null; key = wr.readLine())
55       keys.addElement(new Term("word", key));
56     wr.close();
57
58     Date JavaDoc end = new Date JavaDoc();
59
60     System.out.print(end.getTime() - start.getTime());
61     System.out.println(" milliseconds to read " + keys.size() + " words");
62
63     start = new Date JavaDoc();
64
65     Random JavaDoc gen = new Random JavaDoc(1251971);
66     long fp = (gen.nextInt() & 0xF) + 1;
67     long pp = (gen.nextInt() & 0xF) + 1;
68     int[] docFreqs = new int[keys.size()];
69     long[] freqPointers = new long[keys.size()];
70     long[] proxPointers = new long[keys.size()];
71     for (int i = 0; i < keys.size(); i++) {
72       docFreqs[i] = (gen.nextInt() & 0xF) + 1;
73       freqPointers[i] = fp;
74       proxPointers[i] = pp;
75       fp += (gen.nextInt() & 0xF) + 1;;
76       pp += (gen.nextInt() & 0xF) + 1;;
77     }
78
79     end = new Date JavaDoc();
80
81     System.out.print(end.getTime() - start.getTime());
82     System.out.println(" milliseconds to generate values");
83
84     start = new Date JavaDoc();
85
86     Directory store = FSDirectory.getDirectory("test.store", true);
87     FieldInfos fis = new FieldInfos();
88
89     TermInfosWriter writer = new TermInfosWriter(store, "words", fis);
90     fis.add("word", false);
91
92     for (int i = 0; i < keys.size(); i++)
93       writer.add((Term)keys.elementAt(i),
94          new TermInfo(docFreqs[i], freqPointers[i], proxPointers[i]));
95
96     writer.close();
97
98     end = new Date JavaDoc();
99
100     System.out.print(end.getTime() - start.getTime());
101     System.out.println(" milliseconds to write table");
102
103     System.out.println(" table occupies " +
104                store.fileLength("words.tis") + " bytes");
105
106     start = new Date JavaDoc();
107
108     TermInfosReader reader = new TermInfosReader(store, "words", fis);
109
110     end = new Date JavaDoc();
111
112     System.out.print(end.getTime() - start.getTime());
113     System.out.println(" milliseconds to open table");
114
115     start = new Date JavaDoc();
116
117     SegmentTermEnum enumerator = reader.terms();
118     for (int i = 0; i < keys.size(); i++) {
119       enumerator.next();
120       Term key = (Term)keys.elementAt(i);
121       if (!key.equals(enumerator.term()))
122     throw new Exception JavaDoc("wrong term: " + enumerator.term()
123                 + ", expected: " + key
124                 + " at " + i);
125       TermInfo ti = enumerator.termInfo();
126       if (ti.docFreq != docFreqs[i])
127     throw
128       new Exception JavaDoc("wrong value: " + Long.toString(ti.docFreq, 16)
129             + ", expected: " + Long.toString(docFreqs[i], 16)
130             + " at " + i);
131       if (ti.freqPointer != freqPointers[i])
132     throw
133       new Exception JavaDoc("wrong value: " + Long.toString(ti.freqPointer, 16)
134             + ", expected: " + Long.toString(freqPointers[i], 16)
135             + " at " + i);
136       if (ti.proxPointer != proxPointers[i])
137     throw
138       new Exception JavaDoc("wrong value: " + Long.toString(ti.proxPointer, 16)
139             + ", expected: " + Long.toString(proxPointers[i], 16)
140             + " at " + i);
141     }
142
143     end = new Date JavaDoc();
144
145     System.out.print(end.getTime() - start.getTime());
146     System.out.println(" milliseconds to iterate over " +
147                keys.size() + " words");
148
149     start = new Date JavaDoc();
150
151     for (int i = 0; i < keys.size(); i++) {
152       Term key = (Term)keys.elementAt(i);
153       TermInfo ti = reader.get(key);
154       if (ti.docFreq != docFreqs[i])
155     throw
156       new Exception JavaDoc("wrong value: " + Long.toString(ti.docFreq, 16)
157             + ", expected: " + Long.toString(docFreqs[i], 16)
158             + " at " + i);
159       if (ti.freqPointer != freqPointers[i])
160     throw
161       new Exception JavaDoc("wrong value: " + Long.toString(ti.freqPointer, 16)
162             + ", expected: " + Long.toString(freqPointers[i], 16)
163             + " at " + i);
164       if (ti.proxPointer != proxPointers[i])
165     throw
166       new Exception JavaDoc("wrong value: " + Long.toString(ti.proxPointer, 16)
167             + ", expected: " + Long.toString(proxPointers[i], 16)
168             + " at " + i);
169     }
170
171     end = new Date JavaDoc();
172
173     System.out.print((end.getTime() - start.getTime()) / (float)keys.size());
174     System.out.println(" average milliseconds per lookup");
175
176     TermEnum e = reader.terms(new Term("word", "azz"));
177     System.out.println("Word after azz is " + e.term().text);
178
179     reader.close();
180
181     store.close();
182   }
183 }
184
Popular Tags