/
enumerate.go
144 lines (126 loc) · 3.61 KB
/
enumerate.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"errors"
"fmt"
"net/url"
"time"
"camlistore.org/pkg/blobref"
)
type EnumerateOpts struct {
After string
MaxWait time.Duration // how long to poll for (second granularity), waiting for any blob, or 0 for no limit
Limit int // if non-zero, the max blobs to return
}
// Note: closes ch.
func (c *Client) SimpleEnumerateBlobs(ch chan<- blobref.SizedBlobRef) error {
return c.EnumerateBlobsOpts(ch, EnumerateOpts{})
}
const enumerateBatchSize = 1000
// Note: closes ch.
func (c *Client) EnumerateBlobsOpts(ch chan<- blobref.SizedBlobRef, opts EnumerateOpts) error {
defer close(ch)
if opts.After != "" && opts.MaxWait != 0 {
return errors.New("client error: it's invalid to use enumerate After and MaxWaitSec together")
}
pfx, err := c.prefix()
if err != nil {
return err
}
error := func(msg string, e error) error {
err := errors.New(fmt.Sprintf("client enumerate error: %s: %v", msg, e))
c.log.Print(err.Error())
return err
}
nSent := 0
keepGoing := true
after := opts.After
for keepGoing {
waitSec := 0
if after == "" {
if opts.MaxWait > 0 {
waitSec = int(opts.MaxWait.Seconds())
if waitSec == 0 {
waitSec = 1
}
}
}
url_ := fmt.Sprintf("%s/camli/enumerate-blobs?after=%s&limit=%d&maxwaitsec=%d",
pfx, url.QueryEscape(after), enumerateBatchSize, waitSec)
req := c.newRequest("GET", url_)
resp, err := c.httpClient.Do(req)
if err != nil {
return error("http request", err)
}
json, err := c.jsonFromResponse("enumerate-blobs", resp)
if err != nil {
return error("stat json parse error", err)
}
blobs, ok := getJSONMapArray(json, "blobs")
if !ok {
return error("response JSON didn't contain 'blobs' array", nil)
}
for _, v := range blobs {
itemJSON, ok := v.(map[string]interface{})
if !ok {
return error("item in 'blobs' was malformed", nil)
}
blobrefStr, ok := getJSONMapString(itemJSON, "blobRef")
if !ok {
return error("item in 'blobs' was missing string 'blobRef'", nil)
}
size, ok := getJSONMapInt64(itemJSON, "size")
if !ok {
return error("item in 'blobs' was missing numeric 'size'", nil)
}
br := blobref.Parse(blobrefStr)
if br == nil {
return error("item in 'blobs' had invalid blobref.", nil)
}
ch <- blobref.SizedBlobRef{BlobRef: br, Size: size}
nSent++
if opts.Limit == nSent {
// nSent can't be zero at this point, so opts.Limit being 0
// is okay.
return nil
}
}
after, keepGoing = getJSONMapString(json, "continueAfter")
}
return nil
}
func getJSONMapString(m map[string]interface{}, key string) (string, bool) {
if v, ok := m[key]; ok {
if s, ok := v.(string); ok {
return s, true
}
}
return "", false
}
func getJSONMapInt64(m map[string]interface{}, key string) (int64, bool) {
if v, ok := m[key]; ok {
if n, ok := v.(float64); ok {
return int64(n), true
}
}
return 0, false
}
func getJSONMapArray(m map[string]interface{}, key string) ([]interface{}, bool) {
if v, ok := m[key]; ok {
if a, ok := v.([]interface{}); ok {
return a, true
}
}
return nil, false
}