Skip to content

Commit

Permalink
refactor: Remove unused user argument from reloading ontology cache c…
Browse files Browse the repository at this point in the history
…ommand (#3447)
  • Loading branch information
BalduinLandolt authored Jan 7, 2025
1 parent a8b4bab commit 54033bd
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.typesafe.scalalogging.LazyLogging
import zio.*

import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
import org.knora.webapi.store.triplestore.api.TriplestoreService

Expand All @@ -32,7 +31,7 @@ trait TestStartupUtils extends LazyLogging {
tss <- ZIO.service[TriplestoreService]
_ <- tss.resetTripleStoreContent(rdfDataObjects).timeout(480.seconds)
_ <- ZIO.logInfo("... loading test data done.")
_ <- ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)).orDie
_ <- ZIO.serviceWithZIO[OntologyCache](_.loadOntologies()).orDie
} yield ()

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import zio.ZIO

import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
Expand Down Expand Up @@ -42,7 +41,7 @@ class LoadOntologiesSpec extends CoreSpec with ImplicitSender {
)

UnsafeZioRun
.run(ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)))
.run(ZIO.serviceWithZIO[OntologyCache](_.loadOntologies()))
.toEither
.map(_ => SuccessResponseV2("OK"))
.left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.knora.webapi.messages.OntologyConstants.Rdfs
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.store.triplestoremessages.*
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.messages.util.rdf.SparqlSelectResult
import org.knora.webapi.messages.v2.responder.CanDoResponseV2
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
Expand Down Expand Up @@ -452,9 +451,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {
assert(!cachedMetadataResponse.ontologies.exists(_.ontologyIri == fooIri.get.toSmartIri))

// Reload the ontologies from the triplestore and check again.
UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)),
)
UnsafeZioRun.runOrThrow(ZIO.serviceWithZIO[OntologyCache](_.loadOntologies()))

appActor ! OntologyMetadataGetByProjectRequestV2()

Expand Down Expand Up @@ -795,7 +792,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

// Reload the ontology cache and see if we get the same result.
UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)),
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies()),
)

appActor ! PropertiesGetRequestV2(
Expand Down Expand Up @@ -899,7 +896,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

// Reload the ontology cache and see if we get the same result.
UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)),
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies()),
)

appActor ! PropertiesGetRequestV2(
Expand Down Expand Up @@ -3738,7 +3735,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

// Reload the ontology cache and see if we get the same result.
UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)),
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies()),
)

appActor ! linkPropGetRequest
Expand Down
5 changes: 1 addition & 4 deletions webapi/src/main/scala/org/knora/webapi/core/AppServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import zio.*

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.domain.AppState
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.store.triplestore.api.TriplestoreService
Expand Down Expand Up @@ -66,9 +65,7 @@ final case class AppServer(
private def populateOntologyCaches(requiresRepository: Boolean): Task[Unit] =
for {
_ <- state.set(AppState.LoadingOntologies)
_ <- ontologyCache
.loadOntologies(KnoraSystemInstances.Users.SystemUser)
.when(requiresRepository)
_ <- ontologyCache.loadOntologies().when(requiresRepository)
_ <- state.set(AppState.OntologiesReady)
} yield ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import zio.*
import dsp.errors.ForbiddenException
import org.knora.webapi.config.AppConfig
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.KnoraSystemInstances.Users.SystemUser
import org.knora.webapi.slice.admin.api.MessageResponse
import org.knora.webapi.slice.infrastructure.CacheManager
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
Expand Down Expand Up @@ -42,7 +41,7 @@ final case class StoreRestService(
}
_ <- ZIO.logWarning(s"Resetting triplestore content with ${rdfDataObjects.map(_.name).mkString(", ")}")
_ <- triplestoreService.resetTripleStoreContent(rdfDataObjects, prependDefaults).logError
_ <- ontologyCache.loadOntologies(SystemUser).logError
_ <- ontologyCache.loadOntologies().logError
_ = cacheManager.clearAll()
} yield MessageResponse("success")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.twirl.queries.sparql
import org.knora.webapi.messages.util.ErrorHandlingMap
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.messages.util.OntologyUtil
import org.knora.webapi.messages.v2.responder.ontologymessages.*
import org.knora.webapi.messages.v2.responder.ontologymessages.OwlCardinality.*
import org.knora.webapi.responders.v2.ontology.OntologyHelpers
import org.knora.webapi.responders.v2.ontology.OntologyHelpers.OntologyGraph
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.admin.domain.service.KnoraProjectRepo
import org.knora.webapi.slice.ontology.repo.model.OntologyCacheData
import org.knora.webapi.store.triplestore.api.TriplestoreService
Expand Down Expand Up @@ -419,11 +417,8 @@ trait OntologyCache {

/**
* Loads and caches all ontology information.
*
* @param requestingUser the user making the request.
* @return [[Unit]]
*/
def loadOntologies(requestingUser: User): Task[Unit]
def loadOntologies(): Task[Unit]

/**
* Gets the ontology data from the cache.
Expand Down Expand Up @@ -475,12 +470,6 @@ trait OntologyCache {
updatedClassIri: SmartIri,
): Task[OntologyCacheData]

/**
* Loads and caches all ontology information.
*
* @return [[Unit]]
*/
final def loadOntologies(): Task[Unit] = loadOntologies(KnoraSystemInstances.Users.SystemUser)
}

final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef: Ref[OntologyCacheData])(implicit
Expand All @@ -490,19 +479,9 @@ final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef

/**
* Loads and caches all ontology information.
*
* @param requestingUser the user making the request.
* @return [[Unit]]
*/
override def loadOntologies(requestingUser: User): Task[Unit] =
override def loadOntologies(): Task[Unit] =
for {
_ <-
ZIO
.fail(ForbiddenException(s"Only a system administrator can reload ontologies"))
.when(
!(requestingUser.id == KnoraSystemInstances.Users.SystemUser.id || requestingUser.permissions.isSystemAdmin),
)

// Get all ontology metadata.
_ <- ZIO.logInfo(s"Loading ontologies into cache")
allOntologyMetadataResponse <- triplestore.query(Select(sparql.v2.txt.getAllOntologyMetadata()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import zio.ZLayer

import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.v2.responder.ontologymessages.ReadOntologyV2
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.ontology.repo.model.OntologyCacheData

case class OntologyCacheFake(ref: Ref[OntologyCacheData]) extends OntologyCache {
Expand All @@ -23,11 +22,8 @@ case class OntologyCacheFake(ref: Ref[OntologyCacheData]) extends OntologyCache

/**
* Loads and caches all ontology information.
*
* @param requestingUser the user making the request.
* @return a [[Unit]].
*/
override def loadOntologies(requestingUser: User): Task[Unit] =
override def loadOntologies(): Task[Unit] =
throw new UnsupportedOperationException("Not possible in tests. Provide the respective test data as Ref.")

/**
Expand Down

0 comments on commit 54033bd

Please sign in to comment.