SOLR-11130: V2Request is SolrJ should return the correct collection name

This commit is contained in:
Noble Paul 2017-07-26 21:10:21 +09:30
parent 10875143b2
commit 9f73bcf11d
4 changed files with 34 additions and 6 deletions

View File

@ -16,6 +16,7 @@
*/
package org.apache.solr.client.solrj;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
@ -195,4 +196,8 @@ public abstract class SolrRequest<T extends SolrResponse> implements Serializabl
return process(client, null);
}
public String getCollection() {
return getParams() == null ? null : getParams().get("collection");
}
}

View File

@ -786,10 +786,10 @@ public class CloudSolrClient extends SolrClient {
@Override
public NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException {
SolrParams reqParams = request.getParams();
if (collection == null)
collection = (reqParams != null) ? reqParams.get("collection", getDefaultCollection()) : getDefaultCollection();
if (collection == null) {
collection = request.getCollection();
if (collection == null) collection = defaultCollection;
}
return requestWithRetryOnStaleState(request, 0, collection);
}

View File

@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.solr.client.solrj.SolrClient;
@ -38,14 +39,22 @@ import org.apache.solr.common.util.Utils;
public class V2Request extends SolrRequest<V2Response> {
//only for debugging purposes
public static final ThreadLocal<AtomicLong> v2Calls = new ThreadLocal<>();
static final Pattern COLL_REQ_PATTERN = Pattern.compile("/(c|collections)/[^/]+/(?!shards)");
static final Pattern COLL_REQ_PATTERN = Pattern.compile("/(c|collections)/([^/])+/(?!shards)");
private InputStream payload;
private SolrParams solrParams;
public final boolean useBinary;
private String collection;
private boolean isPerCollectionRequest = false;
private V2Request(METHOD m, String resource, boolean useBinary) {
super(m, resource);
Matcher matcher = COLL_REQ_PATTERN.matcher(getPath());
if (matcher.find()) {
this.collection = matcher.group(2);
isPerCollectionRequest = true;
}
this.useBinary = useBinary;
}
@Override
@ -73,7 +82,12 @@ public class V2Request extends SolrRequest<V2Response> {
}
public boolean isPerCollectionRequest() {
return COLL_REQ_PATTERN.matcher(getPath()).find();
return isPerCollectionRequest;
}
@Override
public String getCollection() {
return collection;
}
@Override

View File

@ -49,6 +49,7 @@ public class TestV2Request extends SolrCloudTestCase {
assertFalse(new V2Request.Builder("/collections/a/shards").build().isPerCollectionRequest());
assertFalse(new V2Request.Builder("/collections/a/shards/").build().isPerCollectionRequest());
assertTrue(new V2Request.Builder("/collections/a/update").build().isPerCollectionRequest());
assertEquals("a", new V2Request.Builder("/collections/a/update").build().getCollection());
assertTrue(new V2Request.Builder("/c/a/update").build().isPerCollectionRequest());
assertTrue(new V2Request.Builder("/c/a/schema").build().isPerCollectionRequest());
assertFalse(new V2Request.Builder("/c/a").build().isPerCollectionRequest());
@ -80,10 +81,18 @@ public class TestV2Request extends SolrCloudTestCase {
"}").build());
assertSuccess(client, new V2Request.Builder("/c").build());
assertSuccess(client, new V2Request.Builder("/c/_introspect").build());
assertSuccess(client, new V2Request.Builder("/c/test/config")
.withMethod(SolrRequest.METHOD.POST)
.withPayload("{'create-requesthandler' : { 'name' : '/x', 'class': 'org.apache.solr.handler.DumpRequestHandler' , 'startup' : 'lazy'}}")
.build());
assertSuccess(client, new V2Request.Builder("/c/test").withMethod(SolrRequest.METHOD.DELETE).build());
NamedList<Object> res = client.request(new V2Request.Builder("/c").build());
List collections = (List) res.get("collections");
assertEquals(0, collections.size());
}
}