forked from Lazyshot/go-hbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
70 lines (60 loc) · 1.15 KB
/
util.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
63
64
65
66
67
68
69
70
package hbase
import (
pb "github.com/golang/protobuf/proto"
"sync"
)
type atomicCounter struct {
n int
lock *sync.RWMutex
}
func newAtomicCounter() *atomicCounter {
return &atomicCounter{
n: 0,
lock: &sync.RWMutex{},
}
}
func (a *atomicCounter) Get() int {
a.lock.RLock()
v := a.n
a.lock.RUnlock()
return v
}
func (a *atomicCounter) IncrAndGet() int {
a.lock.Lock()
a.n++
v := a.n
a.lock.Unlock()
return v
}
func incrementByteString(d []byte, i int) []byte {
r := make([]byte, len(d))
copy(r, d)
if i <= 0 {
return append(make([]byte, 1), r...)
}
r[i]++
return r
}
func merge(cs ...chan pb.Message) chan pb.Message {
var wg sync.WaitGroup
out := make(chan pb.Message)
// Start an output goroutine for each input channel in cs. output
// copies values from c to out until c is closed, then calls wg.Done.
output := func(c <-chan pb.Message) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
// Start a goroutine to close out once all the output goroutines are
// done. This must start after the wg.Add call.
go func() {
wg.Wait()
close(out)
}()
return out
}