portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r967312 - in /portals/jetspeed-2/portal/trunk: components/jetspeed-portal/src/main/java/org/apache/jetspeed/tools/pamanager/ components/jetspeed-registry/src/main/java/org/apache/jetspeed/serializer/ components/jetspeed-search/src/main/java...
Date Sat, 24 Jul 2010 00:13:19 GMT
Author: woonsan
Date: Sat Jul 24 00:13:19 2010
New Revision: 967312

URL: http://svn.apache.org/viewvc?rev=967312&view=rev
Log:
JS2-1198: A refactoring in search engine impl.
Because adding a ParsedObject item includes removing the existing one with same key, adding
does the same as updating.
Also, optimization during adding or removing by reducing index-rewriting calls.

Modified:
    portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManager.java
    portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/serializer/JetspeedRegistrySerializer.java
    portals/jetspeed-2/portal/trunk/components/jetspeed-search/src/main/java/org/apache/jetspeed/search/lucene/SearchEngineImpl.java
    portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/search/SearchEngine.java

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManager.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManager.java?rev=967312&r1=967311&r2=967312&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManager.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/tools/pamanager/PortletApplicationManager.java
Sat Jul 24 00:13:19 2010
@@ -21,14 +21,13 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.jetspeed.cluster.NodeManager;
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
 import org.apache.jetspeed.components.portletregistry.RegistryException;
 import org.apache.jetspeed.descriptor.JetspeedDescriptorService;
 import org.apache.jetspeed.factory.PortletFactory;
 import org.apache.jetspeed.om.portlet.PortletApplication;
+import org.apache.jetspeed.om.portlet.PortletDefinition;
 import org.apache.jetspeed.om.portlet.SecurityRole;
 import org.apache.jetspeed.search.SearchEngine;
 import org.apache.jetspeed.security.JetspeedPermission;
@@ -40,6 +39,8 @@ import org.apache.jetspeed.util.Director
 import org.apache.jetspeed.util.FileSystemHelper;
 import org.apache.jetspeed.util.MultiFileChecksumHelper;
 import org.apache.jetspeed.util.descriptor.PortletApplicationWar;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * PortletApplicationManager
@@ -717,21 +718,26 @@ public class PortletApplicationManager i
     {
         if (searchEngine != null)
         {
+            List<PortletDefinition> portletDefList = pa.getPortlets();
+            List<Object> list = new ArrayList<Object>(portletDefList.size() +
1);
+            
             if (remove)
             {
-                searchEngine.remove(pa);
-                searchEngine.remove(pa.getPortlets());
+                list.addAll(portletDefList);
+                list.add(pa);
+                searchEngine.remove(list);
                 log.info("Un-Registered the portlet application in the search engine... "
+ pa.getName());
             }
             else
             {
-                searchEngine.add(pa);
-                searchEngine.add(pa.getPortlets());
+                list.add(pa);
+                list.addAll(portletDefList);
+                searchEngine.add(list);
                 log.info("Registered the portlet application in the search engine... " +
pa.getName());
             }
         }
-        
     }
+    
     protected void unregisterPortletApplication(PortletApplication pa,
         boolean purgeEntityInfo)
         throws RegistryException

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/serializer/JetspeedRegistrySerializer.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/serializer/JetspeedRegistrySerializer.java?rev=967312&r1=967311&r2=967312&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/serializer/JetspeedRegistrySerializer.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/serializer/JetspeedRegistrySerializer.java
Sat Jul 24 00:13:19 2010
@@ -15,6 +15,7 @@
  */
 package org.apache.jetspeed.serializer;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -108,8 +109,10 @@ public class JetspeedRegistrySerializer 
                     List<PortletDefinition> portlets = pa.getPortlets();
                     if (searchEngine != null)
                     {
-                        searchEngine.remove(pa);
-                        searchEngine.remove(portlets);
+                        List<Object> list = new ArrayList<Object>(portlets.size()
+ 1);
+                        list.add(pa);
+                        list.addAll(portlets);
+                        searchEngine.remove(list);
                     }
                     registry.removeApplication(pa);
                 }

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-search/src/main/java/org/apache/jetspeed/search/lucene/SearchEngineImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-search/src/main/java/org/apache/jetspeed/search/lucene/SearchEngineImpl.java?rev=967312&r1=967311&r2=967312&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-search/src/main/java/org/apache/jetspeed/search/lucene/SearchEngineImpl.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-search/src/main/java/org/apache/jetspeed/search/lucene/SearchEngineImpl.java
Sat Jul 24 00:13:19 2010
@@ -158,185 +158,11 @@ public class SearchEngineImpl implements
     /* (non-Javadoc)
      * @see org.apache.jetspeed.search.SearchEnging#add(java.util.Collection)
      */
-    public synchronized boolean add(Collection objects)
+    public boolean add(Collection objects)
     {
-        IndexWriter indexWriter = null;
-        IndexReader indexReader = null;
-        Searcher searcher = null;
-        
-        try
-        {
-            Iterator it = objects.iterator();
-            while (it.hasNext()) 
-            {
-                if (indexWriter == null)
-                {
-                    indexWriter = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
-                    indexReader = indexWriter.getReader();
-                    searcher = new IndexSearcher(indexReader);
-                }
-                
-                Object o = it.next();
-                // Look up appropriate handler
-                ObjectHandler handler = null;
-                try
-                {
-                    handler = handlerFactory.getHandler(o);
-                }
-                catch (Exception e)
-                {
-                    log.error("Failed to create hanlder for object " + o.getClass().getName());
-                    continue;
-                }
-    
-                // Parse the object
-                ParsedObject parsedObject = handler.parseObject(o);
-                
-                String key = parsedObject.getKey();
-                // if there's an existing one with the same key, then remove it first.
-                if (parsedObject.getKey() != null)
-                {
-                    Term keyTerm = new Term(ParsedObject.FIELDNAME_KEY, key);
-                    TopDocs topDocs = searcher.search(new TermQuery(keyTerm), 1);
-                    if (topDocs.totalHits > 0)
-                    {
-                        indexWriter.deleteDocuments(keyTerm);
-                    }
-                }
-                
-                String type = parsedObject.getType();
-                String title = parsedObject.getTitle();
-                String description = parsedObject.getDescription();
-                String content = parsedObject.getContent();
-                String language = parsedObject.getLanguage();
-                URL url = parsedObject.getURL();
-                String className = parsedObject.getClassName();
-                
-                // Create document
-                Document doc = new Document();
-                
-                // Populate document from the parsed object
-                if (key != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_KEY, key, Field.Store.YES, Field.Index.NOT_ANALYZED));
-                }
-                if (type != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_TYPE, type, Field.Store.YES,
Field.Index.ANALYZED));
-                }
-                if (title != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_TITLE, title, Field.Store.YES,
Field.Index.ANALYZED));
-                }
-                if (description != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_DESCRIPTION, description, Field.Store.YES,
Field.Index.ANALYZED));
-                }
-                if (content != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_CONTENT, content, Field.Store.NO,
Field.Index.ANALYZED));
-                }
-                if (language != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_LANGUAGE, language, Field.Store.YES,
Field.Index.ANALYZED));
-                }
-                if (url != null)
-                {
-                    String urlString = url.toString();
-                    doc.add(new Field(ParsedObject.FIELDNAME_URL, urlString, Field.Store.YES,
Field.Index.ANALYZED));
-                }
-                if (className != null)
-                {
-                    doc.add(new Field(ParsedObject.FIELDNAME_CLASSNAME, className, Field.Store.YES,
Field.Index.ANALYZED));
-                }
-                
-                String[] keywordArray = parsedObject.getKeywords();
-                if(keywordArray != null)
-                {
-                	for(int i=0; i<keywordArray.length; ++i)
-                	{
-                		String keyword = keywordArray[i];
-                		doc.add(new Field(ParsedObject.FIELDNAME_KEYWORDS, keyword, Field.Store.YES,
Field.Index.NOT_ANALYZED));
-                	}
-                }
-    
-                Map keywords = parsedObject.getKeywordsMap();
-                addFieldsToDocument(doc, keywords, KEYWORD);
-                
-                Map fields = parsedObject.getFields();
-                addFieldsToDocument(doc, fields, TEXT);
-                
-                List<String> syntheticField = new ArrayList<String>();
-                for (Fieldable fieldable : doc.getFields())
-                {
-                    String value = fieldable.stringValue();
-                    if (value != null)
-                    {
-                        syntheticField.add(value);
-                    }
-                }
-                doc.add(new Field(ParsedObject.FIELDNAME_SYNTHETIC, StringUtils.join(syntheticField,
' '), Field.Store.NO, Field.Index.ANALYZED));
-
-                // Add the document to search index
-                indexWriter.addDocument(doc);
-                //logger.debug("Index Document Count = " + indexWriter.docCount());
-                //logger.info("Added '" + parsedObject.getTitle() + "' to index");
-            }
-
-        	if (objects.size() > 0 && optimizeAfterUpdate && indexWriter
!= null)
-            {
-        	    try
-        	    {
-        	        indexWriter.optimize();
-        	    }
-                catch (IOException e)
-                {
-                    log.error("Error while trying to optimize index.", e);
-                }
-            }
-        }
-        catch (IOException e)
-        {
-            log.error("Error while writing index.", e);
-            return false;
-        }
-        finally
-        {
-            if (searcher != null)
-            {
-                try
-                {
-                    searcher.close();
-                }
-                catch (IOException ce)
-                {
-                }
-            }
-            if (indexReader != null)
-            {
-                try
-                {
-                    indexReader.close();
-                }
-                catch (IOException ce)
-                {
-                }
-            }
-            if (indexWriter != null)
-            {
-                try
-                {
-                    indexWriter.close();
-                }
-                catch (IOException ce)
-                {
-                }
-            }
-        }
-        
-        return true;
+        return removeIfExistsAndAdd(objects);
     }
-
+    
     /* (non-Javadoc)
      * @see org.apache.jetspeed.search.SearchEnging#remove(java.lang.Object)
      */
@@ -432,36 +258,9 @@ public class SearchEngineImpl implements
     /* (non-Javadoc)
      * @see org.apache.jetspeed.search.SearchEnging#update(java.util.Collection)
      */
-    public synchronized boolean update(Collection objects)
+    public boolean update(Collection objects)
     {
-        boolean result = false;
-        
-        try
-        {
-            // Delete entries from index
-            remove(objects);
-            result = true;
-        }
-        catch (Throwable e)
-        {
-            //logger.error("Exception",  e);
-        }
-
-        try
-        {
-            // Add entries to index
-        	if(result)
-        	{
-        		add(objects);
-        		result = true;
-        	}
-        }
-        catch (Throwable e)
-        {
-            //logger.error("Exception",  e);
-        }
-        
-        return result;
+        return removeIfExistsAndAdd(objects);
     }
 
     /* (non-Javadoc)
@@ -638,6 +437,185 @@ public class SearchEngineImpl implements
         return (results != null ? results : new SearchResultsImpl(new ArrayList<ParsedObject>()));
     }
     
+    private synchronized boolean removeIfExistsAndAdd(Collection objects)
+    {
+        IndexWriter indexWriter = null;
+        IndexReader indexReader = null;
+        Searcher searcher = null;
+        
+        try
+        {
+            Iterator it = objects.iterator();
+            while (it.hasNext()) 
+            {
+                if (indexWriter == null)
+                {
+                    indexWriter = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
+                    indexReader = indexWriter.getReader();
+                    searcher = new IndexSearcher(indexReader);
+                }
+                
+                Object o = it.next();
+                // Look up appropriate handler
+                ObjectHandler handler = null;
+                try
+                {
+                    handler = handlerFactory.getHandler(o);
+                }
+                catch (Exception e)
+                {
+                    log.error("Failed to create hanlder for object " + o.getClass().getName());
+                    continue;
+                }
+    
+                // Parse the object
+                ParsedObject parsedObject = handler.parseObject(o);
+                
+                String key = parsedObject.getKey();
+                // if there's an existing one with the same key, then remove it first.
+                if (parsedObject.getKey() != null)
+                {
+                    Term keyTerm = new Term(ParsedObject.FIELDNAME_KEY, key);
+                    TopDocs topDocs = searcher.search(new TermQuery(keyTerm), 1);
+                    if (topDocs.totalHits > 0)
+                    {
+                        indexWriter.deleteDocuments(keyTerm);
+                    }
+                }
+                
+                String type = parsedObject.getType();
+                String title = parsedObject.getTitle();
+                String description = parsedObject.getDescription();
+                String content = parsedObject.getContent();
+                String language = parsedObject.getLanguage();
+                URL url = parsedObject.getURL();
+                String className = parsedObject.getClassName();
+                
+                // Create document
+                Document doc = new Document();
+                
+                // Populate document from the parsed object
+                if (key != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_KEY, key, Field.Store.YES, Field.Index.NOT_ANALYZED));
+                }
+                if (type != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_TYPE, type, Field.Store.YES,
Field.Index.ANALYZED));
+                }
+                if (title != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_TITLE, title, Field.Store.YES,
Field.Index.ANALYZED));
+                }
+                if (description != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_DESCRIPTION, description, Field.Store.YES,
Field.Index.ANALYZED));
+                }
+                if (content != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_CONTENT, content, Field.Store.NO,
Field.Index.ANALYZED));
+                }
+                if (language != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_LANGUAGE, language, Field.Store.YES,
Field.Index.ANALYZED));
+                }
+                if (url != null)
+                {
+                    String urlString = url.toString();
+                    doc.add(new Field(ParsedObject.FIELDNAME_URL, urlString, Field.Store.YES,
Field.Index.ANALYZED));
+                }
+                if (className != null)
+                {
+                    doc.add(new Field(ParsedObject.FIELDNAME_CLASSNAME, className, Field.Store.YES,
Field.Index.ANALYZED));
+                }
+                
+                String[] keywordArray = parsedObject.getKeywords();
+                if(keywordArray != null)
+                {
+                    for(int i=0; i<keywordArray.length; ++i)
+                    {
+                        String keyword = keywordArray[i];
+                        doc.add(new Field(ParsedObject.FIELDNAME_KEYWORDS, keyword, Field.Store.YES,
Field.Index.NOT_ANALYZED));
+                    }
+                }
+    
+                Map keywords = parsedObject.getKeywordsMap();
+                addFieldsToDocument(doc, keywords, KEYWORD);
+                
+                Map fields = parsedObject.getFields();
+                addFieldsToDocument(doc, fields, TEXT);
+                
+                List<String> syntheticField = new ArrayList<String>();
+                for (Fieldable fieldable : doc.getFields())
+                {
+                    String value = fieldable.stringValue();
+                    if (value != null)
+                    {
+                        syntheticField.add(value);
+                    }
+                }
+                doc.add(new Field(ParsedObject.FIELDNAME_SYNTHETIC, StringUtils.join(syntheticField,
' '), Field.Store.NO, Field.Index.ANALYZED));
+
+                // Add the document to search index
+                indexWriter.addDocument(doc);
+                //logger.debug("Index Document Count = " + indexWriter.docCount());
+                //logger.info("Added '" + parsedObject.getTitle() + "' to index");
+            }
+
+            if (objects.size() > 0 && optimizeAfterUpdate && indexWriter
!= null)
+            {
+                try
+                {
+                    indexWriter.optimize();
+                }
+                catch (IOException e)
+                {
+                    log.error("Error while trying to optimize index.", e);
+                }
+            }
+        }
+        catch (IOException e)
+        {
+            log.error("Error while writing index.", e);
+            return false;
+        }
+        finally
+        {
+            if (searcher != null)
+            {
+                try
+                {
+                    searcher.close();
+                }
+                catch (IOException ce)
+                {
+                }
+            }
+            if (indexReader != null)
+            {
+                try
+                {
+                    indexReader.close();
+                }
+                catch (IOException ce)
+                {
+                }
+            }
+            if (indexWriter != null)
+            {
+                try
+                {
+                    indexWriter.close();
+                }
+                catch (IOException ce)
+                {
+                }
+            }
+        }
+        
+        return true;
+    }
+
     private void addFieldsToDocument(Document doc, Map fields, int type)
     {
         if(fields != null)

Modified: portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/search/SearchEngine.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/search/SearchEngine.java?rev=967312&r1=967311&r2=967312&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/search/SearchEngine.java
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/search/SearchEngine.java
Sat Jul 24 00:13:19 2010
@@ -24,23 +24,98 @@ import java.util.Collection;
  */
 public interface SearchEngine
 {
+    /**
+     * Adds search index document for the argument object.
+     * An object handler for the object type is responsible for parsing.
+     * <BR/>
+     * <P>
+     * <EM>Note: Normally, it is more efficient to invoke {@link #add(Collection)}.</EM>
+     * </P>
+     * @param o
+     * @return
+     * @see {@link org.apache.jetspeed.search.ObjectHandler}
+     * @see {@link org.apache.jetspeed.search.ParsedObject}
+     */
     boolean add(Object o);
     
+    /**
+     * Adds search index documents for the argument object collection.
+     * Each object handler for the object type of each item is responsible for parsing.
+     * @param objects
+     * @return
+     */
     boolean add(Collection objects);
     
+    /**
+     * Removes search index document for the argument object.
+     * An object handler for the object type is responsible for parsing.
+     * <BR/>
+     * <P>
+     * <EM>Note: Normally, it is more efficient to invoke {@link #remove(Collection)}.</EM>
+     * </P>
+     * @param o
+     * @return
+     */
     boolean remove(Object o);
     
+    /**
+     * Removes search index documents for the argument object collection.
+     * Each object handler for the object type of each item is responsible for parsing.
+     * @param objects
+     * @return
+     */
     boolean remove(Collection objects);
     
+    /**
+     * Updates the search index document for the argument object.
+     * An object handler for the object type is responsible for parsing.
+     * <BR/>
+     * <P>
+     * <EM>Note: Normally, it is more efficient to invoke {@link #update(Collection)}.</EM>
+     * </P>
+     * @param o
+     * @return
+     */
     boolean update(Object o);
     
+    /**
+     * Updates the search index documents for the argument object collection.
+     * Each object handler for the object type of each item is responsible for parsing.
+     * @param objects
+     * @return
+     */
     boolean update(Collection objects);
     
+    /**
+     * Requests optimization
+     * @return
+     */
     boolean optimize();
     
+    /**
+     * Searches documents by the query.
+     * The default field name and the default top hits count can be used in a specific implementation.
+     * @param query
+     * @return
+     */
     SearchResults search(String query);
     
+    /**
+     * Searches documents by the query against the default field name.
+     * The default top hits count can be used in a specific implementation.
+     * @param query
+     * @param defaultFieldName
+     * @return
+     */
     SearchResults search(String query, String defaultFieldName);
     
+    /**
+     * Searches documents by the query against the default field name.
+     * The returned item count will not be more than topHitsCount.
+     * @param query
+     * @param defaultFieldName
+     * @param topHitsCount
+     * @return
+     */
     SearchResults search(String query, String defaultFieldName, int topHitsCount);
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message