-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconcurrent.go
executable file
·55 lines (48 loc) · 1.08 KB
/
concurrent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package geobuf
import (
"github.com/flywave/go-geobuf/io"
"github.com/flywave/go-geom"
)
type Concurrent struct {
Reader *Reader
C chan *geom.Feature
Count int
Limit int
FeatureCount int
}
func NewConcurrent(buf *Reader, limit int) *Concurrent {
return &Concurrent{Reader: buf, Limit: limit, Count: limit}
}
func (con *Concurrent) StartProcesses() {
i := 0
for con.Reader.Next() && i < con.Limit {
bytevals := con.Reader.Bytes()
go func(bytevals []byte) {
con.C <- io.ReadFeature(bytevals)
}(bytevals)
i++
}
con.Reader.FeatureCount--
}
func (con *Concurrent) Next() bool {
if con.Count == con.Limit || con.Reader.Reader.EndBool {
if con.Reader.Reader.EndBool && con.Reader.FeatureCount > con.FeatureCount {
return true
} else if con.Reader.Reader.EndBool {
return false
} else {
con.Count = 0
con.C = make(chan *geom.Feature)
go con.StartProcesses()
return true
}
} else {
return true
}
}
func (con *Concurrent) Feature() *geom.Feature {
con.Count++
con.FeatureCount++
feature := <-con.C
return feature
}