Skip to content

Commit

Permalink
Fix cache key in PxWebDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Sep 26, 2024
1 parent 20aeee7 commit afdd0df
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,40 +98,31 @@ private static List<WebSource> loadDefaultSources() throws IOException {
private static PxWebClient newClient(WebSource source, Languages languages, WebContext context) throws IOException {
PxWebClient client = new DefaultPxWebClient(
HasMarker.of(source),
getBaseURL(source, languages),
getDefaultClientBaseURL(source, languages),
RiHttpUtils.newClient(source, context)
);

return CachedPxWebClient.of(
return new CachedPxWebClient(
client,
context.getDriverCache(source),
CACHE_TTL_PROPERTY.get(source.getProperties()),
source,
languages
getCachedClientBaseURI(source, languages),
Duration.ofMillis(CACHE_TTL_PROPERTY.get(source.getProperties()))
);
}

private static @NonNull Connection newConnection(@NonNull WebSource source, @NonNull Languages languages, @NonNull WebContext context) throws IOException {
return new PxWebConnection(newClient(source, languages, context));
}

@VisibleForTesting
static @NonNull URL getBaseURL(@NonNull WebSource source, @NonNull Languages languages) throws IOException {
try {
return UriTemplate.expand(source.getEndpoint(), getUriTemplateVariables(source, languages)).toURL();
} catch (URISyntaxException ex) {
throw new IOException(ex);
}
private static String resolveVersion(WebSource source) {
List<String> versions = VERSIONS_PROPERTY.get(source.getProperties());
return versions != null && !versions.isEmpty() ? versions.get(0) : DEFAULT_VERSION;
}

private static Map<String, String> getUriTemplateVariables(WebSource source, Languages languages) {
List<String> versions = VERSIONS_PROPERTY.get(source.getProperties());
private static String resolveLanguage(WebSource source, Languages requested) {
List<String> availableLanguages = LANGUAGES_PROPERTY.get(source.getProperties());
String language = availableLanguages != null ? lookupLanguage(availableLanguages, languages) : null;
Map<String, String> result = new HashMap<>();
result.put(VERSION_VARIABLE, versions != null && !versions.isEmpty() ? versions.get(0) : DEFAULT_VERSION);
result.put(LANGUAGE_VARIABLE, language != null ? language : DEFAULT_LANG);
return result;
String language = availableLanguages != null ? lookupLanguage(availableLanguages, requested) : null;
return language != null ? language : DEFAULT_LANG;
}

@VisibleForTesting
Expand Down Expand Up @@ -222,6 +213,18 @@ private interface PxWebClient extends HasMarker {
DataCursor getData(@NonNull String dbId, @NonNull String tableId, @NonNull Structure dsd, @NonNull Key key) throws IOException, IllegalArgumentException;
}

@VisibleForTesting
static @NonNull URL getDefaultClientBaseURL(@NonNull WebSource source, @NonNull Languages languages) throws IOException {
Map<String, String> variables = new HashMap<>();
variables.put(VERSION_VARIABLE, resolveVersion(source));
variables.put(LANGUAGE_VARIABLE, resolveLanguage(source, languages));
try {
return UriTemplate.expand(source.getEndpoint(), variables).toURL();
} catch (URISyntaxException ex) {
throw new IOException(ex);
}
}

@lombok.AllArgsConstructor
private static final class DefaultPxWebClient implements PxWebClient {

Expand Down Expand Up @@ -338,39 +341,34 @@ private FileParser<DataCursor> getDataParser(Structure dsd, MediaType ignore) {
}
}

@VisibleForTesting
static URI getCachedClientBaseURI(WebSource source, Languages languages) {
return TypedId.resolveURI(URI.create("cache:pxweb"), source.getEndpoint().getHost(), resolveLanguage(source, languages));
}

@lombok.AllArgsConstructor
private static final class CachedPxWebClient implements PxWebClient {

static @NonNull CachedPxWebClient of(
@NonNull PxWebClient client, @NonNull Cache<DataRepository> cache, long ttlInMillis,
@NonNull WebSource source, @NonNull Languages languages) {
return new CachedPxWebClient(client, cache, getBase(source, languages), Duration.ofMillis(ttlInMillis));
}

private static URI getBase(WebSource source, Languages languages) {
return TypedId.resolveURI(URI.create("cache:rest"), source.getEndpoint().getHost(), languages.toString());
}

@lombok.NonNull
private final PxWebClient delegate;

@lombok.NonNull
private final Cache<DataRepository> cache;

@lombok.NonNull
private final URI base;
private final URI baseURI;

@lombok.NonNull
private final Duration ttl;

@lombok.Getter(lazy = true)
private final TypedId<List<Catalog>> idOfDatabases = initIdOfDatabases(base);
private final TypedId<List<Catalog>> idOfDatabases = initIdOfDatabases(baseURI);

@lombok.Getter(lazy = true)
private final TypedId<List<Flow>> idOfTables = initIdOfTables(base);
private final TypedId<List<Flow>> idOfTables = initIdOfTables(baseURI);

@lombok.Getter(lazy = true)
private final TypedId<Structure> idOfMeta = initIdOfMeta(base);
private final TypedId<Structure> idOfMeta = initIdOfMeta(baseURI);

private static TypedId<List<Catalog>> initIdOfDatabases(URI base) {
return TypedId.of(base,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,59 @@ public void testTableQuery() throws IOException {
}

@Test
public void testGetBaseURL() throws IOException {
public void testGetDefaultClientBaseURL() throws IOException {
WebSource empty = WebSource
.builder().id("").driver("")
.endpointOf("https://localhost/_VERSION_/_LANG_")
.propertyOf(VERSIONS_PROPERTY, "v1")
.build();

assertThat(getBaseURL(empty, ANY)).hasToString("https://localhost/v1/en");
assertThat(getBaseURL(empty, EN)).hasToString("https://localhost/v1/en");
assertThat(getBaseURL(empty, FR_BE)).hasToString("https://localhost/v1/en");
assertThat(getBaseURL(empty, NL)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(empty, ANY)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(empty, EN)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(empty, FR_BE)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(empty, NL)).hasToString("https://localhost/v1/en");

WebSource en = empty.toBuilder().propertyOf(LANGUAGES_PROPERTY, "en").build();

assertThat(getBaseURL(en, ANY)).hasToString("https://localhost/v1/en");
assertThat(getBaseURL(en, EN)).hasToString("https://localhost/v1/en");
assertThat(getBaseURL(en, FR_BE)).hasToString("https://localhost/v1/en");
assertThat(getBaseURL(en, NL)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(en, ANY)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(en, EN)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(en, FR_BE)).hasToString("https://localhost/v1/en");
assertThat(getDefaultClientBaseURL(en, NL)).hasToString("https://localhost/v1/en");

WebSource fr = empty.toBuilder().propertyOf(LANGUAGES_PROPERTY, "fr").build();

assertThat(getBaseURL(fr, ANY)).hasToString("https://localhost/v1/fr");
assertThat(getBaseURL(fr, EN)).hasToString("https://localhost/v1/fr");
assertThat(getBaseURL(fr, FR_BE)).hasToString("https://localhost/v1/fr");
assertThat(getBaseURL(fr, NL)).hasToString("https://localhost/v1/fr");
assertThat(getDefaultClientBaseURL(fr, ANY)).hasToString("https://localhost/v1/fr");
assertThat(getDefaultClientBaseURL(fr, EN)).hasToString("https://localhost/v1/fr");
assertThat(getDefaultClientBaseURL(fr, FR_BE)).hasToString("https://localhost/v1/fr");
assertThat(getDefaultClientBaseURL(fr, NL)).hasToString("https://localhost/v1/fr");
}

@Test
public void testGetCachedClientBaseURI() throws IOException {
WebSource empty = WebSource
.builder().id("").driver("")
.endpointOf("https://localhost/_VERSION_/_LANG_")
.propertyOf(VERSIONS_PROPERTY, "v1")
.build();

assertThat(getCachedClientBaseURI(empty, ANY)).hasToString("cache:pxweb/localhost/en");
assertThat(getCachedClientBaseURI(empty, EN)).hasToString("cache:pxweb/localhost/en");
assertThat(getCachedClientBaseURI(empty, FR_BE)).hasToString("cache:pxweb/localhost/en");
assertThat(getCachedClientBaseURI(empty, NL)).hasToString("cache:pxweb/localhost/en");

WebSource en = empty.toBuilder().propertyOf(LANGUAGES_PROPERTY, "en").build();

assertThat(getCachedClientBaseURI(en, ANY)).hasToString("cache:pxweb/localhost/en");
assertThat(getCachedClientBaseURI(en, EN)).hasToString("cache:pxweb/localhost/en");
assertThat(getCachedClientBaseURI(en, FR_BE)).hasToString("cache:pxweb/localhost/en");
assertThat(getCachedClientBaseURI(en, NL)).hasToString("cache:pxweb/localhost/en");

WebSource fr = empty.toBuilder().propertyOf(LANGUAGES_PROPERTY, "fr").build();

assertThat(getCachedClientBaseURI(fr, ANY)).hasToString("cache:pxweb/localhost/fr");
assertThat(getCachedClientBaseURI(fr, EN)).hasToString("cache:pxweb/localhost/fr");
assertThat(getCachedClientBaseURI(fr, FR_BE)).hasToString("cache:pxweb/localhost/fr");
assertThat(getCachedClientBaseURI(fr, NL)).hasToString("cache:pxweb/localhost/fr");
}

@Test
Expand Down

0 comments on commit afdd0df

Please sign in to comment.