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/10/23 01:35:40 UTC

lucene-solr:master: SOLR-11522: Moved the _get methods to a separate interafce and keep MapWriter clean

Repository: lucene-solr
Updated Branches:
  refs/heads/master d799fd53c -> 576d28f64


SOLR-11522: Moved the _get methods to a separate interafce and keep MapWriter clean


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

Branch: refs/heads/master
Commit: 576d28f643a89de832b59a783ce729402d70fb9f
Parents: d799fd5
Author: noble <no...@apache.org>
Authored: Tue Oct 23 12:30:54 2018 +1100
Committer: noble <no...@apache.org>
Committed: Tue Oct 23 12:35:23 2018 +1100

----------------------------------------------------------------------
 .../java/org/apache/solr/common/MapWriter.java  | 48 +----------
 .../org/apache/solr/common/NavigableObject.java | 89 ++++++++++++++++++++
 .../java/org/apache/solr/common/util/Utils.java | 12 +--
 3 files changed, 96 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/576d28f6/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
index d2f14d8..d6c9efb 100644
--- a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
@@ -23,7 +23,6 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.function.BiConsumer;
 import java.util.function.BiPredicate;
 
 import org.apache.solr.common.util.Utils;
@@ -33,7 +32,7 @@ import org.apache.solr.common.util.Utils;
  * This avoids creating map instances and is supposed to be memory efficient.
  * If the entries are primitives, unnecessary boxing is also avoided.
  */
-public interface MapWriter extends MapSerializable {
+public interface MapWriter extends MapSerializable , NavigableObject {
 
   default String jsonStr(){
     return Utils.toJSONString(this);
@@ -81,51 +80,6 @@ public interface MapWriter extends MapSerializable {
 
   void writeMap(EntryWriter ew) throws IOException;
 
-  /**Get a child object value using json path
-   *
-   * @param path the full path to that object such as a/b/c[4]/d etc
-   * @param def the default
-   * @return the found value or default
-   */
-  default Object _get(String path, Object def) {
-    Object v = Utils.getObjectByPath(this, false, path);
-    return v == null ? def : v;
-  }
-
-  default String _getStr(String path, String def) {
-    Object v = Utils.getObjectByPath(this, false, path);
-    return v == null ? def : String.valueOf(v);
-  }
-
-  default void _forEachEntry(String path, BiConsumer fun) {
-    Utils.forEachMapEntry(this, path, fun);
-  }
-
-  default void _forEachEntry(List<String> path, BiConsumer fun) {
-    Utils.forEachMapEntry(this, path, fun);
-  }
-
-  default void _forEachEntry(BiConsumer fun) {
-    Utils.forEachMapEntry(this, fun);
-  }
-
-  /**
-   * Get a child object value using json path
-   *
-   * @param path the full path to that object such as ["a","b","c[4]","d"] etc
-   * @param def  the default
-   * @return the found value or default
-   */
-  default Object _get(List<String> path, Object def) {
-    Object v = Utils.getObjectByPath(this, false, path);
-    return v == null ? def : v;
-  }
-
-  default String _getStr(List<String> path, String def) {
-    Object v = Utils.getObjectByPath(this, false, path);
-    return v == null ? def : String.valueOf(v);
-  }
-
   /**
    * An interface to push one entry at a time to the output.
    * The order of the keys is not defined, but we assume they are distinct -- don't call {@code put} more than once

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/576d28f6/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java b/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java
new file mode 100644
index 0000000..be50a17
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java
@@ -0,0 +1,89 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.function.BiConsumer;
+
+import org.apache.solr.common.util.Utils;
+
+/**This class contains helper methods for navigating deeply nested Objects. Keep in mind that
+ * it may be expensive depending on the underlying implementation. each level needs an extra lookup
+ * and the lookup may be as expensive as O(log(n)) to O(o) depending on the underlying impl
+ *
+ */
+public interface NavigableObject {
+  /**Get a child object value using json path. This usually ends up in String split operations
+   *  use a list of strings where performance is important
+   *
+   * @param path the full path to that object such as a/b/c[4]/d etc
+   * @param def the default
+   * @return the found value or default
+   */
+  default Object _get(String path, Object def) {
+    Object v = Utils.getObjectByPath(this, false, path);
+    return v == null ? def : v;
+  }
+
+  /**get the value as a String. useful in tests
+   *
+   * @param path the full path
+   * @param def default value
+   */
+  default String _getStr(String path, String def) {
+    Object v = Utils.getObjectByPath(this, false, path);
+    return v == null ? def : String.valueOf(v);
+  }
+
+  /**Iterate through the entries of a navigable Object at a certain path
+   * @param path the json path
+   */
+  default void _forEachEntry(String path, BiConsumer fun) {
+    Utils.forEachMapEntry(this, path, fun);
+  }
+
+  /**Iterate through the entries of a navigable Object at a certain path
+   * @param path the json path
+   */
+  default void _forEachEntry(List<String> path, BiConsumer fun) {
+    Utils.forEachMapEntry(this, path, fun);
+  }
+
+  /**Iterate through each entry in this object
+   */
+  default void _forEachEntry(BiConsumer fun) {
+    Utils.forEachMapEntry(this, fun);
+  }
+
+  /**
+   * Get a child object value using json path
+   *
+   * @param path the full path to that object such as ["a","b","c[4]","d"] etc
+   * @param def  the default
+   * @return the found value or default
+   */
+  default Object _get(List<String> path, Object def) {
+    Object v = Utils.getObjectByPath(this, false, path);
+    return v == null ? def : v;
+  }
+
+  default String _getStr(List<String> path, String def) {
+    Object v = Utils.getObjectByPath(this, false, path);
+    return v == null ? def : String.valueOf(v);
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/576d28f6/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 bdfd03e..a1b34a2 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
@@ -107,14 +107,14 @@ public class Utils {
     return mutable ? copy : Collections.unmodifiableMap(copy);
   }
 
-  public static void forEachMapEntry(MapWriter mw, String path, BiConsumer fun) {
-    Object o = Utils.getObjectByPath(mw, false, path);
-    forEachMapEntry(o, fun);
+  public static void forEachMapEntry(Object o, String path, BiConsumer fun) {
+    Object val = Utils.getObjectByPath(o, false, path);
+    forEachMapEntry(val, fun);
   }
 
-  public static void forEachMapEntry(MapWriter mw, List<String> path, BiConsumer fun) {
-    Object o = Utils.getObjectByPath(mw, false, path);
-    forEachMapEntry(o, fun);
+  public static void forEachMapEntry(Object o, List<String> path, BiConsumer fun) {
+    Object val = Utils.getObjectByPath(o, false, path);
+    forEachMapEntry(val, fun);
   }
 
   public static void forEachMapEntry(Object o, BiConsumer fun) {