Skip to content

Commit

Permalink
Simplify creation of RestrictedSolrClient
Browse files Browse the repository at this point in the history
  • Loading branch information
tokee committed Oct 14, 2023
1 parent 22e5fd0 commit ac60355
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ protected NetarchiveSolrClient() { // private. Singleton
*
*/
public static void initialize(String solrServerUrl) {
SolrClient innerSolrClient = new HttpSolrClient.Builder(solrServerUrl).build();
SolrClient innerSolrClient = RestrictedSolrClient.createSolrClient();

if (PropertiesLoader.SOLR_SERVER_CACHING) {
int maxCachingEntries = PropertiesLoader.SOLR_SERVER_CACHING_MAX_ENTRIES;
int maxCachingSeconds = PropertiesLoader.SOLR_SERVER_CACHING_AGE_SECONDS;
solrServer = new CachingSolrClient(innerSolrClient, maxCachingEntries, maxCachingSeconds, -1); //-1 means no maximum number of connections
log.info("SolrClient initialized with caching properties: maxCachedEntrie="+maxCachingEntries +" cacheAgeSeconds="+maxCachingSeconds);
} else {
solrServer = new HttpSolrClient.Builder(solrServerUrl).build();
log.info("SolClient initialized without caching");
solrServer = innerSolrClient;
log.info("SolrClient initialized without caching");
}

// some of the solr query will never using cache. word cloud(cache memory) + playback resolving etc. (cache poisoning)
noCacheSolrServer = new HttpSolrClient.Builder(solrServerUrl).build();
noCacheSolrServer = innerSolrClient;

// solrServer.setRequestWriter(new BinaryRequestWriter()); // To avoid http
// error code 413/414, due to monster URI. (and it is faster)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* SolrClient wrapper that ensures that {@link PropertiesLoader#SOLR_PARAMS_MAP}
Expand Down Expand Up @@ -67,6 +70,26 @@ public class RestrictedSolrClient extends SolrClient {
*/
private final Map<String, String> fixedParams;

/**
* Create a {@link HttpSolrClient} wrapped as a {@code RestrictedSolrClient} using the property
* {@link PropertiesLoader#SOLR_PARAMS_MAP} for restrictions and the given {@code collection} as
* {@link #defaultCollection}.
* <p>
* The {@code solrBaseURL} and {@code collection} are parsed from the combined version
* {@link PropertiesLoader#SOLR_SERVER}.
* @return a {@code SolrClient} where all calls are restricted aka "safe".
*/
public static RestrictedSolrClient createSolrClient() {
Matcher m = SOLR_COLLECTION_PATTERN.matcher(PropertiesLoader.SOLR_SERVER);
if (!m.matches()) {
throw new IllegalStateException(String.format(
Locale.ROOT, "Unable to match Solr and collection from '%s' using pattern '%s'",
PropertiesLoader.SOLR_SERVER, SOLR_COLLECTION_PATTERN.pattern()));
}
return createSolrClient(m.group(1), m.group(2));
}
private static final Pattern SOLR_COLLECTION_PATTERN = Pattern.compile("(http.*)/([^/]+)/?$");

/**
* Create a {@link HttpSolrClient} wrapped as a {@code RestrictedSolrClient} using the property
* {@link PropertiesLoader#SOLR_PARAMS_MAP} for restrictions and the given {@code collection} as
Expand All @@ -76,6 +99,7 @@ public class RestrictedSolrClient extends SolrClient {
* @return a {@code SolrClient} where all calls are restricted aka "safe".
*/
public static RestrictedSolrClient createSolrClient(String solrBaseURL, String collection) {
log.info("Creating RestrictedSolrClient(solrBaseURL='{}', collection='{}')", solrBaseURL, collection);
return new RestrictedSolrClient(new HttpSolrClient.Builder(solrBaseURL).build(), collection);
}

Expand Down

0 comments on commit ac60355

Please sign in to comment.