mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
refactoring of the file chunck writing
This commit is contained in:
61
pkg/obiformats/file_chunk_write.go
Normal file
61
pkg/obiformats/file_chunk_write.go
Normal file
@ -0,0 +1,61 @@
|
||||
package obiformats
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func WriteFileChunk(
|
||||
writer io.WriteCloser,
|
||||
toBeClosed bool) ChannelFileChunk {
|
||||
|
||||
obiiter.RegisterAPipe()
|
||||
chunk_channel := make(ChannelFileChunk)
|
||||
|
||||
go func() {
|
||||
nextToPrint := 0
|
||||
toBePrinted := make(map[int]FileChunk)
|
||||
for chunk := range chunk_channel {
|
||||
if chunk.Order == nextToPrint {
|
||||
log.Debugf("Writing chunk: %d of length %d bytes",
|
||||
chunk.Order,
|
||||
len(chunk.Raw.Bytes()))
|
||||
|
||||
n, err := writer.Write(chunk.Raw.Bytes())
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot write chunk %d only %d bytes written on %d sended : %v",
|
||||
chunk.Order, n, len(chunk.Raw.Bytes()), err)
|
||||
}
|
||||
nextToPrint++
|
||||
|
||||
chunk, ok := toBePrinted[nextToPrint]
|
||||
for ok {
|
||||
log.Debug("Writing buffered chunk : ", chunk.Order)
|
||||
_, _ = writer.Write(chunk.Raw.Bytes())
|
||||
delete(toBePrinted, nextToPrint)
|
||||
nextToPrint++
|
||||
chunk, ok = toBePrinted[nextToPrint]
|
||||
}
|
||||
} else {
|
||||
toBePrinted[chunk.Order] = chunk
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("FIle have to be closed : %v", toBeClosed)
|
||||
if toBeClosed {
|
||||
err := writer.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot close the writer : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
obiiter.UnregisterPipe()
|
||||
log.Debugf("The writer has been closed")
|
||||
}()
|
||||
|
||||
return chunk_channel
|
||||
}
|
Reference in New Issue
Block a user