From f6235c5cc6a0880abe602a558a98cc9cdbc3f262 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Tue, 18 Nov 2014 23:03:58 -0800 Subject: [PATCH] importer: simplified splitter The splitter is simplified using io.ReadFull, as this function does exactly what we wanted. I believe io.ErrUnexpectedEOF should be handled as an EOF here, but please correct me if I'm wrong. --- importer/chunk/splitting.go | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/importer/chunk/splitting.go b/importer/chunk/splitting.go index 87d48be85..65a79d5ad 100644 --- a/importer/chunk/splitting.go +++ b/importer/chunk/splitting.go @@ -28,28 +28,17 @@ func (ss *SizeSplitter) Split(r io.Reader) chan []byte { for { // log.Infof("making chunk with size: %d", ss.Size) chunk := make([]byte, ss.Size) - sofar := 0 - - // this-chunk loop (keep reading until this chunk full) - for { - nread, err := r.Read(chunk[sofar:]) - sofar += nread - if err == io.EOF { - if sofar > 0 { - // log.Infof("sending out chunk with size: %d", sofar) - out <- chunk[:sofar] - } - return - } - if err != nil { - log.Errorf("Block split error: %s", err) - return - } - if sofar == ss.Size { - // log.Infof("sending out chunk with size: %d", sofar) - out <- chunk[:sofar] - break // break out of this-chunk loop - } + nread, err := io.ReadFull(r, chunk) + if nread > 0 { + // log.Infof("sending out chunk with size: %d", sofar) + out <- chunk[:nread] + } + if err == io.EOF || err == io.ErrUnexpectedEOF { + return + } + if err != nil { + log.Errorf("Block split error: %s", err) + return } } }()