Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lxl 4610 cleanup #1567

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions whelk-core/src/main/groovy/whelk/Whelk.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import whelk.component.PostgreSQLComponent.UpdateAgent
import whelk.component.SparqlQueryClient
import whelk.component.SparqlUpdater
import whelk.converter.marc.MarcFrameConverter
import whelk.exception.LinkValidationException
import whelk.exception.StorageCreateFailedException
import whelk.filter.LanguageLinker
import whelk.exception.WhelkException
Expand All @@ -27,7 +28,6 @@ import java.time.Instant
import java.time.ZoneId

import static whelk.FeatureFlags.Flag.INDEX_BLANK_WORKS
import static whelk.exception.LinkValidationException.IncomingLinksException

/**
* The Whelk is the root component of the XL system.
Expand Down Expand Up @@ -574,10 +574,16 @@ class Whelk {
}

private void assertNoDependers(Document doc) {
boolean isDependedUpon = storage.getIncomingLinkCountByIdAndRelation(doc.getShortId())
.any { relation, _ -> !JsonLd.isWeak(relation) }
if (isDependedUpon) {
throw new IncomingLinksException("Record is referenced by other records")
Set<String> dependingRelations = storage.getIncomingLinkCountByIdAndRelation(doc.getShortId()).keySet()
.findAll { !JsonLd.isWeak(it) }
if (!dependingRelations.isEmpty()) {
Set<String> allDependers = dependingRelations.collect { storage.getDependersOfType(doc.getShortId(), it) }
.flatten()
.toSet() as Set<String>
String example = allDependers.first()
int numDependers = allDependers.size()
String msg = "Record is referenced by $example${numDependers > 1 ? " and ${numDependers - 1} other records" : "" }."
throw new LinkValidationException(msg)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ import java.util.regex.Pattern

import static groovy.transform.TypeCheckingMode.SKIP
import static java.sql.Types.OTHER
import static whelk.JsonLd.Owl.SAME_AS
import static whelk.util.Jackson.mapper
import static whelk.exception.LinkValidationException.OutgoingLinksException

/**
* It is important to not grab more than one connection per request/thread to avoid connection related deadlocks.
Expand Down Expand Up @@ -458,12 +456,6 @@ class PostgreSQLComponent {
private static final String DELETE_USER_DATA =
"DELETE FROM lddb__user_data WHERE id = ?"

private static final String GET_IRI_IS_DELETED = """
SELECT lddb.deleted
FROM lddb__identifiers
JOIN lddb ON lddb__identifiers.id = lddb.id WHERE lddb__identifiers.iri = ?
""".stripIndent()

private static final String GET_ALL_LIBRARIES_HOLDING_ID = """
SELECT l.data#>>'{@graph,1,heldBy,@id}' FROM lddb__dependencies d
LEFT JOIN lddb l ON d.id = l.id
Expand Down Expand Up @@ -776,8 +768,6 @@ class PostgreSQLComponent {
throw new ConflictingHoldException("Already exists a holding record for ${heldBy} and bib: $holdingFor")
}

assertNoLinksToDeleted(doc.getExternalRefs())

//FIXME: throw exception on null changedBy
if (changedBy != null) {
String creator = getDescriptionChangerId(changedBy)
Expand Down Expand Up @@ -996,11 +986,6 @@ class PostgreSQLComponent {

boolean deleted = doc.getDeleted()

if (!deleted) {
var addedLinks = doc.getExternalRefs() - preUpdateDoc.getExternalRefs()
assertNoLinksToDeleted(addedLinks)
}

if (collection == "hold") {
checkLinkedShelfMarkOwnership(doc, connection)

Expand Down Expand Up @@ -2081,26 +2066,6 @@ class PostgreSQLComponent {
}
}

boolean isDeleted(String iri) {
withDbConnection {
PreparedStatement preparedStatement = null
ResultSet rs = null
try {
preparedStatement = getMyConnection().prepareStatement(GET_IRI_IS_DELETED)
preparedStatement.setString(1, iri)
rs = preparedStatement.executeQuery()

if (rs.next())
return rs.getBoolean(1) // deleted
// not in lddb
return false
}
finally {
close(rs, preparedStatement)
}
}
}

String getThingMainIriBySystemId(String id) {
return withDbConnection {
Connection connection = getMyConnection()
Expand Down Expand Up @@ -2955,15 +2920,6 @@ class PostgreSQLComponent {
}
}

private void assertNoLinksToDeleted(Set<Link> links) {
links.each {link ->
// sameAs is allowed because when merging two entities, the id of the deleted entity is added to sameAs of the remaining entity
if (link.property() != SAME_AS && isDeleted(link.iri)) {
throw new OutgoingLinksException("Document contains link to deleted resource $link.iri at path $link.relation")
}
}
}

class NotificationListener extends Thread {
private static final String NAME = 'pg_listener'
private static final Counter counter = Counter.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,4 @@ public class LinkValidationException extends Exception {
public LinkValidationException(String msg) {
super(msg);
}

public static class IncomingLinksException extends LinkValidationException {
public IncomingLinksException(String msg) {
super(msg);
}
}

public static class OutgoingLinksException extends LinkValidationException {
public OutgoingLinksException(String msg) {
super(msg);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import whelk.Document
import whelk.datatool.form.MatchForm
import whelk.exception.LinkValidationException

import static whelk.exception.LinkValidationException.IncomingLinksException
import static whelk.JsonLd.RECORD_KEY
import static whelk.datatool.bulkchange.BulkJobDocument.MATCH_FORM_KEY

Expand All @@ -14,7 +14,7 @@ MatchForm mf = new MatchForm(matchForm, getWhelk())
selectByForm(mf) {
if(mf.matches(getFramedThing(it.doc))) {
it.scheduleDelete(loud: isLoudAllowed, onError: { e ->
if (e instanceof IncomingLinksException) {
if (e instanceof LinkValidationException) {
failed.println("Failed to delete $it.doc.shortId: ${e.getMessage()}")
} else {
throw e
Expand Down