mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
1.) Adding null pointer guards against collections being null
2.) Improving logic of Browse and Search consumers because both API support the same method for both the update and add of items and internally manage decisions about the appropriateness of the item to be indexed. git-svn-id: http://scm.dspace.org/svn/repo/trunk@2163 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
@@ -47,6 +47,7 @@ import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.DSpaceObject;
|
||||
import org.dspace.core.Context;
|
||||
@@ -81,106 +82,95 @@ public class BrowseConsumer implements Consumer
|
||||
{
|
||||
/** log4j logger */
|
||||
private static Logger log = Logger.getLogger(BrowseConsumer.class);
|
||||
|
||||
// items to be added to browse index
|
||||
private Set toAdd = null;
|
||||
|
||||
// items to be updated in browse index
|
||||
private Set toUpdate = null;
|
||||
|
||||
|
||||
public void initialize()
|
||||
throws Exception
|
||||
{
|
||||
toAdd = new HashSet();
|
||||
toUpdate = new HashSet();
|
||||
|
||||
}
|
||||
|
||||
public void consume(Context ctx, Event event)
|
||||
throws Exception
|
||||
{
|
||||
DSpaceObject subj = event.getSubject(ctx);
|
||||
int et = event.getEventType();
|
||||
|
||||
// If an Item is added or modified..
|
||||
if (subj != null && subj.getType() == Constants.ITEM)
|
||||
if(toUpdate == null)
|
||||
{
|
||||
if (et == Event.CREATE)
|
||||
toAdd.add(subj);
|
||||
else
|
||||
toUpdate.add(subj);
|
||||
|
||||
// track ADD and REMOVE from collections, that changes browse index.
|
||||
} else if (subj != null && subj.getType() == Constants.COLLECTION &&
|
||||
event.getObjectType() == Constants.ITEM &&
|
||||
(et == Event.ADD || et == Event.REMOVE))
|
||||
{
|
||||
DSpaceObject obj = event.getObject(ctx);
|
||||
if (obj != null)
|
||||
toUpdate.add(obj);
|
||||
toUpdate = new HashSet();
|
||||
}
|
||||
else if (subj != null)
|
||||
log.warn("consume() got unrecognized event: "+event.toString());
|
||||
|
||||
DSpaceObject subj = event.getSubject(ctx);
|
||||
|
||||
int st = event.getSubjectType();
|
||||
int et = event.getEventType();
|
||||
|
||||
switch (st)
|
||||
{
|
||||
|
||||
// If an Item is created or modified..
|
||||
case Constants.ITEM:
|
||||
toUpdate.add(subj);
|
||||
break;
|
||||
// track ADD and REMOVE from collections, that changes browse index.
|
||||
case Constants.COLLECTION:
|
||||
if (event.getObjectType() == Constants.ITEM
|
||||
&& (et == Event.ADD || et == Event.REMOVE))
|
||||
{
|
||||
DSpaceObject obj = event.getObject(ctx);
|
||||
if (obj != null)
|
||||
toUpdate.add(obj);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
log.warn("consume() got unrecognized event: " + event.toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void end(Context ctx)
|
||||
throws Exception
|
||||
{
|
||||
for (Iterator ai = toAdd.iterator(); ai.hasNext();)
|
||||
{
|
||||
Item i = (Item)ai.next();
|
||||
// FIXME: there is an exception handling problem here
|
||||
try
|
||||
{
|
||||
// Update browse indices
|
||||
IndexBrowse ib = new IndexBrowse(ctx);
|
||||
ib.indexItem(i);
|
||||
}
|
||||
catch (BrowseException e)
|
||||
{
|
||||
log.error("caught exception: ", e);
|
||||
throw new SQLException(e.getMessage());
|
||||
}
|
||||
|
||||
toUpdate.remove(i);
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Added browse indices for Item id="+String.valueOf(i.getID())+", hdl="+i.getHandle());
|
||||
}
|
||||
|
||||
// don't update an item we've just added.
|
||||
for (Iterator ui = toUpdate.iterator(); ui.hasNext();)
|
||||
{
|
||||
Item i = (Item)ui.next();
|
||||
// FIXME: there is an exception handling problem here
|
||||
try
|
||||
{
|
||||
// Update browse indices
|
||||
IndexBrowse ib = new IndexBrowse(ctx);
|
||||
ib.indexItem(i);
|
||||
}
|
||||
catch (BrowseException e)
|
||||
{
|
||||
log.error("caught exception: ", e);
|
||||
throw new SQLException(e.getMessage());
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Updated browse indices for Item id="+String.valueOf(i.getID())+", hdl="+i.getHandle());
|
||||
if (toUpdate != null)
|
||||
{
|
||||
|
||||
// Update/Add items
|
||||
for (Iterator ui = toUpdate.iterator(); ui.hasNext();)
|
||||
{
|
||||
Item i = (Item) ui.next();
|
||||
// FIXME: there is an exception handling problem here
|
||||
try
|
||||
{
|
||||
// Update browse indices
|
||||
IndexBrowse ib = new IndexBrowse(ctx);
|
||||
ib.indexItem(i);
|
||||
}
|
||||
catch (BrowseException e)
|
||||
{
|
||||
log.error("caught exception: ", e);
|
||||
//throw new SQLException(e.getMessage());
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Updated browse indices for Item id="
|
||||
+ String.valueOf(i.getID()) + ", hdl="
|
||||
+ i.getHandle());
|
||||
}
|
||||
|
||||
// NOTE: Removed items are necessarily handled inline (ugh).
|
||||
|
||||
// browse updates wrote to the DB, so we have to commit.
|
||||
ctx.getDBConnection().commit();
|
||||
|
||||
}
|
||||
|
||||
// NOTE: Removed items are necessarily handled inline (ugh).
|
||||
|
||||
// browse updates wrote to the DB, so we have to commit.
|
||||
ctx.getDBConnection().commit();
|
||||
|
||||
// clean out toAdd & toUpdate
|
||||
toAdd.clear();
|
||||
toUpdate.clear();
|
||||
// clean out toUpdate
|
||||
toUpdate = null;
|
||||
}
|
||||
|
||||
public void finish(Context ctx) {
|
||||
|
||||
toAdd = toUpdate = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@@ -66,10 +66,7 @@ public class SearchConsumer implements Consumer
|
||||
/** log4j logger */
|
||||
private static Logger log = Logger.getLogger(SearchConsumer.class);
|
||||
|
||||
// collect Items, Collections, Communities newly created.
|
||||
private Set objectsCreated = null;
|
||||
|
||||
// collect Items, Collections, Communities that need reindexing
|
||||
// collect Items, Collections, Communities that need indexing
|
||||
private Set objectsToUpdate = null;
|
||||
|
||||
// handles to delete since IDs are not useful by now.
|
||||
@@ -93,9 +90,8 @@ public class SearchConsumer implements Consumer
|
||||
public void consume(Context ctx, Event event) throws Exception
|
||||
{
|
||||
|
||||
if (objectsCreated == null)
|
||||
if (objectsToUpdate == null)
|
||||
{
|
||||
objectsCreated = new HashSet();
|
||||
objectsToUpdate = new HashSet();
|
||||
handlesToDelete = new HashSet();
|
||||
}
|
||||
@@ -144,18 +140,10 @@ public class SearchConsumer implements Consumer
|
||||
switch (et)
|
||||
{
|
||||
case Event.CREATE:
|
||||
if (dso == null)
|
||||
log.warn("CREATE event, could not get object for "
|
||||
+ event.getSubjectTypeAsString() + " id="
|
||||
+ String.valueOf(event.getSubjectID())
|
||||
+ ", perhaps it has been deleted.");
|
||||
else
|
||||
objectsCreated.add(dso);
|
||||
break;
|
||||
case Event.MODIFY:
|
||||
case Event.MODIFY_METADATA:
|
||||
if (dso == null)
|
||||
log.warn("MODIFY event, could not get object for "
|
||||
log.warn(event.getEventTypeAsString() + " event, could not get object for "
|
||||
+ event.getSubjectTypeAsString() + " id="
|
||||
+ String.valueOf(event.getSubjectID())
|
||||
+ ", perhaps it has been deleted.");
|
||||
@@ -186,89 +174,56 @@ public class SearchConsumer implements Consumer
|
||||
*/
|
||||
public void end(Context ctx) throws Exception
|
||||
{
|
||||
// add new created items to index, unless they were deleted.
|
||||
for (Iterator ii = objectsCreated.iterator(); ii.hasNext();)
|
||||
|
||||
if(objectsToUpdate != null && handlesToDelete != null)
|
||||
{
|
||||
DSpaceObject ic = (DSpaceObject) ii.next();
|
||||
if (ic.getType() != Constants.ITEM || ((Item) ic).isArchived())
|
||||
|
||||
// update the changed Items not deleted because they were on create list
|
||||
for (Iterator ii = objectsToUpdate.iterator(); ii.hasNext();)
|
||||
{
|
||||
// if handle is NOT in list of deleted objects, index it:
|
||||
String hdl = ic.getHandle();
|
||||
if (hdl != null && !handlesToDelete.contains(hdl))
|
||||
DSpaceObject iu = (DSpaceObject) ii.next();
|
||||
if (iu.getType() != Constants.ITEM || ((Item) iu).isArchived())
|
||||
{
|
||||
try
|
||||
// if handle is NOT in list of deleted objects, index it:
|
||||
String hdl = iu.getHandle();
|
||||
if (hdl != null && !handlesToDelete.contains(hdl))
|
||||
{
|
||||
DSIndexer.indexContent(ctx, ic);
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Indexed NEW "
|
||||
+ Constants.typeText[ic.getType()]
|
||||
+ ", id=" + String.valueOf(ic.getID())
|
||||
+ ", handle=" + hdl);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Failed while indexing new object: ", e);
|
||||
objectsCreated = null;
|
||||
objectsToUpdate = null;
|
||||
handlesToDelete = null;
|
||||
try
|
||||
{
|
||||
DSIndexer.indexContent(ctx, iu);
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Indexed "
|
||||
+ Constants.typeText[iu.getType()]
|
||||
+ ", id=" + String.valueOf(iu.getID())
|
||||
+ ", handle=" + hdl);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Failed while indexing object: ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// remove it from modified list since we just indexed it.
|
||||
objectsToUpdate.remove(ic);
|
||||
}
|
||||
|
||||
// update the changed Items not deleted because they were on create list
|
||||
for (Iterator ii = objectsToUpdate.iterator(); ii.hasNext();)
|
||||
{
|
||||
DSpaceObject iu = (DSpaceObject) ii.next();
|
||||
if (iu.getType() != Constants.ITEM || ((Item) iu).isArchived())
|
||||
for (Iterator ii = handlesToDelete.iterator(); ii.hasNext();)
|
||||
{
|
||||
// if handle is NOT in list of deleted objects, index it:
|
||||
String hdl = iu.getHandle();
|
||||
if (hdl != null && !handlesToDelete.contains(hdl))
|
||||
String hdl = (String) ii.next();
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
DSIndexer.reIndexContent(ctx, iu);
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("RE-Indexed "
|
||||
+ Constants.typeText[iu.getType()]
|
||||
+ ", id=" + String.valueOf(iu.getID())
|
||||
+ ", handle=" + hdl);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Failed while RE-indexing object: ", e);
|
||||
objectsCreated = null;
|
||||
objectsToUpdate = null;
|
||||
handlesToDelete = null;
|
||||
}
|
||||
DSIndexer.unIndexContent(ctx, hdl);
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("UN-Indexed Item, handle=" + hdl);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Failed while UN-indexing object: " + hdl, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Iterator ii = handlesToDelete.iterator(); ii.hasNext();)
|
||||
{
|
||||
String hdl = (String) ii.next();
|
||||
try
|
||||
{
|
||||
DSIndexer.unIndexContent(ctx, hdl);
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("UN-Indexed Item, handle=" + hdl);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Failed while UN-indexing object: " + hdl, e);
|
||||
objectsCreated = new HashSet();
|
||||
objectsToUpdate = new HashSet();
|
||||
handlesToDelete = new HashSet();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// "free" the resources
|
||||
objectsCreated = null;
|
||||
objectsToUpdate = null;
|
||||
handlesToDelete = null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user