Skip to content

Commit

Permalink
Merge pull request #1533 from libris/bugfix/avoid-nested-select
Browse files Browse the repository at this point in the history
Don't do nested selectBy in merge script
  • Loading branch information
kwahlin authored Dec 3, 2024
2 parents fef5958 + 71c006b commit 48265d6
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions whelktool/src/main/resources/bulk-change-scripts/merge.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ String keepId = keepLink[ID_KEY]
if (!deprecateId || !keepId) return

List<String> obsoleteThingUris = []
Set<String> dependsOnObsolete = []

selectByIds([deprecateId]) { obsolete ->
// Assert that the resource to deprecate is not the same as the one to be kept
Expand All @@ -22,37 +23,42 @@ selectByIds([deprecateId]) { obsolete ->
}

obsoleteThingUris = obsolete.doc.getThingIdentifiers()
dependsOnObsolete = obsolete.getDependers()
}

selectByIds(obsolete.getDependers()) { depender ->
if (depender.doc.getThingType() == JOB_TYPE) {
return
}
selectByIds(dependsOnObsolete) { depender ->
if (depender.doc.getThingType() == JOB_TYPE) {
return
}

List<List> modifiedListPaths = []
def modified = DocumentUtil.traverse(depender.graph) { value, path ->
// TODO: What if there are links to a record uri?
if (path && path.last() == ID_KEY && obsoleteThingUris.contains(value)) {
path.dropRight(1).with {
if (it.last() instanceof Integer) {
modifiedListPaths.add(it.dropRight(1))
}
List<List> modifiedListPaths = []
def modified = DocumentUtil.traverse(depender.graph) { value, path ->
// TODO: What if there are links to a record uri?
if (path && path.last() == ID_KEY && obsoleteThingUris.contains(value)) {
path.dropRight(1).with {
if (it.last() instanceof Integer) {
modifiedListPaths.add(it.dropRight(1))
}
return new DocumentUtil.Replace(keepId)
}
}
// Remove duplicates
modifiedListPaths.each {
var obj = DocumentUtil.getAtPath(depender.graph, it)
if (obj instanceof List) {
obj.unique(true)
}
return new DocumentUtil.Replace(keepId)
}
if (modified) {
depender.scheduleSave(loud: isLoudAllowed)
}
// Remove duplicates
modifiedListPaths.each {
var obj = DocumentUtil.getAtPath(depender.graph, it)
if (obj instanceof List) {
obj.unique(true)
}
}
if (modified) {
depender.scheduleSave(loud: isLoudAllowed)
}
}

obsolete.scheduleDelete()
selectByIds([deprecateId]) { obsolete ->
if (obsolete.doc.getThingIdentifiers().first() != keepId) {
obsolete.scheduleDelete()
}
}

selectByIds([keepId]) { kept ->
Expand Down

0 comments on commit 48265d6

Please sign in to comment.