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

fix(cache): cache does not update correctly when an ontology is modified (DEV-939) #2068

Merged
merged 14 commits into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,6 +20,7 @@ import org.knora.webapi.messages.store.triplestoremessages.SmartIriLiteralV2
import org.knora.webapi.messages.store.triplestoremessages.SparqlUpdateRequest
import org.knora.webapi.messages.store.triplestoremessages.SparqlUpdateResponse
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2
import scala.concurrent.duration._
import org.knora.webapi.messages.util.ErrorHandlingMap
import org.knora.webapi.messages.util.ResponderData
import org.knora.webapi.messages.v2.responder.CanDoResponseV2
Expand All @@ -37,6 +38,10 @@ import org.knora.webapi.util._

import java.time.Instant
import scala.concurrent.Future
import scala.concurrent.Await
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.feature.TestFeatureFactoryConfig
import org.knora.webapi.feature.KnoraSettingsFeatureFactoryConfig

/**
* Responds to requests dealing with ontologies.
Expand Down Expand Up @@ -585,13 +590,10 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon

// Update the ontology cache with the unescaped metadata.

_ =
Cache.storeCacheData(
cacheData.copy(
ontologies =
cacheData.ontologies + (internalOntologyIri -> ReadOntologyV2(ontologyMetadata = unescapedNewMetadata))
)
)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(
internalOntologyIri,
ReadOntologyV2(ontologyMetadata = unescapedNewMetadata)
)

} yield ReadOntologyMetadataV2(ontologies = Set(unescapedNewMetadata))

Expand Down Expand Up @@ -741,14 +743,13 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
}

// Update the ontology cache with the unescaped metadata.

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> cacheData
.ontologies(internalOntologyIri)
.copy(ontologyMetadata = unescapedNewMetadata))
)
)
updatedOntology = cacheData
.ontologies(internalOntologyIri)
.copy(ontologyMetadata = unescapedNewMetadata)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(
internalOntologyIri,
updatedOntology
)

} yield ReadOntologyMetadataV2(ontologies = Set(unescapedNewMetadata))

Expand Down Expand Up @@ -840,13 +841,10 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon

// Update the ontology cache with the unescaped metadata.

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> cacheData
.ontologies(internalOntologyIri)
.copy(ontologyMetadata = unescapedNewMetadata))
)
)
updatedOntology = cacheData
.ontologies(internalOntologyIri)
.copy(ontologyMetadata = unescapedNewMetadata)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(internalOntologyIri, updatedOntology)

} yield ReadOntologyMetadataV2(ontologies = Set(unescapedNewMetadata))

Expand Down Expand Up @@ -1036,8 +1034,10 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon

// Update the cache.

updatedSubClassOfRelations = cacheData.subClassOfRelations + (internalClassIri -> allBaseClassIris)
updatedSuperClassOfRelations = OntologyHelpers.calculateSuperClassOfRelations(updatedSubClassOfRelations)
updatedSubClassOfRelations = cacheData.subClassOfRelations + (internalClassIri -> allBaseClassIris)
updatedSuperClassOfRelations = OntologyHelpers.calculateSuperClassOfRelations(updatedSubClassOfRelations)
updatedClassDefinedInOntology = cacheData.classDefinedInOntology + (internalClassIri -> internalOntologyIri)
updatedEntityDefinedInOntology = cacheData.propertyDefinedInOntology ++ updatedClassDefinedInOntology

updatedOntology = ontology.copy(
ontologyMetadata = ontology.ontologyMetadata.copy(
Expand All @@ -1046,13 +1046,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
classes = ontology.classes + (internalClassIri -> readClassInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology),
subClassOfRelations = updatedSubClassOfRelations,
superClassOfRelations = updatedSuperClassOfRelations
)
)
_ <- Cache.cacheUpdatedOntologyWithClass(internalOntologyIri, updatedOntology, internalClassIri)

// Read the data back from the cache.

Expand Down Expand Up @@ -1234,14 +1228,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon

// Update subclasses and write the cache.

_ = Cache.storeCacheData(
Cache.updateSubClasses(
baseClassIri = internalClassIri,
cacheData = cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
)
_ <- Cache.cacheUpdatedOntologyWithClass(internalOntologyIri, updatedOntology, internalClassIri)

// Read the data back from the cache.

Expand Down Expand Up @@ -1481,14 +1468,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
classes = ontology.classes + (internalClassIri -> readClassInfo)
)

_ = Cache.storeCacheData(
Cache.updateSubClasses(
baseClassIri = internalClassIri,
cacheData = cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
)
_ <- Cache.cacheUpdatedOntologyWithClass(internalOntologyIri, updatedOntology, internalClassIri)

// Read the data back from the cache.

Expand Down Expand Up @@ -1739,11 +1719,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
classes = ontology.classes + (internalClassIri -> readClassInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
_ <- Cache.cacheUpdatedOntologyWithClass(internalOntologyIri, updatedOntology, internalClassIri)

// Read the data back from the cache.

Expand Down Expand Up @@ -1964,20 +1940,8 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
classes = ontology.classes - internalClassIri
)

updatedSubClassOfRelations =
(cacheData.subClassOfRelations - internalClassIri).map { case (subClass, baseClasses) =>
subClass -> (baseClasses.toSet - internalClassIri).toSeq
}

updatedSuperClassOfRelations = OntologyHelpers.calculateSuperClassOfRelations(updatedSubClassOfRelations)
_ <- Cache.cacheUpdatedOntologyWithClass(internalOntologyIri, updatedOntology, internalClassIri)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology),
subClassOfRelations = updatedSubClassOfRelations,
superClassOfRelations = updatedSuperClassOfRelations
)
)
} yield ReadOntologyMetadataV2(Set(updatedOntology.ontologyMetadata))

for {
Expand Down Expand Up @@ -2141,24 +2105,16 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon

propertiesToRemoveFromCache = Set(internalPropertyIri) ++ maybeInternalLinkValuePropertyIri

updatedOntology = ontology.copy(
ontologyMetadata = ontology.ontologyMetadata.copy(
lastModificationDate = Some(currentTime)
),
properties = ontology.properties -- propertiesToRemoveFromCache
)
updatedOntology =
ontology.copy(
ontologyMetadata = ontology.ontologyMetadata.copy(
lastModificationDate = Some(currentTime)
),
properties = ontology.properties -- propertiesToRemoveFromCache
)

updatedSubPropertyOfRelations =
(cacheData.subPropertyOfRelations -- propertiesToRemoveFromCache).map { case (subProperty, baseProperties) =>
subProperty -> (baseProperties -- propertiesToRemoveFromCache)
}
_ <- Cache.cacheUpdatedOntology(internalOntologyIri, updatedOntology)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology),
subPropertyOfRelations = updatedSubPropertyOfRelations
)
)
} yield ReadOntologyMetadataV2(Set(updatedOntology.ontologyMetadata))

for {
Expand Down Expand Up @@ -2274,35 +2230,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
}

// Remove the ontology from the cache.

updatedSubClassOfRelations = cacheData.subClassOfRelations.filterNot { case (subClass, _) =>
subClass.getOntologyFromEntity == internalOntologyIri
}.map { case (subClass, baseClasses) =>
subClass -> baseClasses.filterNot(_.getOntologyFromEntity == internalOntologyIri)
}

updatedSuperClassOfRelations = OntologyHelpers.calculateSuperClassOfRelations(updatedSubClassOfRelations)

updatedSubPropertyOfRelations = cacheData.subPropertyOfRelations.filterNot { case (subProperty, _) =>
subProperty.getOntologyFromEntity == internalOntologyIri
}.map { case (subProperty, baseProperties) =>
subProperty -> baseProperties.filterNot(
_.getOntologyFromEntity == internalOntologyIri
)
}

updatedStandoffProperties =
cacheData.standoffProperties.filterNot(_.getOntologyFromEntity == internalOntologyIri)

updatedCacheData = cacheData.copy(
ontologies = cacheData.ontologies - internalOntologyIri,
subClassOfRelations = updatedSubClassOfRelations,
superClassOfRelations = updatedSuperClassOfRelations,
subPropertyOfRelations = updatedSubPropertyOfRelations,
standoffProperties = updatedStandoffProperties
)

_ = Cache.storeCacheData(updatedCacheData)
_ <- Cache.deleteOntology(internalOntologyIri)
} yield SuccessResponseV2(s"Ontology ${internalOntologyIri.toOntologySchema(ApiV2Complex)} has been deleted")

for {
Expand Down Expand Up @@ -2644,28 +2572,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
ontology.properties ++ maybeLinkValuePropertyCacheEntry + (internalPropertyIri -> readPropertyInfo)
)

// if a link value property was created, add its subproperty relation to the ontology's subPropertyOfRelations map
// note: this is only needed for the special case of link properties that are subproperties of custom link properties

maybeSubPropertyOfRelationsForLinkValueProperty: Option[(SmartIri, Set[SmartIri])] =
maybeLinkValuePropertyCacheEntry.map { case (smartIri: SmartIri, readPropertyInfoV2: ReadPropertyInfoV2) =>
smartIri -> readPropertyInfoV2.entityInfoContent.subPropertyOf
}

newSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]] =
maybeSubPropertyOfRelationsForLinkValueProperty match {
case Some(smartIriSetOfSmartIris: (SmartIri, Set[SmartIri])) =>
Map(internalPropertyIri -> allKnoraSuperPropertyIris, smartIriSetOfSmartIris)
case None =>
Map(internalPropertyIri -> allKnoraSuperPropertyIris)
}

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology),
subPropertyOfRelations = cacheData.subPropertyOfRelations ++ newSubPropertyOfRelations
)
)
_ <- Cache.cacheUpdatedOntology(internalOntologyIri, updatedOntology)

// Read the data back from the cache.
response <- getPropertyDefinitionsFromOntologyV2(
Expand Down Expand Up @@ -2888,11 +2795,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
ontology.properties ++ maybeLinkValuePropertyCacheEntry + (internalPropertyIri -> newReadPropertyInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
_ <- Cache.cacheUpdatedOntology(internalOntologyIri, updatedOntology)

// Read the data back from the cache.

Expand Down Expand Up @@ -3096,11 +2999,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
ontology.properties ++ maybeLinkValuePropertyCacheEntry + (internalPropertyIri -> newReadPropertyInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(internalOntologyIri, updatedOntology)

// Read the data back from the cache.

Expand Down Expand Up @@ -3238,11 +3137,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
classes = ontology.classes + (internalClassIri -> newReadClassInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(internalOntologyIri, updatedOntology)

// Read the data back from the cache.

Expand Down Expand Up @@ -3436,11 +3331,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
ontology.properties ++ maybeLinkValuePropertyCacheEntry + (internalPropertyIri -> newReadPropertyInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(internalOntologyIri, updatedOntology)

// Read the data back from the cache.

Expand Down Expand Up @@ -3595,11 +3486,7 @@ class OntologyResponderV2(responderData: ResponderData) extends Responder(respon
classes = ontology.classes + (internalClassIri -> newReadClassInfo)
)

_ = Cache.storeCacheData(
cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
_ <- Cache.cacheUpdatedOntologyWithoutUpdatingMaps(internalOntologyIri, updatedOntology)

// Read the data back from the cache.

Expand Down