You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2014/11/10 17:58:38 UTC

svn commit: r1637945 - in /lucene/dev/trunk/lucene: ./ core/src/java/org/apache/lucene/analysis/tokenattributes/ core/src/java/org/apache/lucene/util/ core/src/test/org/apache/lucene/util/

Author: shaie
Date: Mon Nov 10 16:58:37 2014
New Revision: 1637945

URL: http://svn.apache.org/r1637945
Log:
LUCENE-6055: PayloadAttribute.clone() should deep clone its BytesRef

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1637945&r1=1637944&r2=1637945&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Mon Nov 10 16:58:37 2014
@@ -226,6 +226,9 @@ Bug Fixes
   the only docs in it had fields that hit non-aborting exceptions
   during indexing but also had doc values.  (Mike McCandless)
 
+* LUCENE-6055: PayloadAttribute.clone() now does a deep clone of the underlying
+  bytes. (Shai Erera)
+  
 Documentation
 
 * LUCENE-5392: Add/improve analysis package documentation to reflect

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java?rev=1637945&r1=1637944&r2=1637945&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java Mon Nov 10 16:58:37 2014
@@ -88,7 +88,6 @@ public class CharTermAttributeImpl exten
   @Override
   public void fillBytesRef() {
     bytes.copyChars(termBuffer, 0, termLength);
-    bytes.get();
   }
 
   @Override

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java?rev=1637945&r1=1637944&r2=1637945&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/analysis/tokenattributes/PayloadAttributeImpl.java Mon Nov 10 16:58:37 2014
@@ -55,7 +55,7 @@ public class PayloadAttributeImpl extend
   public PayloadAttributeImpl clone()  {
     PayloadAttributeImpl clone = (PayloadAttributeImpl) super.clone();
     if (payload != null) {
-      clone.payload = payload.clone();
+      clone.payload = BytesRef.deepCopyOf(payload);
     }
     return clone;
   }
@@ -86,7 +86,7 @@ public class PayloadAttributeImpl extend
   @Override
   public void copyTo(AttributeImpl target) {
     PayloadAttribute t = (PayloadAttribute) target;
-    t.setPayload((payload == null) ? null : payload.clone());
+    t.setPayload((payload == null) ? null : BytesRef.deepCopyOf(payload));
   }  
 
   

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java?rev=1637945&r1=1637944&r2=1637945&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/AttributeImpl.java Mon Nov 10 16:58:37 2014
@@ -116,10 +116,10 @@ public abstract class AttributeImpl impl
    * Attributes this implementation supports.
    */
   public abstract void copyTo(AttributeImpl target);
-    
+
   /**
-   * Shallow clone. Subclasses must override this if they 
-   * need to clone any members deeply,
+   * In most cases the clone is, and should be, deep in order to be able to
+   * properly capture the state of all attributes.
    */
   @Override
   public AttributeImpl clone() {

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java?rev=1637945&r1=1637944&r2=1637945&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestAttributeSource.java Mon Nov 10 16:58:37 2014
@@ -159,4 +159,21 @@ public class TestAttributeSource extends
     assertTrue("The hashCode is identical, so the captured state was preserved.", hash1 != src1.hashCode());
     assertEquals(src2.hashCode(), src1.hashCode());
   }
+  
+  public void testClonePayloadAttribute() throws Exception {
+    // LUCENE-6055: verify that PayloadAttribute.clone() does deep cloning.
+    PayloadAttributeImpl src = new PayloadAttributeImpl(new BytesRef(new byte[] { 1, 2, 3 }));
+    
+    // test clone()
+    PayloadAttributeImpl clone = src.clone();
+    clone.getPayload().bytes[0] = 10; // modify one byte, srcBytes shouldn't change
+    assertEquals("clone() wasn't deep", 1, src.getPayload().bytes[0]);
+    
+    // test copyTo()
+    clone = new PayloadAttributeImpl();
+    src.copyTo(clone);
+    clone.getPayload().bytes[0] = 10; // modify one byte, srcBytes shouldn't change
+    assertEquals("clone() wasn't deep", 1, src.getPayload().bytes[0]);
+  }
+  
 }