Amalegeni-Go
 

Example 2 of concurrent SQL and 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
type Rec struct {
    Name string
    Count int64
}

func Select(shard dbid) []Rec
<<
    select 't_node', count(1) 
    from t_node
        union all 
    select 't_tag', count(1) 
    from t_tag 
>>

func Count(numShards int)
{{
    // the channel to communicate the result
    resultChan:=make(chan []Rec)

    // launch the query on each shard
    for sh:=1; sh<numShards; sh++ { 
        go func(shard int) {
            resultChan <- Select(shard) 
        }(sh)
    }

    // to store the totals
    var nodeCount, tagCount int64

    // wait for results
    for shard:=1;shard<numShards;shard++ { 
        fmt.Printf(".")
        r:= <- resultChan 
        for _,w := range(r) {
            switch w.Name {
                case "t_tag":  tagCount+=w.Count
                case "t_node": nodeCount+=w.Count
            }
        }
    }

    fmt.Printf("\nTotals : %d nodes and %d tags.\n", nodeCount, tagCount  )
}}
run
 
© Willem Moors, 2013 - 2020