[hibernate-commits] Hibernate SVN: r10676 - branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Nov 1 10:22:41 EST 2006


Author: epbernard
Date: 2006-11-01 10:22:40 -0500 (Wed, 01 Nov 2006)
New Revision: 10676

Modified:
   branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/LuceneQueryImpl.java
   branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/ScrollableResultsImpl.java
Log:
style

Modified: branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/LuceneQueryImpl.java
===================================================================
--- branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/LuceneQueryImpl.java	2006-11-01 14:31:16 UTC (rev 10675)
+++ branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/LuceneQueryImpl.java	2006-11-01 15:22:40 UTC (rev 10676)
@@ -38,237 +38,242 @@
  */
 //implements setParameter()
 public class LuceneQueryImpl extends AbstractQueryImpl {
-    private static final Log log = LogFactory.getLog(LuceneQueryImpl.class);
-    private org.apache.lucene.search.Query luceneQuery;
-    private Class[] classes;
-    private Integer firstResult;
-    private Integer maxResults;
-    private int resultSize;
+	private static final Log log = LogFactory.getLog( LuceneQueryImpl.class );
+	private org.apache.lucene.search.Query luceneQuery;
+	private Class[] classes;
+	private Integer firstResult;
+	private Integer maxResults;
+	private int resultSize;
 
-    /**
-     * classes must be immutable
-     */
-    public LuceneQueryImpl(org.apache.lucene.search.Query query, Class[] classes, SessionImplementor session, ParameterMetadata parameterMetadata) {
-        //TODO handle flushMode
-        super(query.toString(), null, session, parameterMetadata);
-        this.luceneQuery = query;
-        this.classes = classes;
-    }
+	/**
+	 * classes must be immutable
+	 */
+	public LuceneQueryImpl(org.apache.lucene.search.Query query, Class[] classes, SessionImplementor session,
+						   ParameterMetadata parameterMetadata) {
+		//TODO handle flushMode
+		super( query.toString(), null, session, parameterMetadata );
+		this.luceneQuery = query;
+		this.classes = classes;
+	}
 
-    /**
-     * Return an interator on the results.
-     * Retrieve the object one by one (initialize it during the next() operation)
-     */
-    public Iterator iterate() throws HibernateException {
-        //implement an interator which keep the id/class for each hit and get the object on demand
-        //cause I can't keep the searcher and hence the hit opened. I dont have any hook to know when the
-        //user stop using it
-        //scrollable is better in this area
+	/**
+	 * Return an interator on the results.
+	 * Retrieve the object one by one (initialize it during the next() operation)
+	 */
+	public Iterator iterate() throws HibernateException {
+		//implement an interator which keep the id/class for each hit and get the object on demand
+		//cause I can't keep the searcher and hence the hit opened. I dont have any hook to know when the
+		//user stop using it
+		//scrollable is better in this area
 
-        LuceneEventListener listener = getLuceneEventListener();
-        //find the directories
-        Searcher searcher = buildSearcher(listener);
-        Hits hits;
-        try {
-            hits = searcher.search(luceneQuery);
-            setResultSize(hits);
-            int first = first();
-            int max = max(first, hits);
-            EntityInfo[] entityInfos = new EntityInfo[max - first + 1];
-            for (int index = first; index <= max; index++) {
-                Document document = hits.doc(index);
-                EntityInfo entityInfo = new EntityInfo();
-                entityInfo.clazz = DocumentBuilder.getDocumentClass(document);
-                //FIXME should check that clazz match classes but this complexify a lot the firstResult/maxResult
-                entityInfo.id = DocumentBuilder.getDocumentId(listener, entityInfo.clazz, document);
-                entityInfos[index - first] = entityInfo;
-            }
-            return new IteratorImpl(entityInfos, (Session) this.session);
-        }
-        catch (IOException e) {
-            throw new HibernateException("Unable to query Lucene index", e);
-        }
-        finally {
-            if (searcher != null) try {
-                searcher.close();
-            }
-            catch (IOException e) {
-                log.warn("Unable to properly close searcher during lucene query: " + getQueryString(), e);
-            }
-        }
-    }
+		LuceneEventListener listener = getLuceneEventListener();
+		//find the directories
+		Searcher searcher = buildSearcher( listener );
+		Hits hits;
+		try {
+			hits = searcher.search( luceneQuery );
+			setResultSize( hits );
+			int first = first();
+			int max = max( first, hits );
+			EntityInfo[] entityInfos = new EntityInfo[max - first + 1];
+			for ( int index = first; index <= max; index++ ) {
+				Document document = hits.doc( index );
+				EntityInfo entityInfo = new EntityInfo();
+				entityInfo.clazz = DocumentBuilder.getDocumentClass( document );
+				//FIXME should check that clazz match classes but this complexify a lot the firstResult/maxResult
+				entityInfo.id = DocumentBuilder.getDocumentId( listener, entityInfo.clazz, document );
+				entityInfos[index - first] = entityInfo;
+			}
+			return new IteratorImpl( entityInfos, (Session) this.session );
+		}
+		catch (IOException e) {
+			throw new HibernateException( "Unable to query Lucene index", e );
+		}
+		finally {
+			if ( searcher != null ) try {
+				searcher.close();
+			}
+			catch (IOException e) {
+				log.warn( "Unable to properly close searcher during lucene query: " + getQueryString(), e );
+			}
+		}
+	}
 
-    public ScrollableResults scroll() throws HibernateException {
-        //keep the searcher open until the resultset is closed
-        LuceneEventListener listener = getLuceneEventListener();
-        //find the directories
-        Searcher searcher = buildSearcher(listener);
-        Hits hits;
-        try {
-            hits = searcher.search(luceneQuery);
-            setResultSize(hits);
-            int first = first();
-            int max = max(first, hits);
-            return new ScrollableResultsImpl(searcher, hits, first, max, (Session) this.session, listener);
-        }
-        catch (IOException e) {
-            try {
-                if (searcher != null) searcher.close();
-            }
-            catch (IOException ee) {
-                //we have the initial issue already
-            }
-            throw new HibernateException("Unable to query Lucene index", e);
-        }
-    }
+	public ScrollableResults scroll() throws HibernateException {
+		//keep the searcher open until the resultset is closed
+		LuceneEventListener listener = getLuceneEventListener();
+		//find the directories
+		Searcher searcher = buildSearcher( listener );
+		Hits hits;
+		try {
+			hits = searcher.search( luceneQuery );
+			setResultSize( hits );
+			int first = first();
+			int max = max( first, hits );
+			return new ScrollableResultsImpl( searcher, hits, first, max, (Session) this.session, listener );
+		}
+		catch (IOException e) {
+			try {
+				if ( searcher != null ) searcher.close();
+			}
+			catch (IOException ee) {
+				//we have the initial issue already
+			}
+			throw new HibernateException( "Unable to query Lucene index", e );
+		}
+	}
 
-    public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException {
-        //TODO think about this scrollmode
-        return scroll();
-    }
+	public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException {
+		//TODO think about this scrollmode
+		return scroll();
+	}
 
-    public List list() throws HibernateException {
-        LuceneEventListener listener = getLuceneEventListener();
-        //find the directories
-        Searcher searcher = buildSearcher(listener);
-        Hits hits;
-        try {
-            hits = searcher.search(luceneQuery);
-            setResultSize(hits);
-            int first = first();
-            int max = max(first, hits);
-            List result = new ArrayList(max - first + 1);
-            Session sess = (Session) this.session;
-            for (int index = first; index <= max; index++) {
-                Document document = hits.doc(index);
-                Class clazz = DocumentBuilder.getDocumentClass(document);
-                //FIXME should check that clazz match classes but this complexify a lot the firstResult/maxResult
-                Serializable id = DocumentBuilder.getDocumentId(listener, clazz, document);
-                result.add(sess.load(clazz, id));
-                //use load to benefit from the batch-size (but facing some proxy casting issues...
-            }
-            //then initialize the objects
-            for (Object element : result) {
-                Hibernate.initialize(element);
-            }
-            return result;
-        }
-        catch (IOException e) {
-            throw new HibernateException("Unable to query Lucene index", e);
-        }
-        finally {
-            if (searcher != null) try {
-                searcher.close();
-            }
-            catch (IOException e) {
-                log.warn("Unable to properly close searcher during lucene query: " + getQueryString(), e);
-            }
-        }
-    }
+	public List list() throws HibernateException {
+		LuceneEventListener listener = getLuceneEventListener();
+		//find the directories
+		Searcher searcher = buildSearcher( listener );
+		Hits hits;
+		try {
+			hits = searcher.search( luceneQuery );
+			setResultSize( hits );
+			int first = first();
+			int max = max( first, hits );
+			List result = new ArrayList( max - first + 1 );
+			Session sess = (Session) this.session;
+			for ( int index = first; index <= max; index++ ) {
+				Document document = hits.doc( index );
+				Class clazz = DocumentBuilder.getDocumentClass( document );
+				//FIXME should check that clazz match classes but this complexify a lot the firstResult/maxResult
+				Serializable id = DocumentBuilder.getDocumentId( listener, clazz, document );
+				result.add( sess.load( clazz, id ) );
+				//use load to benefit from the batch-size (but facing some proxy casting issues...
+			}
+			//then initialize the objects
+			for ( Object element : result ) {
+				Hibernate.initialize( element );
+			}
+			return result;
+		}
+		catch (IOException e) {
+			throw new HibernateException( "Unable to query Lucene index", e );
+		}
+		finally {
+			if ( searcher != null ) try {
+				searcher.close();
+			}
+			catch (IOException e) {
+				log.warn( "Unable to properly close searcher during lucene query: " + getQueryString(), e );
+			}
+		}
+	}
 
-    private int max(int first, Hits hits) {
-        return maxResults == null ?
-                first + hits.length() - 1 :
-                maxResults + first < hits.length() ?
-                        first + maxResults :
-                        hits.length() - 1;
-    }
+	private int max(int first, Hits hits) {
+		return maxResults == null ?
+				first + hits.length() - 1 :
+				maxResults + first < hits.length() ?
+						first + maxResults :
+						hits.length() - 1;
+	}
 
-    private int first() {
-        return firstResult != null ? firstResult : 0;
-    }
+	private int first() {
+		return firstResult != null ?
+				firstResult :
+				0;
+	}
 
-    private Searcher buildSearcher(LuceneEventListener listener) {
-        Map<Class, DocumentBuilder<Object>> builders = listener.getDocumentBuilders();
-        Set<Directory> directories = new HashSet<Directory>();
-        if (classes == null || classes.length == 0) {
-            //no class means all classes
-            for (DocumentBuilder builder : builders.values()) {
-                directories.add(builder.getDirectoryProvider().getDirectory());
-            }
-        } else {
-            Set<Class> involvedClasses = new HashSet<Class>(classes.length);
-            Collections.addAll(involvedClasses, classes);
-            for (Class clazz : classes) {
-                DocumentBuilder builder = builders.get(clazz);
-                if (builder != null) involvedClasses.addAll(builder.getMappedSubclasses());
-            }
-            for (Class clazz : involvedClasses) {
-                DocumentBuilder builder = builders.get(clazz);
-                //TODO should we rather choose a polymorphic path and allow non mapped entities
-                if (builder == null) throw new HibernateException("Not a mapped entity: " + clazz);
-                directories.add(builder.getDirectoryProvider().getDirectory());
-            }
-        }
+	private Searcher buildSearcher(LuceneEventListener listener) {
+		Map<Class, DocumentBuilder<Object>> builders = listener.getDocumentBuilders();
+		Set<Directory> directories = new HashSet<Directory>();
+		if ( classes == null || classes.length == 0 ) {
+			//no class means all classes
+			for ( DocumentBuilder builder : builders.values() ) {
+				directories.add( builder.getDirectoryProvider().getDirectory() );
+			}
+		}
+		else {
+			Set<Class> involvedClasses = new HashSet<Class>( classes.length );
+			Collections.addAll( involvedClasses, classes );
+			for ( Class clazz : classes ) {
+				DocumentBuilder builder = builders.get( clazz );
+				if ( builder != null ) involvedClasses.addAll( builder.getMappedSubclasses() );
+			}
+			for ( Class clazz : involvedClasses ) {
+				DocumentBuilder builder = builders.get( clazz );
+				//TODO should we rather choose a polymorphic path and allow non mapped entities
+				if ( builder == null ) throw new HibernateException( "Not a mapped entity: " + clazz );
+				directories.add( builder.getDirectoryProvider().getDirectory() );
+			}
+		}
 
-        //set up the searcher
-        Searcher searcher;
-        int dirNbr = directories.size();
-        if (dirNbr > 1) {
-            try {
-                IndexSearcher[] searchers = new IndexSearcher[dirNbr];
-                Iterator<Directory> it = directories.iterator();
-                for (int index = 0; index < dirNbr; index++) {
-                    searchers[index] = new IndexSearcher(it.next());
-                }
-                searcher = new MultiSearcher(searchers);
-            }
-            catch (IOException e) {
-                throw new HibernateException("Unable to read Lucene directory", e);
-            }
-        } else {
-            try {
-                searcher = new IndexSearcher(directories.iterator().next());
-            }
-            catch (IOException e) {
-                throw new HibernateException("Unable to read Lucene directory", e);
-            }
-        }
-        return searcher;
-    }
+		//set up the searcher
+		Searcher searcher;
+		int dirNbr = directories.size();
+		if ( dirNbr > 1 ) {
+			try {
+				IndexSearcher[] searchers = new IndexSearcher[dirNbr];
+				Iterator<Directory> it = directories.iterator();
+				for ( int index = 0; index < dirNbr; index++ ) {
+					searchers[index] = new IndexSearcher( it.next() );
+				}
+				searcher = new MultiSearcher( searchers );
+			}
+			catch (IOException e) {
+				throw new HibernateException( "Unable to read Lucene directory", e );
+			}
+		}
+		else {
+			try {
+				searcher = new IndexSearcher( directories.iterator().next() );
+			}
+			catch (IOException e) {
+				throw new HibernateException( "Unable to read Lucene directory", e );
+			}
+		}
+		return searcher;
+	}
 
-    private void setResultSize(Hits hits) {
-        resultSize = hits.length();
-    }
+	private void setResultSize(Hits hits) {
+		resultSize = hits.length();
+	}
 
-    //FIXME does it make sense
-    public int resultSize() {
-        return this.resultSize;
-    }
+	//FIXME does it make sense
+	public int resultSize() {
+		return this.resultSize;
+	}
 
-    public Query setFirstResult(int firstResult) {
-        this.firstResult = firstResult;
-        return this;
-    }
+	public Query setFirstResult(int firstResult) {
+		this.firstResult = firstResult;
+		return this;
+	}
 
-    public Query setMaxResults(int maxResults) {
-        this.maxResults = maxResults;
-        return this;
-    }
+	public Query setMaxResults(int maxResults) {
+		this.maxResults = maxResults;
+		return this;
+	}
 
-    private LuceneEventListener getLuceneEventListener() {
-        PostInsertEventListener[] listeners = session.getListeners().getPostInsertEventListeners();
-        LuceneEventListener listener = null;
-        //FIXME this sucks since we mandante the event listener use
-        for (PostInsertEventListener candidate : listeners) {
-            if (candidate instanceof LuceneEventListener) {
-                listener = (LuceneEventListener) candidate;
-                break;
-            }
-        }
-        if (listener == null) throw new HibernateException("Lucene event listener not initialized");
-        return listener;
-    }
+	private LuceneEventListener getLuceneEventListener() {
+		PostInsertEventListener[] listeners = session.getListeners().getPostInsertEventListeners();
+		LuceneEventListener listener = null;
+		//FIXME this sucks since we mandante the event listener use
+		for ( PostInsertEventListener candidate : listeners ) {
+			if ( candidate instanceof LuceneEventListener ) {
+				listener = (LuceneEventListener) candidate;
+				break;
+			}
+		}
+		if ( listener == null ) throw new HibernateException( "Lucene event listener not initialized" );
+		return listener;
+	}
 
-    public int executeUpdate() throws HibernateException {
-        throw new HibernateException("Not supported operation");
-    }
+	public int executeUpdate() throws HibernateException {
+		throw new HibernateException( "Not supported operation" );
+	}
 
-    public Query setLockMode(String alias, LockMode lockMode) {
-        return null;
-    }
+	public Query setLockMode(String alias, LockMode lockMode) {
+		return null;
+	}
 
-    protected Map getLockModes() {
-        return null;
-    }
+	protected Map getLockModes() {
+		return null;
+	}
 }

Modified: branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/ScrollableResultsImpl.java
===================================================================
--- branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/ScrollableResultsImpl.java	2006-11-01 14:31:16 UTC (rev 10675)
+++ branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/query/ScrollableResultsImpl.java	2006-11-01 15:22:40 UTC (rev 10676)
@@ -46,6 +46,7 @@
 		this.listener = listener;
 		entityInfos = new EntityInfo[max - first + 1];
 	}
+
 	public boolean next() throws HibernateException {
 		return ++current <= max;
 	}
@@ -90,12 +91,12 @@
 			searcher.close();
 		}
 		catch (IOException e) {
-			throw new HibernateException( "Unable to close Lucene searcher", e);
+			throw new HibernateException( "Unable to close Lucene searcher", e );
 		}
 	}
 
 	public Object[] get() throws HibernateException {
-		if (current < first || current > max) return null; //or exception?
+		if ( current < first || current > max ) return null; //or exception?
 		EntityInfo info = entityInfos[current - first];
 		if ( info == null ) {
 			info = new EntityInfo();
@@ -104,14 +105,14 @@
 				document = hits.doc( current );
 			}
 			catch (IOException e) {
-				throw new HibernateException( "Unable to read Lucene hits[" + current  + "]", e);
+				throw new HibernateException( "Unable to read Lucene hits[" + current + "]", e );
 			}
 			info.clazz = DocumentBuilder.getDocumentClass( document );
 			//FIXME should check that clazz match classes but this complexify a lot the firstResult/maxResult
 			info.id = DocumentBuilder.getDocumentId( listener, info.clazz, document );
 			entityInfos[current - first] = info;
 		}
-		return new Object[] {
+		return new Object[]{
 				session.get( info.clazz, info.id )
 		};
 	}
@@ -206,7 +207,7 @@
 	}
 
 	public boolean setRowNumber(int rowNumber) throws HibernateException {
-		if (rowNumber >= 0) {
+		if ( rowNumber >= 0 ) {
 			current = first + rowNumber;
 		}
 		else {




More information about the hibernate-commits mailing list