Skip to content

Commit

Permalink
fix TriplestoreServiceInMemory, simplify loadRdfObject along with it
Browse files Browse the repository at this point in the history
  • Loading branch information
siers authored and seakayone committed Feb 13, 2024
1 parent 385b46a commit b9b2246
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,13 @@ case class TriplestoreServiceLive(
uriBuilder
}

inputEntity <- loadRdfObject(elem.path)
rdfContents <- loadRdfObject(elem.path)

httpPost <-
ZIO.attemptBlocking {
val httpPost = new HttpPost(uriBuilder.build())
httpPost.setEntity(inputEntity)
val httpPost = new HttpPost(uriBuilder.build())
val turtleContentType = ContentType.create(mimeTypeTextTurtle, "UTF-8")
httpPost.setEntity(new StringEntity(rdfContents, turtleContentType))
httpPost
}
responseHandler <- ZIO.attempt(returnInsertGraphDataResponse(graphName)(_))
Expand All @@ -278,23 +279,9 @@ case class TriplestoreServiceLive(
/**
* Load the RdfDataObject into some AbstractHttpEntity from either a relative ../$path or through a resource.
*/
private def loadRdfObject(path: String): Task[AbstractHttpEntity] = {
val turtleContentType = ContentType.create(mimeTypeTextTurtle, "UTF-8")

val relativeFileEntity = {
// Add the input file to the body of the request.
// here we need to tweak the base directory path from "webapi"
// to the parent folder where the files can be found
val inputFile = Paths.get("..", path)
ZIO.attemptBlocking {
if (!Files.exists(inputFile))
throw BadRequestException(s"File ${inputFile.toAbsolutePath} does not exist")
new FileEntity(inputFile.toFile, turtleContentType)
}
}

val resourceStringEntity =
ZIO.attemptBlocking(new StringEntity(Source.fromResource(path).mkString, turtleContentType))
private def loadRdfObject(path: String): Task[String] = {
val relativeFileEntity = ZIO.readFile(Paths.get("..", path))
val resourceStringEntity = ZIO.attemptBlocking(Source.fromResource(path).mkString)

relativeFileEntity.orElse(resourceStringEntity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import zio.ZIO
import zio.ZLayer
import zio.macros.accessible

import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -210,18 +211,21 @@ final case class TriplestoreServiceInMemory(datasetRef: Ref[Dataset], implicit v
}
}

private def insertRdfDataObject(elem: RdfDataObject): ZIO[Any, Throwable, Unit] = {
val inputFile = Paths.get("..", elem.path)
private def insertRdfDataObject(elem: RdfDataObject): ZIO[Any, Throwable, Unit] =
ZIO.scoped {
for {
graphName <- checkGraphName(elem)
in <- fileInputStream(inputFile)
in <- loadRdfUrl(elem.path)
ds <- getDataSetWithTransaction(ReadWrite.WRITE)
model = ds.getNamedModel(graphName)
_ = model.read(in, null, "TURTLE")
} yield ()
}
}

private def loadRdfUrl(path: String): ZIO[Any & Scope, Throwable, InputStream] =
ZIO
.attemptBlocking(Option(getClass.getClassLoader.getResourceAsStream(path)).get)
.orElse(fileInputStream(Paths.get("..", path)))

private def checkGraphName(elem: RdfDataObject): Task[String] =
checkGraphName(elem.name)
Expand Down

0 comments on commit b9b2246

Please sign in to comment.