From e400a17636f10198e3555b51ea350b0ddb82a7f7 Mon Sep 17 00:00:00 2001 From: tedyu Date: Mon, 25 Jan 2016 15:48:45 -0800 Subject: [PATCH 1/2] [SPARK-12934] use try-with-resources for streams --- .../spark/util/sketch/CountMinSketchImpl.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java b/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java index 0209446ea3b1d..838cdb09bb683 100644 --- a/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java +++ b/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java @@ -325,47 +325,47 @@ public CountMinSketch mergeInPlace(CountMinSketch other) throws IncompatibleMerg @Override public void writeTo(OutputStream out) throws IOException { - DataOutputStream dos = new DataOutputStream(out); + try (DataOutputStream dos = new DataOutputStream(out)) { + dos.writeInt(version().getVersionNumber()); - dos.writeInt(version().getVersionNumber()); + dos.writeLong(this.totalCount); + dos.writeInt(this.depth); + dos.writeInt(this.width); - dos.writeLong(this.totalCount); - dos.writeInt(this.depth); - dos.writeInt(this.width); - - for (int i = 0; i < this.depth; ++i) { - dos.writeLong(this.hashA[i]); - } + for (int i = 0; i < this.depth; ++i) { + dos.writeLong(this.hashA[i]); + } - for (int i = 0; i < this.depth; ++i) { - for (int j = 0; j < this.width; ++j) { - dos.writeLong(table[i][j]); + for (int i = 0; i < this.depth; ++i) { + for (int j = 0; j < this.width; ++j) { + dos.writeLong(table[i][j]); + } } } } public static CountMinSketchImpl readFrom(InputStream in) throws IOException { - DataInputStream dis = new DataInputStream(in); + try (DataInputStream dis = new DataInputStream(in)) { + // Ignores version number + dis.readInt(); - // Ignores version number - dis.readInt(); + long totalCount = dis.readLong(); + int depth = dis.readInt(); + int width = dis.readInt(); - long totalCount = dis.readLong(); - int depth = dis.readInt(); - int width = dis.readInt(); - - long hashA[] = new long[depth]; - for (int i = 0; i < depth; ++i) { - hashA[i] = dis.readLong(); - } + long hashA[] = new long[depth]; + for (int i = 0; i < depth; ++i) { + hashA[i] = dis.readLong(); + } - long table[][] = new long[depth][width]; - for (int i = 0; i < depth; ++i) { - for (int j = 0; j < width; ++j) { - table[i][j] = dis.readLong(); + long table[][] = new long[depth][width]; + for (int i = 0; i < depth; ++i) { + for (int j = 0; j < width; ++j) { + table[i][j] = dis.readLong(); + } } - } - return new CountMinSketchImpl(depth, width, totalCount, hashA, table); + return new CountMinSketchImpl(depth, width, totalCount, hashA, table); + } } } From 860d3ba6ad15f6fe840bce5f0474a29283928332 Mon Sep 17 00:00:00 2001 From: tedyu Date: Mon, 25 Jan 2016 17:48:32 -0800 Subject: [PATCH 2/2] Address review comments by documenting --- .../spark/util/sketch/CountMinSketch.java | 2 + .../spark/util/sketch/CountMinSketchImpl.java | 58 +++++++++---------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketch.java b/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketch.java index 67938644d9f6c..9f4ff42403c34 100644 --- a/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketch.java +++ b/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketch.java @@ -128,11 +128,13 @@ public abstract CountMinSketch mergeInPlace(CountMinSketch other) /** * Writes out this {@link CountMinSketch} to an output stream in binary format. + * It is the caller's responsibility to close the stream */ public abstract void writeTo(OutputStream out) throws IOException; /** * Reads in a {@link CountMinSketch} from an input stream. + * It is the caller's responsibility to close the stream */ public static CountMinSketch readFrom(InputStream in) throws IOException { return CountMinSketchImpl.readFrom(in); diff --git a/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java b/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java index 838cdb09bb683..0209446ea3b1d 100644 --- a/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java +++ b/common/sketch/src/main/java/org/apache/spark/util/sketch/CountMinSketchImpl.java @@ -325,47 +325,47 @@ public CountMinSketch mergeInPlace(CountMinSketch other) throws IncompatibleMerg @Override public void writeTo(OutputStream out) throws IOException { - try (DataOutputStream dos = new DataOutputStream(out)) { - dos.writeInt(version().getVersionNumber()); + DataOutputStream dos = new DataOutputStream(out); - dos.writeLong(this.totalCount); - dos.writeInt(this.depth); - dos.writeInt(this.width); + dos.writeInt(version().getVersionNumber()); - for (int i = 0; i < this.depth; ++i) { - dos.writeLong(this.hashA[i]); - } + dos.writeLong(this.totalCount); + dos.writeInt(this.depth); + dos.writeInt(this.width); + + for (int i = 0; i < this.depth; ++i) { + dos.writeLong(this.hashA[i]); + } - for (int i = 0; i < this.depth; ++i) { - for (int j = 0; j < this.width; ++j) { - dos.writeLong(table[i][j]); - } + for (int i = 0; i < this.depth; ++i) { + for (int j = 0; j < this.width; ++j) { + dos.writeLong(table[i][j]); } } } public static CountMinSketchImpl readFrom(InputStream in) throws IOException { - try (DataInputStream dis = new DataInputStream(in)) { - // Ignores version number - dis.readInt(); + DataInputStream dis = new DataInputStream(in); - long totalCount = dis.readLong(); - int depth = dis.readInt(); - int width = dis.readInt(); + // Ignores version number + dis.readInt(); - long hashA[] = new long[depth]; - for (int i = 0; i < depth; ++i) { - hashA[i] = dis.readLong(); - } + long totalCount = dis.readLong(); + int depth = dis.readInt(); + int width = dis.readInt(); - long table[][] = new long[depth][width]; - for (int i = 0; i < depth; ++i) { - for (int j = 0; j < width; ++j) { - table[i][j] = dis.readLong(); - } - } + long hashA[] = new long[depth]; + for (int i = 0; i < depth; ++i) { + hashA[i] = dis.readLong(); + } - return new CountMinSketchImpl(depth, width, totalCount, hashA, table); + long table[][] = new long[depth][width]; + for (int i = 0; i < depth; ++i) { + for (int j = 0; j < width; ++j) { + table[i][j] = dis.readLong(); + } } + + return new CountMinSketchImpl(depth, width, totalCount, hashA, table); } }