comparison src/luan/modules/lucene/LuceneIndex.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents 25746915a241
children 8b61c8c4e07a
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
198 Map<String,LuanFunction> map = indexedOnlyFields.get(type); 198 Map<String,LuanFunction> map = indexedOnlyFields.get(type);
199 if( map != null ) { 199 if( map != null ) {
200 for( Map.Entry<String,LuanFunction> entry : map.entrySet() ) { 200 for( Map.Entry<String,LuanFunction> entry : map.entrySet() ) {
201 String name = entry.getKey(); 201 String name = entry.getKey();
202 LuanFunction fn = entry.getValue(); 202 LuanFunction fn = entry.getValue();
203 Object value = Luan.first(fn.call(luan,new Object[]{doc})); 203 Object value = Luan.first(fn.call(doc));
204 doc.put( name, value ); 204 doc.put( name, value );
205 indexedOnlySet.add(name); 205 indexedOnlySet.add(name);
206 } 206 }
207 } 207 }
208 Object obj = doc.get("id"); 208 Object obj = doc.get("id");
228 wrote(); 228 wrote();
229 writeLock.unlock(); 229 writeLock.unlock();
230 } 230 }
231 } 231 }
232 232
233 public void update_in_transaction(Luan luan,LuanFunction fn) throws IOException, LuanException { 233 public void update_in_transaction(LuanFunction fn) throws IOException, LuanException {
234 boolean commit = !writeLock.isHeldByCurrentThread(); 234 boolean commit = !writeLock.isHeldByCurrentThread();
235 writeLock.lock(); 235 writeLock.lock();
236 try { 236 try {
237 fn.call(luan); 237 fn.call();
238 if(commit) writer.commit(); 238 if(commit) writer.commit();
239 } finally { 239 } finally {
240 wrote(); 240 wrote();
241 writeLock.unlock(); 241 writeLock.unlock();
242 } 242 }
243 } 243 }
244 244
245 public void run_in_lock(Luan luan,LuanFunction fn) throws IOException, LuanException { 245 public void run_in_lock(LuanFunction fn) throws IOException, LuanException {
246 if( writeLock.isHeldByCurrentThread() ) 246 if( writeLock.isHeldByCurrentThread() )
247 throw new RuntimeException(); 247 throw new RuntimeException();
248 writeLock.lock(); 248 writeLock.lock();
249 try { 249 try {
250 synchronized(this) { 250 synchronized(this) {
251 fn.call(luan); 251 fn.call();
252 } 252 }
253 } finally { 253 } finally {
254 wrote(); 254 wrote();
255 writeLock.unlock(); 255 writeLock.unlock();
256 } 256 }
316 public Object snapshot(Luan luan,LuanFunction fn) throws LuanException, IOException { 316 public Object snapshot(Luan luan,LuanFunction fn) throws LuanException, IOException {
317 IndexCommit ic = snapshotDeletionPolicy.snapshot(); 317 IndexCommit ic = snapshotDeletionPolicy.snapshot();
318 try { 318 try {
319 String dir = fileDir.toString(); 319 String dir = fileDir.toString();
320 LuanTable fileNames = new LuanTable(luan,new ArrayList(ic.getFileNames())); 320 LuanTable fileNames = new LuanTable(luan,new ArrayList(ic.getFileNames()));
321 return fn.call(luan,new Object[]{dir,fileNames}); 321 return fn.call(dir,fileNames);
322 } finally { 322 } finally {
323 snapshotDeletionPolicy.release(ic); 323 snapshotDeletionPolicy.release(ic);
324 } 324 }
325 } 325 }
326 326
350 350
351 private static class DocFn extends LuanFunction { 351 private static class DocFn extends LuanFunction {
352 final IndexSearcher searcher; 352 final IndexSearcher searcher;
353 int docID; 353 int docID;
354 354
355 DocFn(IndexSearcher searcher) { 355 DocFn(Luan luan,IndexSearcher searcher) {
356 super(luan);
356 this.searcher = searcher; 357 this.searcher = searcher;
357 } 358 }
358 359
359 @Override public Object call(Luan luan,Object[] args) throws LuanException { 360 @Override public Object call(Object[] args) throws LuanException {
360 try { 361 try {
361 return toTable(luan,searcher.doc(docID)); 362 return toTable(luan(),searcher.doc(docID));
362 } catch(IOException e) { 363 } catch(IOException e) {
363 throw new LuanException(e); 364 throw new LuanException(e);
364 } 365 }
365 } 366 }
366 } 367 }
411 searcher = openSearcher(); 412 searcher = openSearcher();
412 try { 413 try {
413 if( fn!=null && n==null ) { 414 if( fn!=null && n==null ) {
414 if( sortStr != null ) 415 if( sortStr != null )
415 throw new LuanException("sort must be nil when n is nil"); 416 throw new LuanException("sort must be nil when n is nil");
416 final DocFn docFn = new DocFn(searcher); 417 final DocFn docFn = new DocFn(luan,searcher);
417 MyCollector col = new MyCollector() { 418 MyCollector col = new MyCollector() {
418 @Override public void collect(int doc) { 419 @Override public void collect(int doc) {
419 try { 420 try {
420 docFn.docID = docBase + doc; 421 docFn.docID = docBase + doc;
421 fn.call(luan,new Object[]{++i,docFn}); 422 fn.call(++i,docFn);
422 } catch(LuanException e) { 423 } catch(LuanException e) {
423 throw new LuanRuntimeException(e); 424 throw new LuanRuntimeException(e);
424 } 425 }
425 } 426 }
426 }; 427 };
437 return thcc.getTotalHits(); 438 return thcc.getTotalHits();
438 } 439 }
439 Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr); 440 Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr);
440 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort); 441 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort);
441 final ScoreDoc[] scoreDocs = td.scoreDocs; 442 final ScoreDoc[] scoreDocs = td.scoreDocs;
442 DocFn docFn = new DocFn(searcher); 443 DocFn docFn = new DocFn(luan,searcher);
443 for( int i=0; i<scoreDocs.length; i++ ) { 444 for( int i=0; i<scoreDocs.length; i++ ) {
444 docFn.docID = scoreDocs[i].doc; 445 docFn.docID = scoreDocs[i].doc;
445 fn.call(luan,new Object[]{i+1,docFn}); 446 fn.call(i+1,docFn);
446 } 447 }
447 return td.totalHits; 448 return td.totalHits;
448 } finally { 449 } finally {
449 if( !inTransaction ) 450 if( !inTransaction )
450 close(searcher); 451 close(searcher);
451 } 452 }
452 } 453 }
453 454
454 public Object search_in_transaction(Luan luan,LuanFunction fn) throws LuanException, IOException { 455 public Object search_in_transaction(LuanFunction fn) throws LuanException, IOException {
455 if( threadLocalSearcher.get() != null ) 456 if( threadLocalSearcher.get() != null )
456 throw new LuanException("can't nest search_in_transaction calls"); 457 throw new LuanException("can't nest search_in_transaction calls");
457 IndexSearcher searcher = openSearcher(); 458 IndexSearcher searcher = openSearcher();
458 threadLocalSearcher.set(searcher); 459 threadLocalSearcher.set(searcher);
459 try { 460 try {
460 return fn.call(luan); 461 return fn.call();
461 } finally { 462 } finally {
462 threadLocalSearcher.set(null); 463 threadLocalSearcher.set(null);
463 close(searcher); 464 close(searcher);
464 } 465 }
465 } 466 }
586 public String highlightTerm(String originalText,TokenGroup tokenGroup) { 587 public String highlightTerm(String originalText,TokenGroup tokenGroup) {
587 return originalText; 588 return originalText;
588 } 589 }
589 }; 590 };
590 591
591 public LuanFunction highlighter(Luan luan,String queryStr,LuanFunction formatter,final Integer fragmentSize,String dotdotdot) throws ParseException { 592 public LuanFunction highlighter(String queryStr,LuanFunction formatter,final Integer fragmentSize,String dotdotdot) throws ParseException {
592 Query query = SaneQueryParser.parseQuery(mfp,queryStr); 593 Query query = SaneQueryParser.parseQuery(mfp,queryStr);
593 Formatter fmt = new Formatter() { 594 Formatter fmt = new Formatter() {
594 public String highlightTerm(String originalText,TokenGroup tokenGroup) { 595 public String highlightTerm(String originalText,TokenGroup tokenGroup) {
595 if( tokenGroup.getTotalScore() <= 0 ) 596 if( tokenGroup.getTotalScore() <= 0 )
596 return originalText; 597 return originalText;
597 try { 598 try {
598 return (String)Luan.first(formatter.call(luan,new Object[]{originalText})); 599 return (String)Luan.first(formatter.call(originalText));
599 } catch(LuanException e) { 600 } catch(LuanException e) {
600 throw new LuanRuntimeException(e); 601 throw new LuanRuntimeException(e);
601 } 602 }
602 } 603 }
603 }; 604 };
605 final Highlighter chooser = fragmentSize==null ? null : new Highlighter(nullFormatter,queryScorer); 606 final Highlighter chooser = fragmentSize==null ? null : new Highlighter(nullFormatter,queryScorer);
606 if( chooser != null ) 607 if( chooser != null )
607 chooser.setTextFragmenter( new SimpleSpanFragmenter(queryScorer,fragmentSize) ); 608 chooser.setTextFragmenter( new SimpleSpanFragmenter(queryScorer,fragmentSize) );
608 final Highlighter hl = new Highlighter(fmt,queryScorer); 609 final Highlighter hl = new Highlighter(fmt,queryScorer);
609 hl.setTextFragmenter( new NullFragmenter() ); 610 hl.setTextFragmenter( new NullFragmenter() );
610 return new LuanFunction() { 611 return new LuanFunction(false) { // ???
611 @Override public String call(Luan luan,Object[] args) throws LuanException { 612 @Override public String call(Object[] args) throws LuanException {
612 String text = (String)args[0]; 613 String text = (String)args[0];
613 try { 614 try {
614 if( chooser != null ) { 615 if( chooser != null ) {
615 String s = chooser.getBestFragment(analyzer,null,text); 616 String s = chooser.getBestFragment(analyzer,null,text);
616 if( s != null ) { 617 if( s != null ) {