From babbd07ed1df9c943b7d08d5f7ed9cdb64cc06cd Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Mon, 10 Mar 2014 10:18:42 +0000 Subject: [PATCH] SOLR-5837 Added .equals method for SolrDocument, SolrInputDocument and SolrInputField git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1575886 13f79535-47bb-0310-9956-ffa450edef68 --- .../solr/update/DocumentBuilderTest.java | 104 ++++++++++++++++++ .../org/apache/solr/common/SolrDocument.java | 25 ++++- .../apache/solr/common/SolrInputDocument.java | 34 +++++- .../apache/solr/common/SolrInputField.java | 34 ++++++ 4 files changed, 195 insertions(+), 2 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java b/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java index c8ffe4a7bfd..3d1f6ecd2e8 100644 --- a/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java +++ b/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java @@ -23,7 +23,9 @@ import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.SlowCompositeReaderWrapper; import org.apache.lucene.search.similarities.DefaultSimilarity; +import org.apache.lucene.util.TestUtil; import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrDocument; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrInputDocument; @@ -394,4 +396,106 @@ public class DocumentBuilderTest extends SolrTestCaseJ4 { assertNull(h.validateUpdate(add(xml, new String[0]))); } + public void testSolrDocumentEquals() { + + String randomString = TestUtil.randomSimpleString(random()); + + SolrDocument doc1 = new SolrDocument(); + doc1.addField("foo", randomString); + + SolrDocument doc2 = new SolrDocument(); + doc2.addField("foo", randomString); + + assertTrue(doc1.equals(doc2)); + + doc1.addField("foo", "bar"); + + assertFalse(doc1.equals(doc2)); + + doc1 = new SolrDocument(); + doc1.addField("bar", randomString); + + assertFalse(doc1.equals(doc2)); + + int randomInt = random().nextInt(); + doc1 = new SolrDocument(); + doc1.addField("foo", randomInt); + doc2 = new SolrDocument(); + doc2.addField("foo", randomInt); + + assertTrue(doc1.equals(doc2)); + + doc2 = new SolrDocument(); + doc2.addField("bar", randomInt); + + assertFalse(doc1.equals(doc2)); + + } + + public void testSolrInputDocumentEquality() { + + String randomString = TestUtil.randomSimpleString(random()); + + SolrInputDocument doc1 = new SolrInputDocument(); + doc1.addField("foo", randomString); + SolrInputDocument doc2 = new SolrInputDocument(); + doc2.addField("foo", randomString); + + assertTrue(doc1.equals(doc2)); + + doc1.setDocumentBoost(1.1f); + assertFalse(doc1.equals(doc2)); + + doc2.setDocumentBoost(1.1f); + assertTrue(doc1.equals(doc2)); + + doc2.setDocumentBoost(20f); + assertFalse(doc1.equals(doc2)); + + + doc1 = new SolrInputDocument(); + doc1.addField("foo", randomString); + doc2 = new SolrInputDocument(); + doc2.addField("foo", randomString); + + SolrInputDocument childDoc = new SolrInputDocument(); + childDoc.addField("foo", "bar"); + + doc1.addChildDocument(childDoc); + assertFalse(doc1.equals(doc2)); + + doc2.addChildDocument(childDoc); + assertTrue(doc1.equals(doc2)); + + SolrInputDocument childDoc1 = new SolrInputDocument(); + childDoc.addField(TestUtil.randomSimpleString(random()), TestUtil.randomSimpleString(random())); + doc2.addChildDocument(childDoc1); + assertFalse(doc1.equals(doc2)); + + } + + public void testSolrInputFieldEquality() { + String randomString = TestUtil.randomSimpleString(random(), 10, 20); + + int val = random().nextInt(); + SolrInputField sif1 = new SolrInputField(randomString); + sif1.setValue(val, 1.0f); + SolrInputField sif2 = new SolrInputField(randomString); + sif2.setValue(val, 1.0f); + + assertTrue(sif1.equals(sif2)); + + sif1.setBoost(2.1f); + sif2.setBoost(2.1f); + assertTrue(sif1.equals(sif2)); + + sif2.setBoost(2.0f); + assertFalse(sif1.equals(sif2)); + + sif2.setName("foo"); + assertFalse(sif1.equals(sif2)); + + + } + } diff --git a/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java b/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java index eae68355b77..f5e183fff72 100644 --- a/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java +++ b/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java @@ -213,7 +213,30 @@ public class SolrDocument implements Map, Iterable> iterator() { return _fields.entrySet().iterator(); } - + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SolrDocument)) { + return false; + } + + SolrDocument solrDocument = (SolrDocument) o; + + if (!_fields.equals(solrDocument._fields)) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + return _fields.hashCode(); + } + //----------------------------------------------------------------------------------------- // JSTL Helpers //----------------------------------------------------------------------------------------- diff --git a/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java b/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java index f731910f196..16e4270e812 100644 --- a/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java +++ b/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java @@ -275,7 +275,39 @@ public class SolrInputDocument implements Map, Iterable values() { return _fields.values(); } - + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SolrInputDocument)) { + return false; + } + + SolrInputDocument sdoc = (SolrInputDocument) o; + + if (!_fields.equals(sdoc._fields)) { + return false; + } + if (Float.compare(sdoc._documentBoost, _documentBoost) != 0) { + return false; + } + if (_childDocuments != null ? !_childDocuments.equals(sdoc._childDocuments) : sdoc._childDocuments != null) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = _fields.hashCode(); + result = 31 * result + (_documentBoost != +0.0f ? Float.floatToIntBits(_documentBoost) : 0); + result = 31 * result + (_childDocuments != null ? _childDocuments.hashCode() : 0); + return result; + } + public void addChildDocument(SolrInputDocument child) { if (_childDocuments == null) { _childDocuments = new ArrayList(); diff --git a/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java b/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java index 4c1a72940ab..9e40183b4f1 100644 --- a/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java +++ b/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java @@ -229,4 +229,38 @@ public class SolrInputField implements Iterable, Serializable } return clone; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SolrInputField)) { + return false; + } + + SolrInputField sif = (SolrInputField) o; + + if (!name.equals(sif.name)) { + return false; + } + + if (!value.equals(sif.value)) { + return false; + } + + if (Float.compare(sif.boost, boost) != 0) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = name.hashCode(); + result = 31 * result + value.hashCode(); + result = 31 * result + (boost != +0.0f ? Float.floatToIntBits(boost) : 0); + return result; + } }