mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Add some code refactoring from the blackboard branch
This commit is contained in:
51
pkg/obiformats/seqfile_chunk_write.go
Normal file
51
pkg/obiformats/seqfile_chunk_write.go
Normal file
@ -0,0 +1,51 @@
|
||||
package obiformats
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func WriteSeqFileChunk(
|
||||
writer io.WriteCloser,
|
||||
toBeClosed bool) ChannelSeqFileChunk {
|
||||
|
||||
obiiter.RegisterAPipe()
|
||||
|
||||
chunk_channel := make(ChannelSeqFileChunk)
|
||||
|
||||
go func() {
|
||||
nextToPrint := 0
|
||||
toBePrinted := make(map[int]SeqFileChunk)
|
||||
for chunk := range chunk_channel {
|
||||
if chunk.Order == nextToPrint {
|
||||
_, _ = writer.Write(chunk.Raw.Bytes())
|
||||
nextToPrint++
|
||||
|
||||
chunk, ok := toBePrinted[nextToPrint]
|
||||
for ok {
|
||||
_, _ = writer.Write(chunk.Raw.Bytes())
|
||||
delete(toBePrinted, nextToPrint)
|
||||
nextToPrint++
|
||||
chunk, ok = toBePrinted[nextToPrint]
|
||||
}
|
||||
} else {
|
||||
toBePrinted[chunk.Order] = chunk
|
||||
}
|
||||
}
|
||||
|
||||
if toBeClosed {
|
||||
err := writer.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot close the writer : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
obiiter.UnregisterPipe()
|
||||
log.Warnf("The writer has been closed")
|
||||
}()
|
||||
|
||||
return chunk_channel
|
||||
}
|
Reference in New Issue
Block a user