Golint cleanup pkg/utils/split

master
Harshavardhana 10 years ago
parent 237921338a
commit 3e321b6631
  1. 26
      pkg/utils/split/split.go
  2. 6
      pkg/utils/split/split_test.go

@ -27,13 +27,13 @@ import (
"strings" "strings"
) )
// Message structure for results from the SplitStream goroutine // Message - message structure for results from the Stream goroutine
type SplitMessage struct { type Message struct {
Data []byte Data []byte
Err error Err error
} }
// SplitStream reads from io.Reader, splits the data into chunks, and sends // Stream reads from io.Reader, splits the data into chunks, and sends
// each chunk to the channel. This method runs until an EOF or error occurs. If // each chunk to the channel. This method runs until an EOF or error occurs. If
// an error occurs, the method sends the error over the channel and returns. // an error occurs, the method sends the error over the channel and returns.
// Before returning, the channel is always closed. // Before returning, the channel is always closed.
@ -41,18 +41,18 @@ type SplitMessage struct {
// The user should run this as a gorountine and retrieve the data over the // The user should run this as a gorountine and retrieve the data over the
// channel. // channel.
// //
// channel := make(chan SplitMessage) // channel := make(chan Message)
// go SplitStream(reader, chunkSize, channel) // go Stream(reader, chunkSize, channel)
// for chunk := range channel { // for chunk := range channel {
// log.Println(chunk.Data) // log.Println(chunk.Data)
// } // }
func SplitStream(reader io.Reader, chunkSize uint64) <-chan SplitMessage { func Stream(reader io.Reader, chunkSize uint64) <-chan Message {
ch := make(chan SplitMessage) ch := make(chan Message)
go splitStreamGoRoutine(reader, chunkSize, ch) go splitStreamGoRoutine(reader, chunkSize, ch)
return ch return ch
} }
func splitStreamGoRoutine(reader io.Reader, chunkSize uint64, ch chan SplitMessage) { func splitStreamGoRoutine(reader io.Reader, chunkSize uint64, ch chan Message) {
// we read until EOF or another error // we read until EOF or another error
var readError error var readError error
@ -81,12 +81,12 @@ func splitStreamGoRoutine(reader io.Reader, chunkSize uint64, ch chan SplitMessa
bytesWriter.Flush() bytesWriter.Flush()
// if we have data available, send it over the channel // if we have data available, send it over the channel
if bytesBuffer.Len() != 0 { if bytesBuffer.Len() != 0 {
ch <- SplitMessage{bytesBuffer.Bytes(), nil} ch <- Message{bytesBuffer.Bytes(), nil}
} }
} }
// if we have an error other than an EOF, send it over the channel // if we have an error other than an EOF, send it over the channel
if readError != io.EOF { if readError != io.EOF {
ch <- SplitMessage{nil, readError} ch <- Message{nil, readError}
} }
// close the channel, signaling the channel reader that the stream is complete // close the channel, signaling the channel reader that the stream is complete
close(ch) close(ch)
@ -142,9 +142,9 @@ func joinFilesGoRoutine(fileInfos []os.FileInfo, writer *io.PipeWriter) {
writer.Close() writer.Close()
} }
// Takes a file and splits it into chunks with size chunkSize. The output // FileWithPrefix - Takes a file and splits it into chunks with size chunkSize. The output
// filename is given with outputPrefix. // filename is given with outputPrefix.
func SplitFileWithPrefix(filename string, chunkSize uint64, outputPrefix string) error { func FileWithPrefix(filename string, chunkSize uint64, outputPrefix string) error {
// open file // open file
file, err := os.Open(filename) file, err := os.Open(filename)
defer file.Close() defer file.Close()
@ -157,7 +157,7 @@ func SplitFileWithPrefix(filename string, chunkSize uint64, outputPrefix string)
} }
// start stream splitting goroutine // start stream splitting goroutine
ch := SplitStream(file, chunkSize) ch := Stream(file, chunkSize)
// used to write each chunk out as a separate file. {{outputPrefix}}.{{i}} // used to write each chunk out as a separate file. {{outputPrefix}}.{{i}}
i := 0 i := 0

@ -41,7 +41,7 @@ func (s *MySuite) TestSplitStream(c *C) {
} }
bytesWriter.Flush() bytesWriter.Flush()
reader := bytes.NewReader(bytesBuffer.Bytes()) reader := bytes.NewReader(bytesBuffer.Bytes())
ch := SplitStream(reader, 25) ch := Stream(reader, 25)
var resultsBuffer bytes.Buffer var resultsBuffer bytes.Buffer
resultsWriter := bufio.NewWriter(&resultsBuffer) resultsWriter := bufio.NewWriter(&resultsBuffer)
for chunk := range ch { for chunk := range ch {
@ -52,9 +52,9 @@ func (s *MySuite) TestSplitStream(c *C) {
} }
func (s *MySuite) TestFileSplitJoin(c *C) { func (s *MySuite) TestFileSplitJoin(c *C) {
err := SplitFileWithPrefix("test-data/TESTFILE", 1024, "TESTPREFIX") err := FileWithPrefix("test-data/TESTFILE", 1024, "TESTPREFIX")
c.Assert(err, IsNil) c.Assert(err, IsNil)
err = SplitFileWithPrefix("test-data/TESTFILE", 1024, "") err = FileWithPrefix("test-data/TESTFILE", 1024, "")
c.Assert(err, Not(IsNil)) c.Assert(err, Not(IsNil))
devnull, err := os.OpenFile(os.DevNull, 2, os.ModeAppend) devnull, err := os.OpenFile(os.DevNull, 2, os.ModeAppend)

Loading…
Cancel
Save