2011-03-02 02:02:01 +00:00
|
|
|
/*
|
|
|
|
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 (
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2011-03-02 02:02:01 +00:00
|
|
|
"fmt"
|
2013-06-09 09:36:26 +00:00
|
|
|
"log"
|
2014-01-28 20:46:52 +00:00
|
|
|
"math"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
2011-08-25 15:14:47 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-12-02 21:20:51 +00:00
|
|
|
"camlistore.org/pkg/context"
|
2011-03-02 02:02:01 +00:00
|
|
|
)
|
|
|
|
|
2014-03-01 19:48:20 +00:00
|
|
|
// EnumerateOpts are the options to Client.EnumerateBlobsOpts.
|
2011-03-05 17:53:51 +00:00
|
|
|
type EnumerateOpts struct {
|
2014-03-01 19:48:20 +00:00
|
|
|
After string // last blobref seen; start with ones greater than this
|
2012-10-09 14:47:43 +00:00
|
|
|
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
|
2011-03-05 17:53:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 19:48:20 +00:00
|
|
|
// SimpleEnumerateBlobs sends all blobs to the provided channel.
|
|
|
|
// The channel will be closed, regardless of whether an error is returned.
|
2013-12-02 21:20:51 +00:00
|
|
|
func (c *Client) SimpleEnumerateBlobs(ctx *context.Context, ch chan<- blob.SizedRef) error {
|
|
|
|
return c.EnumerateBlobsOpts(ctx, ch, EnumerateOpts{})
|
2011-03-02 02:02:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 21:20:51 +00:00
|
|
|
func (c *Client) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) error {
|
2014-02-07 18:59:23 +00:00
|
|
|
if c.sto != nil {
|
|
|
|
return c.sto.EnumerateBlobs(ctx, dest, after, limit)
|
|
|
|
}
|
2013-06-09 09:36:26 +00:00
|
|
|
if limit == 0 {
|
|
|
|
log.Printf("Warning: Client.EnumerateBlobs called with a limit of zero")
|
|
|
|
close(dest)
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-02 21:20:51 +00:00
|
|
|
return c.EnumerateBlobsOpts(ctx, dest, EnumerateOpts{
|
2013-08-21 20:57:28 +00:00
|
|
|
After: after,
|
|
|
|
Limit: limit,
|
2013-06-09 09:36:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2011-03-13 04:44:01 +00:00
|
|
|
const enumerateBatchSize = 1000
|
2011-03-02 02:02:01 +00:00
|
|
|
|
2014-03-01 19:48:20 +00:00
|
|
|
// EnumerateBlobsOpts sends blobs to the provided channel, as directed by opts.
|
|
|
|
// The channel will be closed, regardless of whether an error is returned.
|
2013-12-02 21:20:51 +00:00
|
|
|
func (c *Client) EnumerateBlobsOpts(ctx *context.Context, ch chan<- blob.SizedRef, opts EnumerateOpts) error {
|
2011-03-03 04:03:09 +00:00
|
|
|
defer close(ch)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
if opts.After != "" && opts.MaxWait != 0 {
|
|
|
|
return errors.New("client error: it's invalid to use enumerate After and MaxWaitSec together")
|
2011-03-05 17:53:51 +00:00
|
|
|
}
|
2012-11-08 04:23:45 +00:00
|
|
|
pfx, err := c.prefix()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2011-03-03 04:03:09 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
error := func(msg string, e error) error {
|
2014-01-29 06:00:52 +00:00
|
|
|
err := fmt.Errorf("client enumerate error: %s: %v", msg, e)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
c.log.Print(err.Error())
|
2011-03-02 02:02:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
nSent := 0
|
2011-03-02 02:02:01 +00:00
|
|
|
keepGoing := true
|
2011-03-05 17:53:51 +00:00
|
|
|
after := opts.After
|
2011-03-02 02:02:01 +00:00
|
|
|
for keepGoing {
|
2011-03-05 17:53:51 +00:00
|
|
|
waitSec := 0
|
|
|
|
if after == "" {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
if opts.MaxWait > 0 {
|
|
|
|
waitSec = int(opts.MaxWait.Seconds())
|
|
|
|
if waitSec == 0 {
|
|
|
|
waitSec = 1
|
|
|
|
}
|
|
|
|
}
|
2011-03-05 17:53:51 +00:00
|
|
|
}
|
2011-08-25 15:14:47 +00:00
|
|
|
url_ := fmt.Sprintf("%s/camli/enumerate-blobs?after=%s&limit=%d&maxwaitsec=%d",
|
2012-11-08 04:23:45 +00:00
|
|
|
pfx, url.QueryEscape(after), enumerateBatchSize, waitSec)
|
2011-08-25 15:14:47 +00:00
|
|
|
req := c.newRequest("GET", url_)
|
2011-05-10 13:25:18 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2011-03-02 02:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return error("http request", err)
|
|
|
|
}
|
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
json, err := c.responseJSONMap("enumerate-blobs", resp)
|
2011-03-02 02:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return error("stat json parse error", err)
|
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:24 +00:00
|
|
|
blobs, ok := getJSONMapArray(json, "blobs")
|
2011-03-02 02:02:01 +00:00
|
|
|
if !ok {
|
|
|
|
return error("response JSON didn't contain 'blobs' array", nil)
|
|
|
|
}
|
|
|
|
for _, v := range blobs {
|
2012-12-22 01:00:24 +00:00
|
|
|
itemJSON, ok := v.(map[string]interface{})
|
2011-03-02 02:02:01 +00:00
|
|
|
if !ok {
|
|
|
|
return error("item in 'blobs' was malformed", nil)
|
|
|
|
}
|
2012-12-22 01:00:24 +00:00
|
|
|
blobrefStr, ok := getJSONMapString(itemJSON, "blobRef")
|
2011-03-02 02:02:01 +00:00
|
|
|
if !ok {
|
|
|
|
return error("item in 'blobs' was missing string 'blobRef'", nil)
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
size, ok := getJSONMapUint32(itemJSON, "size")
|
2011-03-02 02:02:01 +00:00
|
|
|
if !ok {
|
|
|
|
return error("item in 'blobs' was missing numeric 'size'", nil)
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
br, ok := blob.Parse(blobrefStr)
|
|
|
|
if !ok {
|
2011-03-02 02:02:01 +00:00
|
|
|
return error("item in 'blobs' had invalid blobref.", nil)
|
|
|
|
}
|
2013-12-02 21:20:51 +00:00
|
|
|
select {
|
2014-01-28 20:46:52 +00:00
|
|
|
case ch <- blob.SizedRef{Ref: br, Size: uint32(size)}:
|
2013-12-02 21:20:51 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return context.ErrCanceled
|
|
|
|
}
|
2011-05-10 18:32:47 +00:00
|
|
|
nSent++
|
|
|
|
if opts.Limit == nSent {
|
|
|
|
// nSent can't be zero at this point, so opts.Limit being 0
|
|
|
|
// is okay.
|
|
|
|
return nil
|
|
|
|
}
|
2011-03-02 02:02:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:24 +00:00
|
|
|
after, keepGoing = getJSONMapString(json, "continueAfter")
|
2011-03-02 02:02:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:24 +00:00
|
|
|
func getJSONMapString(m map[string]interface{}, key string) (string, bool) {
|
2011-03-02 02:02:01 +00:00
|
|
|
if v, ok := m[key]; ok {
|
|
|
|
if s, ok := v.(string); ok {
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:24 +00:00
|
|
|
func getJSONMapInt64(m map[string]interface{}, key string) (int64, bool) {
|
2011-03-02 02:02:01 +00:00
|
|
|
if v, ok := m[key]; ok {
|
|
|
|
if n, ok := v.(float64); ok {
|
|
|
|
return int64(n), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2014-01-28 20:46:52 +00:00
|
|
|
func getJSONMapUint32(m map[string]interface{}, key string) (uint32, bool) {
|
|
|
|
u, ok := getJSONMapInt64(m, key)
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
if u < 0 || u > math.MaxUint32 {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return uint32(u), true
|
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:24 +00:00
|
|
|
func getJSONMapArray(m map[string]interface{}, key string) ([]interface{}, bool) {
|
2011-03-02 02:02:01 +00:00
|
|
|
if v, ok := m[key]; ok {
|
|
|
|
if a, ok := v.([]interface{}); ok {
|
|
|
|
return a, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|