SOLR-14846 Clean up Optional use (#1843)

* Remove Optional.ofNullable.orElse pattern
* Remove use of Optional as method parameter
This commit is contained in:
Mike Drob 2020-09-09 09:19:48 -05:00 committed by GitHub
parent 7da15706da
commit c902837bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 31 additions and 45 deletions

View File

@ -81,7 +81,7 @@ public class BackupCmd implements OverseerCollectionMessageHandler.Cmd {
Instant startTime = Instant.now();
CoreContainer cc = ocmh.overseer.getCoreContainer();
BackupRepository repository = cc.newBackupRepository(Optional.ofNullable(repo));
BackupRepository repository = cc.newBackupRepository(repo);
BackupManager backupMgr = new BackupManager(repository, ocmh.zkStateReader);
// Backup location

View File

@ -28,7 +28,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
@ -95,7 +94,7 @@ public class RestoreCmd implements OverseerCollectionMessageHandler.Cmd {
String repo = message.getStr(CoreAdminParams.BACKUP_REPOSITORY);
CoreContainer cc = ocmh.overseer.getCoreContainer();
BackupRepository repository = cc.newBackupRepository(Optional.ofNullable(repo));
BackupRepository repository = cc.newBackupRepository(repo);
URI location = repository.createURI(message.getStr(CoreAdminParams.BACKUP_LOCATION));
URI backupPath = repository.resolve(location, backupName);

View File

@ -265,10 +265,10 @@ public class CoreContainer {
* If not specified, a default implementation is used.
* @return a new instance of {@linkplain BackupRepository}.
*/
public BackupRepository newBackupRepository(Optional<String> repositoryName) {
public BackupRepository newBackupRepository(String repositoryName) {
BackupRepository repository;
if (repositoryName.isPresent()) {
repository = backupRepoFactory.newInstance(getResourceLoader(), repositoryName.get());
if (repositoryName != null) {
repository = backupRepoFactory.newInstance(getResourceLoader(), repositoryName);
} else {
repository = backupRepoFactory.newInstance(getResourceLoader());
}

View File

@ -20,7 +20,6 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Optional;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
@ -48,7 +47,8 @@ public interface BackupRepository extends NamedListInitializedPlugin, Closeable
* Otherwise return the default configuration value for the {@linkplain CoreAdminParams#BACKUP_LOCATION} parameter.
*/
default String getBackupLocation(String override) {
return Optional.ofNullable(override).orElse(getConfigProperty(CoreAdminParams.BACKUP_LOCATION));
// If override is null and default backup location is unset, what do we do?
return override != null ? override : getConfigProperty(CoreAdminParams.BACKUP_LOCATION);
}
/**

View File

@ -39,7 +39,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ExecutorService;
@ -484,7 +483,7 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAw
CoreContainer cc = core.getCoreContainer();
BackupRepository repo = null;
if (repoName != null) {
repo = cc.newBackupRepository(Optional.of(repoName));
repo = cc.newBackupRepository(repoName);
location = repo.getBackupLocation(location);
if (location == null) {
throw new IllegalArgumentException("location is required");
@ -593,7 +592,7 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAw
CoreContainer cc = core.getCoreContainer();
BackupRepository repo = null;
if (repoName != null) {
repo = cc.newBackupRepository(Optional.of(repoName));
repo = cc.newBackupRepository(repoName);
location = repo.getBackupLocation(location);
if (location == null) {
throw new IllegalArgumentException("location is required");

View File

@ -18,7 +18,6 @@
package org.apache.solr.handler.admin;
import java.net.URI;
import java.util.Optional;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CoreAdminParams;
@ -39,7 +38,7 @@ class BackupCoreOp implements CoreAdminHandler.CoreAdminOp {
String name = params.required().get(NAME);
String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
BackupRepository repository = it.handler.coreContainer.newBackupRepository(Optional.ofNullable(repoName));
BackupRepository repository = it.handler.coreContainer.newBackupRepository(repoName);
String location = repository.getBackupLocation(params.get(CoreAdminParams.BACKUP_LOCATION));
if (location == null) {

View File

@ -96,7 +96,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@ -1062,7 +1061,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
CoreContainer cc = h.coreContainer;
String repo = req.getParams().get(CoreAdminParams.BACKUP_REPOSITORY);
BackupRepository repository = cc.newBackupRepository(Optional.ofNullable(repo));
BackupRepository repository = cc.newBackupRepository(repo);
String location = repository.getBackupLocation(req.getParams().get(CoreAdminParams.BACKUP_LOCATION));
if (location == null) {
@ -1114,7 +1113,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
final CoreContainer cc = h.coreContainer;
final String repo = req.getParams().get(CoreAdminParams.BACKUP_REPOSITORY);
final BackupRepository repository = cc.newBackupRepository(Optional.ofNullable(repo));
final BackupRepository repository = cc.newBackupRepository(repo);
String location = repository.getBackupLocation(req.getParams().get(CoreAdminParams.BACKUP_LOCATION));
if (location == null) {

View File

@ -18,7 +18,6 @@
package org.apache.solr.handler.admin;
import java.net.URI;
import java.util.Optional;
import org.apache.solr.cloud.CloudDescriptor;
import org.apache.solr.cloud.ZkController;
@ -46,7 +45,7 @@ class RestoreCoreOp implements CoreAdminHandler.CoreAdminOp {
}
String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
BackupRepository repository = it.handler.coreContainer.newBackupRepository(Optional.ofNullable(repoName));
BackupRepository repository = it.handler.coreContainer.newBackupRepository(repoName);
String location = repository.getBackupLocation(params.get(CoreAdminParams.BACKUP_LOCATION));
if (location == null) {

View File

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Map;
import java.util.function.IntFunction;
@ -652,10 +651,10 @@ public class RelatednessAgg extends AggValueSource {
public void merge(Object facetResult, Context mcontext) {
@SuppressWarnings({"unchecked"})
final NamedList<Object> shardData = (NamedList<Object>)facetResult;
final boolean shardImplied = Objects.requireNonNullElse((Boolean)shardData.remove(IMPLIED_KEY), false);
final boolean shardImplied = Optional.ofNullable((Boolean)shardData.remove(IMPLIED_KEY)).orElse(false);
// regardless of wether this shard is implied, we want to know it's size info...
// regardless of whether this shard is implied, we want to know its size info...
mergedData.incSizes((Long)shardData.remove(FG_SIZE), (Long)shardData.remove(BG_SIZE));
if (! shardImplied) {

View File

@ -19,7 +19,6 @@ package org.apache.solr.security;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.apache.solr.client.solrj.impl.HttpClientBuilderFactory;
@ -60,7 +59,7 @@ public class ConfigurableInternodeAuthHadoopPlugin extends HadoopAuthPlugin impl
@Override
public SolrHttpClientBuilder getHttpClientBuilder(SolrHttpClientBuilder builder) {
return factory.getHttpClientBuilder(Optional.ofNullable(builder));
return factory.getHttpClientBuilder(builder);
}
@Override

View File

@ -17,7 +17,6 @@
package org.apache.solr.client.solrj.impl;
import java.io.Closeable;
import java.util.Optional;
/**
* Factory interface for configuring {@linkplain SolrHttpClientBuilder}. This
@ -36,7 +35,7 @@ public interface HttpClientBuilderFactory extends Closeable {
* by configured (optional).
* @return the {@linkplain SolrHttpClientBuilder}
*/
public SolrHttpClientBuilder getHttpClientBuilder(Optional<SolrHttpClientBuilder> builder);
public SolrHttpClientBuilder getHttpClientBuilder(SolrHttpClientBuilder builder);
public default void setup(Http2SolrClient client) {

View File

@ -21,7 +21,6 @@ import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@ -170,7 +169,7 @@ public class HttpClientUtil {
log.debug ("Using {}", factoryClassName);
try {
HttpClientBuilderFactory factory = (HttpClientBuilderFactory)Class.forName(factoryClassName).getConstructor().newInstance();
httpClientBuilder = factory.getHttpClientBuilder(Optional.of(SolrHttpClientBuilder.create()));
httpClientBuilder = factory.getHttpClientBuilder(SolrHttpClientBuilder.create());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to instantiate Solr HttpClientBuilderFactory", e);
}

View File

@ -26,7 +26,6 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.apache.http.HttpEntity;
@ -82,8 +81,8 @@ public class Krb5HttpClientBuilder implements HttpClientBuilderFactory {
}
@Override
public SolrHttpClientBuilder getHttpClientBuilder(Optional<SolrHttpClientBuilder> builder) {
return builder.isPresent() ? getBuilder(builder.get()) : getBuilder();
public SolrHttpClientBuilder getHttpClientBuilder(SolrHttpClientBuilder builder) {
return builder == null ? getBuilder() : getBuilder(builder);
}
private SPNEGOAuthentication createSPNEGOAuthentication() {

View File

@ -23,7 +23,6 @@ import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import org.apache.http.auth.AuthScope;
@ -122,17 +121,14 @@ public class PreemptiveBasicAuthClientBuilderFactory implements HttpClientBuilde
}
@Override
public SolrHttpClientBuilder getHttpClientBuilder(Optional<SolrHttpClientBuilder> optionalBuilder) {
public SolrHttpClientBuilder getHttpClientBuilder(SolrHttpClientBuilder builder) {
final String basicAuthUser = defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_USER);
final String basicAuthPass = defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_PASS);
if(basicAuthUser == null || basicAuthPass == null) {
throw new IllegalArgumentException("username & password must be specified with " + getClass().getName());
}
SolrHttpClientBuilder builder = optionalBuilder.isPresent() ?
initHttpClientBuilder(optionalBuilder.get(), basicAuthUser, basicAuthPass)
: initHttpClientBuilder(SolrHttpClientBuilder.create(), basicAuthUser, basicAuthPass);
return builder;
return initHttpClientBuilder(builder == null ? SolrHttpClientBuilder.create() : builder, basicAuthUser, basicAuthPass);
}
private SolrHttpClientBuilder initHttpClientBuilder(SolrHttpClientBuilder builder, String basicAuthUser, String basicAuthPass) {

View File

@ -20,7 +20,7 @@ import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Objects;
import java.util.Random;
import org.apache.solr.common.SolrException;
@ -64,9 +64,9 @@ public class RequestReplicaListTransformerGenerator {
}
public RequestReplicaListTransformerGenerator(ReplicaListTransformerFactory defaultRltFactory, ReplicaListTransformerFactory stableRltFactory, String defaultShardPreferences, String nodeName, String localHostAddress, NodesSysPropsCacher sysPropsCacher) {
this.defaultRltFactory = Optional.ofNullable(defaultRltFactory).orElse(RANDOM_RLTF);
this.stableRltFactory = Optional.ofNullable(stableRltFactory).orElseGet(AffinityReplicaListTransformerFactory::new);
this.defaultShardPreferences = Optional.ofNullable(defaultShardPreferences).orElse("");
this.defaultRltFactory = Objects.requireNonNullElse(defaultRltFactory, RANDOM_RLTF);
this.stableRltFactory = Objects.requireNonNullElseGet(stableRltFactory, AffinityReplicaListTransformerFactory::new);
this.defaultShardPreferences = Objects.requireNonNullElse(defaultShardPreferences, "");
this.nodeName = nodeName;
this.localHostAddress = localHostAddress;
this.sysPropsCacher = sysPropsCacher;
@ -83,7 +83,7 @@ public class RequestReplicaListTransformerGenerator {
public ReplicaListTransformer getReplicaListTransformer(final SolrParams requestParams, String defaultShardPreferences, String nodeName, String localHostAddress, NodesSysPropsCacher sysPropsCacher) {
@SuppressWarnings("deprecation")
final boolean preferLocalShards = requestParams.getBool(CommonParams.PREFER_LOCAL_SHARDS, false);
defaultShardPreferences = Optional.ofNullable(defaultShardPreferences).orElse(this.defaultShardPreferences);
defaultShardPreferences = Objects.requireNonNullElse(defaultShardPreferences, this.defaultShardPreferences);
final String shardsPreferenceSpec = requestParams.get(ShardParams.SHARDS_PREFERENCE, defaultShardPreferences);
if (preferLocalShards || !shardsPreferenceSpec.isEmpty()) {
@ -102,9 +102,9 @@ public class RequestReplicaListTransformerGenerator {
new NodePreferenceRulesComparator(
preferenceRules,
requestParams,
Optional.ofNullable(nodeName).orElse(this.nodeName),
Optional.ofNullable(localHostAddress).orElse(this.localHostAddress),
Optional.ofNullable(sysPropsCacher).orElse(this.sysPropsCacher),
nodeName != null ? nodeName : this.nodeName, // could be still null
localHostAddress != null ? localHostAddress : this.localHostAddress, // could still be null
sysPropsCacher != null ? sysPropsCacher : this.sysPropsCacher, // could still be null
defaultRltFactory,
stableRltFactory);
ReplicaListTransformer baseReplicaListTransformer = replicaComp.getBaseReplicaListTransformer();