1 package org.apache.lucene.index; 2 3 18 19 import org.apache.lucene.store.Directory; 20 import org.apache.lucene.store.FSDirectory; 21 22 import java.util.Date ; 23 import java.util.Random ; 24 import java.util.Vector ; 25 import java.io.BufferedReader ; 26 import java.io.InputStreamReader ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 30 class TermInfosTest { 31 public static void main(String [] args) { 32 try { 33 test(); 34 } catch (Exception e) { 35 System.out.println(" caught a " + e.getClass() + 36 "\n with message: " + e.getMessage()); 37 } 38 } 39 40 public static void test() 42 throws Exception { 43 44 File file = new File ("words.txt"); 45 System.out.println(" reading word file containing " + 46 file.length() + " bytes"); 47 48 Date start = new Date (); 49 50 Vector keys = new Vector (); 51 FileInputStream ws = new FileInputStream (file); 52 BufferedReader wr = new BufferedReader (new InputStreamReader (ws)); 53 54 for (String key = wr.readLine(); key!=null; key = wr.readLine()) 55 keys.addElement(new Term("word", key)); 56 wr.close(); 57 58 Date end = new Date (); 59 60 System.out.print(end.getTime() - start.getTime()); 61 System.out.println(" milliseconds to read " + keys.size() + " words"); 62 63 start = new Date (); 64 65 Random gen = new Random (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 (); 80 81 System.out.print(end.getTime() - start.getTime()); 82 System.out.println(" milliseconds to generate values"); 83 84 start = new Date (); 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 (); 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 (); 107 108 TermInfosReader reader = new TermInfosReader(store, "words", fis); 109 110 end = new Date (); 111 112 System.out.print(end.getTime() - start.getTime()); 113 System.out.println(" milliseconds to open table"); 114 115 start = new Date (); 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 ("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 ("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 ("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 ("wrong value: " + Long.toString(ti.proxPointer, 16) 139 + ", expected: " + Long.toString(proxPointers[i], 16) 140 + " at " + i); 141 } 142 143 end = new Date (); 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 (); 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 ("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 ("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 ("wrong value: " + Long.toString(ti.proxPointer, 16) 167 + ", expected: " + Long.toString(proxPointers[i], 16) 168 + " at " + i); 169 } 170 171 end = new Date (); 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 |