mirror of https://github.com/perkeep/perkeep.git
159 lines
4.0 KiB
Go
159 lines
4.0 KiB
Go
/*
|
|
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"
|
|
"log"
|
|
"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{})
|
|
}
|
|
|
|
func (c *Client) EnumerateBlobs(dest chan<- blobref.SizedBlobRef, after string, limit int, wait time.Duration) error {
|
|
if limit == 0 {
|
|
log.Printf("Warning: Client.EnumerateBlobs called with a limit of zero")
|
|
close(dest)
|
|
return nil
|
|
}
|
|
return c.EnumerateBlobsOpts(dest, EnumerateOpts{
|
|
After: after,
|
|
Limit: limit,
|
|
MaxWait: wait,
|
|
})
|
|
}
|
|
|
|
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
|
|
}
|