Files
obitools4/pkg/obichunk/chunks.go

77 lines
1.3 KiB
Go
Raw Normal View History

package obichunk
import (
"log"
"sync"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
)
func ISequenceChunk(iterator obiseq.IBioSequenceBatch,
2022-02-18 22:53:09 +01:00
classifier *obiseq.BioSequenceClassifier,
sizes ...int) (obiseq.IBioSequenceBatch, error) {
bufferSize := iterator.BufferSize()
if len(sizes) > 0 {
bufferSize = sizes[0]
}
newIter := obiseq.MakeIBioSequenceBatch(bufferSize)
newIter.Add(1)
go func() {
newIter.Wait()
newIter.Close()
}()
go func() {
lock := sync.Mutex{}
dispatcher := iterator.Distribute(classifier)
jobDone := sync.WaitGroup{}
2022-02-18 22:53:09 +01:00
chunks := make(map[int]*obiseq.BioSequenceSlice, 1000)
for newflux := range dispatcher.News() {
jobDone.Add(1)
2022-02-18 22:53:09 +01:00
go func(newflux int) {
data, err := dispatcher.Outputs(newflux)
if err != nil {
log.Fatalf("Cannot retreive the new chanel : %v", err)
}
chunk := obiseq.NewBioSequenceSlice()
2022-02-18 22:53:09 +01:00
lock.Lock()
chunks[newflux] = chunk
lock.Unlock()
for data.Next() {
b := data.Get()
2022-02-18 22:53:09 +01:00
*chunk = append(*chunk, b.Slice()...)
b.Recycle()
}
jobDone.Done()
}(newflux)
}
jobDone.Wait()
order := 0
for _, chunck := range chunks {
if len(*chunck) > 0 {
newIter.Push(obiseq.MakeBioSequenceBatch(order, *chunck))
order++
}
}
newIter.Done()
}()
return newIter, nil
}