comparison src/luan/modules/lucene/LuceneIndex.java @ 1388:2024d23ddd64

add restore_from_postgres
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 03 Sep 2019 22:12:53 -0600
parents bc40bc9aab3a
children 179c4882c6b6
comparison
equal deleted inserted replaced
1387:bc40bc9aab3a 1388:2024d23ddd64
180 postgresBackup = completer!=null ? PostgresBackup.newInstance() : null; 180 postgresBackup = completer!=null ? PostgresBackup.newInstance() : null;
181 if( postgresBackup != null && postgresBackup.wasCreated ) 181 if( postgresBackup != null && postgresBackup.wasCreated )
182 rebuild_postgres_backup(completer); 182 rebuild_postgres_backup(completer);
183 } 183 }
184 184
185 public void reopen() throws LuanException, IOException { 185 public void reopen() throws IOException {
186 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); 186 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer);
187 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); 187 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy());
188 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); 188 conf.setIndexDeletionPolicy(snapshotDeletionPolicy);
189 FSDirectory dir = FSDirectory.open(indexDir); 189 FSDirectory dir = FSDirectory.open(indexDir);
190 fileDir = dir.getDirectory(); 190 fileDir = dir.getDirectory();
343 343
344 private long id; 344 private long id;
345 private long idLim; 345 private long idLim;
346 private final int idBatch = 10; 346 private final int idBatch = 10;
347 347
348 private void initId() throws LuanException, IOException { 348 private void initId() throws IOException {
349 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1); 349 TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1);
350 switch(td.totalHits) { 350 switch(td.totalHits) {
351 case 0: 351 case 0:
352 id = 0; 352 id = 0;
353 idLim = 0; 353 idLim = 0;
359 default: 359 default:
360 throw new RuntimeException(); 360 throw new RuntimeException();
361 } 361 }
362 } 362 }
363 363
364 private void saveNextId(long nextId) throws LuanException, IOException {
365 Map doc = new HashMap();
366 doc.put( "type", "next_id" );
367 doc.put( FLD_NEXT_ID, idLim );
368 writer.updateDocument(new Term("type","next_id"),toLucene(doc.entrySet(),null));
369 }
370
364 public synchronized long nextId() throws LuanException, IOException { 371 public synchronized long nextId() throws LuanException, IOException {
365 if( ++id > idLim ) { 372 if( ++id > idLim ) {
366 idLim += idBatch; 373 idLim += idBatch;
367 Map doc = new HashMap(); 374 saveNextId(idLim);
368 doc.put( "type", "next_id" );
369 doc.put( FLD_NEXT_ID, idLim );
370 writer.updateDocument(new Term("type","next_id"),toLucene(doc.entrySet(),null));
371 wrote(); 375 wrote();
372 } 376 }
373 return id; 377 return id;
374 } 378 }
375 379
822 postgresBackup.rollback(); 826 postgresBackup.rollback();
823 writeLock.unlock(); 827 writeLock.unlock();
824 } 828 }
825 } 829 }
826 830
831 public void restore_from_postgres()
832 throws IOException, LuanException
833 {
834 if( postgresBackup==null )
835 throw new NullPointerException();
836 if( writeLock.isHeldByCurrentThread() )
837 throw new RuntimeException();
838 writeLock.lock();
839 boolean ok = false;
840 try {
841 writer.deleteAll();
842 long nextId = postgresBackup.maxId() + 1;
843 postgresBackup.restoreLucene(this);
844 id = idLim = nextId;
845 ok = true;
846 writer.commit();
847 } finally {
848 if( !ok ) {
849 writer.rollback();
850 reopen();
851 }
852 wrote();
853 writeLock.unlock();
854 }
855 }
856
857 void restore(LuanTable doc)
858 throws LuanException, IOException
859 {
860 writer.addDocument(toLucene(doc,null));
861 }
862
827 } 863 }