2012-03-26 18:11:57 +00:00
|
|
|
// +build appengine
|
|
|
|
|
2011-10-05 21:34:55 +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 appengine
|
|
|
|
|
|
|
|
import (
|
2011-10-07 01:10:29 +00:00
|
|
|
"bytes"
|
2011-10-11 01:48:34 +00:00
|
|
|
"fmt"
|
2011-10-05 21:34:55 +00:00
|
|
|
"io"
|
2011-10-10 01:41:54 +00:00
|
|
|
"io/ioutil"
|
2011-10-07 06:24:40 +00:00
|
|
|
"log"
|
2013-01-09 02:41:06 +00:00
|
|
|
"net/http"
|
2011-10-05 21:34:55 +00:00
|
|
|
"os"
|
2011-12-04 23:19:28 +00:00
|
|
|
"regexp"
|
2011-10-13 01:10:58 +00:00
|
|
|
"strings"
|
2013-01-09 03:43:09 +00:00
|
|
|
"sync"
|
2013-01-08 01:20:38 +00:00
|
|
|
"time"
|
2011-10-05 21:34:55 +00:00
|
|
|
|
2011-10-07 00:44:30 +00:00
|
|
|
"appengine"
|
2011-10-07 01:10:29 +00:00
|
|
|
"appengine/blobstore"
|
2013-01-09 02:41:06 +00:00
|
|
|
"appengine/datastore"
|
2011-10-07 00:44:30 +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
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2011-10-05 21:34:55 +00:00
|
|
|
)
|
|
|
|
|
2011-10-07 06:24:40 +00:00
|
|
|
var _ = log.Printf
|
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
const (
|
|
|
|
blobKind = "Blob"
|
|
|
|
memKind = "NsBlobMember" // blob membership in a namespace
|
|
|
|
)
|
2011-10-07 06:24:40 +00:00
|
|
|
|
2011-11-02 04:37:10 +00:00
|
|
|
var _ blobserver.Storage = (*appengineStorage)(nil)
|
|
|
|
|
2011-10-05 21:34:55 +00:00
|
|
|
type appengineStorage struct {
|
|
|
|
*blobserver.SimpleBlobHubPartitionMap
|
2011-10-13 01:10:58 +00:00
|
|
|
namespace string // never empty; config initializes to at least "-"
|
|
|
|
ctx appengine.Context
|
2011-10-05 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
// blobEnt is stored once per unique blob, keyed by blobref.
|
2011-10-07 01:10:29 +00:00
|
|
|
type blobEnt struct {
|
2013-01-09 22:23:22 +00:00
|
|
|
Size int64 `datastore:"Size,noindex"`
|
|
|
|
BlobKey appengine.BlobKey `datastore:"BlobKey,noindex"`
|
|
|
|
Namespaces string `datastore:"Namespaces,noindex"` // |-separated string of namespaces
|
2011-10-11 00:58:59 +00:00
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
// TODO(bradfitz): IsCamliSchemaBlob bool? ... probably want
|
|
|
|
// on enumeration (memEnt) too.
|
|
|
|
}
|
|
|
|
|
|
|
|
// memEnt is stored once per blob in a namespace, keyed by "ns|blobref"
|
|
|
|
type memEnt struct {
|
2013-01-09 22:23:22 +00:00
|
|
|
Size int64 `datastore:"Size,noindex"`
|
2011-10-13 01:10:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func byteDecSize(b []byte) (int64, error) {
|
2011-10-11 01:48:34 +00:00
|
|
|
var size int64
|
2011-10-13 01:10:58 +00:00
|
|
|
n, err := fmt.Fscanf(bytes.NewBuffer(b), "%d", &size)
|
2011-10-11 01:48:34 +00:00
|
|
|
if n != 1 || err != nil {
|
2011-10-13 01:10:58 +00:00
|
|
|
return 0, fmt.Errorf("invalid Size column in datastore: %q", string(b))
|
2011-10-11 01:48:34 +00:00
|
|
|
}
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
func (b *blobEnt) inNamespace(ns string) (out bool) {
|
2013-01-09 22:23:22 +00:00
|
|
|
for _, in := range strings.Split(b.Namespaces, "|") {
|
2011-10-13 01:10:58 +00:00
|
|
|
if ns == in {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func entKey(c appengine.Context, br *blobref.BlobRef) *datastore.Key {
|
|
|
|
return datastore.NewKey(c, blobKind, br.String(), 0, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *appengineStorage) memKey(c appengine.Context, br *blobref.BlobRef) *datastore.Key {
|
|
|
|
return datastore.NewKey(c, memKind, fmt.Sprintf("%s|%s", s.namespace, br.String()), 0, nil)
|
|
|
|
}
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func fetchEnt(c appengine.Context, br *blobref.BlobRef) (*blobEnt, error) {
|
2011-10-13 01:10:58 +00:00
|
|
|
row := new(blobEnt)
|
|
|
|
err := datastore.Get(c, entKey(c, br), row)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return row, nil
|
|
|
|
}
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) {
|
2011-10-05 21:34:55 +00:00
|
|
|
sto := &appengineStorage{
|
|
|
|
SimpleBlobHubPartitionMap: &blobserver.SimpleBlobHubPartitionMap{},
|
|
|
|
}
|
2011-10-13 01:10:58 +00:00
|
|
|
sto.namespace = config.OptionalString("namespace", "")
|
2011-10-05 21:34:55 +00:00
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-10-22 01:42:09 +00:00
|
|
|
sto.namespace, err = sanitizeNamespace(sto.namespace)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-10-13 01:10:58 +00:00
|
|
|
}
|
2011-10-05 21:34:55 +00:00
|
|
|
return sto, nil
|
|
|
|
}
|
|
|
|
|
2013-01-09 03:43:09 +00:00
|
|
|
// TODO(bradfitz): delete all this context wrapper stuff
|
2011-10-07 00:44:30 +00:00
|
|
|
var _ blobserver.ContextWrapper = (*appengineStorage)(nil)
|
|
|
|
|
2013-01-09 03:43:09 +00:00
|
|
|
// TODO(bradfitz): delete all this context wrapper stuff
|
2011-10-07 00:44:30 +00:00
|
|
|
func (sto *appengineStorage) WrapContext(req *http.Request) blobserver.Storage {
|
|
|
|
s2 := new(appengineStorage)
|
|
|
|
*s2 = *sto
|
|
|
|
s2.ctx = appengine.NewContext(req)
|
|
|
|
return s2
|
|
|
|
}
|
|
|
|
|
2013-01-09 03:43:09 +00:00
|
|
|
var dummyCloser = ioutil.NopCloser(strings.NewReader(""))
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func (sto *appengineStorage) FetchStreaming(br *blobref.BlobRef) (file io.ReadCloser, size int64, err error) {
|
2013-01-09 03:43:09 +00:00
|
|
|
ctx := sto.ctx
|
|
|
|
var loan ContextLoan
|
|
|
|
if ctx == nil {
|
|
|
|
loan = ctxPool.Get()
|
|
|
|
ctx = loan
|
|
|
|
defer func() {
|
|
|
|
if loan != nil {
|
|
|
|
loan.Return()
|
|
|
|
}
|
|
|
|
}()
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
2013-01-09 03:43:09 +00:00
|
|
|
|
|
|
|
row, err := fetchEnt(ctx, br)
|
2011-10-10 01:41:54 +00:00
|
|
|
if err == datastore.ErrNoSuchEntity {
|
2012-05-13 19:06:21 +00:00
|
|
|
err = os.ErrNotExist
|
2011-10-10 01:41:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-10-13 01:10:58 +00:00
|
|
|
if !row.inNamespace(sto.namespace) {
|
2012-05-13 19:06:21 +00:00
|
|
|
err = os.ErrNotExist
|
2011-10-13 01:10:58 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-09 03:43:09 +00:00
|
|
|
var c io.Closer
|
|
|
|
if loan != nil {
|
|
|
|
closeLoan := loan
|
|
|
|
c = &onceCloser{fn: func() { closeLoan.Return() }}
|
|
|
|
loan = nil // take it, so it's not defer-closed
|
|
|
|
} else {
|
|
|
|
c = dummyCloser
|
|
|
|
}
|
|
|
|
reader := blobstore.NewReader(ctx, appengine.BlobKey(string(row.BlobKey)))
|
|
|
|
type readCloser struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
2013-01-09 22:23:22 +00:00
|
|
|
return readCloser{reader, c}, row.Size, nil
|
2013-01-09 03:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type onceCloser struct {
|
|
|
|
once sync.Once
|
|
|
|
fn func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oc *onceCloser) Close() error {
|
|
|
|
oc.once.Do(oc.fn)
|
|
|
|
return nil
|
2011-10-05 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
var crossGroupTransaction = &datastore.TransactionOptions{XG: true}
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func (sto *appengineStorage) ReceiveBlob(br *blobref.BlobRef, in io.Reader) (sb blobref.SizedBlobRef, err error) {
|
2013-01-09 03:43:09 +00:00
|
|
|
ctx := sto.ctx
|
|
|
|
if ctx == nil {
|
|
|
|
loan := ctxPool.Get()
|
|
|
|
defer loan.Return()
|
|
|
|
ctx = loan
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
2011-10-07 01:10:29 +00:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
hash := br.Hash()
|
|
|
|
written, err := io.Copy(io.MultiWriter(hash, &b), in)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !br.HashMatches(hash) {
|
|
|
|
err = blobserver.ErrCorruptBlob
|
|
|
|
return
|
|
|
|
}
|
2011-10-13 01:10:58 +00:00
|
|
|
|
|
|
|
// bkey is non-empty once we've uploaded the blob.
|
|
|
|
var bkey appengine.BlobKey
|
|
|
|
|
|
|
|
// uploadBlob uploads the blob, unless it's already been done.
|
2013-01-08 01:20:38 +00:00
|
|
|
uploadBlob := func(ctx appengine.Context) error {
|
2011-10-13 01:10:58 +00:00
|
|
|
if len(bkey) > 0 {
|
|
|
|
return nil // already done in previous transaction attempt
|
|
|
|
}
|
|
|
|
bw, err := blobstore.Create(ctx, "application/octet-stream")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(bw, &b)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(bradfitz): try to clean up; close it, see if we can find the key, delete it.
|
|
|
|
ctx.Errorf("blobstore Copy error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bw.Close()
|
|
|
|
if err != nil {
|
|
|
|
// TODO(bradfitz): try to clean up; see if we can find the key, delete it.
|
|
|
|
ctx.Errorf("blobstore Close error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k, err := bw.Key()
|
|
|
|
if err == nil {
|
|
|
|
bkey = k
|
|
|
|
}
|
|
|
|
return err
|
2011-10-07 06:24:40 +00:00
|
|
|
}
|
2011-10-07 01:10:29 +00:00
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
tryFunc := func(tc appengine.Context) error {
|
2013-01-09 03:43:09 +00:00
|
|
|
row, err := fetchEnt(tc, br)
|
2011-10-13 01:10:58 +00:00
|
|
|
switch err {
|
|
|
|
case datastore.ErrNoSuchEntity:
|
2013-01-09 03:43:09 +00:00
|
|
|
if err := uploadBlob(tc); err != nil {
|
2011-10-13 01:10:58 +00:00
|
|
|
tc.Errorf("uploadBlob failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
row = &blobEnt{
|
2013-01-09 22:23:22 +00:00
|
|
|
Size: written,
|
|
|
|
BlobKey: bkey,
|
|
|
|
Namespaces: sto.namespace,
|
2011-10-13 01:10:58 +00:00
|
|
|
}
|
|
|
|
_, err = datastore.Put(tc, entKey(tc, br), row)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
if row.inNamespace(sto.namespace) {
|
|
|
|
// Nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
2013-01-09 22:23:22 +00:00
|
|
|
row.Namespaces = row.Namespaces + "|" + sto.namespace
|
2011-10-13 01:10:58 +00:00
|
|
|
_, err = datastore.Put(tc, entKey(tc, br), row)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2011-10-07 01:10:29 +00:00
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
// Add membership row
|
|
|
|
_, err = datastore.Put(tc, sto.memKey(tc, br), &memEnt{
|
2013-01-09 22:23:22 +00:00
|
|
|
Size: written,
|
2011-10-13 01:10:58 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2013-01-09 03:43:09 +00:00
|
|
|
err = datastore.RunInTransaction(ctx, tryFunc, crossGroupTransaction)
|
2011-10-07 01:10:29 +00:00
|
|
|
if err != nil {
|
2011-10-13 01:10:58 +00:00
|
|
|
if len(bkey) > 0 {
|
|
|
|
// If we just created this blob but we
|
|
|
|
// ultimately failed, try our best to delete
|
|
|
|
// it so it's not orphaned.
|
2013-01-09 03:43:09 +00:00
|
|
|
blobstore.Delete(ctx, bkey)
|
2011-10-13 01:10:58 +00:00
|
|
|
}
|
2011-10-07 01:10:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return blobref.SizedBlobRef{br, written}, nil
|
2011-10-05 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 07:05:02 +00:00
|
|
|
// NOTE(bslatkin): No fucking clue if this works.
|
2013-01-08 01:20:38 +00:00
|
|
|
func (sto *appengineStorage) RemoveBlobs(blobs []*blobref.BlobRef) error {
|
2013-01-09 03:43:09 +00:00
|
|
|
ctx := sto.ctx
|
|
|
|
if ctx == nil {
|
|
|
|
loan := ctxPool.Get()
|
|
|
|
defer loan.Return()
|
|
|
|
ctx = loan
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
2011-11-02 07:05:02 +00:00
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
tryFunc := func(tc appengine.Context, br *blobref.BlobRef) error {
|
2011-11-02 07:05:02 +00:00
|
|
|
// TODO(bslatkin): Make the DB gets in this a multi-get.
|
|
|
|
// Remove the namespace from the blobEnt
|
2013-01-09 03:43:09 +00:00
|
|
|
row, err := fetchEnt(tc, br)
|
2011-11-02 07:05:02 +00:00
|
|
|
switch err {
|
|
|
|
case datastore.ErrNoSuchEntity:
|
|
|
|
// Doesn't exist, that means there should be no memEnt, but let's be
|
|
|
|
// paranoid and double check anyways.
|
|
|
|
case nil:
|
|
|
|
// blobEnt exists, remove our namespace from it if possible.
|
|
|
|
newNS := []string{}
|
|
|
|
for _, val := range strings.Split(string(row.Namespaces), "|") {
|
|
|
|
if val != sto.namespace {
|
|
|
|
newNS = append(newNS, val)
|
|
|
|
}
|
|
|
|
}
|
2013-01-09 22:23:22 +00:00
|
|
|
if v := strings.Join(newNS, "|"); v != row.Namespaces {
|
|
|
|
row.Namespaces = v
|
2011-11-02 07:05:02 +00:00
|
|
|
_, err = datastore.Put(tc, entKey(tc, br), row)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blindly delete the memEnt.
|
2013-01-09 02:41:06 +00:00
|
|
|
err = datastore.Delete(tc, sto.memKey(tc, br))
|
2011-11-02 07:05:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, br := range blobs {
|
|
|
|
ret := datastore.RunInTransaction(
|
2013-01-09 03:43:09 +00:00
|
|
|
ctx,
|
2013-01-08 01:20:38 +00:00
|
|
|
func(tc appengine.Context) error {
|
2011-11-02 07:05:02 +00:00
|
|
|
return tryFunc(tc, br)
|
|
|
|
},
|
|
|
|
crossGroupTransaction)
|
|
|
|
if ret != nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2011-10-05 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func (sto *appengineStorage) StatBlobs(dest chan<- blobref.SizedBlobRef, blobs []*blobref.BlobRef, wait time.Duration) error {
|
2013-01-09 03:43:09 +00:00
|
|
|
ctx := sto.ctx
|
|
|
|
if ctx == nil {
|
|
|
|
loan := ctxPool.Get()
|
|
|
|
defer loan.Return()
|
|
|
|
ctx = loan
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
2013-01-09 03:43:09 +00:00
|
|
|
|
2011-10-07 06:24:40 +00:00
|
|
|
var (
|
|
|
|
keys = make([]*datastore.Key, 0, len(blobs))
|
|
|
|
out = make([]interface{}, 0, len(blobs))
|
2013-01-08 01:20:38 +00:00
|
|
|
errs = make([]error, len(blobs))
|
2011-10-07 06:24:40 +00:00
|
|
|
)
|
|
|
|
for _, br := range blobs {
|
2013-01-09 03:43:09 +00:00
|
|
|
keys = append(keys, sto.memKey(ctx, br))
|
2011-10-13 01:10:58 +00:00
|
|
|
out = append(out, new(memEnt))
|
2011-10-07 06:24:40 +00:00
|
|
|
}
|
2013-01-09 03:43:09 +00:00
|
|
|
err := datastore.GetMulti(ctx, keys, out)
|
2013-01-08 01:20:38 +00:00
|
|
|
if merr, ok := err.(appengine.MultiError); ok {
|
|
|
|
errs = []error(merr)
|
2011-10-07 06:24:40 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, br := range blobs {
|
|
|
|
thisErr := errs[i]
|
|
|
|
if thisErr == datastore.ErrNoSuchEntity {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if thisErr != nil {
|
|
|
|
err = errs[i] // just return last one found?
|
|
|
|
continue
|
|
|
|
}
|
2011-10-13 01:10:58 +00:00
|
|
|
ent := out[i].(*memEnt)
|
2013-01-09 22:23:22 +00:00
|
|
|
dest <- blobref.SizedBlobRef{br, ent.Size}
|
2011-10-07 06:24:40 +00:00
|
|
|
}
|
|
|
|
return err
|
2011-10-05 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func (sto *appengineStorage) EnumerateBlobs(dest chan<- blobref.SizedBlobRef, after string, limit int, wait time.Duration) error {
|
2011-10-11 00:58:59 +00:00
|
|
|
defer close(dest)
|
2013-01-09 03:43:09 +00:00
|
|
|
|
|
|
|
ctx := sto.ctx
|
|
|
|
if ctx == nil {
|
|
|
|
loan := ctxPool.Get()
|
|
|
|
defer loan.Return()
|
|
|
|
ctx = loan
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
2013-01-09 03:43:09 +00:00
|
|
|
|
2011-10-13 01:10:58 +00:00
|
|
|
prefix := sto.namespace + "|"
|
2013-01-09 03:43:09 +00:00
|
|
|
keyBegin := datastore.NewKey(ctx, memKind, prefix+after, 0, nil)
|
|
|
|
keyEnd := datastore.NewKey(ctx, memKind, sto.namespace+"~", 0, nil)
|
2011-10-13 01:10:58 +00:00
|
|
|
|
|
|
|
q := datastore.NewQuery(memKind).Limit(int(limit)).Filter("__key__>", keyBegin).Filter("__key__<", keyEnd)
|
2013-01-09 03:43:09 +00:00
|
|
|
it := q.Run(ctx)
|
2011-10-13 01:10:58 +00:00
|
|
|
var row memEnt
|
2011-10-11 00:58:59 +00:00
|
|
|
for {
|
2011-10-13 01:10:58 +00:00
|
|
|
key, err := it.Next(&row)
|
2011-10-11 00:58:59 +00:00
|
|
|
if err == datastore.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-09 22:23:22 +00:00
|
|
|
dest <- blobref.SizedBlobRef{blobref.Parse(key.StringID()[len(prefix):]), row.Size}
|
2011-10-11 00:58:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
2011-10-05 21:34:55 +00:00
|
|
|
}
|
2011-12-04 23:19:28 +00:00
|
|
|
|
|
|
|
var validQueueName = regexp.MustCompile(`^[a-zA-Z0-9\-\_]+$`)
|
|
|
|
|
|
|
|
// TODO(bslatkin): This does not work on App Engine yet because there are no
|
|
|
|
// background threads to do the sync loop. The plan is to break the
|
|
|
|
// syncer code up into two parts: 1) accepts notifications of new blobs to
|
|
|
|
// sync, 2) does one unit of work enumerating recent blobs and syncing them.
|
|
|
|
// In App Engine land, 1) will result in a task to be enqueued, and 2) will
|
|
|
|
// be called from within that queue context.
|
|
|
|
|
2013-01-08 01:20:38 +00:00
|
|
|
func (sto *appengineStorage) CreateQueue(name string) (blobserver.Storage, error) {
|
2011-12-04 23:19:28 +00:00
|
|
|
if !validQueueName.MatchString(name) {
|
|
|
|
return nil, fmt.Errorf("invalid queue name %q", name)
|
|
|
|
}
|
|
|
|
if sto.namespace != "" && strings.HasPrefix(sto.namespace, "queue-") {
|
|
|
|
return nil, fmt.Errorf("can't create queue %q on existing queue %q",
|
|
|
|
name, sto.namespace)
|
|
|
|
}
|
|
|
|
q := &appengineStorage{
|
|
|
|
SimpleBlobHubPartitionMap: &blobserver.SimpleBlobHubPartitionMap{},
|
2013-01-09 02:41:06 +00:00
|
|
|
namespace: "queue-" + name,
|
2011-12-04 23:19:28 +00:00
|
|
|
}
|
|
|
|
return q, nil
|
|
|
|
}
|