Skip to content

Commit

Permalink
Optimize Viewcache
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Sep 16, 2024
1 parent 1f6a523 commit 292c903
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ private DefaultTreeNode buildStructureTree() {
addParentLinksRecursive(dataEditor.getProcess(), invisibleRootNode);
List<Integer> processIds = getAllLinkedProcessIds(structure);
Map<Integer, String> processTypeMap = processIds.isEmpty() ? Collections.emptyMap() : fetchProcessTypes(processIds);
buildStructureTreeRecursively(structure, invisibleRootNode, processTypeMap);
Map<String, StructuralElementViewInterface> viewCache = new HashMap<>();
buildStructureTreeRecursively(structure, invisibleRootNode, processTypeMap, viewCache);
return invisibleRootNode;
}

Expand Down Expand Up @@ -603,18 +604,20 @@ private String buildPageRangeFromLogicalDivision(LogicalDivision structure) {
* @param structure the logical division
* @return the StructureTreeNode instance
*/
private StructureTreeNode buildStructureTreeNode(LogicalDivision structure, Map<Integer, String> idTypeMap) {
private StructureTreeNode buildStructureTreeNode(LogicalDivision structure, Map<Integer, String> idTypeMap,
Map<String, StructuralElementViewInterface> viewCache) {
StructureTreeNode node;
if (Objects.isNull(structure.getLink())) {
StructuralElementViewInterface divisionView = dataEditor.getRulesetManagement().getStructuralElementView(
structure.getType(), dataEditor.getAcquisitionStage(), dataEditor.getPriorityList());
StructuralElementViewInterface divisionView = viewCache.computeIfAbsent(structure.getType(), key ->
dataEditor.getRulesetManagement().getStructuralElementView(
key, dataEditor.getAcquisitionStage(), dataEditor.getPriorityList())
);
String label = divisionView.getLabel();
String pageRange = buildPageRangeFromLogicalDivision(structure);
boolean undefined = divisionView.isUndefined() && Objects.nonNull(structure.getType());
node = new StructureTreeNode(label, pageRange, undefined, false, structure);
} else {
node = new StructureTreeNode(structure.getLink().getUri().toString(), null, true, true, structure);
Map<String, StructuralElementViewInterface> viewCache = new HashMap<>();
for (Process child : dataEditor.getCurrentChildren()) {
if (child.getId() == ServiceManager.getProcessService().processIdFromUri(structure.getLink().getUri())) {
String type = idTypeMap.get(child.getId());
Expand All @@ -640,8 +643,8 @@ private StructureTreeNode buildStructureTreeNode(LogicalDivision structure, Map
* @return a collection of views that contains all views of the full sub-tree
*/
private Collection<View> buildStructureTreeRecursively(LogicalDivision structure, TreeNode result, Map<Integer,
String> processTypeMap) {
StructureTreeNode node = buildStructureTreeNode(structure, processTypeMap);
String> processTypeMap, Map<String, StructuralElementViewInterface> viewCache) {
StructureTreeNode node = buildStructureTreeNode(structure, processTypeMap, viewCache);
/*
* Creating the tree node by handing over the parent node automatically
* appends it to the parent as a child. That’s the logic of the JSF
Expand All @@ -655,15 +658,16 @@ private Collection<View> buildStructureTreeRecursively(LogicalDivision structure
Set<View> viewsShowingOnAChild = new HashSet<>();
if (!this.logicalStructureTreeContainsMedia()) {
for (LogicalDivision child : structure.getChildren()) {
viewsShowingOnAChild.addAll(buildStructureTreeRecursively(child, parent, processTypeMap));
viewsShowingOnAChild.addAll(buildStructureTreeRecursively(child, parent, processTypeMap, viewCache));
}
} else {
// iterate through children and views ordered by the ORDER attribute
List<Pair<View, LogicalDivision>> merged = mergeLogicalStructureViewsAndChildren(structure);
for (Pair<View, LogicalDivision> pair : merged) {
if (Objects.nonNull(pair.getRight())) {
// add child and their views
viewsShowingOnAChild.addAll(buildStructureTreeRecursively(pair.getRight(), parent, processTypeMap));
viewsShowingOnAChild.addAll(buildStructureTreeRecursively(pair.getRight(), parent,
processTypeMap, viewCache));
} else if (!viewsShowingOnAChild.contains(pair.getLeft())) {
// add views of current logical division as leaf nodes
DefaultTreeNode viewNode = addTreeNode(buildViewLabel(pair.getLeft()), false, false, pair.getLeft(), parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.junit.jupiter.api.Test;
import org.kitodo.DummyRulesetManagement;
import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface;
import org.kitodo.api.dataformat.LogicalDivision;
import org.kitodo.api.dataformat.mets.LinkedMetsResource;
import org.kitodo.data.database.beans.Process;
Expand Down Expand Up @@ -51,13 +52,14 @@ public void testBuildStructureTreeRecursively() throws Exception {
link.setUri(URI.create("database://?process.id=42"));
structure.setLink(link);
Map<Integer, String> processTypeMap = new HashMap<>();
Map<String, StructuralElementViewInterface> viewCache = new HashMap<>();
processTypeMap.put(ServiceManager.getProcessService().processIdFromUri(link.getUri()), "Monograph");
TreeNode result = new DefaultTreeNode();

Method buildStructureTreeRecursively = StructurePanel.class.getDeclaredMethod("buildStructureTreeRecursively",
LogicalDivision.class, TreeNode.class, Map.class);
LogicalDivision.class, TreeNode.class, Map.class, Map.class);
buildStructureTreeRecursively.setAccessible(true);
buildStructureTreeRecursively.invoke(underTest, structure, result, processTypeMap);
buildStructureTreeRecursively.invoke(underTest, structure, result, processTypeMap, viewCache);

assertTrue(((StructureTreeNode) result.getChildren().get(0).getData()).isLinked());
}
Expand Down

0 comments on commit 292c903

Please sign in to comment.