Skip to content

Commit

Permalink
PIG-2182: Add more append support to DataByteArray
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pig/trunk@1301829 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Jianyong Dai committed Mar 16, 2012
1 parent 67881e4 commit 58abb99
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ INCOMPATIBLE CHANGES

IMPROVEMENTS

PIG-2182: Add more append support to DataByteArray (gsingers via daijy)

PIG-438: Handle realiasing of existing Alias (A=B;) (daijy)

PIG-2548: Support for providing parameters to python script (daijy)
Expand Down
29 changes: 24 additions & 5 deletions src/org/apache/pig/data/DataByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,38 @@ public void set(String s) {
* @param b byte array who's contents to append. The contents of the byte array are
* copied.
*/
public void append(DataByteArray b) {
public DataByteArray append(DataByteArray b) {

byte[] ba = (b == null) ? null : b.get();
return append(ba, 0, ba == null ? 0 : ba.length);

}

public DataByteArray append(byte [] ba){
return append(ba, 0, ba.length);
}

public DataByteArray append(byte [] ba, int start, int baLength){
int mDataLength = (mData == null) ? 0 : mData.length;
int baLength = (ba == null) ? 0 : ba.length;


int totalSize = mDataLength + baLength;
if(totalSize == 0) {
return;
return this;
}
byte[] oldData = mData == null ? new byte[0] : mData.clone();
System.arraycopy(oldData, 0, mData = new byte[totalSize], 0, mDataLength);
System.arraycopy(ba, 0, mData, mDataLength, baLength);
System.arraycopy(ba, start, mData, mDataLength, baLength);
return this;
}

public DataByteArray append(String str){
try {
return append(str.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//TODO: better error here
throw new RuntimeException("Unable to append str: " + str);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions test/org/apache/pig/test/TestDataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,33 @@ public void testByteArrayAppend() throws Exception {
db1.append(db2);
assertTrue("appends as expected", db1.equals(expected));
}

@Test
public void testByteArrayAppendMore() throws Exception {
DataByteArray expected = new DataByteArray("hello world!");
DataByteArray db1 = new DataByteArray("hello ");
DataByteArray db2 = new DataByteArray("world");
DataByteArray db3 = new DataByteArray("!");
db1.append(db2).append(db3);
assertTrue("appends as expected", db1.equals(expected));
}

@Test
public void testByteArrayAppendBytes() throws Exception {
DataByteArray expected = new DataByteArray("hello world");
DataByteArray db1 = new DataByteArray("hello ");
byte[] db2 = "world".getBytes();
db1.append(db2);
assertTrue("appends as expected", db1.equals(expected));
}

@Test
public void testByteArrayAppendString() throws Exception {
DataByteArray expected = new DataByteArray("hello world");
DataByteArray db1 = new DataByteArray("hello ");
db1.append("world");
assertTrue("appends as expected", db1.equals(expected));
}

@Test
public void testMapConversionErr() throws Exception {
Expand Down

0 comments on commit 58abb99

Please sign in to comment.