Skip to content

Commit

Permalink
cli speed improvement: periodically uncache entities when processing …
Browse files Browse the repository at this point in the history
…many
  • Loading branch information
MarieVerdonck committed Jan 29, 2025
1 parent 5a43e6b commit e8a54e6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dspace-api/src/main/java/org/dspace/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,4 +951,16 @@ public void setAuthenticationMethod(final String authenticationMethod) {
public boolean isContextUserSwitched() {
return currentUserPreviousState != null;
}

/**
* Remove all entities from the cache and reload the current user entity. This is useful when batch processing
* a large number of entities when the calling code requires the cache to be completely cleared before continuing.
*
* @throws SQLException if a database error occurs.
*/
public void uncacheEntities() throws SQLException {
dbConnection.uncacheEntities();
reloadContextBoundEntities();
}

}
12 changes: 12 additions & 0 deletions dspace-api/src/main/java/org/dspace/core/DBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,16 @@ public interface DBConnection<T> {
* @throws java.sql.SQLException passed through.
*/
public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLException;

/**
* Remove all entities from the session cache.
*
* <p>Entities removed from cache are not saved in any way. Therefore, if you
* have modified any entities, you should be sure to {@link #commit()} changes
* before calling this method.
*
* @throws SQLException passed through.
*/
public void uncacheEntities() throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,10 @@ public <E extends ReloadableEntity> void uncacheEntity(E entity) throws SQLExcep
}
}
}

@Override
public void uncacheEntities() throws SQLException {
getSession().clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.dspace.discovery.indexobject.IndexableItem;
import org.dspace.discovery.indexobject.factory.IndexFactory;
import org.dspace.discovery.indexobject.factory.IndexObjectFactoryFactory;
import org.dspace.discovery.indexobject.factory.ItemIndexFactory;
import org.dspace.eperson.Group;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.GroupService;
Expand Down Expand Up @@ -341,13 +342,18 @@ public void updateIndex(Context context, boolean force, String type) {
try {
final List<IndexFactory> indexableObjectServices = indexObjectServiceFactory.
getIndexFactories();
int indexObject = 0;
for (IndexFactory indexableObjectService : indexableObjectServices) {
if (type == null || StringUtils.equals(indexableObjectService.getType(), type)) {
final Iterator<IndexableObject> indexableObjects = indexableObjectService.findAll(context);
while (indexableObjects.hasNext()) {
final IndexableObject indexableObject = indexableObjects.next();
indexContent(context, indexableObject, force);
context.uncacheEntity(indexableObject.getIndexedObject());
indexObject++;
if ((indexObject % 100) == 0 && indexableObjectService instanceof ItemIndexFactory) {
context.uncacheEntities();
}
}
}
}
Expand Down

0 comments on commit e8a54e6

Please sign in to comment.