Skip to content

Commit c7567ac

Browse files
authored
feat: generic document cache api + dynamic json schema validation (#5889)
1 parent 311eb86 commit c7567ac

85 files changed

Lines changed: 3574 additions & 422 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
File renamed without changes.

core/common/json-ld-cache-core/src/main/java/org/eclipse/edc/jsonld/cache/CachedJsonLdContextServiceImpl.java renamed to core/common/document-cache-core/src/main/java/org/eclipse/edc/document/cache/CachedDocumentServiceImpl.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.jsonld.cache;
16-
17-
import org.eclipse.edc.jsonld.cache.spi.CachedJsonLdContext;
18-
import org.eclipse.edc.jsonld.cache.spi.CachedJsonLdContextService;
19-
import org.eclipse.edc.jsonld.cache.spi.PullStrategy;
20-
import org.eclipse.edc.jsonld.cache.spi.resolver.JsonLdContextResolver;
21-
import org.eclipse.edc.jsonld.cache.spi.store.CachedJsonLdContextStore;
15+
package org.eclipse.edc.document.cache;
16+
17+
import org.eclipse.edc.document.cache.spi.CachedDocument;
18+
import org.eclipse.edc.document.cache.spi.CachedDocumentService;
19+
import org.eclipse.edc.document.cache.spi.CachedDocumentType;
20+
import org.eclipse.edc.document.cache.spi.PullStrategy;
21+
import org.eclipse.edc.document.cache.spi.resolver.DocumentResolver;
22+
import org.eclipse.edc.document.cache.spi.store.CachedDocumentStore;
2223
import org.eclipse.edc.jsonld.spi.JsonLd;
2324
import org.eclipse.edc.spi.monitor.Monitor;
2425
import org.eclipse.edc.spi.query.QuerySpec;
@@ -30,20 +31,20 @@
3031
import java.util.List;
3132

3233
import static java.lang.String.format;
33-
import static org.eclipse.edc.jsonld.cache.JsonLdContexts.toJsonObject;
34-
import static org.eclipse.edc.jsonld.cache.spi.store.CachedJsonLdContextStore.NOT_FOUND;
34+
import static org.eclipse.edc.document.cache.CachedDocuments.toJsonObject;
35+
import static org.eclipse.edc.document.cache.spi.store.CachedDocumentStore.NOT_FOUND;
3536

36-
public class CachedJsonLdContextServiceImpl implements CachedJsonLdContextService {
37+
public class CachedDocumentServiceImpl implements CachedDocumentService {
3738

3839
private final TransactionContext transactionContext;
39-
private final CachedJsonLdContextStore store;
40-
private final JsonLdContextResolver resolver;
40+
private final CachedDocumentStore store;
41+
private final DocumentResolver resolver;
4142
private final JsonLd jsonLd;
4243
private final Monitor monitor;
4344
private final Clock clock;
4445

45-
public CachedJsonLdContextServiceImpl(TransactionContext transactionContext, CachedJsonLdContextStore store,
46-
JsonLdContextResolver resolver, JsonLd jsonLd, Monitor monitor, Clock clock) {
46+
public CachedDocumentServiceImpl(TransactionContext transactionContext, CachedDocumentStore store,
47+
DocumentResolver resolver, JsonLd jsonLd, Monitor monitor, Clock clock) {
4748
this.transactionContext = transactionContext;
4849
this.store = store;
4950
this.resolver = resolver;
@@ -53,12 +54,12 @@ public CachedJsonLdContextServiceImpl(TransactionContext transactionContext, Cac
5354
}
5455

5556
@Override
56-
public CachedJsonLdContext findById(String id) {
57+
public CachedDocument findById(String id) {
5758
return transactionContext.execute(() -> store.findById(id));
5859
}
5960

6061
@Override
61-
public ServiceResult<List<CachedJsonLdContext>> search(QuerySpec query) {
62+
public ServiceResult<List<CachedDocument>> search(QuerySpec query) {
6263
return transactionContext.execute(() -> {
6364
try (var stream = store.findAll(query)) {
6465
return ServiceResult.success(stream.toList());
@@ -67,25 +68,29 @@ public ServiceResult<List<CachedJsonLdContext>> search(QuerySpec query) {
6768
}
6869

6970
@Override
70-
public @NotNull ServiceResult<CachedJsonLdContext> create(CachedJsonLdContext context) {
71+
public @NotNull ServiceResult<CachedDocument> create(CachedDocument context) {
7172
return transactionContext.execute(() -> prepare(context)
7273
.compose(prepared -> ServiceResult.from(store.create(prepared)).onSuccess(this::register)));
7374
}
7475

7576
@Override
76-
public @NotNull ServiceResult<CachedJsonLdContext> update(CachedJsonLdContext context) {
77+
public @NotNull ServiceResult<CachedDocument> update(CachedDocument context) {
7778
return transactionContext.execute(() -> prepare(context)
7879
.compose(prepared -> ServiceResult.from(store.update(prepared)).onSuccess(this::register)));
7980
}
8081

8182
@Override
82-
public @NotNull ServiceResult<CachedJsonLdContext> deleteById(String id) {
83+
public @NotNull ServiceResult<CachedDocument> deleteById(String id) {
8384
return transactionContext.execute(() -> ServiceResult.from(store.delete(id))
84-
.onSuccess(deleted -> jsonLd.unregisterCachedDocument(deleted.getUrl())));
85+
.onSuccess(deleted -> {
86+
if (deleted.getType() == CachedDocumentType.JSON_LD) {
87+
jsonLd.unregisterCachedDocument(deleted.getUrl());
88+
}
89+
}));
8590
}
8691

8792
@Override
88-
public @NotNull ServiceResult<CachedJsonLdContext> refresh(String id) {
93+
public @NotNull ServiceResult<CachedDocument> refresh(String id) {
8994
return transactionContext.execute(() -> {
9095
var existing = store.findById(id);
9196
if (existing == null) {
@@ -111,7 +116,7 @@ public ServiceResult<List<CachedJsonLdContext>> search(QuerySpec query) {
111116
* {@code NEVER} requires content up front, {@code IF_NOT_PRESENT} pulls only when content is missing,
112117
* and {@code ALWAYS} pulls a fresh copy from the url.
113118
*/
114-
private ServiceResult<CachedJsonLdContext> prepare(CachedJsonLdContext context) {
119+
private ServiceResult<CachedDocument> prepare(CachedDocument context) {
115120
return switch (context.getPullStrategy()) {
116121
case NEVER -> context.getContent() != null
117122
? ServiceResult.success(context)
@@ -131,7 +136,15 @@ private ServiceResult<String> pull(String url) {
131136
return ServiceResult.success(result.getContent().toString());
132137
}
133138

134-
private void register(CachedJsonLdContext context) {
139+
/**
140+
* Registers the document so it can be resolved at runtime. Only {@link CachedDocumentType#JSON_LD} documents
141+
* are registered into the {@link JsonLd} service; {@link CachedDocumentType#JSON_SCHEMA} documents are served
142+
* to the management API schema validator directly from the store and therefore need no action here.
143+
*/
144+
private void register(CachedDocument context) {
145+
if (context.getType() != CachedDocumentType.JSON_LD) {
146+
return;
147+
}
135148
toJsonObject(context.getContent())
136149
.onSuccess(json -> jsonLd.registerCachedDocument(context.getUrl(), json))
137150
.onFailure(f -> monitor.warning("Cannot register cached JSON-LD context '%s': %s"

core/common/json-ld-cache-core/src/main/java/org/eclipse/edc/jsonld/cache/JsonLdContexts.java renamed to core/common/document-cache-core/src/main/java/org/eclipse/edc/document/cache/CachedDocuments.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.jsonld.cache;
15+
package org.eclipse.edc.document.cache;
1616

1717
import jakarta.json.Json;
1818
import jakarta.json.JsonObject;
@@ -21,11 +21,11 @@
2121
import java.io.StringReader;
2222

2323
/**
24-
* Helpers to parse the raw JSON content stored in a {@link org.eclipse.edc.jsonld.cache.spi.CachedJsonLdContext}.
24+
* Helpers to parse the raw JSON content stored in a {@link org.eclipse.edc.document.cache.spi.CachedDocument}.
2525
*/
26-
public final class JsonLdContexts {
26+
public final class CachedDocuments {
2727

28-
private JsonLdContexts() {
28+
private CachedDocuments() {
2929
}
3030

3131
/**

core/common/json-ld-cache-core/src/main/java/org/eclipse/edc/jsonld/cache/JsonLdContextCacheDefaultServicesExtension.java renamed to core/common/document-cache-core/src/main/java/org/eclipse/edc/document/cache/DocumentCacheDefaultServicesExtension.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.jsonld.cache;
15+
package org.eclipse.edc.document.cache;
1616

17+
import org.eclipse.edc.document.cache.resolver.HttpDocumentResolver;
18+
import org.eclipse.edc.document.cache.spi.resolver.DocumentResolver;
19+
import org.eclipse.edc.document.cache.spi.store.CachedDocumentStore;
20+
import org.eclipse.edc.document.cache.store.InMemoryCachedDocumentStore;
1721
import org.eclipse.edc.http.spi.EdcHttpClient;
18-
import org.eclipse.edc.jsonld.cache.resolver.HttpJsonLdContextResolver;
19-
import org.eclipse.edc.jsonld.cache.spi.resolver.JsonLdContextResolver;
20-
import org.eclipse.edc.jsonld.cache.spi.store.CachedJsonLdContextStore;
21-
import org.eclipse.edc.jsonld.cache.store.InMemoryCachedJsonLdContextStore;
2222
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
2323
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
2424
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
2525
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
2626
import org.eclipse.edc.spi.system.ServiceExtension;
2727

28-
import static org.eclipse.edc.jsonld.cache.JsonLdContextCacheDefaultServicesExtension.NAME;
28+
import static org.eclipse.edc.document.cache.DocumentCacheDefaultServicesExtension.NAME;
2929

3030
@Extension(NAME)
31-
public class JsonLdContextCacheDefaultServicesExtension implements ServiceExtension {
31+
public class DocumentCacheDefaultServicesExtension implements ServiceExtension {
3232

33-
public static final String NAME = "JSON-LD Context Cache Default Services";
33+
public static final String NAME = "Document Cache Default Services";
3434

3535
@Inject
3636
private CriterionOperatorRegistry criterionOperatorRegistry;
@@ -44,12 +44,12 @@ public String name() {
4444
}
4545

4646
@Provider(isDefault = true)
47-
public CachedJsonLdContextStore cachedJsonLdContextStore() {
48-
return new InMemoryCachedJsonLdContextStore(criterionOperatorRegistry);
47+
public CachedDocumentStore cachedDocumentStore() {
48+
return new InMemoryCachedDocumentStore(criterionOperatorRegistry);
4949
}
5050

5151
@Provider(isDefault = true)
52-
public JsonLdContextResolver jsonLdContextResolver() {
53-
return new HttpJsonLdContextResolver(httpClient);
52+
public DocumentResolver documentResolver() {
53+
return new HttpDocumentResolver(httpClient);
5454
}
5555
}

core/common/json-ld-cache-core/src/main/java/org/eclipse/edc/jsonld/cache/JsonLdContextCacheExtension.java renamed to core/common/document-cache-core/src/main/java/org/eclipse/edc/document/cache/DocumentCacheExtension.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.jsonld.cache;
16-
17-
import org.eclipse.edc.jsonld.cache.spi.CachedJsonLdContext;
18-
import org.eclipse.edc.jsonld.cache.spi.CachedJsonLdContextService;
19-
import org.eclipse.edc.jsonld.cache.spi.PullStrategy;
20-
import org.eclipse.edc.jsonld.cache.spi.resolver.JsonLdContextResolver;
21-
import org.eclipse.edc.jsonld.cache.spi.store.CachedJsonLdContextStore;
15+
package org.eclipse.edc.document.cache;
16+
17+
import org.eclipse.edc.document.cache.spi.CachedDocument;
18+
import org.eclipse.edc.document.cache.spi.CachedDocumentService;
19+
import org.eclipse.edc.document.cache.spi.CachedDocumentType;
20+
import org.eclipse.edc.document.cache.spi.PullStrategy;
21+
import org.eclipse.edc.document.cache.spi.resolver.DocumentResolver;
22+
import org.eclipse.edc.document.cache.spi.store.CachedDocumentStore;
2223
import org.eclipse.edc.jsonld.spi.JsonLd;
2324
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
2425
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
@@ -36,32 +37,32 @@
3637
import java.util.concurrent.ScheduledExecutorService;
3738
import java.util.concurrent.TimeUnit;
3839

39-
import static org.eclipse.edc.jsonld.cache.JsonLdContextCacheExtension.NAME;
40+
import static org.eclipse.edc.document.cache.DocumentCacheExtension.NAME;
4041

4142
/**
42-
* Provides the {@link CachedJsonLdContextService}, synchronizes the persisted cache into the {@link JsonLd}
43+
* Provides the {@link CachedDocumentService}, synchronizes the persisted cache into the {@link JsonLd}
4344
* service at boot, and periodically refreshes refreshable entries.
4445
*/
4546
@Extension(NAME)
46-
public class JsonLdContextCacheExtension implements ServiceExtension {
47+
public class DocumentCacheExtension implements ServiceExtension {
4748

48-
public static final String NAME = "JSON-LD Context Cache";
49+
public static final String NAME = "Document Cache";
4950

5051
private static final long DEFAULT_REFRESH_INTERVAL_MS = 3600_000L;
5152

52-
@Setting(description = "Enables the background refresh of cached JSON-LD contexts", key = "edc.jsonld.cache.refresh.enabled", defaultValue = "true")
53+
@Setting(description = "Enables the background refresh of cached documents", key = "edc.document.cache.refresh.enabled", defaultValue = "true")
5354
private boolean refreshEnabled;
5455

55-
@Setting(description = "Interval (in milliseconds) between background refreshes of cached JSON-LD contexts with pull strategy 'always'.",
56-
key = "edc.jsonld.cache.refresh.interval.ms", defaultValue = DEFAULT_REFRESH_INTERVAL_MS + "")
56+
@Setting(description = "Interval (in milliseconds) between background refreshes of cached documents with pull strategy 'always'.",
57+
key = "edc.document.cache.refresh.interval.ms", defaultValue = DEFAULT_REFRESH_INTERVAL_MS + "")
5758
private long refreshIntervalMs;
5859

5960
@Inject
6061
private JsonLd jsonLd;
6162
@Inject
62-
private CachedJsonLdContextStore store;
63+
private CachedDocumentStore store;
6364
@Inject
64-
private JsonLdContextResolver resolver;
65+
private DocumentResolver resolver;
6566
@Inject
6667
private TransactionContext transactionContext;
6768
@Inject
@@ -70,7 +71,7 @@ public class JsonLdContextCacheExtension implements ServiceExtension {
7071
private Clock clock;
7172

7273
private Monitor monitor;
73-
private CachedJsonLdContextService service;
74+
private CachedDocumentService service;
7475
private ScheduledExecutorService scheduler;
7576

7677

@@ -82,11 +83,11 @@ public String name() {
8283
@Override
8384
public void initialize(ServiceExtensionContext context) {
8485
monitor = context.getMonitor();
85-
service = new CachedJsonLdContextServiceImpl(transactionContext, store, resolver, jsonLd, monitor, clock);
86+
service = new CachedDocumentServiceImpl(transactionContext, store, resolver, jsonLd, monitor, clock);
8687
}
8788

8889
@Provider
89-
public CachedJsonLdContextService cachedJsonLdContextService() {
90+
public CachedDocumentService cachedDocumentService() {
9091
return service;
9192
}
9293

@@ -114,11 +115,11 @@ private void syncAll() {
114115
});
115116
}
116117

117-
private void register(CachedJsonLdContext context) {
118-
if (context.getContent() == null) {
118+
private void register(CachedDocument context) {
119+
if (context.getType() != CachedDocumentType.JSON_LD || context.getContent() == null) {
119120
return;
120121
}
121-
JsonLdContexts.toJsonObject(context.getContent())
122+
CachedDocuments.toJsonObject(context.getContent())
122123
.onSuccess(json -> jsonLd.registerCachedDocument(context.getUrl(), json))
123124
.onFailure(f -> monitor.warning("Cannot register cached JSON-LD context '%s': %s"
124125
.formatted(context.getUrl(), f.getFailureDetail())));
@@ -130,7 +131,7 @@ private void refreshAlways() {
130131
try (var stream = store.findAll(QuerySpec.max())) {
131132
return stream
132133
.filter(this::shouldFetch)
133-
.map(CachedJsonLdContext::getId)
134+
.map(CachedDocument::getId)
134135
.toList();
135136
}
136137
});
@@ -140,7 +141,7 @@ private void refreshAlways() {
140141
}
141142
}
142143

143-
private boolean shouldFetch(CachedJsonLdContext c) {
144+
private boolean shouldFetch(CachedDocument c) {
144145
return (c.getPullStrategy() == PullStrategy.ALWAYS) || (c.getPullStrategy() == PullStrategy.IF_NOT_PRESENT && c.getContent() == null);
145146
}
146147
}

core/common/json-ld-cache-core/src/main/java/org/eclipse/edc/jsonld/cache/resolver/HttpJsonLdContextResolver.java renamed to core/common/document-cache-core/src/main/java/org/eclipse/edc/document/cache/resolver/HttpDocumentResolver.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.jsonld.cache.resolver;
15+
package org.eclipse.edc.document.cache.resolver;
1616

1717
import jakarta.json.JsonObject;
1818
import okhttp3.Request;
19+
import org.eclipse.edc.document.cache.spi.resolver.DocumentResolver;
1920
import org.eclipse.edc.http.spi.EdcHttpClient;
20-
import org.eclipse.edc.jsonld.cache.spi.resolver.JsonLdContextResolver;
2121
import org.eclipse.edc.spi.result.Result;
2222

2323
import java.io.IOException;
2424

25-
import static org.eclipse.edc.jsonld.cache.JsonLdContexts.toJsonObject;
25+
import static org.eclipse.edc.document.cache.CachedDocuments.toJsonObject;
2626

2727
/**
2828
* Resolves a JSON-LD context document by fetching its {@code url} over http/https.
2929
*/
30-
public class HttpJsonLdContextResolver implements JsonLdContextResolver {
30+
public class HttpDocumentResolver implements DocumentResolver {
3131

3232
private final EdcHttpClient httpClient;
3333

34-
public HttpJsonLdContextResolver(EdcHttpClient httpClient) {
34+
public HttpDocumentResolver(EdcHttpClient httpClient) {
3535
this.httpClient = httpClient;
3636
}
3737

0 commit comments

Comments
 (0)