2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-28 15:56:37 +00:00

Update github.com/kurin/blazer

This commit is contained in:
Alexander Neumann 2018-06-09 12:32:18 +02:00
parent ab37c6095a
commit f761068f4e
7 changed files with 65 additions and 31 deletions

4
Gopkg.lock generated
View File

@ -94,8 +94,8 @@
[[projects]] [[projects]]
name = "github.com/kurin/blazer" name = "github.com/kurin/blazer"
packages = ["b2","base","internal/b2assets","internal/b2types","internal/blog","x/window"] packages = ["b2","base","internal/b2assets","internal/b2types","internal/blog","x/window"]
revision = "3c18ed98a4120a440c8f45d8fbf41d414612a501" revision = "318e9768bf9a0fe52a64b9f8fe74f4f5caef6452"
version = "v0.4.2" version = "v0.4.4"
[[projects]] [[projects]]
name = "github.com/marstr/guid" name = "github.com/marstr/guid"

View File

@ -97,7 +97,7 @@ func downloadFile(ctx context.Context, bucket *b2.Bucket, downloads int, src, ds
```go ```go
func printObjects(ctx context.Context, bucket *b2.Bucket) error { func printObjects(ctx context.Context, bucket *b2.Bucket) error {
iterator := bucket.List() iterator := bucket.List(ctx)
for iterator.Next() { for iterator.Next() {
fmt.Println(itrator.Object()) fmt.Println(itrator.Object())
} }

View File

@ -64,8 +64,8 @@ func TestReadWriteLive(t *testing.T) {
t.Error(err) t.Error(err)
} }
iter := bucket.List(ListHidden()) iter := bucket.List(ctx, ListHidden())
for iter.Next(ctx) { for iter.Next() {
if err := iter.Object().Delete(ctx); err != nil { if err := iter.Object().Delete(ctx); err != nil {
t.Error(err) t.Error(err)
} }
@ -168,7 +168,7 @@ func TestHideShowLive(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
got, err := countObjects(ctx, bucket.List()) got, err := countObjects(bucket.List(ctx))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -186,7 +186,7 @@ func TestHideShowLive(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
got, err = countObjects(ctx, bucket.List()) got, err = countObjects(bucket.List(ctx))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -200,7 +200,7 @@ func TestHideShowLive(t *testing.T) {
} }
// count see the object again // count see the object again
got, err = countObjects(ctx, bucket.List()) got, err = countObjects(bucket.List(ctx))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -552,9 +552,9 @@ func TestListObjectsWithPrefix(t *testing.T) {
} }
for _, entry := range table { for _, entry := range table {
iter := bucket.List(entry.opts...) iter := bucket.List(ctx, entry.opts...)
var res []string var res []string
for iter.Next(ctx) { for iter.Next() {
o := iter.Object() o := iter.Object()
attrs, err := o.Attrs(ctx) attrs, err := o.Attrs(ctx)
if err != nil { if err != nil {
@ -743,8 +743,8 @@ func TestAttrsNoRoundtrip(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
iter := bucket.List() iter := bucket.List(ctx)
iter.Next(ctx) iter.Next()
obj := iter.Object() obj := iter.Object()
var trips int var trips int
@ -852,8 +852,8 @@ func TestListUnfinishedLargeFiles(t *testing.T) {
if _, err := io.Copy(w, io.LimitReader(zReader{}, 1e6)); err != nil { if _, err := io.Copy(w, io.LimitReader(zReader{}, 1e6)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
iter := bucket.List(ListUnfinished()) iter := bucket.List(ctx, ListUnfinished())
if !iter.Next(ctx) { if !iter.Next() {
t.Errorf("ListUnfinishedLargeFiles: got none, want 1 (error %v)", iter.Err()) t.Errorf("ListUnfinishedLargeFiles: got none, want 1 (error %v)", iter.Err())
} }
} }
@ -894,9 +894,9 @@ type object struct {
err error err error
} }
func countObjects(ctx context.Context, iter *ObjectIterator) (int, error) { func countObjects(iter *ObjectIterator) (int, error) {
var got int var got int
for iter.Next(ctx) { for iter.Next() {
got++ got++
} }
return got, iter.Err() return got, iter.Err()
@ -1004,8 +1004,8 @@ func startLiveTest(ctx context.Context, t *testing.T) (*Bucket, func()) {
} }
f := func() { f := func() {
defer ccport.done() defer ccport.done()
iter := bucket.List(ListHidden()) iter := bucket.List(ctx, ListHidden())
for iter.Next(ctx) { for iter.Next() {
if err := iter.Object().Delete(ctx); err != nil { if err := iter.Object().Delete(ctx); err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -22,9 +22,10 @@ import (
// List returns an iterator for selecting objects in a bucket. The default // List returns an iterator for selecting objects in a bucket. The default
// behavior, with no options, is to list all currently un-hidden objects. // behavior, with no options, is to list all currently un-hidden objects.
func (b *Bucket) List(opts ...ListOption) *ObjectIterator { func (b *Bucket) List(ctx context.Context, opts ...ListOption) *ObjectIterator {
o := &ObjectIterator{ o := &ObjectIterator{
bucket: b, bucket: b,
ctx: ctx,
} }
for _, opt := range opts { for _, opt := range opts {
opt(&o.opts) opt(&o.opts)
@ -36,7 +37,7 @@ func (b *Bucket) List(opts ...ListOption) *ObjectIterator {
// contents. // contents.
// //
// It is intended to be called in a loop: // It is intended to be called in a loop:
// for iter.Next(ctx) { // for iter.Next() {
// obj := iter.Object() // obj := iter.Object()
// // act on obj // // act on obj
// } // }
@ -45,6 +46,7 @@ func (b *Bucket) List(opts ...ListOption) *ObjectIterator {
// } // }
type ObjectIterator struct { type ObjectIterator struct {
bucket *Bucket bucket *Bucket
ctx context.Context
final bool final bool
err error err error
idx int idx int
@ -58,7 +60,11 @@ type ObjectIterator struct {
type lister func(context.Context, int, *Cursor) ([]*Object, *Cursor, error) type lister func(context.Context, int, *Cursor) ([]*Object, *Cursor, error)
func (o *ObjectIterator) frame(ctx context.Context) error { func (o *ObjectIterator) page(ctx context.Context) error {
if o.opts.locker != nil {
o.opts.locker.Lock()
defer o.opts.locker.Unlock()
}
objs, c, err := o.l(ctx, o.count, o.c) objs, c, err := o.l(ctx, o.count, o.c)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
if bNotExist.MatchString(err.Error()) { if bNotExist.MatchString(err.Error()) {
@ -82,13 +88,18 @@ func (o *ObjectIterator) frame(ctx context.Context) error {
// any calls to Object(). If Next returns true, then the next call to Object() // any calls to Object(). If Next returns true, then the next call to Object()
// will be valid. Once Next returns false, it is important to check the return // will be valid. Once Next returns false, it is important to check the return
// value of Err(). // value of Err().
func (o *ObjectIterator) Next(ctx context.Context) bool { func (o *ObjectIterator) Next() bool {
o.init.Do(func() { o.init.Do(func() {
o.count = 1000 o.count = o.opts.pageSize
if o.count < 0 || o.count > 1000 {
o.count = 1000
}
switch { switch {
case o.opts.unfinished: case o.opts.unfinished:
o.l = o.bucket.ListUnfinishedLargeFiles o.l = o.bucket.ListUnfinishedLargeFiles
o.count = 100 if o.count > 100 {
o.count = 100
}
case o.opts.hidden: case o.opts.hidden:
o.l = o.bucket.ListObjects o.l = o.bucket.ListObjects
default: default:
@ -102,16 +113,20 @@ func (o *ObjectIterator) Next(ctx context.Context) bool {
if o.err != nil { if o.err != nil {
return false return false
} }
if o.ctx.Err() != nil {
o.err = o.ctx.Err()
return false
}
if o.idx >= len(o.objs) { if o.idx >= len(o.objs) {
if o.final { if o.final {
o.err = io.EOF o.err = io.EOF
return false return false
} }
if err := o.frame(ctx); err != nil { if err := o.page(o.ctx); err != nil {
o.err = err o.err = err
return false return false
} }
return o.Next(ctx) return o.Next()
} }
o.idx++ o.idx++
return true return true
@ -136,6 +151,8 @@ type objectIteratorOptions struct {
unfinished bool unfinished bool
prefix string prefix string
delimiter string delimiter string
pageSize int
locker sync.Locker
} }
// A ListOption alters the default behavor of List. // A ListOption alters the default behavor of List.
@ -181,3 +198,20 @@ func ListDelimiter(delimiter string) ListOption {
o.delimiter = delimiter o.delimiter = delimiter
} }
} }
// ListPageSize configures the iterator to request the given number of objects
// per network round-trip. The default (and maximum) is 1000 objects, except
// for unfinished large files, which is 100.
func ListPageSize(count int) ListOption {
return func(o *objectIteratorOptions) {
o.pageSize = count
}
}
// ListLocker passes the iterator a lock which will be held during network
// round-trips.
func ListLocker(l sync.Locker) ListOption {
return func(o *objectIteratorOptions) {
o.locker = l
}
}

View File

@ -42,7 +42,7 @@ import (
const ( const (
APIBase = "https://api.backblazeb2.com" APIBase = "https://api.backblazeb2.com"
DefaultUserAgent = "blazer/0.4.2" DefaultUserAgent = "blazer/0.4.4"
) )
type b2err struct { type b2err struct {

View File

@ -61,8 +61,8 @@ func killBucket(ctx context.Context, client *b2.Client, name string) error {
return err return err
} }
defer bucket.Delete(ctx) defer bucket.Delete(ctx)
iter := bucket.List(b2.ListHidden()) iter := bucket.List(ctx, b2.ListHidden())
for iter.Next(ctx) { for iter.Next() {
if err := iter.Object().Delete(ctx); err != nil { if err := iter.Object().Delete(ctx); err != nil {
fmt.Println(err) fmt.Println(err)
} }

View File

@ -141,8 +141,8 @@ func startLiveTest(ctx context.Context, t *testing.T) (*b2.Bucket, func()) {
return nil, nil return nil, nil
} }
f := func() { f := func() {
iter := bucket.List(b2.ListHidden()) iter := bucket.List(ctx, b2.ListHidden())
for iter.Next(ctx) { for iter.Next() {
if err := iter.Object().Delete(ctx); err != nil { if err := iter.Object().Delete(ctx); err != nil {
t.Error(err) t.Error(err)
} }