2011-03-24 23:47:51 +00:00
|
|
|
/*
|
2014-08-26 18:43:29 +00:00
|
|
|
Copyright 2011 The Camlistore Authors
|
2011-03-24 23:47:51 +00:00
|
|
|
|
|
|
|
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 test
|
|
|
|
|
|
|
|
import (
|
2011-05-06 04:45:24 +00:00
|
|
|
"io"
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-07-07 20:02:42 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2014-08-26 18:43:29 +00:00
|
|
|
"camlistore.org/pkg/blobserver/memory"
|
2011-03-24 23:47:51 +00:00
|
|
|
)
|
|
|
|
|
2013-07-07 20:02:42 +00:00
|
|
|
// Fetcher is an in-memory implementation of the blobserver Storage
|
|
|
|
// interface. It started as just a fetcher and grew. It also includes
|
|
|
|
// other convenience methods for testing.
|
2011-03-24 23:47:51 +00:00
|
|
|
type Fetcher struct {
|
2014-08-26 18:43:29 +00:00
|
|
|
memory.Storage
|
2014-08-11 22:38:53 +00:00
|
|
|
|
2014-01-19 06:53:03 +00:00
|
|
|
// ReceiveErr optionally returns the error to return on receive.
|
|
|
|
ReceiveErr error
|
|
|
|
|
2013-09-22 12:10:16 +00:00
|
|
|
// FetchErr, if non-nil, specifies the error to return on the next fetch call.
|
|
|
|
// If it returns nil, fetches proceed as normal.
|
|
|
|
FetchErr func() error
|
2011-03-24 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2013-07-07 20:02:42 +00:00
|
|
|
var _ blobserver.Storage = (*Fetcher)(nil)
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (tf *Fetcher) Fetch(ref blob.Ref) (file io.ReadCloser, size uint32, err error) {
|
2013-09-22 12:10:16 +00:00
|
|
|
if tf.FetchErr != nil {
|
|
|
|
if err = tf.FetchErr(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 18:43:29 +00:00
|
|
|
file, size, err = tf.Storage.Fetch(ref)
|
|
|
|
if err != nil {
|
2011-03-24 23:47:51 +00:00
|
|
|
return
|
|
|
|
}
|
2014-08-26 18:43:29 +00:00
|
|
|
return file, size, nil
|
2011-03-24 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 23:47:05 +00:00
|
|
|
func (tf *Fetcher) SubFetch(ref blob.Ref, offset, length int64) (io.ReadCloser, error) {
|
|
|
|
if tf.FetchErr != nil {
|
|
|
|
if err := tf.FetchErr(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 18:43:29 +00:00
|
|
|
rc, err := tf.Storage.SubFetch(ref, offset, length)
|
|
|
|
if err != nil {
|
|
|
|
return rc, err
|
2012-12-25 20:17:45 +00:00
|
|
|
}
|
2014-08-26 18:43:29 +00:00
|
|
|
return rc, nil
|
2012-12-25 20:17:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (tf *Fetcher) ReceiveBlob(br blob.Ref, source io.Reader) (blob.SizedRef, error) {
|
2014-08-26 18:43:29 +00:00
|
|
|
sb, err := tf.Storage.ReceiveBlob(br, source)
|
2012-12-25 20:17:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return sb, err
|
|
|
|
}
|
2014-01-19 06:53:03 +00:00
|
|
|
if err := tf.ReceiveErr; err != nil {
|
2014-08-26 18:43:29 +00:00
|
|
|
tf.RemoveBlobs([]blob.Ref{br})
|
2014-01-19 06:53:03 +00:00
|
|
|
return sb, err
|
|
|
|
}
|
2014-08-26 18:43:29 +00:00
|
|
|
return sb, nil
|
2013-07-07 20:02:42 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:43:29 +00:00
|
|
|
func (tf *Fetcher) AddBlob(b *Blob) {
|
|
|
|
_, err := tf.ReceiveBlob(b.BlobRef(), b.Reader())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2013-07-07 20:02:42 +00:00
|
|
|
}
|
|
|
|
}
|