Call uncache item

This commit is contained in:
Terry Brady
2016-08-18 17:15:48 -07:00
parent d64722a628
commit 17af928b05
5 changed files with 84 additions and 35 deletions

View File

@@ -643,27 +643,6 @@ public class Context
dbConnection.shutdown(); dbConnection.shutdown();
} }
/**
* Clear the cache of all object that have been read from the database so far. This will also free up
* (heap space) memory. You should use this method when processing a large number of records.
*
* <b>WARNING: After calling this method all previously fetched entities are "detached" (pending
* changes are not tracked anymore). You have to reload all entities you still want to work with
* manually after this method call (see {@link Context#reloadEntity(ReloadableEntity)}).</b>
*
* This method will take care of reloading the current user.
*
* @throws SQLException When clearing the entity cache fails
*/
public void clearCache() throws SQLException {
if(log.isDebugEnabled()) {
log.debug("Cache size before clear cache is " + getCacheSize());
}
this.getDBConnection().clearCache();
reloadContextBoundEntities();
}
/** /**
* Returns the size of the cache of all object that have been read from the database so far. A larger number * Returns the size of the cache of all object that have been read from the database so far. A larger number

View File

@@ -40,8 +40,6 @@ public interface DBConnection<T> {
public DatabaseConfigVO getDatabaseConfig() throws SQLException; public DatabaseConfigVO getDatabaseConfig() throws SQLException;
public void clearCache() throws SQLException;
public void setOptimizedForBatchProcessing(boolean batchOptimized) throws SQLException; public void setOptimizedForBatchProcessing(boolean batchOptimized) throws SQLException;
public boolean isOptimizedForBatchProcessing(); public boolean isOptimizedForBatchProcessing();

View File

@@ -116,11 +116,6 @@ public class HibernateDBConnection implements DBConnection<Session> {
return databaseConfigVO; return databaseConfigVO;
} }
@Override
public void clearCache() throws SQLException {
getSession().flush();
getSession().clear();
}
@Override @Override
public long getCacheSize() throws SQLException { public long getCacheSize() throws SQLException {

View File

@@ -9,11 +9,20 @@ package org.dspace.discovery;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.handle.factory.HandleServiceFactory;
import org.dspace.services.factory.DSpaceServicesFactory; import org.dspace.services.factory.DSpaceServicesFactory;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Iterator;
/** /**
* Class used to reindex dspace communities/collections/items into discovery * Class used to reindex dspace communities/collections/items into discovery
@@ -41,19 +50,26 @@ public class IndexClient {
Context context = new Context(); Context context = new Context();
context.setIgnoreAuthorization(true); context.setIgnoreAuthorization(true);
String usage = "org.dspace.discovery.IndexClient [-cbhf[r <item handle>]] or nothing to update/clean an existing index."; String usage = "org.dspace.discovery.IndexClient [-cbhf] | [-r <handle>] | [-i <handle>] or nothing to update/clean an existing index.";
Options options = new Options(); Options options = new Options();
HelpFormatter formatter = new HelpFormatter(); HelpFormatter formatter = new HelpFormatter();
CommandLine line = null; CommandLine line = null;
options options
.addOption(OptionBuilder .addOption(OptionBuilder
.withArgName("item handle") .withArgName("handle to remove")
.hasArg(true) .hasArg(true)
.withDescription( .withDescription(
"remove an Item, Collection or Community from index based on its handle") "remove an Item, Collection or Community from index based on its handle")
.create("r")); .create("r"));
options
.addOption(OptionBuilder
.withArgName("handle to add or update")
.hasArg(true)
.withDescription(
"add or update an Item, Collection or Community based on its handle")
.create("i"));
options options
.addOption(OptionBuilder .addOption(OptionBuilder
@@ -119,6 +135,19 @@ public class IndexClient {
indexer.optimize(); indexer.optimize();
} else if(line.hasOption('s')) { } else if(line.hasOption('s')) {
checkRebuildSpellCheck(line, indexer); checkRebuildSpellCheck(line, indexer);
} else if(line.hasOption('i')) {
final String handle = line.getOptionValue('i');
final DSpaceObject dso = HandleServiceFactory.getInstance().getHandleService().resolveToObject(context, handle);
if (dso == null) {
throw new IllegalArgumentException("Cannot resolve " + handle + " to a DSpace object");
}
log.info("Forcibly Indexing " + handle);
// Enable batch mode; we may be indexing a large number of items
context.enableBatchMode(true);
final long startTimeMillis = System.currentTimeMillis();
final long count = indexAll(indexer, ContentServiceFactory.getInstance().getItemService(), context, dso);
final long seconds = (System.currentTimeMillis() - startTimeMillis ) / 1000;
log.info("Indexed " + count + " DSpace object" + (count > 1 ? "s" : "") + " in " + seconds + " seconds");
} else { } else {
log.info("Updating and Cleaning Index"); log.info("Updating and Cleaning Index");
indexer.cleanIndex(line.hasOption("f")); indexer.cleanIndex(line.hasOption("f"));
@@ -129,6 +158,57 @@ public class IndexClient {
log.info("Done with indexing"); log.info("Done with indexing");
} }
/**
* Indexes the given object and all children, if applicable.
*/
private static long indexAll(final IndexingService indexingService,
final ItemService itemService,
final Context context,
final DSpaceObject dso) throws IOException, SearchServiceException, SQLException {
long count = 0;
indexingService.indexContent(context, dso, true, true);
count++;
if (dso.getType() == Constants.COMMUNITY) {
final Community community = (Community) dso;
final String communityHandle = community.getHandle();
for (final Community subcommunity : community.getSubcommunities()) {
count += indexAll(indexingService, itemService, context, subcommunity);
}
final Community reloadedCommunity = (Community) HandleServiceFactory.getInstance().getHandleService().resolveToObject(context, communityHandle);
for (final Collection collection : reloadedCommunity.getCollections()) {
count++;
indexingService.indexContent(context, collection, true, true);
count += indexItems(indexingService, itemService, context, collection);
}
} else if (dso.getType() == Constants.COLLECTION) {
count += indexItems(indexingService, itemService, context, (Collection) dso);
}
return count;
}
/**
* Indexes all items in the given collection.
*/
private static long indexItems(final IndexingService indexingService,
final ItemService itemService,
final Context context,
final Collection collection) throws IOException, SearchServiceException, SQLException {
long count = 0;
final Iterator<Item> itemIterator = itemService.findByCollection(context, collection);
while (itemIterator.hasNext()) {
Item item = itemIterator.next();
indexingService.indexContent(context, itemIterator.next(), true, false);
count++;
context.uncacheEntity(item);
}
indexingService.commit();
return count;
}
/** /**
* Check the command line options and rebuild the spell check if active. * Check the command line options and rebuild the spell check if active.
* @param line the command line options * @param line the command line options
@@ -141,4 +221,4 @@ public class IndexClient {
indexer.buildSpellCheck(); indexer.buildSpellCheck();
} }
} }
} }

View File

@@ -397,10 +397,7 @@ public class SolrServiceImpl implements SearchService, IndexingService {
{ {
Item item = items.next(); Item item = items.next();
indexContent(context, item, force); indexContent(context, item, force);
if (itemCount++ >= 1000) { context.uncacheEntity(item);
context.clearCache();
itemCount = 0;
}
} }
List<Collection> collections = collectionService.findAll(context); List<Collection> collections = collectionService.findAll(context);