lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cpoersc...@apache.org
Subject lucene-solr:master: SOLR-12699: Make contrib/ltr LTRScoringModel immutable and cache its hashCode. (Stanislav Livotov, Edward Ribeiro, Christine Poerschke)
Date Mon, 05 Nov 2018 19:32:55 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master 01808eee9 -> be65b95e8


SOLR-12699: Make contrib/ltr LTRScoringModel immutable and cache its hashCode.
(Stanislav Livotov, Edward Ribeiro, Christine Poerschke)


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

Branch: refs/heads/master
Commit: be65b95e80fdddea109a9d850506d6c524911ecb
Parents: 01808ee
Author: Christine Poerschke <cpoerschke@apache.org>
Authored: Mon Nov 5 18:56:40 2018 +0000
Committer: Christine Poerschke <cpoerschke@apache.org>
Committed: Mon Nov 5 18:56:40 2018 +0000

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  3 ++
 .../solr/ltr/model/DefaultWrapperModel.java     |  5 +++
 .../apache/solr/ltr/model/LTRScoringModel.java  | 33 +++++++++++++-------
 .../org/apache/solr/ltr/model/LinearModel.java  |  5 +++
 .../ltr/model/MultipleAdditiveTreesModel.java   | 11 +++++++
 .../solr/ltr/model/NeuralNetworkModel.java      |  5 +++
 .../apache/solr/ltr/model/TestAdapterModel.java | 10 ++++++
 7 files changed, 61 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 198ccf6..8e25a4d 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -178,6 +178,9 @@ Improvements
 
 * SOLR-12892: MapWriter to use CharSequence instead of String (noble)
 
+* SOLR-12699: Make contrib/ltr LTRScoringModel immutable and cache its hashCode.
+  (Stanislav Livotov, Edward Ribeiro, Christine Poerschke)
+
 ==================  7.5.0 ==================
 
 Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java
----------------------------------------------------------------------
diff --git a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java
b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java
index b21b6c3..bdb62d9 100644
--- a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java
+++ b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/DefaultWrapperModel.java
@@ -52,6 +52,11 @@ import org.noggit.ObjectBuilder;
  */
 public class DefaultWrapperModel extends WrapperModel {
 
+  /**
+   * resource is part of the LTRScoringModel params map
+   * and therefore here it does not individually
+   * influence the class hashCode, equals, etc.
+   */
   private String resource;
 
   public DefaultWrapperModel(String name, List<Feature> features, List<Normalizer>
norms, String featureStoreName,

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java
----------------------------------------------------------------------
diff --git a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java
index 7eb3436..cf20d7e 100644
--- a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java
+++ b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LTRScoringModel.java
@@ -16,11 +16,14 @@
  */
 package org.apache.solr.ltr.model;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.search.Explanation;
@@ -81,6 +84,7 @@ public abstract class LTRScoringModel {
   private final List<Feature> allFeatures;
   private final Map<String,Object> params;
   protected final List<Normalizer> norms;
+  private Integer hashCode; // cached since it shouldn't actually change after construction
 
   public static LTRScoringModel getInstance(SolrResourceLoader solrResourceLoader,
       String className, String name, List<Feature> features,
@@ -111,11 +115,11 @@ public abstract class LTRScoringModel {
       String featureStoreName, List<Feature> allFeatures,
       Map<String,Object> params) {
     this.name = name;
-    this.features = features;
+    this.features = features != null ? Collections.unmodifiableList(new ArrayList<>(features))
: null;
     this.featureStoreName = featureStoreName;
-    this.allFeatures = allFeatures;
-    this.params = params;
-    this.norms = norms;
+    this.allFeatures = allFeatures != null ? Collections.unmodifiableList(new ArrayList<>(allFeatures))
: null;
+    this.params = params != null ? Collections.unmodifiableMap(new LinkedHashMap<>(params))
: null;
+    this.norms = norms != null ? Collections.unmodifiableList(new ArrayList<>(norms))
: null;
   }
 
   /**
@@ -144,7 +148,7 @@ public abstract class LTRScoringModel {
    * @return the norms
    */
   public List<Normalizer> getNorms() {
-    return Collections.unmodifiableList(norms);
+    return norms;
   }
 
   /**
@@ -158,7 +162,7 @@ public abstract class LTRScoringModel {
    * @return the features
    */
   public List<Feature> getFeatures() {
-    return Collections.unmodifiableList(features);
+    return features;
   }
 
   public Map<String,Object> getParams() {
@@ -167,13 +171,20 @@ public abstract class LTRScoringModel {
 
   @Override
   public int hashCode() {
+    if(hashCode == null) {
+      hashCode = calculateHashCode();
+    }
+    return hashCode;
+  }
+
+  final private int calculateHashCode() {
     final int prime = 31;
     int result = 1;
-    result = (prime * result) + ((features == null) ? 0 : features.hashCode());
-    result = (prime * result) + ((name == null) ? 0 : name.hashCode());
-    result = (prime * result) + ((params == null) ? 0 : params.hashCode());
-    result = (prime * result) + ((norms == null) ? 0 : norms.hashCode());
-    result = (prime * result) + ((featureStoreName == null) ? 0 : featureStoreName.hashCode());
+    result = (prime * result) + Objects.hashCode(features);
+    result = (prime * result) + Objects.hashCode(name);
+    result = (prime * result) + Objects.hashCode(params);
+    result = (prime * result) + Objects.hashCode(norms);
+    result = (prime * result) + Objects.hashCode(featureStoreName);
     return result;
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java
----------------------------------------------------------------------
diff --git a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java
index 94eacab..908b073 100644
--- a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java
+++ b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/LinearModel.java
@@ -71,6 +71,11 @@ import org.apache.solr.ltr.norm.Normalizer;
  */
 public class LinearModel extends LTRScoringModel {
 
+  /**
+   * featureToWeight is part of the LTRScoringModel params map
+   * and therefore here it does not individually
+   * influence the class hashCode, equals, etc.
+   */
   protected Float[] featureToWeight;
 
   public void setWeights(Object weights) {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java
----------------------------------------------------------------------
diff --git a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java
b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java
index a626189..c3e434b 100644
--- a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java
+++ b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/MultipleAdditiveTreesModel.java
@@ -90,7 +90,18 @@ import org.apache.solr.util.SolrPluginUtils;
  */
 public class MultipleAdditiveTreesModel extends LTRScoringModel {
 
+  /**
+   * fname2index is filled from constructor arguments
+   * (that are already part of the base class hashCode)
+   * and therefore here it does not individually
+   * influence the class hashCode, equals, etc.
+   */
   private final HashMap<String,Integer> fname2index;
+  /**
+   * trees is part of the LTRScoringModel params map
+   * and therefore here it does not individually
+   * influence the class hashCode, equals, etc.
+   */
   private List<RegressionTree> trees;
 
   private RegressionTree createRegressionTree(Map<String,Object> map) {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java
----------------------------------------------------------------------
diff --git a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java
index fa92374..8c43296 100644
--- a/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java
+++ b/solr/contrib/ltr/src/java/org/apache/solr/ltr/model/NeuralNetworkModel.java
@@ -95,6 +95,11 @@ import org.apache.solr.util.SolrPluginUtils;
  */
 public class NeuralNetworkModel extends LTRScoringModel {
 
+  /**
+   * layers is part of the LTRScoringModel params map
+   * and therefore here it does not individually
+   * influence the class hashCode, equals, etc.
+   */
   private List<Layer> layers;
 
   protected interface Activation {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/be65b95e/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestAdapterModel.java
----------------------------------------------------------------------
diff --git a/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestAdapterModel.java b/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestAdapterModel.java
index d60d46e..0981401 100644
--- a/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestAdapterModel.java
+++ b/solr/contrib/ltr/src/test/org/apache/solr/ltr/model/TestAdapterModel.java
@@ -93,7 +93,17 @@ public class TestAdapterModel extends TestRerankBase {
 
   public static class CustomModel extends AdapterModel {
 
+    /**
+     * answerFileName is part of the LTRScoringModel params map
+     * and therefore here it does not individually
+     * influence the class hashCode, equals, etc.
+     */
     private String answerFileName;
+    /**
+     * answerValue is obtained from answerFileName
+     * and therefore here it does not individually
+     * influence the class hashCode, equals, etc.
+     */
     private float answerValue;
 
     public CustomModel(String name, List<Feature> features, List<Normalizer>
norms, String featureStoreName,


Mime
View raw message