forked from couchbaselabs/cbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathgen.go
102 lines (88 loc) · 1.58 KB
/
pathgen.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"log"
"strings"
"sync"
cb "github.com/couchbaselabs/go-couchbase"
)
type namedFile struct {
name string
meta fileMeta
err error
}
func pathDataFetcher(wg *sync.WaitGroup, quit <-chan bool,
in <-chan string, out chan<- *namedFile) {
defer wg.Done()
for {
select {
case s, ok := <-in:
if !ok {
return
}
ob := namedFile{name: s}
ob.err = couchbase.Get(shortName(s), &ob.meta)
out <- &ob
case <-quit:
return
}
}
}
func pathGenerator(from string, ch chan *namedFile,
errs chan error, quit chan bool) {
parts := strings.Split(from, "/")
viewRes := struct {
Rows []struct {
Key []string
Id string
}
Errors []cb.ViewError
}{}
limit := 1000
fetchch := make(chan string, limit)
startKey := parts
done := false
wg := &sync.WaitGroup{}
for i := 0; i < 8; i++ {
wg.Add(1)
go pathDataFetcher(wg, quit, fetchch, ch)
}
defer func() {
close(fetchch)
wg.Wait()
close(ch)
close(errs)
}()
for !done {
err := couchbase.ViewCustom("cbfs", "file_browse",
map[string]interface{}{
"stale": false,
"reduce": false,
"limit": limit,
"startkey": startKey,
}, &viewRes)
if err != nil {
log.Printf("View error: %v", err)
select {
case errs <- err:
case <-quit:
}
return
}
for _, e := range viewRes.Errors {
select {
case errs <- e:
case <-quit:
return
}
}
done = len(viewRes.Rows) < limit
for _, r := range viewRes.Rows {
k := strings.Join(r.Key, "/")
if !strings.HasPrefix(k, from) {
return
}
startKey = r.Key
fetchch <- k
}
}
}