1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.search.simpleindexer; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.jot.logger.JOTLogger; import net.jot.utils.JOTUtilities; /** * Helper to JOTSimpleSearchEngine to handle the index files. * The master file: index.txt * and the index files ex: 0A/EF.txt * * @author thibautc */ public class JOTIndexHandler { File indexRoot = null; File indexFile = null; File tempIndexFile = null; Hashtable indexById = new Hashtable(); Hashtable indexByKey = new Hashtable(); Hashtable indexStamps = new Hashtable(); // pattern for reading index master file entries static Pattern pattern = Pattern.compile("^(\\d+)\\s+(\\d+)\\s+(.*)$"); public JOTIndexHandler(File rootFolder) throws Exception { indexRoot = rootFolder; indexFile = new File(rootFolder, "index.txt"); tempIndexFile = new File(rootFolder, "index.tmp"); if (!indexFile.exists()) { indexFile.createNewFile(); } loadMasterFile(); } /** * Called from constructor * loads existing index.txt * @throws java.lang.Exception */ protected void loadMasterFile() throws Exception { BufferedReader reader = new BufferedReader(new FileReader(indexFile)); try { String s = null; while ((s = reader.readLine()) != null) { Matcher m = pattern.matcher(s); if (m.matches()) { String id = m.group(1); String stamp = m.group(2); String key = m.group(3); JOTLogger.log(JOTLogger.DEBUG_LEVEL, this, "Found in index: " + id + " " + stamp + " " + key); addToMasterInMemory(id, key, stamp); } } } catch (Exception e) { throw (e); } finally { reader.close(); } } /** * saves index.txt * @throws java.lang.Exception */ public void saveMasterFile() throws Exception { Enumeration e = indexById.keys(); BufferedWriter p = new BufferedWriter(new PrintWriter(new FileOutputStream(indexFile))); while (e.hasMoreElements()) { String id = (String) e.nextElement(); String key = (String) indexById.get(id); String stamp = (String) indexStamps.get(key); p.write(id + " " + stamp + " " + key + "\n"); } p.close(); } /** * Get the timestamp of last indexing of an entry * @param key * @return */ public long getEntryStamp(String key) { String stamp = (String) indexStamps.get(key); long l = 0; try { l = new Long(stamp).longValue(); } catch (Exception e) { } return l; } /** * wether the key is new or already indexed * @param key * @return */ public boolean isNewKey(String key) { return !indexByKey.containsKey(key); } /** * Adds a new entry to index.txt * @param id * @param key * @param lastModified * @throws java.lang.Exception */ public void addMasterEntry(int id, String key, long lastModified) throws Exception { addToMasterInMemory("" + id, key, "" + lastModified); //append to file right away .. safer to have it now so that indexes won't point to missing entry if indexing is shut down before completion FileOutputStream fos = null; try { fos = new FileOutputStream(indexFile, true); fos.write(("" + id + " " + lastModified + " " + key + "\n").getBytes()); } catch (Exception e) { if (fos != null) { fos.close(); } throw (e); } if (fos != null) { fos.close(); } } public String getMasterKeyById(String id) { return (String) indexById.get(id); } public String getMasterIdByKey(String key) { return (String) indexByKey.get(key); } /** * Remove an entry keywords from indexes * Does not remove the entry itself(ie: file) from the index master itself, call removeMasterEntry for that. * @param id */ public void removeEntries(String key) throws Exception { String id = (String) indexByKey.get(key); //System.out.println("id["+id+"]"); String entryPattern = " " + id + ":\\d+"; if (id != null) { // crawl through all index files and remove entries ! File[] dirs = indexRoot.listFiles(); for (int i = 0; i != dirs.length; i++) { if (dirs[i].isDirectory()) { File[] files = dirs[i].listFiles(); for (int j = 0; j != files.length; j++) { if (files[j].isFile() && files[j].getName().endsWith(".txt")) { // we found an index file BufferedReader reader = new BufferedReader(new FileReader(files[j])); File tmpFile = new File(files[j].getParent(), files[j].getName().replace(".txt", ".tmp")); PrintWriter p = new PrintWriter(new FileOutputStream(tmpFile)); try { String s = null; while ((s = reader.readLine()) != null) { s = s.replaceAll(entryPattern, ""); // write the line back p.write(s + "\n"); } } catch (Exception e) { throw (e); } finally { reader.close(); p.close(); } synchronized (this) { JOTUtilities.moveFile(files[j], tmpFile); } } } } } } } /** * Remove the file/key from the master index (ie: deleted file) * @param id */ public void removeMasterEntry(String key) throws Exception { String id = (String) indexByKey.get(key); if (id != null) { indexByKey.remove(key); indexStamps.remove(key); indexById.remove(id); // remove from the file itself. synchronized (this) { // if existing file, search if keyword already in it BufferedReader reader = new BufferedReader(new FileReader(indexFile)); PrintWriter p = new PrintWriter(new FileOutputStream(tempIndexFile)); try { String s = null; while ((s = reader.readLine()) != null) { if (!s.startsWith(id + " ")) { p.write(s + "\n"); } } } catch (Exception e) { throw (e); } finally { reader.close(); p.close(); } JOTUtilities.moveFile(indexFile, tempIndexFile); } } } private void addToMasterInMemory(String id, String key, String stamp) { indexByKey.put(key, id); indexById.put(id, key); indexStamps.put(key, stamp); } /** * Add a keyword to the index * id: id of the key in index.txt * word: the keyword to be indexed * lines: vector of line numbers * return: wether it was a new keyword or not * @throws java.lang.Exception */ public boolean indexKeyword(String id, String word, Vector lines) throws Exception { boolean newKeyword = true; Character c = new Character(word.charAt(0)); Character c2 = new Character(word.charAt(1)); /* Uses the character unicode hashcode value in hexadecimal (uppercase) as unique ID. * We store data in (keyword.letter1) / (keyword.letter2).txt (in hexaddecimal) (Index) so we can search faster */ String folder = Integer.toString(c.hashCode(), 16).toUpperCase(); String file = Integer.toString(c2.hashCode(), 16).toUpperCase() + ".txt"; //System.out.println("" + c + " -> " + folder + "/" + file); File dir = new File(indexRoot, folder); File tempFile = new File(indexRoot, Integer.toString(c2.hashCode(), 16).toUpperCase() + ".tmp"); // create the index folder and file if necessary if (!dir.exists()) { dir.mkdir(); } File f = new File(dir, file); // build the new locations string String loc = ""; for (int i = 0; i != lines.size(); i++) { loc += id + ":" + (String) lines.get(i) + " "; } //System.out.println("loc for " + word + " -> " + loc); // write to file if (!f.exists()) { // if new file just create and add new entry right away f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); try { fos.write((word + " " + loc + "\n").getBytes()); } catch (Exception e) { throw (e); } finally { fos.close(); } } else { // if existing file, search if keyword already in it BufferedReader reader = new BufferedReader(new FileReader(f)); PrintWriter p = new PrintWriter(new FileOutputStream(tempFile)); try { String s = null; while ((s = reader.readLine()) != null) { if (s.startsWith(word + " ")) { newKeyword = false; // we need to update the keyword(update this line) p.write(s + loc + "\n"); } else { // leave the line alone, not our keyword. p.write(s + "\n"); } } if (newKeyword) { // append new keyword to file p.write(word + " " + loc + "\n"); } } catch (Exception e) { throw (e); } finally { reader.close(); p.close(); } // move temp file as real one. synchronized (this) { JOTUtilities.moveFile(f, tempFile); } } return newKeyword; } /** * Return the index file line for a specific keyword or empty string if none found. */ public String findKeywordIndexLine(String keyword) throws Exception { String word = keyword.trim().toLowerCase(); Character c = new Character(word.charAt(0)); Character c2 = new Character(word.charAt(1)); /* Uses the character unicode hashcode value in hexadecimal (uppercase) as unique ID. * We store data in (keyword.letter1) / (keyword.letter2).txt (in hexaddecimal) (Index) so we can search faster */ String folder = Integer.toString(c.hashCode(), 16).toUpperCase(); String file = Integer.toString(c2.hashCode(), 16).toUpperCase() + ".txt"; String line = ""; File f = new File(new File(indexRoot, folder), file); if (f.exists() && f.isFile()) { BufferedReader reader = new BufferedReader(new FileReader(f)); try { String s = ""; while (((s = reader.readLine()) != null) && line.length() == 0) { if (s.startsWith(word + " ")) { line = s; } } } catch (Exception e) { throw (e); } finally { reader.close(); } } return line; } }