You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by no...@apache.org on 2018/06/10 04:05:15 UTC

lucene-solr:master: SOLR-12455: Use the new serializarion in V2 and schema requests

Repository: lucene-solr
Updated Branches:
  refs/heads/master a06256cce -> b7e9fb43d


SOLR-12455: Use the new serializarion in V2 and schema requests


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/b7e9fb43
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/b7e9fb43
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/b7e9fb43

Branch: refs/heads/master
Commit: b7e9fb43d0f08b68ffa6050f62351dd57495ea87
Parents: a06256c
Author: Noble Paul <no...@apache.org>
Authored: Sun Jun 10 14:05:00 2018 +1000
Committer: Noble Paul <no...@apache.org>
Committed: Sun Jun 10 14:05:00 2018 +1000

----------------------------------------------------------------------
 .../solr/client/solrj/request/V2Request.java    |  7 +-
 .../request/schema/AbstractSchemaRequest.java   |  8 --
 .../solrj/request/schema/SchemaRequest.java     | 81 +-------------------
 .../java/org/apache/solr/common/util/Utils.java | 12 +++
 .../solrj/cloud/autoscaling/TestPolicy.java     | 11 ++-
 5 files changed, 24 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b7e9fb43/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java
index c854833..c858f7b 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/V2Request.java
@@ -19,7 +19,6 @@ package org.apache.solr.client.solrj.request;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -68,17 +67,13 @@ public class V2Request extends SolrRequest<V2Response> implements MapWriter {
     if (payload instanceof String) {
       return new RequestWriter.StringPayloadContentWriter((String) payload, JSON_MIME);
     }
-    if (payload instanceof Map) {
-      payload = Utils.getDeepCopy((Map) payload, 5);
-    }
     return new RequestWriter.ContentWriter() {
       @Override
       public void write(OutputStream os) throws IOException {
         if (useBinary) {
           new JavaBinCodec().marshal(payload, os);
         } else {
-          byte[] b = Utils.toJSON(payload);
-          os.write(b);
+          Utils.toJSON(payload, os, false);
         }
       }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b7e9fb43/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/AbstractSchemaRequest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/AbstractSchemaRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/AbstractSchemaRequest.java
index 0f49530..b20a570 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/AbstractSchemaRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/AbstractSchemaRequest.java
@@ -16,13 +16,9 @@
  */
 package org.apache.solr.client.solrj.request.schema;
 
-import java.io.IOException;
-import java.util.Collection;
-
 import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.SolrResponse;
 import org.apache.solr.common.params.SolrParams;
-import org.apache.solr.common.util.ContentStream;
 
 public abstract class AbstractSchemaRequest<T extends SolrResponse> extends SolrRequest<T> {
   private SolrParams params = null;
@@ -41,8 +37,4 @@ public abstract class AbstractSchemaRequest<T extends SolrResponse> extends Solr
     return params;
   }
 
-  @Override
-  public Collection<ContentStream> getContentStreams() throws IOException {
-    return null;
-  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b7e9fb43/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/SchemaRequest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/SchemaRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/SchemaRequest.java
index 6c63bb4..6add379 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/SchemaRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/schema/SchemaRequest.java
@@ -18,10 +18,6 @@ package org.apache.solr.client.solrj.request.schema;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -31,11 +27,8 @@ import org.apache.solr.client.solrj.request.RequestWriter;
 import org.apache.solr.client.solrj.response.schema.SchemaResponse;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.params.SolrParams;
-import org.apache.solr.common.util.ContentStream;
-import org.apache.solr.common.util.ContentStreamBase;
 import org.apache.solr.common.util.NamedList;
-import org.noggit.CharArr;
-import org.noggit.JSONWriter;
+import org.apache.solr.common.util.Utils;
 
 /**
  * <p>This class offers access to the operations exposed by the Solr Schema API.</p>
@@ -710,22 +703,12 @@ public class SchemaRequest extends AbstractSchemaRequest<SchemaResponse> {
     protected abstract NamedList<Object> getRequestParameters();
 
     @Override
-    @Deprecated
-    public Collection<ContentStream> getContentStreams() throws IOException {
-      CharArr json = new CharArr();
-      new SchemaRequestJSONWriter(json).write(getRequestParameters());
-      return Collections.singletonList(new ContentStreamBase.StringStream(json.toString()));
-    }
-
-    @Override
     public RequestWriter.ContentWriter getContentWriter(String expectedType) {
       return new RequestWriter.ContentWriter() {
         @Override
         public void write(OutputStream os) throws IOException {
-          //TODO :  find a way to do streaming write
-          CharArr json = new CharArr();
-          new SchemaRequestJSONWriter(json).write(getRequestParameters());
-          os.write(json.toString().getBytes(StandardCharsets.UTF_8));
+          Utils.toJSON(getRequestParameters(),
+              os, false);
         }
 
         @Override
@@ -794,62 +777,4 @@ public class SchemaRequest extends AbstractSchemaRequest<SchemaResponse> {
     }
   }
 
-  /**
-   * Simple extension of the noggit JSONWriter used to be write objects
-   * of type {@link NamedList}.
-   * Writing of objects of the type {@link NamedList} is done in very much
-   * the same way as for a map.
-   * <p>
-   * This writer is particularly useful when doing multiple update requests.
-   * In update Schema API there can be done multiple add operations of the same
-   * type (e.g. : add-field-type), they are grouped in an associative array, even though
-   * this can't be done normally in JSON. For such a use-case, the {@link NamedList}
-   * objects are particularly useful because they can group key-value mappings
-   * having the same values for the keys (unlike maps).
-   */
-  private static class SchemaRequestJSONWriter extends JSONWriter {
-    public SchemaRequestJSONWriter(CharArr out, int indentSize) {
-      super(out, indentSize);
-    }
-
-    public SchemaRequestJSONWriter(CharArr out) {
-      super(out);
-    }
-
-    public void write(Object o) {
-      if (o instanceof NamedList) {
-        write((NamedList) o);
-      } else super.write(o);
-    }
-
-    /**
-     * @see #write(Map)
-     */
-    @SuppressWarnings("unchecked")
-    public void write(NamedList<?> val) {
-      this.startObject();
-      int sz = val.size();
-      boolean first = true;
-      Iterator i$ = val.iterator();
-
-      while (i$.hasNext()) {
-        Map.Entry<String, ?> entry = (Map.Entry<String, ?>) i$.next();
-        if (first) {
-          first = false;
-        } else {
-          this.writeValueSeparator();
-        }
-
-        if (sz > 1) {
-          this.indent();
-        }
-
-        this.writeString(entry.getKey());
-        this.writeNameSeparator();
-        this.write(entry.getValue());
-      }
-
-      this.endObject();
-    }
-  }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b7e9fb43/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
index 70c9d81..ca76c4c 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
@@ -19,6 +19,8 @@ package org.apache.solr.common.util;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.UnsupportedEncodingException;
@@ -63,6 +65,7 @@ import org.noggit.ObjectBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.Collections.unmodifiableList;
 import static java.util.Collections.unmodifiableSet;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
@@ -135,6 +138,15 @@ public class Utils {
     return mutable ? result : result instanceof Set ? unmodifiableSet((Set) result) : unmodifiableList((List) result);
   }
 
+  public static void toJSON(Object o, OutputStream os, boolean indent) throws IOException {
+    OutputStreamWriter writer = new OutputStreamWriter(os, UTF_8);
+    new SolrJSONWriter(writer)
+        .setIndent(indent)
+        .writeObj(o)
+        .close();
+    writer.flush();
+  }
+
   public static byte[] toJSON(Object o) {
     if(o == null) return new byte[0];
     CharArr out = new CharArr();

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b7e9fb43/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
index 30506c2..aedc0d5 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
@@ -56,6 +56,7 @@ import org.apache.solr.common.util.JsonTextWriter;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.ObjectCache;
 import org.apache.solr.common.util.Pair;
+import org.apache.solr.common.util.SolrJSONWriter;
 import org.apache.solr.common.util.TimeSource;
 import org.apache.solr.common.util.Utils;
 import org.apache.solr.common.util.ValidatingJsonMap;
@@ -2526,14 +2527,18 @@ public void testUtilizeNodeFailure2() throws Exception {
     StringWriter writer = new StringWriter();
     NamedList<Object> val = new NamedList<>();
     val.add("violations", violations);
+    new SolrJSONWriter(writer)
+        .writeObj(val)
+        .close();
     JSONWriter.write (writer, true, JsonTextWriter.JSON_NL_MAP, val);
 
+    Object root = Utils.fromJSONString(writer.toString());
     assertEquals(2l,
-        Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/NRT"));
+        Utils.getObjectByPath(root, true, "violations[0]/violation/replica/NRT"));
     assertEquals(0l,
-        Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/PULL"));
+        Utils.getObjectByPath(root, true, "violations[0]/violation/replica/PULL"));
     assertEquals(0l,
-        Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/TLOG"));
+        Utils.getObjectByPath(root, true, "violations[0]/violation/replica/TLOG"));
 
   }