Mercurial Hosting > junotu
changeset 63:1304d3d5b4a4
Moved all Lucene exception handling to Database class
author | Fox |
---|---|
date | Fri, 23 Dec 2022 20:43:31 +0100 |
parents | e28cb683c561 |
children | e8d83f1a6100 |
files | src/junotu/Database.java src/junotu/Main.java src/junotu/TabEdit.java src/junotu/TabSimpleSearch.java |
diffstat | 4 files changed, 136 insertions(+), 115 deletions(-) [+] |
line wrap: on
line diff
--- a/src/junotu/Database.java Fri Dec 23 18:15:24 2022 +0100 +++ b/src/junotu/Database.java Fri Dec 23 20:43:31 2022 +0100 @@ -1,6 +1,7 @@ package junotu; import java.lang.RuntimeException; +import java.io.IOException; import java.io.File; import java.util.Set; @@ -42,59 +43,75 @@ private long highestIdentifier; - public Database() throws Exception + public Database() { - Directory indexDirectory = FSDirectory.open( new File( DATABASE_DIRECTORY ) ); - luceneWriter = new IndexWriter( - indexDirectory, - new StandardAnalyzer(LUCENE_VERSION), - null, - IndexWriter.MaxFieldLength.UNLIMITED - ); - luceneSearcher = new IndexSearcher( luceneWriter.getReader() ); + try { + Directory indexDirectory = FSDirectory.open( new File( DATABASE_DIRECTORY ) ); + luceneWriter = new IndexWriter( + indexDirectory, + new StandardAnalyzer(LUCENE_VERSION), + null, + IndexWriter.MaxFieldLength.UNLIMITED + ); + luceneSearcher = new IndexSearcher( luceneWriter.getReader() ); + + /* Find highest unique identifier. */ + TopDocs topDocuments = luceneSearcher.search( + new MatchAllDocsQuery(), + null, + 1, + new Sort( new SortField( Card.TAG_IDENTIFIER, SortField.LONG, true ) ) + ); - /* Find highest unique identifier. */ - TopDocs topDocuments = luceneSearcher.search( - new MatchAllDocsQuery(), - null, - 1, - new Sort( new SortField( Card.TAG_IDENTIFIER, SortField.LONG, true ) ) - ); - - if( topDocuments.scoreDocs.length == 0 ) { - highestIdentifier = 0; - } else { - /** TODO: Find a way to get NumericField from document. */ - highestIdentifier = Long.valueOf( luceneSearcher.doc( topDocuments.scoreDocs[0].doc ).get( Card.TAG_IDENTIFIER ) ); + if( topDocuments.scoreDocs.length == 0 ) { + highestIdentifier = 0; + } else { + /** TODO: Find a way to get NumericField from document. */ + highestIdentifier = Long.valueOf( luceneSearcher.doc( topDocuments.scoreDocs[0].doc ).get( Card.TAG_IDENTIFIER ) ); + } + } catch( IOException e ) { /* Also catches CorruptIndexException from Lucene */ + throw new RuntimeException(e); } } - public void databaseCommit() throws Exception + public void databaseCommit() { System.out.print( "Saving database to disk..\n" ); - luceneWriter.commit(); + try { + luceneWriter.commit(); + } catch( IOException e ) { + throw new RuntimeException(e); + } } - private void searcherRefresh() throws Exception + private void searcherRefresh() { - luceneSearcher = new IndexSearcher( luceneWriter.getReader() ); + try { + luceneSearcher = new IndexSearcher( luceneWriter.getReader() ); + } catch( IOException e ) { + throw new RuntimeException(e); + } } - private Document documentByIdentifier( long identifier ) throws Exception + private Document documentByIdentifier( long identifier ) { - - TopDocs topDocuments = luceneSearcher.search( NumericRangeQuery.newLongRange( Card.TAG_IDENTIFIER, identifier, identifier, true, true ), 1 ); - if( topDocuments.scoreDocs.length == 0 ) { - return null; + try { + TopDocs topDocuments = luceneSearcher.search( NumericRangeQuery.newLongRange( Card.TAG_IDENTIFIER, identifier, identifier, true, true ), 1 ); + + if( topDocuments.scoreDocs.length == 0 ) { + return null; + } + + return luceneSearcher.doc( topDocuments.scoreDocs[0].doc ); + } catch( IOException e ) { + throw new RuntimeException(e); } - return luceneSearcher.doc( topDocuments.scoreDocs[0].doc ); - } - private Document cardToDocument( Card card ) throws Exception + private Document cardToDocument( Card card ) { Document document = new Document(); @@ -131,7 +148,7 @@ } - private Card cardFromDocument( Document document ) throws Exception + private Card cardFromDocument( Document document ) { Card card = new Card(); @@ -148,14 +165,19 @@ } - public long cardAdd( Card card ) throws Exception + public long cardAdd( Card card ) { highestIdentifier++; card.tagValueSetOnly( Card.TAG_IDENTIFIER, new Long( highestIdentifier ) ); card.tagValueSetOnly( Card.TAG_LAST_EDIT, new Long( System.currentTimeMillis() ) ); + + try { + luceneWriter.addDocument( cardToDocument( card ) ); + } catch( IOException e ) { + throw new RuntimeException(e); + } - luceneWriter.addDocument( cardToDocument( card ) ); System.out.print( "Added card with identifier "+Long.toString(highestIdentifier)+": '"+card.titleGet()+"'\n" ); searcherRefresh(); //luceneWriter.commit(); @@ -164,11 +186,16 @@ } - public void cardUpdate( Card card ) throws Exception + public void cardUpdate( Card card ) { + TopDocs topDocuments; Query query = NumericRangeQuery.newLongRange( card.TAG_IDENTIFIER, card.identifierGet(), card.identifierGet(), true, true ); - TopDocs topDocuments = luceneSearcher.search( query, 1 ); + try { + topDocuments = luceneSearcher.search( query, 1 ); + } catch( IOException e ) { + throw new RuntimeException(e); + } if( topDocuments.scoreDocs.length == 0 ) { throw new RuntimeException( "Failed to update card with identifier "+Long.toString( card.identifierGet() )+", not found." ); @@ -178,18 +205,27 @@ int documentNumber = topDocuments.scoreDocs[0].doc; - luceneWriter.deleteDocuments( query ); - luceneWriter.addDocument( cardToDocument( card ) ); + try { + luceneWriter.deleteDocuments( query ); + luceneWriter.addDocument( cardToDocument( card ) ); + } catch( IOException e ) { + throw new RuntimeException(e); + } System.out.print( "Updated card with identifier "+Long.toString(card.identifierGet())+": '"+card.titleGet()+"'\n" ); searcherRefresh(); //luceneWriter.commit(); } - public void cardDelete( long identifier ) throws Exception + public void cardDelete( long identifier ) { + TopDocs topDocuments; Query query = NumericRangeQuery.newLongRange( Card.TAG_IDENTIFIER, identifier, identifier, true, true ); - TopDocs topDocuments = luceneSearcher.search( query, 1 ); + try { + topDocuments = luceneSearcher.search( query, 1 ); + } catch( IOException e ) { + throw new RuntimeException(e); + } if( topDocuments.scoreDocs.length == 0 ) { throw new RuntimeException( "Failed to delete card with identifier "+Long.toString( identifier )+", not found." ); @@ -197,13 +233,17 @@ int documentNumber = topDocuments.scoreDocs[0].doc; - luceneWriter.deleteDocuments( query ); + try { + luceneWriter.deleteDocuments( query ); + } catch( IOException e ) { + throw new RuntimeException(e); + } System.out.print("Deleted card with identifier "+Long.toString(identifier)+"\n"); searcherRefresh(); } - public Card cardGetByIdentifier( long identifier ) throws Exception + public Card cardGetByIdentifier( long identifier ) { Document document = documentByIdentifier( identifier ); @@ -217,28 +257,31 @@ } /** Return up to 'amount' of recently modified cards. */ - public Card[] searchTopRecent( int amount ) throws Exception + public Card[] searchTopRecent( int amount ) { - - TopDocs topDocuments = luceneSearcher.search( - new MatchAllDocsQuery(), - null, - amount, - new Sort( new SortField( Card.TAG_LAST_EDIT, SortField.LONG, true ) ) - ); + try { + TopDocs topDocuments = luceneSearcher.search( + new MatchAllDocsQuery(), + null, + amount, + new Sort( new SortField( Card.TAG_LAST_EDIT, SortField.LONG, true ) ) + ); - Card[] cards = new Card[topDocuments.scoreDocs.length]; - - for( int i = 0; i < topDocuments.scoreDocs.length; i++ ) { - Document document = luceneSearcher.doc( topDocuments.scoreDocs[i].doc ); - cards[i] = cardFromDocument( document ); + Card[] cards = new Card[topDocuments.scoreDocs.length]; + + for( int i = 0; i < topDocuments.scoreDocs.length; i++ ) { + Document document = luceneSearcher.doc( topDocuments.scoreDocs[i].doc ); + cards[i] = cardFromDocument( document ); + } + + return cards; + + } catch( IOException e ) { + throw new RuntimeException(e); } - - return cards; - } - public Card[] searchSimple( String query ) throws Exception + public Card[] searchSimple( String query ) { Query parsedQuery; @@ -259,15 +302,21 @@ return new Card[0]; } - TopDocs hits = luceneSearcher.search( parsedQuery, 32 ); - Card[] cards = new Card[hits.scoreDocs.length]; - - for( int i = 0; i < hits.scoreDocs.length; i++ ) { - Document document = luceneSearcher.doc( hits.scoreDocs[i].doc ); - cards[i] = cardFromDocument( document ); + try { + + TopDocs hits = luceneSearcher.search( parsedQuery, 32 ); + Card[] cards = new Card[hits.scoreDocs.length]; + + for( int i = 0; i < hits.scoreDocs.length; i++ ) { + Document document = luceneSearcher.doc( hits.scoreDocs[i].doc ); + cards[i] = cardFromDocument( document ); + } + + return cards; + + } catch( IOException e ) { + throw new RuntimeException(e); } - - return cards; }
--- a/src/junotu/Main.java Fri Dec 23 18:15:24 2022 +0100 +++ b/src/junotu/Main.java Fri Dec 23 20:43:31 2022 +0100 @@ -55,11 +55,7 @@ } if( openWindowCount == 0 ) { System.out.print( "No windows open, closing program..\n" ); - try { - database.databaseCommit(); - } catch( Exception e ) { - throw new RuntimeException(e); - } + database.databaseCommit(); System.exit(0); } } @@ -85,14 +81,10 @@ public static void actionCardEdit( Window window, long identifier ) { //window = windowGetActive(); - try { - Card card = database.cardGetByIdentifier( identifier ); - window.tabSwitch( Tab.EDIT ); - window.tabEdit.cardEdit( card ); - System.out.print( "Opening edit tab to edit '"+card.titleGet()+"'.\n" ); - } catch( Exception e ) { - throw new RuntimeException(e); - } + Card card = database.cardGetByIdentifier( identifier ); + window.tabSwitch( Tab.EDIT ); + window.tabEdit.cardEdit( card ); + System.out.print( "Opening edit tab to edit '"+card.titleGet()+"'.\n" ); } }
--- a/src/junotu/TabEdit.java Fri Dec 23 18:15:24 2022 +0100 +++ b/src/junotu/TabEdit.java Fri Dec 23 20:43:31 2022 +0100 @@ -443,11 +443,7 @@ return; } - try { - Main.database.cardDelete( card.identifierGet() ); - } catch( Exception e ) { - throw new RuntimeException(e); - } + Main.database.cardDelete( card.identifierGet() ); Window window = (Window)this.getTopLevelAncestor(); window.tabSearch.search(); @@ -472,14 +468,10 @@ card.titleSet( title.getText() ); card.contentSet( content.getText() ); - try { - if( newCard ) { - Main.database.cardAdd( card ); - } else { - Main.database.cardUpdate( card ); - } - } catch( Exception e ) { - throw new RuntimeException(e); + if( newCard ) { + Main.database.cardAdd( card ); + } else { + Main.database.cardUpdate( card ); } Window window = (Window)this.getTopLevelAncestor(); @@ -508,11 +500,7 @@ case KEY_ACTION_SAVE: { buttonClickedSave( true ); - try { - Main.database.databaseCommit(); - } catch( Exception ex ) { - System.out.print( "Failed to write database: "+ex.getMessage()+"\n" ); - } + Main.database.databaseCommit(); break; }
--- a/src/junotu/TabSimpleSearch.java Fri Dec 23 18:15:24 2022 +0100 +++ b/src/junotu/TabSimpleSearch.java Fri Dec 23 20:43:31 2022 +0100 @@ -116,15 +116,11 @@ Card[] cards; - try { - String text = field.getText(); - if( text.length() > 0 ) { - cards = Main.database.searchSimple( field.getText() ); - } else { - cards = Main.database.searchTopRecent( 32 ); - } - } catch( Exception e ) { - throw new RuntimeException(e); + String text = field.getText(); + if( text.length() > 0 ) { + cards = Main.database.searchSimple( field.getText() ); + } else { + cards = Main.database.searchTopRecent( 32 ); } System.out.print("Search: Found "+cards.length+" matches.\n"); @@ -183,11 +179,7 @@ switch( e.getActionCommand() ){ case KEY_ACTION_COMMIT: { - try { - Main.database.databaseCommit(); - } catch( Exception ex ) { - System.out.print( "Failed to write database: "+ex.getMessage()+"\n" ); - } + Main.database.databaseCommit(); break; }