SOLR-13484: refactored code

This commit is contained in:
Noble Paul 2019-05-23 12:50:09 +10:00
parent 750a5fdd02
commit a1c9f8f7db
7 changed files with 56 additions and 38 deletions

View File

@ -29,8 +29,10 @@ import java.util.HashSet;
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.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -102,6 +104,20 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
DEFAULT_ACTIONS.add(map);
}
Optional<BiConsumer<SolrQueryResponse, AutoScalingConfig>> getSubpathExecutor(List<String> path) {
if (path.size() == 3) {
if (DIAGNOSTICS.equals(path.get(2))) {
return Optional.of(this::handleDiagnostics);
} else if (SUGGESTIONS.equals(path.get(2))) {
return Optional.of(this::handleSuggestions);
} else {
return Optional.empty();
}
}
return Optional.empty();
}
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
try {
@ -111,8 +127,7 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
if ("GET".equals(httpMethod)) {
String path = (String) req.getContext().get("path");
if (path == null) path = "/cluster/autoscaling";
List<String> parts = StrUtils.splitSmart(path, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
List<String> parts = StrUtils.splitSmart(path, '/', true);
if (parts.size() < 2 || parts.size() > 3) {
// invalid
@ -129,12 +144,8 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
return this;
}
});
} else if (parts.size() == 3) {
if (DIAGNOSTICS.equals(parts.get(2))) {
handleDiagnostics(rsp, autoScalingConf);
} else if (SUGGESTIONS.equals(parts.get(2))) {
handleSuggestions(rsp, autoScalingConf);
}
} else {
getSubpathExecutor(parts).ifPresent(it -> it.accept(rsp, autoScalingConf));
}
} else {
if (req.getContentStreams() == null) {
@ -142,20 +153,22 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
}
String path = (String) req.getContext().get("path");
if (path != null) {
List<String> parts = StrUtils.splitSmart(path, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
if(parts.size() == 3) {
Map map = (Map) Utils.fromJSON(req.getContentStreams().iterator().next().getStream());
if (SUGGESTIONS.equals(parts.get(2))) {
handleSuggestions(rsp, new AutoScalingConfig(map));
return;
} else if (DIAGNOSTICS.equals(parts.get(2))) {
handleDiagnostics(rsp, new AutoScalingConfig(map));
return;
}
}
}
List<String> parts = StrUtils.splitSmart(path, '/', true);
if(parts.size() == 3){
getSubpathExecutor(parts).ifPresent(it -> {
Map map = null;
try {
map = (Map) Utils.fromJSON(req.getContentStreams().iterator().next().getStream());
} catch (IOException e1) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error parsing payload", e1);
}
it.accept(rsp, new AutoScalingConfig(map));
});
return;
}
}
List<CommandOperation> ops = CommandOperation.readCommands(req.getContentStreams(), rsp.getValues(), singletonCommands);
if (ops == null) {
// errors have already been added to the response so there's nothing left to do
@ -163,6 +176,7 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
}
processOps(req, rsp, ops);
}
} catch (Exception e) {
rsp.getValues().add("result", "failure");
throw e;
@ -172,7 +186,7 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
}
private void handleSuggestions(SolrQueryResponse rsp, AutoScalingConfig autoScalingConf) throws IOException {
private void handleSuggestions(SolrQueryResponse rsp, AutoScalingConfig autoScalingConf) {
rsp.getValues().add("suggestions",
PolicyHelper.getSuggestions(autoScalingConf, cloudManager));
}
@ -254,7 +268,7 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
return currentConfig.withProperties(configProps);
}
private void handleDiagnostics(SolrQueryResponse rsp, AutoScalingConfig autoScalingConf) throws IOException {
private void handleDiagnostics(SolrQueryResponse rsp, AutoScalingConfig autoScalingConf) {
Policy policy = autoScalingConf.getPolicy();
rsp.getValues().add("diagnostics", PolicyHelper.getDiagnostics(policy, cloudManager));
}
@ -689,8 +703,11 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission
switch (request.getHttpMethod()) {
case "GET":
return Name.AUTOSCALING_READ_PERM;
case "POST":
return Name.AUTOSCALING_WRITE_PERM;
case "POST": {
return StrUtils.splitSmart(request.getResource(), '/', true).size() == 3 ?
Name.AUTOSCALING_READ_PERM :
Name.AUTOSCALING_WRITE_PERM;
}
default:
return null;
}

View File

@ -159,8 +159,7 @@ public class SchemaHandler extends RequestHandlerBase implements SolrCoreAware,
break;
}
default: {
List<String> parts = StrUtils.splitSmart(path, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
List<String> parts = StrUtils.splitSmart(path, '/', true);
if (parts.size() > 1 && level2.containsKey(parts.get(1))) {
String realName = parts.get(1);
String fieldName = IndexSchema.nameMapping.get(realName);
@ -216,8 +215,7 @@ public class SchemaHandler extends RequestHandlerBase implements SolrCoreAware,
@Override
public SolrRequestHandler getSubHandler(String subPath) {
List<String> parts = StrUtils.splitSmart(subPath, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
List<String> parts = StrUtils.splitSmart(subPath, '/', true);
String prefix = parts.get(0);
if(subPaths.contains(prefix)) return this;

View File

@ -168,8 +168,7 @@ public class SolrConfigHandler extends RequestHandlerBase implements SolrCoreAwa
this.method = httpMethod;
path = (String) req.getContext().get("path");
if (path == null) path = getDefaultPath();
parts = StrUtils.splitSmart(path, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
parts = StrUtils.splitSmart(path, '/', true);
}
private String getDefaultPath() {

View File

@ -42,7 +42,7 @@ public class SolrRequestInfo {
protected SolrQueryRequest req;
protected SolrQueryResponse rsp;
protected Date now;
protected HttpServletRequest httpRequest;
public HttpServletRequest httpRequest;
protected TimeZone tz;
protected ResponseBuilder rb;
protected List<Closeable> closeHooks;

View File

@ -201,8 +201,7 @@ public class TestApiFramework extends SolrTestCaseJ4 {
for (Object o : conditions.entrySet()) {
Map.Entry e = (Map.Entry) o;
String path = (String) e.getKey();
List<String> parts = StrUtils.splitSmart(path, path.charAt(0) == '/' ? '/':' ');
if (parts.get(0).isEmpty()) parts.remove(0);
List<String> parts = StrUtils.splitSmart(path, path.charAt(0) == '/' ? '/':' ', true);
Object val = Utils.getObjectByPath(root, false, parts);
if (e.getValue() instanceof ValidatingJsonMap.PredicateWithErrMsg) {
ValidatingJsonMap.PredicateWithErrMsg value = (ValidatingJsonMap.PredicateWithErrMsg) e.getValue();

View File

@ -39,6 +39,13 @@ public class StrUtils {
return lst;
}
public static List<String> splitSmart(String s, char separator, boolean trimEmpty) {
List<String> l = splitSmart(s, separator);
if (l.size() > 0 && l.get(0).isEmpty()) l.remove(0);
return l;
}
/**
* Split a string based on a separator, but don't split if it's inside
* a string. Assume '\' escapes the next char both inside and

View File

@ -357,14 +357,12 @@ public class Utils {
public static Object getObjectByPath(Object root, boolean onlyPrimitive, String hierarchy) {
if (hierarchy == null) return getObjectByPath(root, onlyPrimitive, singletonList(null));
List<String> parts = StrUtils.splitSmart(hierarchy, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
List<String> parts = StrUtils.splitSmart(hierarchy, '/', true);
return getObjectByPath(root, onlyPrimitive, parts);
}
public static boolean setObjectByPath(Object root, String hierarchy, Object value) {
List<String> parts = StrUtils.splitSmart(hierarchy, '/');
if (parts.get(0).isEmpty()) parts.remove(0);
List<String> parts = StrUtils.splitSmart(hierarchy, '/', true);
return setObjectByPath(root, parts, value);
}