changeset 106:9ece57b1895d

Database: Getting non-string-typed tag values back in a better way, and better sorting for cards without 'last edit' timestamp
author Fox
date Thu, 06 Apr 2023 13:06:40 +0200
parents 33f090b497c8
children e4588b8a8ddc
files src/junotu/Card.java src/junotu/Database.java
diffstat 2 files changed, 40 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Card.java	Thu Apr 06 10:48:15 2023 +0200
+++ b/src/junotu/Card.java	Thu Apr 06 13:06:40 2023 +0200
@@ -42,6 +42,14 @@
 		TAG_BOARD_COLUMN_CARD, VALUE_BOARD_COLUMN_CARD_ONLY,
 	};
 
+	public static final SortedMap< String, Class<?> > STRICT_TAG_TYPES;
+	static {
+		STRICT_TAG_TYPES = new TreeMap< String, Class<?> >();
+		STRICT_TAG_TYPES.put( Card.TAG_IDENTIFIER, Long.class );
+		STRICT_TAG_TYPES.put( Card.TAG_SAVED, Long.class );
+		STRICT_TAG_TYPES.put( Card.TAG_LAST_EDIT, Long.class );
+	}
+
 	public SortedMap< String, Set<Object> > tags = new TreeMap< String, Set<Object> >();
 
 	public Card() {}
--- a/src/junotu/Database.java	Thu Apr 06 10:48:15 2023 +0200
+++ b/src/junotu/Database.java	Thu Apr 06 13:06:40 2023 +0200
@@ -72,8 +72,8 @@
 			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 ) );
+				Card card = cardFromDocument( luceneSearcher.doc( topDocuments.scoreDocs[0].doc ) );
+				highestIdentifier = card.identifierGet().longValue();
 			}
 		} catch( IOException e ) { /* Also catches CorruptIndexException from Lucene */
 			throw new RuntimeException(e);
@@ -161,6 +161,7 @@
 
 		String search = "";
 		for( String tag : card.tags.keySet() ) {
+			Class<?> strict_type = Card.STRICT_TAG_TYPES.get(tag);
 			Set<Object> values = card.tags.get( tag );
 			for( Object value : values ) {
 
@@ -180,6 +181,14 @@
 					}
 				}
 
+				
+				if( strict_type != null && !strict_type.isAssignableFrom(value.getClass()) ) {
+					throw new RuntimeException(
+						"Assigned a value of unconforming class ("+value.getClass().getName()+") to a tag of strict type ("+
+						strict_type.getName()+") '"+tag+"', card identifier "+String.valueOf(card.identifierGet())+"."
+					);
+				}
+
 				if( value == null || "".equals(value) ) {
 					if( !tag.equals("") ) {
 						/* It seems that if a field with empty string value is analyzed, it isn't searchable at all. */
@@ -210,12 +219,26 @@
 		Card card = new Card();
 
 		for( Fieldable field : document.getFields() ) {
-			/** TODO: Find how to get NumericField from document. */
+			String name = field.name();
 			String value = field.stringValue();
-			card.tagValueAdd( field.name(), value.equals("") ? null : value );
+			
+			Class<?> specific_type = Card.STRICT_TAG_TYPES.get(name);
+
+			if( specific_type == null ) {
+				card.tagValueAdd( name, value.equals("") ? null : value );
+				continue;
+			}
+
+			if( Long.class.isAssignableFrom(specific_type) ) {
+				card.tagValueAdd( name, Long.valueOf(value) );
+			} else {
+				throw new RuntimeException( "Specific type of tag '"+name+"' is not convertable. (Programmer error.)" );
+			}
 		}
 
-		card.tagValueSetOnly( Card.TAG_IDENTIFIER, Long.valueOf( document.get( Card.TAG_IDENTIFIER ) ) );
+		if( !card.tagHas( Card.TAG_IDENTIFIER ) ) {
+			throw new RuntimeException("Card with no identifier!");
+		}
 
 		return card;
 
@@ -403,7 +426,10 @@
 				finalQuery,
 				null,
 				amount,
-				new Sort( new SortField( Card.TAG_LAST_EDIT, SortField.LONG, true ) )
+				new Sort(
+					new SortField( Card.TAG_LAST_EDIT, SortField.LONG, true ),
+					new SortField( Card.TAG_SAVED, SortField.LONG, true )
+				)
 			);
 
 			Card[] cards = new Card[topDocuments.scoreDocs.length];