You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2021/03/10 09:59:05 UTC

[lucene] 01/01: SOLR-13488: refactoring

This is an automated email from the ASF dual-hosted git repository.

dweiss pushed a commit to branch jira/SOLR-13488
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit 9d1255a5838493f386754ba1c29a9d7781d0f329
Author: Noble Paul <no...@apache.org>
AuthorDate: Mon May 27 15:21:10 2019 +1000

    SOLR-13488: refactoring
---
 .../org/apache/solr/common/IteratorWriter.java     |  19 ++++
 .../src/java/org/apache/solr/common/MapWriter.java |  70 +++++++++++++
 .../org/apache/solr/common/NavigableObject.java    |   8 ++
 .../org/apache/solr/common/util/NamedList.java     |  15 +++
 .../java/org/apache/solr/common/util/StrUtils.java |   7 ++
 .../java/org/apache/solr/common/util/Utils.java    | 110 +++++++++------------
 6 files changed, 166 insertions(+), 63 deletions(-)

diff --git a/solr/solrj/src/java/org/apache/solr/common/IteratorWriter.java b/solr/solrj/src/java/org/apache/solr/common/IteratorWriter.java
index ec11c78..00a68564 100644
--- a/solr/solrj/src/java/org/apache/solr/common/IteratorWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/common/IteratorWriter.java
@@ -90,4 +90,23 @@ public interface IteratorWriter {
     }
     return l;
   }
+
+  default Object __get(int idx) {
+    Object[] result = new Object[1];
+    try {
+      writeIter(new IteratorWriter.ItemWriter() {
+        int i = -1;
+
+        @Override
+        public IteratorWriter.ItemWriter add(Object o) {
+          if (++i == idx || idx == -1) result[0] = o;
+          return this;
+        }
+      });
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+    return result[0];
+
+  }
 }
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 926cf4c..0af0192 100644
--- a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
@@ -19,6 +19,7 @@ package org.apache.solr.common;
 
 
 import java.io.IOException;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -40,6 +41,75 @@ public interface MapWriter extends MapSerializable , NavigableObject {
   }
 
   @Override
+  default Object __getVal(String key) {
+    Object[] result = new Object[1];
+    try {
+      writeMap(new EntryWriter() {
+        @Override
+        public EntryWriter put(CharSequence k, Object v) {
+          if (result[0] != null) return this;
+          if (k.equals(key)) result[0] = v;
+          return this;
+        }
+      });
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+
+    return result[0];
+  }
+
+  class MapWriterEntry<V> extends AbstractMap.SimpleEntry<CharSequence, V> implements MapWriter, Map.Entry<CharSequence, V> {
+    public MapWriterEntry(CharSequence key, V value) {
+      super(key, value);
+    }
+
+    @Override
+    public void writeMap(EntryWriter ew) throws IOException {
+      ew.put("key", getKey());
+      ew.put("value", getValue());
+    }
+
+  }
+
+  @Override
+  default MapWriterEntry __getVal(int idx) {
+    class EW implements EntryWriter {
+      private CharSequence key;
+      private Object val;
+      private final int idx;
+      int count = -1;
+
+      public EW(int idx) {
+        this.idx = idx;
+        count = -1;
+      }
+
+      @Override
+      public EntryWriter put(CharSequence k, Object v) {
+        if (idx == -1) {
+          key = k;
+          val = v;
+          return this;
+        }
+        if (key != null) return this;
+        if (++count == idx) {
+          key = k;
+          val = v;
+        }
+        return this;
+      }
+    }
+    try {
+      EW ew = new EW(idx);
+      writeMap(ew);
+      return ew.key != null ? new MapWriterEntry<>(ew.key.toString(), ew.val): null;
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
   default Map toMap(Map<String, Object> map) {
     try {
       writeMap(new EntryWriter() {
diff --git a/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java b/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java
index ccef7e2..7a1b1a3 100644
--- a/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java
+++ b/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java
@@ -50,6 +50,14 @@ public interface NavigableObject {
     return v == null ? def : String.valueOf(v);
   }
 
+  /**Get the value for a given key
+   */
+  Object __getVal(String key);
+
+  /** Get the [key, value] tuple at the index . -1 = last entry
+   */
+  MapWriter.MapWriterEntry __getVal(int idx);
+
   /**Iterate through the entries of a navigable Object at a certain path
    * @param path the json path
    */
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
index ec40f97..65119bf 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
@@ -849,4 +849,19 @@ public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entry
       action.accept(getName(i), getVal(i));
     }
   }
+
+  @Override
+  public Object __getVal(String key) {
+    return get(key);
+  }
+
+  @Override
+  public MapWriterEntry __getVal(int idx) {
+    if (idx == -1 && size() > 0) idx = size() - 1;
+    if (idx >= 0) {
+      return idx < this.size() ? new MapWriter.MapWriterEntry<>(getName(idx), getVal(idx)) : null;
+    }
+    return null;
+
+  }
 }
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java b/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
index c0b19f5..79c6743 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
@@ -326,4 +326,11 @@ public class StrUtils {
   public static String formatString(String pattern, Object... args)  {
     return new MessageFormat(pattern, Locale.ROOT).format(args);
   }
+
+  public static void main(String[] args) {
+    List<String> x = splitSmart("a/b['x/y/z' == 'k']/c", '/');
+    for (String s : x) {
+      System.out.println(s);
+    }
+  }
 }
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 4a8d987..b0eb482 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
@@ -30,7 +30,6 @@ import java.net.URL;
 import java.net.URLDecoder;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
-import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -61,6 +60,7 @@ import org.apache.solr.common.IteratorWriter;
 import org.apache.solr.common.LinkedHashMapWriter;
 import org.apache.solr.common.MapWriter;
 import org.apache.solr.common.MapWriterMap;
+import org.apache.solr.common.NavigableObject;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SpecProvider;
 import org.apache.solr.common.cloud.SolrZkClient;
@@ -365,6 +365,19 @@ public class Utils {
     List<String> parts = StrUtils.splitSmart(hierarchy, '/', true);
     return setObjectByPath(root, parts, value);
   }
+  static class PathParser {
+
+    PathParser(String s){
+
+
+    }
+    String path, value;
+    Operator op;
+    enum Operator {
+      EQUAL,NONE, NOT_EQUAL
+    }
+
+  }
 
   public static boolean setObjectByPath(Object root, List<String> hierarchy, Object value) {
     if (root == null) return false;
@@ -373,11 +386,18 @@ public class Utils {
     for (int i = 0; i < hierarchy.size(); i++) {
       int idx = -2; //-1 means append to list, -2 means not found
       String s = hierarchy.get(i);
-      if (s.endsWith("]")) {
-        Matcher matcher = ARRAY_ELEMENT_INDEX.matcher(s);
-        if (matcher.find()) {
-          s = matcher.group(1);
-          idx = Integer.parseInt(matcher.group(2));
+      String strim = s.trim();
+      if (strim.charAt(0) == '[' && strim.charAt(strim.length()-1) == ']') {
+        int singleQuote = 0;
+        for (int j=0;j<strim.length();j++){
+          if(strim.charAt(j) == '\'') singleQuote++;
+        }
+        if(singleQuote == 0) {
+          Matcher matcher = ARRAY_ELEMENT_INDEX.matcher(s);
+          if (matcher.find()) {
+            s = matcher.group(1);
+            idx = Integer.parseInt(matcher.group(2));
+          }
         }
       }
       if (i < hierarchy.size() - 1) {
@@ -457,7 +477,7 @@ public class Utils {
         if (val == null) return null;
         if (idx > -1) {
           if (val instanceof IteratorWriter) {
-            val = getValueAt((IteratorWriter) val, idx);
+            val = ((IteratorWriter) val).__get(idx);
           } else {
             List l = (List) val;
             val = idx < l.size() ? l.get(idx) : null;
@@ -474,68 +494,32 @@ public class Utils {
   }
 
 
-  private static Object getValueAt(IteratorWriter iteratorWriter, int idx) {
-    Object[] result = new Object[1];
-    try {
-      iteratorWriter.writeIter(new IteratorWriter.ItemWriter() {
-        int i = -1;
-
-        @Override
-        public IteratorWriter.ItemWriter add(Object o) {
-          ++i;
-          if (i > idx) return this;
-          if (i == idx) result[0] = o;
-          return this;
-        }
-      });
-    } catch (IOException e) {
-      throw new RuntimeException(e);
-    }
-    return result[0];
-
-  }
-
-  static class MapWriterEntry<V> extends AbstractMap.SimpleEntry<CharSequence, V> implements MapWriter, Map.Entry<CharSequence, V> {
-    MapWriterEntry(CharSequence key, V value) {
-      super(key, value);
-    }
-
-    @Override
-    public void writeMap(EntryWriter ew) throws IOException {
-      ew.put("key", getKey());
-      ew.put("value", getValue());
-    }
-
-  }
-
   private static boolean isMapLike(Object o) {
-    return o instanceof Map || o instanceof NamedList || o instanceof MapWriter;
+    return o instanceof Map || o instanceof NavigableObject;
   }
 
   private static Object getVal(Object obj, String key, int idx) {
-    if (obj instanceof MapWriter) {
-      Object[] result = new Object[1];
-      try {
-        ((MapWriter) obj).writeMap(new MapWriter.EntryWriter() {
-          int count = -1;
-          @Override
-          public MapWriter.EntryWriter put(CharSequence k, Object v) {
-            if (result[0] != null) return this;
-            if (idx < 0) {
-              if (k.equals(key)) result[0] = v;
-            } else {
-              if (++count == idx) result[0] = new MapWriterEntry(k, v);
-            }
-            return this;
+    if (obj instanceof NavigableObject) {
+      return key == null ? ((NavigableObject) obj).__getVal(idx) : ((NavigableObject) obj).__getVal(idx);
+    } else if (obj instanceof Map) {
+      if (key != null) return ((Map) obj).get(key);
+      if (((Map) obj).isEmpty()) return null;
+      Object result[] = new Object[1];
+      int index = idx == -1 ? ((Map) obj).size() - 1 : idx;
+
+      ((Map) obj).forEach(new BiConsumer() {
+        int count = 0;
+
+        @Override
+        public void accept(Object k, Object v) {
+          if (result[0] != null) return;
+          if (index == count++) {
+            result[0] = new MapWriter.MapWriterEntry<>(String.valueOf(k), v);
           }
-        });
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
+        }
+      });
       return result[0];
-    }
-    else if (obj instanceof Map) return ((Map) obj).get(key);
-    else throw new RuntimeException("must be a NamedList or Map");
+    } else throw new RuntimeException("must be a NavigableObject or Map");
   }
 
   /**