lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From no...@apache.org
Subject [1/2] lucene-solr:branch_7x: SOLR-12455: Refactor JSON serialization code into SolrJ package
Date Sat, 09 Jun 2018 13:42:09 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x 56559f089 -> fef87d718


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fef87d71/solr/solrj/src/java/org/apache/solr/common/util/WriteableValue.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/WriteableValue.java b/solr/solrj/src/java/org/apache/solr/common/util/WriteableValue.java
new file mode 100644
index 0000000..82e7d00
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/util/WriteableValue.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.common.util;
+
+import java.io.IOException;
+
+import org.apache.solr.common.util.JavaBinCodec.ObjectResolver;
+
+public abstract class WriteableValue implements ObjectResolver {
+  public abstract void write(String name, TextWriter writer) throws IOException;
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fef87d71/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 60c84b7..30506c2 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
@@ -52,6 +52,7 @@ import org.apache.solr.common.cloud.ZkStateReader;
 import org.apache.solr.common.params.CollectionParams;
 import org.apache.solr.common.params.CollectionParams.CollectionAction;
 import org.apache.solr.common.params.SolrParams;
+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;
@@ -2525,7 +2526,7 @@ public void testUtilizeNodeFailure2() throws Exception {
     StringWriter writer = new StringWriter();
     NamedList<Object> val = new NamedList<>();
     val.add("violations", violations);
-    JSONWriter.write (writer, true, JSONWriter.JSON_NL_MAP, val);
+    JSONWriter.write (writer, true, JsonTextWriter.JSON_NL_MAP, val);
 
     assertEquals(2l,
         Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/NRT"));

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fef87d71/solr/solrj/src/test/org/apache/solr/common/util/TestFastWriter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestFastWriter.java b/solr/solrj/src/test/org/apache/solr/common/util/TestFastWriter.java
new file mode 100644
index 0000000..5b240fe
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestFastWriter.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.common.util;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.lucene.util.LuceneTestCase;
+
+
+class MemWriter extends FastWriter {
+  public List<char[]> buffers = new LinkedList<>();
+
+  Random r;
+  public MemWriter(char[] tempBuffer, Random r) {
+    super(null, tempBuffer, 0);
+    this.r = r;
+  }
+
+  @Override
+  public void flush(char[] arr, int offset, int len) throws IOException {
+    if (arr == buf && offset==0 && len==buf.length) {
+      buffers.add(buf);  // steal the buffer
+      buf = new char[r.nextInt(9000)+1];
+    } else if (len > 0) {
+      char[] newBuf = new char[len];
+      System.arraycopy(arr, offset, newBuf, 0, len);
+      buffers.add(newBuf);
+    }
+  }
+
+  @Override
+  public void flush(String str, int offset, int len) throws IOException {
+    if (len == 0) return;
+    buffers.add( str.substring(offset, offset+len).toCharArray() );
+  }
+}
+
+
+
+public class TestFastWriter extends LuceneTestCase {
+
+  Random rand;
+  char[] arr;
+  String s;
+
+  public void testRandomWrites() throws Exception {
+    rand = random();
+
+    arr = new char[20000];
+    for (int i=0; i<arr.length; i++) {
+      arr[i] = (char)rand.nextInt();
+    }
+    s = new String(arr);
+
+    for (int i=0; i<1000; i++) {
+      doRandomWrites();
+    }
+  }
+
+
+  public void doRandomWrites() throws Exception {
+    int bufSize = ( rand.nextBoolean() ? rand.nextInt(10) : rand.nextInt(20000) )+1;
+    MemWriter out = new MemWriter(new char[bufSize], rand);
+
+    int hash = 0;
+    long written = 0;
+    int iter = rand.nextInt(20)+1;
+    for (int i=0; i<iter; i++) {
+      int which = rand.nextInt(3);
+
+
+      int off = rand.nextInt(arr.length);
+      int len = off < arr.length ? rand.nextInt(arr.length - off) : 0;
+
+
+
+      if (which == 0) {
+        out.write(arr, off, len);
+      } else if (which == 1) {
+        out.write(s, off, len);
+      } else {
+        len = 1;
+        out.write(arr[off]);
+      }
+
+      hash = incHash(hash, arr, off, len);
+      written += len;
+    }
+
+    out.close();
+
+    int hash2 = 0;
+    for (char[] buffer : out.buffers) {
+      hash2 = incHash(hash2, buffer, 0, buffer.length);
+    }
+
+    assertEquals(hash, hash2);
+  }
+
+
+  public int incHash(int hash, char[] arr, int off, int len) {
+    for (int i=off; i<off+len; i++) {
+      hash = hash * 31 + arr[i];
+    }
+    return hash;
+  }
+}
+

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fef87d71/solr/solrj/src/test/org/apache/solr/common/util/TestSolrJsonWriter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestSolrJsonWriter.java b/solr/solrj/src/test/org/apache/solr/common/util/TestSolrJsonWriter.java
new file mode 100644
index 0000000..3dd2cdd
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestSolrJsonWriter.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.common.util;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.IteratorWriter;
+import org.apache.solr.common.MapWriter;
+
+public class TestSolrJsonWriter  extends SolrTestCaseJ4 {
+  public void test() throws IOException {
+    StringWriter writer = new StringWriter();
+
+    Map<String, Object> map = new HashMap<>();
+    map.put("k1","v1");
+    map.put("k2",1);
+    map.put("k3",false);
+    map.put("k4",Utils.makeMap("k41", "v41", "k42","v42"));
+    map.put("k5", (MapWriter) ew -> {
+      ew.put("k61","v61");
+      ew.put("k62","v62");
+      ew.put("k63", (IteratorWriter) iw -> iw.add("v631")
+          .add("v632"));
+    });
+
+    new SolrJSONWriter(writer)
+        .setIndent(true)
+        .writeObj(map)
+        .close();
+    Object o = Utils.fromJSONString(writer.toString());
+    assertEquals("v1", Utils.getObjectByPath(o, true, "k1"));
+    assertEquals(1l, Utils.getObjectByPath(o, true, "k2"));
+    assertEquals(Boolean.FALSE, Utils.getObjectByPath(o, true, "k3"));
+    assertEquals("v41", Utils.getObjectByPath(o, true, "k4/k41"));
+    assertEquals("v42", Utils.getObjectByPath(o, true, "k4/k42"));
+    assertEquals("v61", Utils.getObjectByPath(o, true, "k5/k61"));
+    assertEquals("v62", Utils.getObjectByPath(o, true, "k5/k62"));
+    assertEquals("v631", Utils.getObjectByPath(o, true, "k5/k63[0]"));
+    assertEquals("v632", Utils.getObjectByPath(o, true, "k5/k63[1]"));
+  }
+}


Mime
View raw message