comparison src/luan/modules/lucene/LuceneIndex.java @ 1539:c27dc6af87ca

remove nextId
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 06 Sep 2020 11:35:30 -0600
parents 634f6765830e
children 79f67662d3e7
comparison
equal deleted inserted replaced
1538:634f6765830e 1539:c27dc6af87ca
49 import org.apache.lucene.util.BytesRef; 49 import org.apache.lucene.util.BytesRef;
50 import org.apache.lucene.util.NumericUtils; 50 import org.apache.lucene.util.NumericUtils;
51 import org.apache.lucene.search.Query; 51 import org.apache.lucene.search.Query;
52 import org.apache.lucene.search.PrefixQuery; 52 import org.apache.lucene.search.PrefixQuery;
53 import org.apache.lucene.search.TermQuery; 53 import org.apache.lucene.search.TermQuery;
54 import org.apache.lucene.search.MatchAllDocsQuery;
54 import org.apache.lucene.search.TopDocs; 55 import org.apache.lucene.search.TopDocs;
55 import org.apache.lucene.search.Sort; 56 import org.apache.lucene.search.Sort;
56 import org.apache.lucene.search.SortField; 57 import org.apache.lucene.search.SortField;
57 import org.apache.lucene.search.IndexSearcher; 58 import org.apache.lucene.search.IndexSearcher;
58 import org.apache.lucene.search.TotalHitCountCollector; 59 import org.apache.lucene.search.TotalHitCountCollector;
116 return li; 117 return li;
117 } 118 }
118 } 119 }
119 120
120 private static final Version luceneVersion = Version.LUCENE_4_9; 121 private static final Version luceneVersion = Version.LUCENE_4_9;
121 private static final String FLD_NEXT_ID = "nextId"; 122 // private static final String FLD_NEXT_ID = "nextId";
122 public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer()); 123 public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer());
123 public static final StringFieldParser LOWERCASE_FIELD_PARSER = new StringFieldParser(new LowercaseAnalyzer(luceneVersion)); 124 public static final StringFieldParser LOWERCASE_FIELD_PARSER = new StringFieldParser(new LowercaseAnalyzer(luceneVersion));
124 public static final StringFieldParser ENGLISH_FIELD_PARSER = new StringFieldParser(new EnglishAnalyzer(luceneVersion)); 125 public static final StringFieldParser ENGLISH_FIELD_PARSER = new StringFieldParser(new EnglishAnalyzer(luceneVersion));
125 private static final SortField ID_SORT = new SortField("id",SortField.Type.LONG); 126 private static final SortField ID_SORT = new SortField("id",SortField.Type.LONG);
127 private static final SortField ID_DESC_SORT = new SortField("id",SortField.Type.LONG,true);
126 128
127 private final Object version; 129 private final Object version;
128 130
129 private final ReentrantLock writeLock = new ReentrantLock(); 131 private final ReentrantLock writeLock = new ReentrantLock();
130 private final File indexDir; 132 private final File indexDir;
204 public void delete_all() throws IOException, SQLException { 206 public void delete_all() throws IOException, SQLException {
205 boolean commit = !writeLock.isHeldByCurrentThread(); 207 boolean commit = !writeLock.isHeldByCurrentThread();
206 writeLock.lock(); 208 writeLock.lock();
207 try { 209 try {
208 writer.deleteAll(); 210 writer.deleteAll();
209 id = idLim = 0; 211 id = 0;
210 if( postgresBackup != null ) 212 if( postgresBackup != null )
211 postgresBackup.deleteAll(); 213 postgresBackup.deleteAll();
212 if(commit) writer.commit(); 214 if(commit) writer.commit();
213 } finally { 215 } finally {
214 wrote(); 216 wrote();
294 296
295 boolean commit = !writeLock.isHeldByCurrentThread(); 297 boolean commit = !writeLock.isHeldByCurrentThread();
296 writeLock.lock(); 298 writeLock.lock();
297 try { 299 try {
298 if( id == null ) { 300 if( id == null ) {
299 id = nextId(); 301 id = ++this.id;
300 doc.put("id",id); 302 doc.put("id",id);
301 if( postgresBackup != null ) 303 if( postgresBackup != null )
302 postgresBackup.add(doc); 304 postgresBackup.add(doc);
303 writer.addDocument(toLucene(doc)); 305 writer.addDocument(toLucene(doc));
304 } else { 306 } else {
361 } 363 }
362 } 364 }
363 365
364 366
365 private long id; 367 private long id;
366 private long idLim;
367 private final int idBatch = 10;
368 368
369 private void initId() throws IOException { 369 private void initId() throws IOException {
370 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); 370 // TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1);
371 switch(td.totalHits) { 371 TopDocs td = searcher.search(new MatchAllDocsQuery(),1,new Sort(ID_DESC_SORT));
372 switch(td.scoreDocs.length) {
372 case 0: 373 case 0:
373 id = 0; 374 id = 0;
374 idLim = 0;
375 break; 375 break;
376 case 1: 376 case 1:
377 idLim = (Long)searcher.doc(td.scoreDocs[0].doc).getField(FLD_NEXT_ID).numericValue(); 377 id = (Long)searcher.doc(td.scoreDocs[0].doc).getField("id").numericValue();
378 id = idLim;
379 break; 378 break;
380 default: 379 default:
381 throw new RuntimeException(); 380 throw new RuntimeException();
382 } 381 }
383 }
384
385 private void saveNextId(long nextId) throws LuanException, IOException {
386 Map<String,Object> doc = new HashMap();
387 doc.put( "type", "next_id" );
388 doc.put( FLD_NEXT_ID, idLim );
389 writer.updateDocument("type",doc);
390 }
391
392 public synchronized long nextId() throws LuanException, IOException {
393 if( ++id > idLim ) {
394 idLim += idBatch;
395 saveNextId(idLim);
396 wrote();
397 }
398 return id;
399 } 382 }
400 383
401 /* 384 /*
402 public void backup(String zipFile) throws LuanException, IOException { 385 public void backup(String zipFile) throws LuanException, IOException {
403 if( !zipFile.endsWith(".zip") ) 386 if( !zipFile.endsWith(".zip") )
417 snapshotDeletionPolicy.release(ic); 400 snapshotDeletionPolicy.release(ic);
418 } 401 }
419 } 402 }
420 */ 403 */
421 public SnapshotDeletionPolicy snapshotDeletionPolicy() { 404 public SnapshotDeletionPolicy snapshotDeletionPolicy() {
422 return (SnapshotDeletionPolicy)writer.getLuceneConfig().getIndexDeletionPolicy(); 405 return (SnapshotDeletionPolicy)writer.getLuceneIndexWriter().getConfig().getIndexDeletionPolicy();
423 } 406 }
424 407
425 public Object snapshot(LuanFunction fn) throws LuanException, IOException { 408 public Object snapshot(LuanFunction fn) throws LuanException, IOException {
426 SnapshotDeletionPolicy snapshotDeletionPolicy = snapshotDeletionPolicy(); 409 SnapshotDeletionPolicy snapshotDeletionPolicy = snapshotDeletionPolicy();
427 IndexCommit ic = snapshotDeletionPolicy.snapshot(); 410 IndexCommit ic = snapshotDeletionPolicy.snapshot();
435 } 418 }
436 419
437 420
438 421
439 public String to_string() { 422 public String to_string() {
440 return writer.getDirectory().toString(); 423 return writer.getLuceneIndexWriter().getDirectory().toString();
441 } 424 }
442 425
443 protected void finalize() throws Throwable { 426 protected void finalize() throws Throwable {
444 close(); 427 close();
445 super.finalize(); 428 super.finalize();
504 private synchronized IndexSearcher openSearcher() throws IOException { 487 private synchronized IndexSearcher openSearcher() throws IOException {
505 int gwc = writeCounter.get(); 488 int gwc = writeCounter.get();
506 if( writeCount != gwc ) { 489 if( writeCount != gwc ) {
507 writeCount = gwc; 490 writeCount = gwc;
508 DirectoryReader newReader = DirectoryReader.openIfChanged(reader); 491 DirectoryReader newReader = DirectoryReader.openIfChanged(reader);
492 // DirectoryReader newReader = DirectoryReader.openIfChanged(reader,writer.getLuceneIndexWriter(),true);
509 if( newReader != null ) { 493 if( newReader != null ) {
510 reader.decRef(); 494 reader.decRef();
511 reader = newReader; 495 reader = newReader;
512 searcher = new IndexSearcher(reader); 496 searcher = new IndexSearcher(reader);
513 } 497 }
752 throw new RuntimeException(); 736 throw new RuntimeException();
753 writeLock.lock(); 737 writeLock.lock();
754 boolean ok = false; 738 boolean ok = false;
755 try { 739 try {
756 writer.deleteAll(); 740 writer.deleteAll();
757 long nextId = postgresBackup.maxId() + 1;
758 postgresBackup.restoreLucene(this); 741 postgresBackup.restoreLucene(this);
759 id = idLim = nextId;
760 saveNextId(nextId);
761 ok = true; 742 ok = true;
762 writer.commit(); 743 writer.commit();
744 wrote();
745 ensure_open(); // refresh searcher
746 initId();
763 wasCreated = false; 747 wasCreated = false;
764 } finally { 748 } finally {
765 if( !ok ) { 749 if( !ok ) {
766 writer.rollback(); 750 writer.rollback();
767 reopen(); 751 reopen();