Skip to content

Commit

Permalink
[FLINK-2090] [core] Truncate 'toString()' of CollectionInputFormat wh…
Browse files Browse the repository at this point in the history
…en the collection is huge

This closes apache#2323
  • Loading branch information
mushketyk authored and StephanEwen committed Aug 9, 2016
1 parent 46b427f commit b5d5893
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
public class CollectionInputFormat<T> extends GenericInputFormat<T> implements NonParallelInput {

private static final long serialVersionUID = 1L;
private static final int MAX_TO_STRING_LEN = 100;

private TypeSerializer<T> serializer;

Expand Down Expand Up @@ -117,7 +118,23 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE

@Override
public String toString() {
return this.dataSet.toString();
StringBuilder sb = new StringBuilder();
sb.append('[');

int num = 0;
for (T e : dataSet) {
sb.append(e);
if (num != dataSet.size() - 1) {
sb.append(", ");
if (sb.length() > MAX_TO_STRING_LEN) {
sb.append("...");
break;
}
}
num++;
}
sb.append(']');
return sb.toString();
}

// --------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public boolean equals(Object obj) {
public int hashCode() {
return id;
}

@Override
public String toString() {
return "ElementType{" +
"id=" + id +
'}';
}
}

@Test
Expand Down Expand Up @@ -253,7 +260,37 @@ public void testDeserializationFailure() {
fail(e.getMessage());
}
}


@Test
public void testToStringOnSmallCollection() {
ArrayList<ElementType> smallList = new ArrayList<>();
smallList.add(new ElementType(1));
smallList.add(new ElementType(2));
CollectionInputFormat<ElementType> inputFormat = new CollectionInputFormat<>(
smallList,
new TestSerializer(true, false)
);

assertEquals("[ElementType{id=1}, ElementType{id=2}]", inputFormat.toString());
}

@Test
public void testToStringOnBigCollection() {
ArrayList<ElementType> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new ElementType(i));
}
CollectionInputFormat<ElementType> inputFormat = new CollectionInputFormat<>(
list,
new TestSerializer(true, false)
);

assertEquals(
"[ElementType{id=0}, ElementType{id=1}, ElementType{id=2}, " +
"ElementType{id=3}, ElementType{id=4}, ElementType{id=5}, ...]",
inputFormat.toString());
}

private static class TestException extends IOException{
private static final long serialVersionUID = 1L;
}
Expand Down

0 comments on commit b5d5893

Please sign in to comment.