Skip to content

Commit

Permalink
Authority virtual metadata: improve init
Browse files Browse the repository at this point in the history
Remove spring init for a 'first lookup' init
  • Loading branch information
kshepherd committed Sep 30, 2024
1 parent 1234754 commit 96c8b99
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,29 @@ public class AuthorityVirtualMetadataServiceImpl implements AuthorityVirtualMeta
@Autowired
protected MetadataAuthorityService metadataAuthorityService;

/**
* Map of virtual field names that are configured for authority control and exist in metadata registry
*/
private HashMap<String, MetadataField> validVirtualFieldNames;
/**
* Map of authority virtual metadata configurations, see virtual-metadata.xml
*/
private Map<String, HashMap<String, AuthorityVirtualMetadataConfiguration>> authorityVirtualMaps;

private static final Logger log = LogManager.getLogger();

public void init() {
validVirtualFieldNames = new HashMap<>();
authorityVirtualMaps = new HashMap<>();
Context context = null;
// Obtain new context and initialise lists and maps
try {
context = new Context(Context.Mode.READ_ONLY);
/**
* Initialize hashmaps for field / configuration lookups. This is only executed the first time a lookup
* is attempted, to keep database load down for data retrieval that typically will not change between
* service restarts.
*/
public void initMaps() throws SQLException {
if (validVirtualFieldNames == null && authorityVirtualMaps == null) {
validVirtualFieldNames = new HashMap<>();
authorityVirtualMaps = new HashMap<>();
Context context = new Context(Context.Mode.READ_ONLY);
// Get field maps configured in virtual metadata spring configuration
authorityVirtualMaps = authorityVirtualMetadataPopulator.getMap();
authorityVirtualMaps = authorityVirtualMetadataPopulator.getMap();
// Iterate map of maps, just to check each virtual field name (key of 2nd-level map) exists
// and populate the virtual field names list so we can look it up later without further database calls
for (String configuredAuthorityField : authorityVirtualMaps.keySet()) {
Expand All @@ -71,13 +80,6 @@ public void init() {
}
}
}
} catch (SQLException e) {
log.error("Could not obtain context" + e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (context != null) {
context.close();
}
}
}

Expand All @@ -93,6 +95,16 @@ public void init() {
*/
public List<MetadataValue> getAuthorityVirtualMetadata(Item item, List<MetadataValue> dbMetadataValues) {
List<MetadataValue> authorityMetadataValues = new LinkedList<>();
// If maps are not initialized, do it now
if (authorityVirtualMaps == null || validVirtualFieldNames == null) {
try {
initMaps();
} catch (SQLException e) {
log.error("Error initializing authority virtual metadata configuration", e);
return authorityMetadataValues;
}
}

// Make a map of counts - we want to calc a real 'place'
Map<String, Integer> fieldCounts = new HashMap<>();
for (MetadataValue mv : dbMetadataValues) {
Expand Down
2 changes: 1 addition & 1 deletion dspace/config/spring/api/core-services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<bean class="org.dspace.content.EntityServiceImpl"/>
<bean class="org.dspace.content.RelationshipTypeServiceImpl"/>
<bean class="org.dspace.content.RelationshipMetadataServiceImpl"/>
<bean class="org.dspace.content.AuthorityVirtualMetadataServiceImpl" init-method="init"/>
<bean class="org.dspace.content.AuthorityVirtualMetadataServiceImpl"/>
<bean class="org.dspace.content.FeedbackServiceImpl"/>
<bean class="org.dspace.content.DuplicateDetectionServiceImpl"/>

Expand Down

0 comments on commit 96c8b99

Please sign in to comment.