-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* GH-3654 add caching in document loader * GH-3654 update javadocs * remove problematic test * fix copyright * try to fix junit issues * try to fix junit issues
- Loading branch information
1 parent
26be6a5
commit 641d65c
Showing
10 changed files
with
449 additions
and
47 deletions.
There are no files selected for viewing
35 changes: 0 additions & 35 deletions
35
compliance/rio/src/test/java/org/eclipse/rdf4j/rio/n3/N3ParserTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
core/rio/jsonld/src/main/java/org/eclipse/rdf4j/rio/jsonld/CachingDocumentLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Eclipse RDF4J contributors. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
******************************************************************************/ | ||
|
||
package org.eclipse.rdf4j.rio.jsonld; | ||
|
||
import java.net.URI; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.rdf4j.rio.RDFParseException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
|
||
import no.hasmac.jsonld.JsonLdError; | ||
import no.hasmac.jsonld.document.Document; | ||
import no.hasmac.jsonld.loader.DocumentLoader; | ||
import no.hasmac.jsonld.loader.DocumentLoaderOptions; | ||
import no.hasmac.jsonld.loader.SchemeRouter; | ||
|
||
public class CachingDocumentLoader implements DocumentLoader { | ||
private static final DocumentLoader defaultLoader = SchemeRouter.defaultInstance(); | ||
private static final Logger logger = LoggerFactory.getLogger(CachingDocumentLoader.class); | ||
|
||
private static final LoadingCache<URI, Document> cache = CacheBuilder.newBuilder() | ||
.maximumSize(1000) // Maximum 1000 documents in cache | ||
.expireAfterWrite(1, TimeUnit.HOURS) // Expire after 1 hour | ||
.concurrencyLevel(8) // Optimize for 8 concurrent threads | ||
.build(new CacheLoader<>() { | ||
@Override | ||
public Document load(URI url) throws Exception { | ||
return defaultLoader.loadDocument(url, new DocumentLoaderOptions()); | ||
} | ||
}); | ||
|
||
private final boolean secureMode; | ||
private final Set<String> whitelist; | ||
private final boolean documentLoaderCache; | ||
|
||
public CachingDocumentLoader(boolean secureMode, Set<String> whitelist, boolean documentLoaderCache) { | ||
this.secureMode = secureMode; | ||
this.whitelist = whitelist; | ||
this.documentLoaderCache = documentLoaderCache; | ||
} | ||
|
||
@Override | ||
public Document loadDocument(URI uri, DocumentLoaderOptions options) { | ||
|
||
try { | ||
if (!secureMode || whitelist.contains(uri.toString())) { | ||
if (documentLoaderCache) { | ||
try { | ||
return cache.get(uri); | ||
} catch (ExecutionException e) { | ||
if (e.getCause() != null) { | ||
throw new RDFParseException("Could not load document from " + uri, e.getCause()); | ||
} | ||
throw new RDFParseException("Could not load document from " + uri, e); | ||
} | ||
} else { | ||
try { | ||
return defaultLoader.loadDocument(uri, options); | ||
} catch (JsonLdError e) { | ||
throw new RDFParseException("Could not load document from " + uri, e); | ||
} | ||
} | ||
} else { | ||
throw new RDFParseException("Could not load document from " + uri | ||
+ " because it is not whitelisted. See: JSONLDSettings.WHITELIST and JSONLDSettings.SECURE_MODE"); | ||
} | ||
} catch (RDFParseException e) { | ||
logger.error(e.getMessage(), e); | ||
throw e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.