-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeobuf_concurrent.go
executable file
·62 lines (54 loc) · 1.26 KB
/
geobuf_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
56
57
58
59
60
61
62
package geobuf
import (
//"fmt"
raw "github.com/murphy214/geobuf/geobuf_raw"
"github.com/paulmach/go.geojson"
)
type Concurrent struct {
Reader *Reader
C chan *geojson.Feature
Count int
Limit int
FeatureCount int
}
// intiating a new concurrent reader
func NewConcurrent(buf *Reader, limit int) *Concurrent {
return &Concurrent{Reader: buf, Limit: limit, Count: limit}
}
// a start process read concurrently
func (con *Concurrent) StartProcesses() {
i := 0
for con.Reader.Next() && i < con.Limit {
bytevals := con.Reader.Bytes()
go func(bytevals []byte) {
con.C <- raw.ReadFeature(bytevals)
}(bytevals)
i++
}
con.Reader.FeatureCount--
}
// a next read concurrently
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 *geojson.Feature)
go con.StartProcesses()
return true
}
} else {
return true
}
return false
}
// recieving a feature from a channel
func (con *Concurrent) Feature() *geojson.Feature {
con.Count++
con.FeatureCount++
feature := <-con.C
return feature
}