Skip to content

Commit

Permalink
#43 new Jena version and json-ld 1.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
manonthegithub committed Nov 19, 2024
1 parent 69b1eb4 commit 0e4c7a7
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 168 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Keys._
import org.dbpedia.sbt.Codegen

scalaVersion := "2.12.6"
scalaVersion := "2.12.13"

organization := "org.dbpedia"
name := "gstore"
version := "0.2.0-SNAPSHOT"

val ScalatraVersion = "2.6.3"
val jenaVersion = "4.10.0"
val jenaVersion = "5.2.0"
val jettyVersion = "9.4.9.v20180320"

libraryDependencies ++= Seq(
Expand All @@ -19,7 +19,6 @@ libraryDependencies ++= Seq(
"org.apache.jena" % "apache-jena-libs" % jenaVersion,
"org.apache.jena" % "jena-shacl" % jenaVersion,

"org.apache.jena" % "jena-jdbc-driver-remote" % jenaVersion,
"com.openlink.virtuoso" % "virtjdbc4" % "x.x.x" from "http://download3.openlinksw.com/uda/virtuoso/jdbc/virtjdbc4.jar",
"c3p0" % "c3p0" % "0.9.1.2",

Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/com/apicatalog/jsonld/loader/JsonLdInit.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.apicatalog.jsonld.loader

import com.apicatalog.jsonld.http.media.MediaType

object JsonLdInit {
def initLoader: DocumentLoader = {
val dl = HttpLoader.defaultInstance()
dl.fallbackContentType(MediaType.JSON_LD)
dl
}

}
7 changes: 4 additions & 3 deletions src/main/scala/org/dbpedia/databus/ApiImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class ApiImpl(config: Config, batchSize: Int = 1000) extends DatabusApi {

override def getGraphMapException404(e: Throwable)(request: javax.servlet.http.HttpServletRequest): Option[org.dbpedia.databus.swagger.model.OperationFailure] =
getFileMapException404(e)(request)

override def shaclValidate(dataid: Array[Byte], shacl: Array[Byte])(request: HttpServletRequest): Try[String] = {
val outLang = getLangFromAcceptHeader(request).flatMap(rdf).getOrElse(DefaultFormat)
setResponseHeaders(Map("Content-Type" -> outLang.lang.getContentType.toHeaderString))(request)
Expand Down Expand Up @@ -186,8 +187,9 @@ class ApiImpl(config: Config, batchSize: Int = 1000) extends DatabusApi {
.collect { case c: RDFContentFormat => c }
.getOrElse(DefaultFormat)
val graphId = generateGraphId(prefix.getOrElse(getPrefix(request)), username, path)
wrapWithUnsupportedException(formatFromPath(path)
.flatMap(rdfExtractor),
wrapWithUnsupportedException(
formatFromPath(path)
.flatMap(rdfExtractor),
path)
.flatMap(format => {
setResponseHeaders(Map("Content-Type" -> outFormat.lang.getContentType.toHeaderString))(request)
Expand Down Expand Up @@ -326,7 +328,6 @@ object ApiImpl {
defaultJsonldLocalhostContextLocation: Option[String])



object Config {

def default: Config = fromMapper(SystemMapper)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
package org.dbpedia.databus

import java.util.concurrent.ConcurrentHashMap
import com.apicatalog.jsonld.context.cache.Cache
import com.apicatalog.jsonld.document.Document
import com.apicatalog.jsonld.loader.{DocumentLoader, DocumentLoaderOptions, JsonLdInit}

import com.github.jsonldjava.core.{Context, JsonLdOptions}
import org.dbpedia.databus.CachingJsonldContext.ApproxSizeStringKeyCache
import java.util.concurrent.ConcurrentHashMap
import org.dbpedia.databus.CachingJsonldContexts.ApproxSizeStringKeyCache

import java.net.URI
import scala.collection.JavaConverters._

class CachingJsonldContext(sizeLimit: Int, opts: JsonLdOptions) extends Context(opts) {

private val cache = new ApproxSizeStringKeyCache[Context](sizeLimit)

override def parse(ctx: Object): Context =
ctx match {
case s: String =>
cache.get(s)
.map(c => super.parse(c))
.getOrElse({
val re = super.parse(ctx)
cache.put(s, re)
re
})
case _ => super.parse(ctx)
}
def putInCache(contextUri: String, ctx: Context) =
cache.put(contextUri, ctx)
class CachingJsonldContexts(sizeLimit: Int) extends Cache[String, Document] {

private val cache = new ApproxSizeStringKeyCache[Document](sizeLimit)

override def containsKey(key: String): Boolean = cache.get(key).isDefined

override def get(key: String): Document = cache.get(key).orNull

override def put(key: String, value: Document): Unit = cache.put(key, value)
}

object CachingJsonldContext {
object CachingJsonldContexts {

// not the most efficient impl, but should work for now :)
class ApproxSizeStringKeyCache[T](sizeLimit: Int) {
Expand Down Expand Up @@ -65,3 +58,19 @@ object CachingJsonldContext {
}

}

class LRUDocumentCacheLoader(private val cache: Cache[String, Document], private val documentLoader: DocumentLoader) extends DocumentLoader {

override def loadDocument(url: URI, options: DocumentLoaderOptions): Document = {
val k = url.toString
var result = cache.get(k)
if (result == null) {
result = documentLoader.loadDocument(url, options)
cache.put(k, result)
}
result
}

def put(k: String, v: Document) = cache.put(k, v)

}
Loading

0 comments on commit 0e4c7a7

Please sign in to comment.