backend: Remove Get()

This is the first commit that removes the (redundant) Get() method of
the backend interface. Get(x, y) is equivalent to GetReader(x, y, 0, 0).
This commit is contained in:
Alexander Neumann 2016-01-23 13:13:05 +01:00
parent d3a6e2a991
commit 8b7bf8691d
12 changed files with 12 additions and 105 deletions

View File

@ -17,7 +17,7 @@ import (
func testBackendConfig(b backend.Backend, t *testing.T) { func testBackendConfig(b backend.Backend, t *testing.T) {
// create config and read it back // create config and read it back
_, err := b.Get(backend.Config, "") _, err := b.GetReader(backend.Config, "", 0, 0)
Assert(t, err != nil, "did not get expected error for non-existing config") Assert(t, err != nil, "did not get expected error for non-existing config")
blob, err := b.Create() blob, err := b.Create()
@ -30,7 +30,7 @@ func testBackendConfig(b backend.Backend, t *testing.T) {
// try accessing the config with different names, should all return the // try accessing the config with different names, should all return the
// same config // same config
for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} { for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} {
rd, err := b.Get(backend.Config, name) rd, err := b.GetReader(backend.Config, name, 0, 0)
Assert(t, err == nil, "unable to read config") Assert(t, err == nil, "unable to read config")
buf, err := ioutil.ReadAll(rd) buf, err := ioutil.ReadAll(rd)
@ -116,7 +116,7 @@ func testWrite(b backend.Backend, t testing.TB) {
name := fmt.Sprintf("%s-%d", id, i) name := fmt.Sprintf("%s-%d", id, i)
OK(t, blob.Finalize(backend.Data, name)) OK(t, blob.Finalize(backend.Data, name))
rd, err := b.Get(backend.Data, name) rd, err := b.GetReader(backend.Data, name, 0, 0)
OK(t, err) OK(t, err)
buf, err := ioutil.ReadAll(rd) buf, err := ioutil.ReadAll(rd)
@ -169,7 +169,7 @@ func testBackend(b backend.Backend, t *testing.T) {
Assert(t, !ret, "blob was found to exist before creating") Assert(t, !ret, "blob was found to exist before creating")
// try to open not existing blob // try to open not existing blob
_, err = b.Get(tpe, id.String()) _, err = b.GetReader(tpe, id.String(), 0, 0)
Assert(t, err != nil, "blob data could be extracted before creation") Assert(t, err != nil, "blob data could be extracted before creation")
// try to read not existing blob // try to read not existing blob
@ -186,16 +186,8 @@ func testBackend(b backend.Backend, t *testing.T) {
for _, test := range TestStrings { for _, test := range TestStrings {
store(t, b, tpe, []byte(test.data)) store(t, b, tpe, []byte(test.data))
// test Get()
rd, err := b.Get(tpe, test.id)
OK(t, err)
Assert(t, rd != nil, "Get() returned nil")
read(t, rd, []byte(test.data))
OK(t, rd.Close())
// test GetReader() // test GetReader()
rd, err = b.GetReader(tpe, test.id, 0, uint(len(test.data))) rd, err := b.GetReader(tpe, test.id, 0, uint(len(test.data)))
OK(t, err) OK(t, err)
Assert(t, rd != nil, "GetReader() returned nil") Assert(t, rd != nil, "GetReader() returned nil")

View File

@ -42,9 +42,6 @@ type Backend interface {
// has been called on the returned Blob. // has been called on the returned Blob.
Create() (Blob, error) Create() (Blob, error)
// Get returns an io.ReadCloser for the Blob with the given name of type t.
Get(t Type, name string) (io.ReadCloser, error)
// GetReader returns an io.ReadCloser for the Blob with the given name of // GetReader returns an io.ReadCloser for the Blob with the given name of
// type t at offset and length. // type t at offset and length.
GetReader(t Type, name string, offset, length uint) (io.ReadCloser, error) GetReader(t Type, name string, offset, length uint) (io.ReadCloser, error)

View File

@ -196,20 +196,6 @@ func dirname(base string, t backend.Type, name string) string {
return filepath.Join(base, n) return filepath.Join(base, n)
} }
// Get returns a reader that yields the content stored under the given
// name. The reader should be closed after draining it.
func (b *Local) Get(t backend.Type, name string) (io.ReadCloser, error) {
file, err := os.Open(filename(b.p, t, name))
if err != nil {
return nil, err
}
b.mu.Lock()
open, _ := b.open[filename(b.p, t, name)]
b.open[filename(b.p, t, name)] = append(open, file)
b.mu.Unlock()
return file, nil
}
// GetReader returns an io.ReadCloser for the Blob with the given name of // GetReader returns an io.ReadCloser for the Blob with the given name of
// type t at offset and length. If length is 0, the reader reads until EOF. // type t at offset and length. If length is 0, the reader reads until EOF.
func (b *Local) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { func (b *Local) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {

View File

@ -41,10 +41,6 @@ func NewMemoryBackend() *MemoryBackend {
return memCreate(be) return memCreate(be)
} }
be.MockBackend.GetFn = func(t Type, name string) (io.ReadCloser, error) {
return memGet(be, t, name)
}
be.MockBackend.GetReaderFn = func(t Type, name string, offset, length uint) (io.ReadCloser, error) { be.MockBackend.GetReaderFn = func(t Type, name string, offset, length uint) (io.ReadCloser, error) {
return memGetReader(be, t, name, offset, length) return memGetReader(be, t, name, offset, length)
} }
@ -143,23 +139,6 @@ func (rd readCloser) Close() error {
return nil return nil
} }
func memGet(be *MemoryBackend, t Type, name string) (io.ReadCloser, error) {
be.m.Lock()
defer be.m.Unlock()
if t == Config {
name = ""
}
debug.Log("MemoryBackend.Get", "get %v %v", t, name)
if _, ok := be.data[entry{t, name}]; !ok {
return nil, errors.New("no such data")
}
return readCloser{bytes.NewReader(be.data[entry{t, name}])}, nil
}
func memGetReader(be *MemoryBackend, t Type, name string, offset, length uint) (io.ReadCloser, error) { func memGetReader(be *MemoryBackend, t Type, name string, offset, length uint) (io.ReadCloser, error) {
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()

View File

@ -10,7 +10,6 @@ import (
type MockBackend struct { type MockBackend struct {
CloseFn func() error CloseFn func() error
CreateFn func() (Blob, error) CreateFn func() (Blob, error)
GetFn func(Type, string) (io.ReadCloser, error)
GetReaderFn func(Type, string, uint, uint) (io.ReadCloser, error) GetReaderFn func(Type, string, uint, uint) (io.ReadCloser, error)
ListFn func(Type, <-chan struct{}) <-chan string ListFn func(Type, <-chan struct{}) <-chan string
RemoveFn func(Type, string) error RemoveFn func(Type, string) error
@ -43,14 +42,6 @@ func (m *MockBackend) Create() (Blob, error) {
return m.CreateFn() return m.CreateFn()
} }
func (m *MockBackend) Get(t Type, name string) (io.ReadCloser, error) {
if m.GetFn == nil {
return nil, errors.New("not implemented")
}
return m.GetFn(t, name)
}
func (m *MockBackend) GetReader(t Type, name string, offset, len uint) (io.ReadCloser, error) { func (m *MockBackend) GetReader(t Type, name string, offset, len uint) (io.ReadCloser, error) {
if m.GetReaderFn == nil { if m.GetReaderFn == nil {
return nil, errors.New("not implemented") return nil, errors.New("not implemented")

View File

@ -145,19 +145,6 @@ func (be *S3Backend) Create() (backend.Blob, error) {
return &blob, nil return &blob, nil
} }
// Get returns a reader that yields the content stored under the given
// name. The reader should be closed after draining it.
func (be *S3Backend) Get(t backend.Type, name string) (io.ReadCloser, error) {
path := s3path(t, name)
rc, err := be.client.GetObject(be.bucketname, path)
debug.Log("s3.Get", "%v %v -> err %v", t, name, err)
if err != nil {
return nil, err
}
return rc, nil
}
// GetReader returns an io.ReadCloser for the Blob with the given name of // GetReader returns an io.ReadCloser for the Blob with the given name of
// type t at offset and length. If length is 0, the reader reads until EOF. // type t at offset and length. If length is 0, the reader reads until EOF.
func (be *S3Backend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { func (be *S3Backend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {

View File

@ -344,18 +344,6 @@ func (r *SFTP) dirname(t backend.Type, name string) string {
return Join(r.p, n) return Join(r.p, n)
} }
// Get returns a reader that yields the content stored under the given
// name. The reader should be closed after draining it.
func (r *SFTP) Get(t backend.Type, name string) (io.ReadCloser, error) {
// try to open file
file, err := r.c.Open(r.filename(t, name))
if err != nil {
return nil, err
}
return file, nil
}
// GetReader returns an io.ReadCloser for the Blob with the given name of // GetReader returns an io.ReadCloser for the Blob with the given name of
// type t at offset and length. If length is 0, the reader reads until EOF. // type t at offset and length. If length is 0, the reader reads until EOF.
func (r *SFTP) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { func (r *SFTP) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {

View File

@ -647,7 +647,7 @@ func (c *Checker) CountPacks() uint64 {
// checkPack reads a pack and checks the integrity of all blobs. // checkPack reads a pack and checks the integrity of all blobs.
func checkPack(r *repository.Repository, id backend.ID) error { func checkPack(r *repository.Repository, id backend.ID) error {
debug.Log("Checker.checkPack", "checking pack %v", id.Str()) debug.Log("Checker.checkPack", "checking pack %v", id.Str())
rd, err := r.Backend().Get(backend.Data, id.String()) rd, err := r.Backend().GetReader(backend.Data, id.String(), 0, 0)
if err != nil { if err != nil {
return err return err
} }

View File

@ -214,19 +214,6 @@ type errorBackend struct {
backend.Backend backend.Backend
} }
func (b errorBackend) Get(t backend.Type, name string) (io.ReadCloser, error) {
rd, err := b.Backend.Get(t, name)
if err != nil {
return rd, err
}
if t != backend.Data {
return rd, err
}
return backend.ReadCloser(faultReader{rd}), nil
}
func (b errorBackend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { func (b errorBackend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
rd, err := b.Backend.GetReader(t, name, offset, length) rd, err := b.Backend.GetReader(t, name, offset, length)
if err != nil { if err != nil {

View File

@ -101,7 +101,7 @@ func (cmd CmdCat) Execute(args []string) error {
return nil return nil
case "key": case "key":
rd, err := repo.Backend().Get(backend.Key, id.String()) rd, err := repo.Backend().GetReader(backend.Key, id.String(), 0, 0)
if err != nil { if err != nil {
return err return err
} }
@ -153,7 +153,7 @@ func (cmd CmdCat) Execute(args []string) error {
switch tpe { switch tpe {
case "pack": case "pack":
rd, err := repo.Backend().Get(backend.Data, id.String()) rd, err := repo.Backend().GetReader(backend.Data, id.String(), 0, 0)
if err != nil { if err != nil {
return err return err
} }

View File

@ -120,7 +120,7 @@ func SearchKey(s *Repository, password string) (*Key, error) {
// LoadKey loads a key from the backend. // LoadKey loads a key from the backend.
func LoadKey(s *Repository, name string) (k *Key, err error) { func LoadKey(s *Repository, name string) (k *Key, err error) {
// extract data from repo // extract data from repo
rd, err := s.be.Get(backend.Key, name) rd, err := s.be.GetReader(backend.Key, name, 0, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -56,7 +56,7 @@ func (r *Repository) PrefixLength(t backend.Type) (int, error) {
func (r *Repository) LoadAndDecrypt(t backend.Type, id backend.ID) ([]byte, error) { func (r *Repository) LoadAndDecrypt(t backend.Type, id backend.ID) ([]byte, error) {
debug.Log("Repo.Load", "load %v with id %v", t, id.Str()) debug.Log("Repo.Load", "load %v with id %v", t, id.Str())
rd, err := r.be.Get(t, id.String()) rd, err := r.be.GetReader(t, id.String(), 0, 0)
if err != nil { if err != nil {
debug.Log("Repo.Load", "error loading %v: %v", id.Str(), err) debug.Log("Repo.Load", "error loading %v: %v", id.Str(), err)
return nil, err return nil, err
@ -157,7 +157,7 @@ func closeOrErr(cl io.Closer, err *error) {
// the item. // the item.
func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) (err error) { func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) (err error) {
// load blob from backend // load blob from backend
rd, err := r.be.Get(t, id.String()) rd, err := r.be.GetReader(t, id.String(), 0, 0)
if err != nil { if err != nil {
return err return err
} }
@ -548,7 +548,7 @@ func LoadIndex(repo *Repository, id string) (*Index, error) {
// GetDecryptReader opens the file id stored in the backend and returns a // GetDecryptReader opens the file id stored in the backend and returns a
// reader that yields the decrypted content. The reader must be closed. // reader that yields the decrypted content. The reader must be closed.
func (r *Repository) GetDecryptReader(t backend.Type, id string) (io.ReadCloser, error) { func (r *Repository) GetDecryptReader(t backend.Type, id string) (io.ReadCloser, error) {
rd, err := r.be.Get(t, id) rd, err := r.be.GetReader(t, id, 0, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }