Skip to content

Commit

Permalink
feat: cache registered LD documents in memory (#3620)
Browse files Browse the repository at this point in the history
* feat: cache registered LD documents in memory

* pr remark

* Update extensions/common/json-ld/src/main/java/org/eclipse/edc/jsonld/TitaniumJsonLd.java

Co-authored-by: Jim Marino <[email protected]>

* DEPENDENCIES
---------

Co-authored-by: Jim Marino <[email protected]>
  • Loading branch information
paullatzelsperger and jimmarino authored Nov 17, 2023
1 parent ab11d89 commit fa978df
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
8 changes: 4 additions & 4 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ maven/mavencentral/io.swagger.core.v3/swagger-jaxrs2/2.2.15, Apache-2.0, approve
maven/mavencentral/io.swagger.core.v3/swagger-models-jakarta/2.2.15, Apache-2.0, approved, #5919
maven/mavencentral/io.swagger.core.v3/swagger-models/2.2.15, Apache-2.0, approved, #10353
maven/mavencentral/io.swagger.core.v3/swagger-models/2.2.8, Apache-2.0, approved, #10353
maven/mavencentral/io.swagger.parser.v3/swagger-parser-core/2.1.10, None, restricted, #11478
maven/mavencentral/io.swagger.parser.v3/swagger-parser-core/2.1.10, Apache-2.0, approved, #11478
maven/mavencentral/io.swagger.parser.v3/swagger-parser-v2-converter/2.1.10, Apache-2.0, approved, #9330
maven/mavencentral/io.swagger.parser.v3/swagger-parser-v3/2.1.10, Apache-2.0, approved, #9323
maven/mavencentral/io.swagger.parser.v3/swagger-parser/2.1.10, None, restricted, #11316
maven/mavencentral/io.swagger.parser.v3/swagger-parser/2.1.10, Apache-2.0, approved, #11316
maven/mavencentral/io.swagger/swagger-annotations/1.6.9, Apache-2.0, approved, #3792
maven/mavencentral/io.swagger/swagger-compat-spec-parser/1.0.64, None, restricted, #11479
maven/mavencentral/io.swagger/swagger-compat-spec-parser/1.0.64, Apache-2.0, approved, #11479
maven/mavencentral/io.swagger/swagger-core/1.6.9, Apache-2.0, approved, #4358
maven/mavencentral/io.swagger/swagger-models/1.6.9, LicenseRef-scancode-proprietary-license, restricted, #11476
maven/mavencentral/io.swagger/swagger-models/1.6.9, Apache-2.0, approved, #11476
maven/mavencentral/io.swagger/swagger-parser/1.0.64, Apache-2.0, approved, #4359
maven/mavencentral/jakarta.activation/jakarta.activation-api/1.2.1, EPL-2.0 OR BSD-3-Clause OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jaf
maven/mavencentral/jakarta.activation/jakarta.activation-api/2.1.0, EPL-2.0 OR BSD-3-Clause OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jaf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public TitaniumJsonLd(Monitor monitor) {

public TitaniumJsonLd(Monitor monitor, JsonLdConfiguration configuration) {
this.monitor = monitor;
this.documentLoader = new CachedDocumentLoader(configuration);
this.documentLoader = new CachedDocumentLoader(configuration, monitor);
}

@Override
Expand Down Expand Up @@ -177,28 +177,37 @@ private Stream<String> contextsForScope(String scope) {

private static class CachedDocumentLoader implements DocumentLoader {

private final Map<String, URI> cache = new HashMap<>();
private final Map<String, URI> uriCache = new HashMap<>();
private final Map<URI, Document> documentCache = new HashMap<>();
private final DocumentLoader loader;
private final Monitor monitor;

CachedDocumentLoader(JsonLdConfiguration configuration) {
CachedDocumentLoader(JsonLdConfiguration configuration, Monitor monitor) {
loader = new SchemeRouter()
.set("http", configuration.isHttpEnabled() ? HttpLoader.defaultInstance() : null)
.set("https", configuration.isHttpsEnabled() ? HttpLoader.defaultInstance() : null)
.set("file", new FileLoader())
.set("jar", new JarLoader());
this.monitor = monitor;
}

@Override
public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError {
var uri = Optional.of(url.toString())
.map(cache::get)
.map(uriCache::get)
.orElse(url);

return loader.loadDocument(uri, options);
return Optional.ofNullable(documentCache.get(uri))
.orElse(loader.loadDocument(uri, options));
}

public void register(String contextUrl, URI uri) {
cache.put(contextUrl, uri);
uriCache.put(contextUrl, uri);
try {
documentCache.put(uri, loader.loadDocument(uri, new DocumentLoaderOptions()));
} catch (JsonLdError e) {
monitor.warning("Error caching context URL '%s' for URI '%s'. Subsequent attempts to expand this context URL may fail.".formatted(contextUrl, uri));
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void verifyCachedDocsFromConfig_oneValidEntry(ServiceExtensionContext context, J
var service = (TitaniumJsonLd) extension.createJsonLdService(context);

DocumentLoader documentLoader = getFieldValue("documentLoader", service);
Map<String, URI> cache = getFieldValue("cache", documentLoader);
Map<String, URI> cache = getFieldValue("uriCache", documentLoader);

assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json"));
}
Expand All @@ -65,7 +65,7 @@ void verifyCachedDocsFromConfig_oneValidEntry_withSuperfluous(ServiceExtensionCo
var service = (TitaniumJsonLd) extension.createJsonLdService(context);

DocumentLoader documentLoader = getFieldValue("documentLoader", service);
Map<String, URI> cache = getFieldValue("cache", documentLoader);
Map<String, URI> cache = getFieldValue("uriCache", documentLoader);

assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json"));
}
Expand All @@ -82,7 +82,7 @@ void verifyCachedDocsFromConfig_multipleValidEntries(ServiceExtensionContext con
var service = (TitaniumJsonLd) extension.createJsonLdService(context);

DocumentLoader documentLoader = getFieldValue("documentLoader", service);
Map<String, URI> cache = getFieldValue("cache", documentLoader);
Map<String, URI> cache = getFieldValue("uriCache", documentLoader);

assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json"));
assertThat(cache).containsEntry("http://bar.org/doc.json", new URI("file:/tmp/bar/doc.json"));
Expand All @@ -99,7 +99,7 @@ void verifyCachedDocsFromConfig_multipleEntries_oneIncomplete(ServiceExtensionCo
var service = (TitaniumJsonLd) extension.createJsonLdService(context);

DocumentLoader documentLoader = getFieldValue("documentLoader", service);
Map<String, URI> cache = getFieldValue("cache", documentLoader);
Map<String, URI> cache = getFieldValue("uriCache", documentLoader);

assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json"))
.noneSatisfy((s, uri) -> assertThat(s).isEqualTo("http://bar.org/doc.json"));
Expand All @@ -117,7 +117,7 @@ void verifyCachedDocsFromConfig_multipleEntries_oneInvalid(ServiceExtensionConte
var service = (TitaniumJsonLd) extension.createJsonLdService(context);

DocumentLoader documentLoader = getFieldValue("documentLoader", service);
Map<String, URI> cache = getFieldValue("cache", documentLoader);
Map<String, URI> cache = getFieldValue("uriCache", documentLoader);

assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json"))
.noneSatisfy((s, uri) -> assertThat(s).isEqualTo("http://bar.org/doc.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ void documentResolution_shouldFailByDefault_whenContextIsNotRegisteredAndHttpIsN
.add("test:key", "value")
.build();
var service = defaultService();
service.registerCachedDocument("http//any.other/url", URI.create("any:uri"));

var expanded = service.expand(jsonObject);

Expand All @@ -280,7 +279,7 @@ void documentResolution_shouldCallHttpEndpoint_whenContextIsNotRegistered_andHtt
.add("test:key", "value")
.build();
var service = httpEnabledService();
service.registerCachedDocument("http//any.other/url", URI.create("any:uri"));
service.registerCachedDocument("http//any.other/url", URI.create("http://localhost:" + server.getLocalPort()));

var expanded = service.expand(jsonObject);

Expand Down

0 comments on commit fa978df

Please sign in to comment.