This is an automated email from the ASF dual-hosted git repository.
psalagnac pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/branch_9x by this push:
new 30e5dc9e381 SOLR-17518: Restore compilation backward compability of
RequestWriter. (#3288)
30e5dc9e381 is described below
commit 30e5dc9e38116e73ba95bd271cd0504a4ef72e43
Author: Pierre Salagnac <[email protected]>
AuthorDate: Wed Mar 26 10:34:46 2025 +0100
SOLR-17518: Restore compilation backward compability of RequestWriter.
(#3288)
---
solr/CHANGES.txt | 2 +-
.../solr/client/solrj/impl/XMLRequestWriter.java | 194 +--------------------
.../solr/client/solrj/request/RequestWriter.java | 181 ++++++++++++++++++-
3 files changed, 180 insertions(+), 197 deletions(-)
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 7ac29882a2e..f5591cc2b6e 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -101,7 +101,7 @@ Other Changes
* SOLR-17667: Simplify zombie server logic in LBSolrClient (Houston Putman)
-* SOLR-17518: Deprecate UpdateRequest.getXml() and replace it with
XMLRequestWriter. (Pierre Salagnac)
+* SOLR-17518: Deprecate UpdateRequest.getXml(). Usage of XMLRequestWriter is
now preferred. (Pierre Salagnac)
* SOLR-17716: Handle interrupted exception in SolrCores.waitAddPendingCoreOps.
(Bruno Roustant)
diff --git
a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/XMLRequestWriter.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/XMLRequestWriter.java
index f3e48a31640..0268e74639d 100644
---
a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/XMLRequestWriter.java
+++
b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/XMLRequestWriter.java
@@ -16,198 +16,12 @@
*/
package org.apache.solr.client.solrj.impl;
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.request.RequestWriter;
-import org.apache.solr.client.solrj.request.UpdateRequest;
-import org.apache.solr.client.solrj.util.ClientUtils;
-import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.common.params.ShardParams;
-import org.apache.solr.common.util.ContentStream;
-import org.apache.solr.common.util.XML;
public class XMLRequestWriter extends RequestWriter {
- @Override
- public RequestWriter.ContentWriter getContentWriter(SolrRequest<?> req) {
- if (req instanceof UpdateRequest) {
- UpdateRequest updateRequest = (UpdateRequest) req;
- if (isEmpty(updateRequest)) return null;
- return new RequestWriter.ContentWriter() {
- @Override
- public void write(OutputStream os) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(os,
StandardCharsets.UTF_8);
- writeXML(updateRequest, writer);
- writer.flush();
- }
-
- @Override
- public String getContentType() {
- return ClientUtils.TEXT_XML;
- }
- };
- }
- return req.getContentWriter(ClientUtils.TEXT_XML);
- }
-
- @Override
- public Collection<ContentStream> getContentStreams(SolrRequest<?> req)
throws IOException {
- if (req instanceof UpdateRequest) {
- return null;
- }
- return req.getContentStreams();
- }
-
- @Override
- public void write(SolrRequest<?> request, OutputStream os) throws
IOException {
- if (request instanceof UpdateRequest) {
- UpdateRequest updateRequest = (UpdateRequest) request;
- BufferedWriter writer =
- new BufferedWriter(new OutputStreamWriter(os,
StandardCharsets.UTF_8));
- writeXML(updateRequest, writer);
- writer.flush();
- }
- }
-
- @Override
- public String getUpdateContentType() {
- return ClientUtils.TEXT_XML;
- }
-
- public void writeXML(UpdateRequest request, Writer writer) throws
IOException {
- List<Map<SolrInputDocument, Map<String, Object>>> getDocLists =
getDocLists(request);
-
- for (Map<SolrInputDocument, Map<String, Object>> docs : getDocLists) {
-
- if (docs != null && !docs.isEmpty()) {
- Map.Entry<SolrInputDocument, Map<String, Object>> firstDoc =
- docs.entrySet().iterator().next();
- Map<String, Object> map = firstDoc.getValue();
- Integer cw = null;
- Boolean ow = null;
- if (map != null) {
- cw = (Integer) firstDoc.getValue().get(UpdateRequest.COMMIT_WITHIN);
- ow = (Boolean) firstDoc.getValue().get(UpdateRequest.OVERWRITE);
- }
- if (ow == null) ow = true;
- int commitWithin = (cw != null && cw != -1) ? cw :
request.getCommitWithin();
- boolean overwrite = ow;
- if (commitWithin > -1 || overwrite != true) {
- writer.write(
- "<add commitWithin=\"" + commitWithin + "\" " + "overwrite=\"" +
overwrite + "\">");
- } else {
- writer.write("<add>");
- }
-
- Set<Map.Entry<SolrInputDocument, Map<String, Object>>> entries =
docs.entrySet();
- for (Map.Entry<SolrInputDocument, Map<String, Object>> entry :
entries) {
- ClientUtils.writeXML(entry.getKey(), writer);
- }
-
- writer.write("</add>");
- }
- }
-
- // Add the delete commands
- Map<String, Map<String, Object>> deleteById = request.getDeleteByIdMap();
- List<String> deleteQuery = request.getDeleteQuery();
- boolean hasDeleteById = deleteById != null && !deleteById.isEmpty();
- boolean hasDeleteByQuery = deleteQuery != null && !deleteQuery.isEmpty();
- if (hasDeleteById || hasDeleteByQuery) {
- if (request.getCommitWithin() > 0) {
- writer
- .append("<delete commitWithin=\"")
- .append(String.valueOf(request.getCommitWithin()))
- .append("\">");
- } else {
- writer.append("<delete>");
- }
- if (hasDeleteById) {
- for (Map.Entry<String, Map<String, Object>> entry :
deleteById.entrySet()) {
- writer.append("<id");
- Map<String, Object> map = entry.getValue();
- if (map != null) {
- Long version = (Long) map.get(UpdateRequest.VER);
- String route = (String) map.get(ShardParams._ROUTE_);
- if (version != null) {
- writer.append("
version=\"").append(String.valueOf(version)).append('"');
- }
-
- if (route != null) {
- writer.append(" _route_=\"").append(route).append('"');
- }
- }
- writer.append(">");
-
- XML.escapeCharData(entry.getKey(), writer);
- writer.append("</id>");
- }
- }
- if (hasDeleteByQuery) {
- for (String q : deleteQuery) {
- writer.append("<query>");
- XML.escapeCharData(q, writer);
- writer.append("</query>");
- }
- }
- writer.append("</delete>");
- }
- }
-
- private List<Map<SolrInputDocument, Map<String, Object>>>
getDocLists(UpdateRequest request) {
- List<Map<SolrInputDocument, Map<String, Object>>> docLists = new
ArrayList<>();
- Map<SolrInputDocument, Map<String, Object>> docList = null;
- if (request.getDocumentsMap() != null) {
-
- Boolean lastOverwrite = true;
- Integer lastCommitWithin = -1;
-
- Map<SolrInputDocument, Map<String, Object>> documents =
request.getDocumentsMap();
- for (Map.Entry<SolrInputDocument, Map<String, Object>> entry :
documents.entrySet()) {
- Map<String, Object> map = entry.getValue();
- Boolean overwrite = null;
- Integer commitWithin = null;
- if (map != null) {
- overwrite = (Boolean) entry.getValue().get(UpdateRequest.OVERWRITE);
- commitWithin = (Integer)
entry.getValue().get(UpdateRequest.COMMIT_WITHIN);
- }
- if (!Objects.equals(overwrite, lastOverwrite)
- || !Objects.equals(commitWithin, lastCommitWithin)
- || docLists.isEmpty()) {
- docList = new LinkedHashMap<>();
- docLists.add(docList);
- }
- docList.put(entry.getKey(), entry.getValue());
- lastCommitWithin = commitWithin;
- lastOverwrite = overwrite;
- }
- }
-
- Iterator<SolrInputDocument> docIterator = request.getDocIterator();
- if (docIterator != null) {
- docList = new LinkedHashMap<>();
- docLists.add(docList);
- while (docIterator.hasNext()) {
- SolrInputDocument doc = docIterator.next();
- if (doc != null) {
- docList.put(doc, null);
- }
- }
- }
-
- return docLists;
- }
+ // This class is intentionally left in blank in Solr 9.
+ // This is to ensure backward compatibility of SolrJ 9 minor versions.
+ // In Solr 10, the parent class RequestWriter is made abstract and all XML
specific
+ // code is moved into this class.
}
diff --git
a/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
index 9c606a92b04..aa26db0e607 100644
---
a/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
+++
b/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
@@ -16,14 +16,26 @@
*/
package org.apache.solr.client.solrj.request;
+import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.util.ClientUtils;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.XML;
/**
* A RequestWriter is used to write requests to Solr.
@@ -33,7 +45,7 @@ import org.apache.solr.common.util.ContentStream;
*
* @since solr 1.4
*/
-public abstract class RequestWriter {
+public class RequestWriter {
public interface ContentWriter {
@@ -49,14 +61,37 @@ public abstract class RequestWriter {
*
org.apache.solr.client.solrj.request.RequestWriter#getContentStreams(SolrRequest)}
is then
* invoked to get content.
*/
- public abstract ContentWriter getContentWriter(SolrRequest<?> req);
+ public ContentWriter getContentWriter(SolrRequest<?> req) {
+ if (req instanceof UpdateRequest) {
+ UpdateRequest updateRequest = (UpdateRequest) req;
+ if (isEmpty(updateRequest)) return null;
+ return new RequestWriter.ContentWriter() {
+ @Override
+ public void write(OutputStream os) throws IOException {
+ OutputStreamWriter writer = new OutputStreamWriter(os,
StandardCharsets.UTF_8);
+ writeXML(updateRequest, writer);
+ writer.flush();
+ }
+
+ @Override
+ public String getContentType() {
+ return ClientUtils.TEXT_XML;
+ }
+ };
+ }
+ return req.getContentWriter(ClientUtils.TEXT_XML);
+ }
/**
* @deprecated Use {@link #getContentWriter(SolrRequest)}.
*/
@Deprecated
- public abstract Collection<ContentStream> getContentStreams(SolrRequest<?>
req)
- throws IOException;
+ public Collection<ContentStream> getContentStreams(SolrRequest<?> req)
throws IOException {
+ if (req instanceof UpdateRequest) {
+ return null;
+ }
+ return req.getContentStreams();
+ }
protected boolean isEmpty(UpdateRequest updateRequest) {
return isNull(updateRequest.getDocuments())
@@ -70,9 +105,19 @@ public abstract class RequestWriter {
return req.getPath();
}
- public abstract void write(SolrRequest<?> request, OutputStream os) throws
IOException;
+ public void write(SolrRequest<?> request, OutputStream os) throws
IOException {
+ if (request instanceof UpdateRequest) {
+ UpdateRequest updateRequest = (UpdateRequest) request;
+ BufferedWriter writer =
+ new BufferedWriter(new OutputStreamWriter(os,
StandardCharsets.UTF_8));
+ writeXML(updateRequest, writer);
+ writer.flush();
+ }
+ }
- public abstract String getUpdateContentType();
+ public String getUpdateContentType() {
+ return ClientUtils.TEXT_XML;
+ }
public static class StringPayloadContentWriter implements ContentWriter {
public final String payload;
@@ -102,4 +147,128 @@ public abstract class RequestWriter {
protected boolean isNull(Map<?, ?> l) {
return l == null || l.isEmpty();
}
+
+ public void writeXML(UpdateRequest request, Writer writer) throws
IOException {
+ List<Map<SolrInputDocument, Map<String, Object>>> getDocLists =
getDocLists(request);
+
+ for (Map<SolrInputDocument, Map<String, Object>> docs : getDocLists) {
+
+ if (docs != null && !docs.isEmpty()) {
+ Map.Entry<SolrInputDocument, Map<String, Object>> firstDoc =
+ docs.entrySet().iterator().next();
+ Map<String, Object> map = firstDoc.getValue();
+ Integer cw = null;
+ Boolean ow = null;
+ if (map != null) {
+ cw = (Integer) firstDoc.getValue().get(UpdateRequest.COMMIT_WITHIN);
+ ow = (Boolean) firstDoc.getValue().get(UpdateRequest.OVERWRITE);
+ }
+ if (ow == null) ow = true;
+ int commitWithin = (cw != null && cw != -1) ? cw :
request.getCommitWithin();
+ boolean overwrite = ow;
+ if (commitWithin > -1 || overwrite != true) {
+ writer.write(
+ "<add commitWithin=\"" + commitWithin + "\" " + "overwrite=\"" +
overwrite + "\">");
+ } else {
+ writer.write("<add>");
+ }
+
+ Set<Map.Entry<SolrInputDocument, Map<String, Object>>> entries =
docs.entrySet();
+ for (Map.Entry<SolrInputDocument, Map<String, Object>> entry :
entries) {
+ ClientUtils.writeXML(entry.getKey(), writer);
+ }
+
+ writer.write("</add>");
+ }
+ }
+
+ // Add the delete commands
+ Map<String, Map<String, Object>> deleteById = request.getDeleteByIdMap();
+ List<String> deleteQuery = request.getDeleteQuery();
+ boolean hasDeleteById = deleteById != null && !deleteById.isEmpty();
+ boolean hasDeleteByQuery = deleteQuery != null && !deleteQuery.isEmpty();
+ if (hasDeleteById || hasDeleteByQuery) {
+ if (request.getCommitWithin() > 0) {
+ writer
+ .append("<delete commitWithin=\"")
+ .append(String.valueOf(request.getCommitWithin()))
+ .append("\">");
+ } else {
+ writer.append("<delete>");
+ }
+ if (hasDeleteById) {
+ for (Map.Entry<String, Map<String, Object>> entry :
deleteById.entrySet()) {
+ writer.append("<id");
+ Map<String, Object> map = entry.getValue();
+ if (map != null) {
+ Long version = (Long) map.get(UpdateRequest.VER);
+ String route = (String) map.get(ShardParams._ROUTE_);
+ if (version != null) {
+ writer.append("
version=\"").append(String.valueOf(version)).append('"');
+ }
+
+ if (route != null) {
+ writer.append(" _route_=\"").append(route).append('"');
+ }
+ }
+ writer.append(">");
+
+ XML.escapeCharData(entry.getKey(), writer);
+ writer.append("</id>");
+ }
+ }
+ if (hasDeleteByQuery) {
+ for (String q : deleteQuery) {
+ writer.append("<query>");
+ XML.escapeCharData(q, writer);
+ writer.append("</query>");
+ }
+ }
+ writer.append("</delete>");
+ }
+ }
+
+ private List<Map<SolrInputDocument, Map<String, Object>>>
getDocLists(UpdateRequest request) {
+ List<Map<SolrInputDocument, Map<String, Object>>> docLists = new
ArrayList<>();
+ Map<SolrInputDocument, Map<String, Object>> docList = null;
+ if (request.getDocumentsMap() != null) {
+
+ Boolean lastOverwrite = true;
+ Integer lastCommitWithin = -1;
+
+ Map<SolrInputDocument, Map<String, Object>> documents =
request.getDocumentsMap();
+ for (Map.Entry<SolrInputDocument, Map<String, Object>> entry :
documents.entrySet()) {
+ Map<String, Object> map = entry.getValue();
+ Boolean overwrite = null;
+ Integer commitWithin = null;
+ if (map != null) {
+ overwrite = (Boolean) entry.getValue().get(UpdateRequest.OVERWRITE);
+ commitWithin = (Integer)
entry.getValue().get(UpdateRequest.COMMIT_WITHIN);
+ }
+ if (!Objects.equals(overwrite, lastOverwrite)
+ || !Objects.equals(commitWithin, lastCommitWithin)
+ || docLists.isEmpty()) {
+ docList = new LinkedHashMap<>();
+ docLists.add(docList);
+ }
+ docList.put(entry.getKey(), entry.getValue());
+ lastCommitWithin = commitWithin;
+ lastOverwrite = overwrite;
+ }
+ }
+
+ Iterator<SolrInputDocument> docIterator = request.getDocIterator();
+ if (docIterator != null) {
+ docList = new LinkedHashMap<>();
+ docLists.add(docList);
+ while (docIterator.hasNext()) {
+ SolrInputDocument doc = docIterator.next();
+ if (doc != null) {
+ docList.put(doc, null);
+ }
+ }
+ }
+
+ return docLists;
+ }
}