seojangho commented on a change in pull request #48: [NEMO-72] Instance-based Encoder/Decoder interface URL: https://github.com/apache/incubator-nemo/pull/48#discussion_r196278303
########## File path: common/src/main/java/edu/snu/nemo/common/coder/Decoder.java ########## @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2018 Seoul National University + * + * Licensed 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 edu.snu.nemo.common.coder; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; + +/** + * A decoder object decodes values of type {@code T} into byte streams. + * To avoid to generate instance-based coder such as Spark serializer for every decoding, + * user need to instantiate a decoder instance and use it. + * + * @param <T> element type. + */ +public interface Decoder<T> extends Serializable { + + /** + * Get an instance of this decoder. + * + * @param inputStream the input stream to decode. + * @return the decoder instance. + * @throws IOException if fail to get the instance. + */ + DecoderInstance<T> getDecoderInstance(InputStream inputStream) throws IOException; + + /** + * Interface of DecoderInstance. + * + * @param <T> element type. + */ + interface DecoderInstance<T> { + + /** + * Decodes the a value from the given input stream. + * It have to be able to decode the given stream consequently by calling this method repeatedly. + * Because there are many elements in the input stream, the stream should not be closed. + * + * @return the decoded element + * @throws IOException if fail to decode + */ + T decode() throws IOException; Review comment: One more side note. It would be lovely if `decode` can tell `EOFException` from other `IOException` cases. * `EOFException`: reached the end of stream * `IOException`: other unexpected errors Since two cases are merged into `IOException`, `DataUtil` cannot properly handle I/O errors since it should treat all `IOException`s as EOF case. It may require non-trivial efforts on `BeamEncoder`/`BeamDecoder` and `SparkEncoder`/`SparkDecoder`. I'll create separate JIRA ticket if this is too big to handle in this PR. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services