Author: zjffdu
Date: Tue Jan 19 02:37:47 2010
New Revision: 900650

URL: http://svn.apache.org/viewvc?rev=900650&view=rev
Log:
Pig-1166: A bit change of the interface of Tuple & DataBag ( make the set and 
append method return this) (zjffdu)

Added:
    hadoop/pig/trunk/test/org/apache/pig/test/TestTupleBagInterface.java
Modified:
    hadoop/pig/trunk/CHANGES.txt
    hadoop/pig/trunk/src/org/apache/pig/data/AccumulativeBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/DataBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/DefaultAbstractBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/DefaultTuple.java
    hadoop/pig/trunk/src/org/apache/pig/data/DistinctDataBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/InternalCachedBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/InternalDistinctBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/InternalSortedBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/NonSpillableDataBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/ReadOnceBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/SingleTupleBag.java
    hadoop/pig/trunk/src/org/apache/pig/data/TargetedTuple.java
    hadoop/pig/trunk/src/org/apache/pig/data/Tuple.java
    hadoop/pig/trunk/src/org/apache/pig/pen/util/ExampleTuple.java

Modified: hadoop/pig/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/CHANGES.txt?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/CHANGES.txt (original)
+++ hadoop/pig/trunk/CHANGES.txt Tue Jan 19 02:37:47 2010
@@ -24,6 +24,8 @@
 
 IMPROVEMENTS
 
+PIG-1166: A bit change of the interface of Tuple & DataBag:make the set and 
append method return this (zjffdu)
+
 PIG-1177: Pig 0.6 Docs - Zebra docs (chandec via olgan)
 
 PIG-1175: Pig 0.6 Docs - Store v. Dump (chandec via olgan)

Modified: hadoop/pig/trunk/src/org/apache/pig/data/AccumulativeBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/AccumulativeBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/AccumulativeBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/AccumulativeBag.java Tue Jan 19 
02:37:47 2010
@@ -35,11 +35,11 @@
         this.index = index;
     }
     
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
         throw new RuntimeException("AccumulativeBag does not support add 
operation");
     }
 
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
         throw new RuntimeException("AccumulativeBag does not support add 
operation");
     }
 

Modified: hadoop/pig/trunk/src/org/apache/pig/data/DataBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/DataBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/DataBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/DataBag.java Tue Jan 19 02:37:47 
2010
@@ -94,14 +94,16 @@
     /**
      * Add a tuple to the bag.
      * @param t tuple to add.
+     * @return The DataBag itself
      */
-    void add(Tuple t);
+    DataBag add(Tuple t);
 
     /**
      * Add contents of a bag to the bag.
      * @param b bag to add contents of.
+     * @return The DataBag itself
      */
-    void addAll(DataBag b);
+    DataBag addAll(DataBag b);
 
     /**
      * Clear out the contents of the bag, both on disk and in memory.

Modified: hadoop/pig/trunk/src/org/apache/pig/data/DefaultAbstractBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/DefaultAbstractBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/DefaultAbstractBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/DefaultAbstractBag.java Tue Jan 19 
02:37:47 2010
@@ -76,11 +76,12 @@
      * Add a tuple to the bag.
      * @param t tuple to add.
      */
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
         synchronized (mContents) {
             mMemSizeChanged = true;
             mSize++;
             mContents.add(t);
+            return this;
         }
     }
 
@@ -88,12 +89,13 @@
      * Add contents of a bag to the bag.
      * @param b bag to add contents of.
      */
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
         synchronized (mContents) {
             mMemSizeChanged = true;
             mSize += b.size();
             Iterator<Tuple> i = b.iterator();
             while (i.hasNext()) mContents.add(i.next());
+            return this;
         }
     }
 

Modified: hadoop/pig/trunk/src/org/apache/pig/data/DefaultTuple.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/DefaultTuple.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/DefaultTuple.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/DefaultTuple.java Tue Jan 19 
02:37:47 2010
@@ -159,8 +159,9 @@
      * @throws ExecException if the field number is greater than or equal to
      * the number of fields in the tuple.
      */
-    public void set(int fieldNum, Object val) throws ExecException {
+    public Tuple set(int fieldNum, Object val) throws ExecException {
         mFields.set(fieldNum, val);
+        return this;
     }
 
     /**
@@ -171,8 +172,9 @@
      * than construct it with newTuple() and append values.
      * @param val Object to append to the tuple.
      */
-    public void append(Object val) {
+    public Tuple append(Object val) {
         mFields.add(val);
+        return this;
     }
 
     /**

Modified: hadoop/pig/trunk/src/org/apache/pig/data/DistinctDataBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/DistinctDataBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/DistinctDataBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/DistinctDataBag.java Tue Jan 19 
02:37:47 2010
@@ -102,16 +102,17 @@
     }
 
     @Override
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
         synchronized (mContents) {
             if (mContents.add(t)) {
                 mSize++;
             }
+            return this;
         }
     }
 
     @Override
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
         synchronized (mContents) {
             Iterator<Tuple> i = b.iterator();
             while (i.hasNext()) {
@@ -119,6 +120,7 @@
                     mSize++;
                 }
             }
+            return this;
         }
     }
 

Modified: hadoop/pig/trunk/src/org/apache/pig/data/InternalCachedBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/InternalCachedBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/InternalCachedBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/InternalCachedBag.java Tue Jan 19 
02:37:47 2010
@@ -76,7 +76,7 @@
         addDone = false;
     }
 
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
        
         if(addDone) {
             throw new IllegalStateException("InternalCachedBag is closed for 
adding new tuples");
@@ -115,13 +115,15 @@
         }
         
         mSize++;
+        return this;
     }
 
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
        Iterator<Tuple> iter = b.iterator();
        while(iter.hasNext()) {
                add(iter.next());
        }
+       return this;
     }
 
     public void addAll(Collection<Tuple> c) {

Modified: hadoop/pig/trunk/src/org/apache/pig/data/InternalDistinctBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/InternalDistinctBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/InternalDistinctBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/InternalDistinctBag.java Tue Jan 
19 02:37:47 2010
@@ -138,7 +138,7 @@
     }
 
     @Override
-    public void add(Tuple t) {        
+    public DataBag add(Tuple t) {        
         
         if(mReadStarted) {
             throw new IllegalStateException("InternalDistinctBag is closed for 
adding new tuples");
@@ -162,14 +162,16 @@
                        log.debug("Memory can hold "+ cacheLimit + " records.");
                 }
             }          
-        }      
+        }
+        return this;
     }
 
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
        Iterator<Tuple> iter = b.iterator();
        while(iter.hasNext()) {
                add(iter.next());
        }
+       return this;
     }
 
     public void addAll(Collection<Tuple> c) {

Modified: hadoop/pig/trunk/src/org/apache/pig/data/InternalSortedBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/InternalSortedBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/InternalSortedBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/InternalSortedBag.java Tue Jan 19 
02:37:47 2010
@@ -136,7 +136,7 @@
         }        
     }
     
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
        if(mReadStarted) {
             throw new IllegalStateException("InternalSortedBag is closed for 
adding new tuples");
         }
@@ -160,13 +160,15 @@
         }
                 
         mSize++;
+        return this;
     }
     
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
        Iterator<Tuple> iter = b.iterator();
        while(iter.hasNext()) {
                add(iter.next());
        }
+       return this;
     }
 
     public void addAll(Collection<Tuple> c) {

Modified: hadoop/pig/trunk/src/org/apache/pig/data/NonSpillableDataBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/NonSpillableDataBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/NonSpillableDataBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/NonSpillableDataBag.java Tue Jan 
19 02:37:47 2010
@@ -111,15 +111,17 @@
     }
 
     @Override
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
         mContents.add(t);
+        return this;
     }
 
     @Override
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
         for (Tuple t : b) {
             mContents.add(t);
         }
+        return this;
     }
 
     @Override

Modified: hadoop/pig/trunk/src/org/apache/pig/data/ReadOnceBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/ReadOnceBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/ReadOnceBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/ReadOnceBag.java Tue Jan 19 
02:37:47 2010
@@ -92,7 +92,7 @@
      * @see org.apache.pig.data.DataBag#add(org.apache.pig.data.Tuple)
      */
     @Override
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
         throw new RuntimeException("ReadOnceBag does not support add 
operation");              
     }
 
@@ -100,7 +100,7 @@
      * @see org.apache.pig.data.DataBag#addAll(org.apache.pig.data.DataBag)
      */
     @Override
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
         throw new RuntimeException("ReadOnceBag does not support addAll 
operation");
     }
 

Modified: hadoop/pig/trunk/src/org/apache/pig/data/SingleTupleBag.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/SingleTupleBag.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/SingleTupleBag.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/SingleTupleBag.java Tue Jan 19 
02:37:47 2010
@@ -50,15 +50,16 @@
      * Tuple is ever added into a SingleTupleBag
      */
     @Override
-    public void add(Tuple t) {
+    public DataBag add(Tuple t) {
         item = t;
+        return this;
     }
 
     /* (non-Javadoc)
      * @see org.apache.pig.data.DataBag#addAll(org.apache.pig.data.DataBag)
      */
     @Override
-    public void addAll(DataBag b) {
+    public DataBag addAll(DataBag b) {
         throw new RuntimeException("Cannot create SingleTupleBag from another 
DataBag");
     }
 

Modified: hadoop/pig/trunk/src/org/apache/pig/data/TargetedTuple.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/TargetedTuple.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/TargetedTuple.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/TargetedTuple.java Tue Jan 19 
02:37:47 2010
@@ -106,8 +106,9 @@
         this.targetOps = targetOps;
     }
 
-    public void append(Object val) {
+    public Tuple append(Object val) {
         t.append(val);
+        return this;
     }
 
     public Object get(int fieldNum) throws ExecException {
@@ -134,8 +135,9 @@
         this.t = t;
     }
 
-    public void set(int fieldNum, Object val) throws ExecException {
+    public Tuple set(int fieldNum, Object val) throws ExecException {
         t.set(fieldNum, val);
+        return this;
     }
 
     public int size() {

Modified: hadoop/pig/trunk/src/org/apache/pig/data/Tuple.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/Tuple.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/Tuple.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/Tuple.java Tue Jan 19 02:37:47 2010
@@ -110,8 +110,9 @@
      * @param val Object to put in the indicated field.
      * @throws ExecException if the field number is greater than or equal to
      * the number of fields in the tuple.
+     * @return The Tuple itself
      */
-    void set(int fieldNum, Object val) throws ExecException;
+    Tuple set(int fieldNum, Object val) throws ExecException;
 
     /**
      * Append a field to a tuple.  This method is not efficient as it may
@@ -120,8 +121,9 @@
      * newTuple(int) method and then fill in the values with set(), rather
      * than construct it with newTuple() and append values.
      * @param val Object to append to the tuple.
+     * @return The Tuple itself
      */
-    void append(Object val);
+    Tuple append(Object val);
 
     /**
      * Determine the size of tuple in memory.  This is used by data bags

Modified: hadoop/pig/trunk/src/org/apache/pig/pen/util/ExampleTuple.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/pen/util/ExampleTuple.java?rev=900650&r1=900649&r2=900650&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/pen/util/ExampleTuple.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/pen/util/ExampleTuple.java Tue Jan 19 
02:37:47 2010
@@ -71,9 +71,9 @@
         return t;
     }
 
-    public void append(Object val) {
+    public Tuple append(Object val) {
         t.append(val);
-
+        return this;
     }
 
     public Object get(int fieldNum) throws ExecException {
@@ -104,8 +104,9 @@
         t.reference(t);
     }
 
-    public void set(int fieldNum, Object val) throws ExecException {
+    public Tuple set(int fieldNum, Object val) throws ExecException {
         t.set(fieldNum, val);
+        return this;
     }
 
     public void setNull(boolean isNull) {

Added: hadoop/pig/trunk/test/org/apache/pig/test/TestTupleBagInterface.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/test/org/apache/pig/test/TestTupleBagInterface.java?rev=900650&view=auto
==============================================================================
--- hadoop/pig/trunk/test/org/apache/pig/test/TestTupleBagInterface.java (added)
+++ hadoop/pig/trunk/test/org/apache/pig/test/TestTupleBagInterface.java Tue 
Jan 19 02:37:47 2010
@@ -0,0 +1,73 @@
+/*
+ * 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.pig.test;
+
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.TupleFactory;
+import org.junit.Test;
+
+public class TestTupleBagInterface extends TestCase {
+
+    private static TupleFactory TUPLEFACTORY = TupleFactory.getInstance();
+
+    private static BagFactory BAGFACTORY = BagFactory.getInstance();
+
+    @Test
+    public void testJIRA_1166() throws ExecException {
+
+        // Tuple set method
+        Tuple tuple = TUPLEFACTORY.newTuple(3).set(0, 1).set(1, 1.2).set(2,
+                "hello");
+        assertEquals(1, tuple.get(0));
+        assertEquals(1.2, tuple.get(1));
+        assertEquals("hello", tuple.get(2));
+
+        // Tuple append method
+        tuple = TUPLEFACTORY.newTuple().append(1).append(1.2).append("hello");
+        assertEquals(1, tuple.get(0));
+        assertEquals(1.2, tuple.get(1));
+        assertEquals("hello", tuple.get(2));
+
+        // DataBag add method
+        DataBag bag = BAGFACTORY.newDefaultBag().add(
+                TUPLEFACTORY.newTuple().append(1)).add(
+                TUPLEFACTORY.newTuple().append(1.2)).add(
+                TUPLEFACTORY.newTuple().append("hello"));
+        assertEquals(3, bag.size());
+        Iterator<Tuple> iter=bag.iterator();
+        assertEquals(1, iter.next().get(0));
+        assertEquals(1.2, iter.next().get(0));
+        assertEquals("hello", iter.next().get(0));
+        
+        // DataBag addAll method
+        DataBag bag_2 = BAGFACTORY.newDefaultBag().addAll(bag);
+        assertEquals(3, bag_2.size());
+        iter=bag_2.iterator();
+        assertEquals(1, iter.next().get(0));
+        assertEquals(1.2, iter.next().get(0));
+        assertEquals("hello", iter.next().get(0));
+    }
+}


Reply via email to