changeset 7:4ee26d904fe3

Added card identifier, move execution current directory to './run'
author Fox
date Thu, 07 Apr 2022 21:47:36 +0200
parents fbe307da953a
children 9d3256f86803
files .hgignore run.gui_sh src/junotu/Database.java
diffstat 3 files changed, 69 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Apr 05 02:20:25 2022 +0200
+++ b/.hgignore	Thu Apr 07 21:47:36 2022 +0200
@@ -1,4 +1,4 @@
 syntax: glob
 
 build/
-
+run/
--- a/run.gui_sh	Tue Apr 05 02:20:25 2022 +0200
+++ b/run.gui_sh	Thu Apr 07 21:47:36 2022 +0200
@@ -1,11 +1,17 @@
 #!/bin/sh
 
 #export _JAVA_AWT_WM_NONREPARENTING=1
-export CLASSPATH=./src:./build:./lib/lucene-core-3.0.3.jar
+export CLASSPATH=../src:../build:../lib/lucene-core-3.0.3.jar
 #JAVA=/usr/lib/jvm/java-8-openjdk-amd64/bin/java
 
-if [ ! -d 'build/' ]; then
-	mkdir build/
+if [ ! -d './run' ]; then
+	mkdir ./run
 fi
 
-javac -d ./build $(find . -name '*.java' | xargs) && java junotu.Main
+cd ./run
+
+if [ ! -d '../build' ]; then
+	mkdir ../build/
+fi
+
+javac -d ../build $(find ../src -name '*.java' | xargs) && java junotu.Main
--- a/src/junotu/Database.java	Tue Apr 05 02:20:25 2022 +0200
+++ b/src/junotu/Database.java	Thu Apr 07 21:47:36 2022 +0200
@@ -8,13 +8,18 @@
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.util.Version;
 import org.apache.lucene.document.Field;
+import org.apache.lucene.document.NumericField;
 
 import org.apache.lucene.document.Document;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
 
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.queryParser.QueryParser;
 
 import org.apache.lucene.search.BooleanClause;
@@ -22,6 +27,7 @@
 public class Database {
 
     public class Card {
+	public long identifier;
 	public String title;
 	public String content;
     }
@@ -29,11 +35,14 @@
     public static final String DATABASE_DIRECTORY = "./Database";
     public static final Version LUCENE_VERSION = Version.LUCENE_30;
 
+    public static final String TAG_IDENTIFIER = "_junotu_identifier";
     public static final String TAG_TITLE = "_junotu_title";
     public static final String TAG_CONTENT = "_junotu_content";
     
     private IndexWriter luceneWriter;
     private IndexSearcher luceneSearcher;
+
+    private long highestIdentifier;
     
     public Database() throws Exception
     {
@@ -45,16 +54,61 @@
 				       IndexWriter.MaxFieldLength.UNLIMITED
 				       );
 	luceneSearcher = new IndexSearcher( indexDirectory );
+
+	/* Find highest unique identifier. */
+	TopDocs topDocuments = luceneSearcher.search(
+					       new MatchAllDocsQuery(),
+					       null,
+					       1,
+					       new Sort( new SortField( TAG_IDENTIFIER, SortField.LONG, true ) )
+					       );
+
+	if( topDocuments.scoreDocs.length == 0 ) {
+	    highestIdentifier = 0;
+	} else {
+	    highestIdentifier = (Long)((NumericField)luceneSearcher.doc( topDocuments.scoreDocs[0].doc ).getFieldable( TAG_IDENTIFIER )).getNumericValue();
+	}
+	
     }
     
-    public void cardAdd( String title, String content ) throws Exception
+    public long cardAdd( String title, String content ) throws Exception
     {
 	Document document = new Document();
 
-	document.add( new Field( TAG_TITLE, title, Field.Store.YES, Field.Index.ANALYZED ) );
-	document.add( new Field( TAG_CONTENT, content, Field.Store.YES, Field.Index.ANALYZED ) );
+	NumericField fieldIdentifier = new NumericField( TAG_IDENTIFIER, Field.Store.YES, true );
+	Field        fieldTitle      = new Field( TAG_TITLE, title, Field.Store.YES, Field.Index.ANALYZED );
+	Field        fieldContent    = new Field( TAG_CONTENT, content, Field.Store.YES, Field.Index.ANALYZED );
+
+	highestIdentifier++;
+	
+	fieldIdentifier.setLongValue( highestIdentifier );
+	    
+	document.add( fieldIdentifier );
+	document.add( fieldTitle );
+	document.add( fieldContent );
 
 	luceneWriter.addDocument( document );
+
+	return highestIdentifier;
+	
+    }
+
+    public Card cardGetByIdentifier( long identifier ) throws Exception
+    {
+	TopDocs topDocuments = luceneSearcher.search( NumericRangeQuery.newLongRange( TAG_IDENTIFIER, identifier, identifier, true, true ), 1 );
+
+	if( topDocuments.scoreDocs.length == 0 ) {
+	    return null;
+	}
+	
+	Document document = luceneSearcher.doc( topDocuments.scoreDocs[0].doc );
+	Card card = new Card();
+
+	card.identifier = (long)((NumericField)document.getFieldable( TAG_IDENTIFIER )).getNumericValue();
+	card.title = document.get( TAG_TITLE );
+	card.content = document.get( TAG_CONTENT );
+
+	return card;
 	
     }
 
@@ -83,6 +137,7 @@
 	for( int i = 0; i < hits.scoreDocs.length; i++ ) {
 	    Document document = luceneSearcher.doc( hits.scoreDocs[i].doc );
 	    cards[i] = new Card();
+	    cards[i].identifier = (long)((NumericField)document.getFieldable( TAG_IDENTIFIER )).getNumericValue();
 	    cards[i].title = document.get( TAG_TITLE );
 	    cards[i].content = document.get( TAG_CONTENT );
 	}