Move MemoryBackend to backend/mem

This commit is contained in:
Alexander Neumann 2016-01-23 19:19:26 +01:00
parent f05a32509e
commit 0b50f9e02c
6 changed files with 54 additions and 48 deletions

View file

@ -1,5 +1,5 @@
// DO NOT EDIT, AUTOMATICALLY GENERATED // DO NOT EDIT, AUTOMATICALLY GENERATED
package backend_test package mem_test
import ( import (
"testing" "testing"

View file

@ -1,4 +1,4 @@
package backend package mem
import ( import (
"bytes" "bytes"
@ -7,11 +7,12 @@ import (
"sort" "sort"
"sync" "sync"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug" "github.com/restic/restic/debug"
) )
type entry struct { type entry struct {
Type Type Type backend.Type
Name string Name string
} }
@ -23,37 +24,36 @@ type MemoryBackend struct {
data memMap data memMap
m sync.Mutex m sync.Mutex
MockBackend backend.MockBackend
} }
// NewMemoryBackend returns a new backend that saves all data in a map in // New returns a new backend that saves all data in a map in memory.
// memory. func New() *MemoryBackend {
func NewMemoryBackend() *MemoryBackend {
be := &MemoryBackend{ be := &MemoryBackend{
data: make(memMap), data: make(memMap),
} }
be.MockBackend.TestFn = func(t Type, name string) (bool, error) { be.MockBackend.TestFn = func(t backend.Type, name string) (bool, error) {
return memTest(be, t, name) return memTest(be, t, name)
} }
be.MockBackend.CreateFn = func() (Blob, error) { be.MockBackend.CreateFn = func() (backend.Blob, error) {
return memCreate(be) return memCreate(be)
} }
be.MockBackend.GetReaderFn = func(t Type, name string, offset, length uint) (io.ReadCloser, error) { be.MockBackend.GetReaderFn = func(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
return memGetReader(be, t, name, offset, length) return memGetReader(be, t, name, offset, length)
} }
be.MockBackend.LoadFn = func(h Handle, p []byte, off int64) (int, error) { be.MockBackend.LoadFn = func(h backend.Handle, p []byte, off int64) (int, error) {
return memLoad(be, h, p, off) return memLoad(be, h, p, off)
} }
be.MockBackend.RemoveFn = func(t Type, name string) error { be.MockBackend.RemoveFn = func(t backend.Type, name string) error {
return memRemove(be, t, name) return memRemove(be, t, name)
} }
be.MockBackend.ListFn = func(t Type, done <-chan struct{}) <-chan string { be.MockBackend.ListFn = func(t backend.Type, done <-chan struct{}) <-chan string {
return memList(be, t, done) return memList(be, t, done)
} }
@ -74,7 +74,7 @@ func NewMemoryBackend() *MemoryBackend {
return be return be
} }
func (be *MemoryBackend) insert(t Type, name string, data []byte) error { func (be *MemoryBackend) insert(t backend.Type, name string, data []byte) error {
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -86,7 +86,7 @@ func (be *MemoryBackend) insert(t Type, name string, data []byte) error {
return nil return nil
} }
func memTest(be *MemoryBackend, t Type, name string) (bool, error) { func memTest(be *MemoryBackend, t backend.Type, name string) (bool, error) {
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -114,8 +114,8 @@ func (e *tempMemEntry) Size() uint {
return uint(len(e.data.Bytes())) return uint(len(e.data.Bytes()))
} }
func (e *tempMemEntry) Finalize(t Type, name string) error { func (e *tempMemEntry) Finalize(t backend.Type, name string) error {
if t == Config { if t == backend.Config {
name = "" name = ""
} }
@ -123,35 +123,17 @@ func (e *tempMemEntry) Finalize(t Type, name string) error {
return e.be.insert(t, name, e.data.Bytes()) return e.be.insert(t, name, e.data.Bytes())
} }
func memCreate(be *MemoryBackend) (Blob, error) { func memCreate(be *MemoryBackend) (backend.Blob, error) {
blob := &tempMemEntry{be: be} blob := &tempMemEntry{be: be}
debug.Log("MemoryBackend.Create", "create new blob %p", blob) debug.Log("MemoryBackend.Create", "create new blob %p", blob)
return blob, nil return blob, nil
} }
// ReadCloser wraps a reader and adds a noop Close method if rd does not implement io.Closer. func memGetReader(be *MemoryBackend, t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
func ReadCloser(rd io.Reader) io.ReadCloser {
return readCloser{rd}
}
// readCloser wraps a reader and adds a noop Close method if rd does not implement io.Closer.
type readCloser struct {
io.Reader
}
func (rd readCloser) Close() error {
if r, ok := rd.Reader.(io.Closer); ok {
return r.Close()
}
return nil
}
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()
if t == Config { if t == backend.Config {
name = "" name = ""
} }
@ -176,10 +158,10 @@ func memGetReader(be *MemoryBackend, t Type, name string, offset, length uint) (
buf = buf[:length] buf = buf[:length]
} }
return readCloser{bytes.NewReader(buf)}, nil return backend.ReadCloser(bytes.NewReader(buf)), nil
} }
func memLoad(be *MemoryBackend, h Handle, p []byte, off int64) (int, error) { func memLoad(be *MemoryBackend, h backend.Handle, p []byte, off int64) (int, error) {
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -187,7 +169,7 @@ func memLoad(be *MemoryBackend, h Handle, p []byte, off int64) (int, error) {
return 0, err return 0, err
} }
if h.Type == Config { if h.Type == backend.Config {
h.Name = "" h.Name = ""
} }
@ -213,7 +195,7 @@ func memLoad(be *MemoryBackend, h Handle, p []byte, off int64) (int, error) {
return n, nil return n, nil
} }
func memRemove(be *MemoryBackend, t Type, name string) error { func memRemove(be *MemoryBackend, t backend.Type, name string) error {
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()
@ -228,7 +210,7 @@ func memRemove(be *MemoryBackend, t Type, name string) error {
return nil return nil
} }
func memList(be *MemoryBackend, t Type, done <-chan struct{}) <-chan string { func memList(be *MemoryBackend, t backend.Type, done <-chan struct{}) <-chan string {
be.m.Lock() be.m.Lock()
defer be.m.Unlock() defer be.m.Unlock()

View file

@ -1,15 +1,16 @@
package backend_test package mem_test
import ( import (
"errors" "errors"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
"github.com/restic/restic/backend/mem"
"github.com/restic/restic/backend/test" "github.com/restic/restic/backend/test"
) )
var be backend.Backend var be backend.Backend
//go:generate go run test/generate_backend_tests.go -testfile test/tests.go -output mem_backend_test.go -prefix MemBackend //go:generate go run ../test/generate_backend_tests.go
func init() { func init() {
test.CreateFn = func() (backend.Backend, error) { test.CreateFn = func() (backend.Backend, error) {
@ -17,7 +18,7 @@ func init() {
return nil, errors.New("temporary memory backend dir already exists") return nil, errors.New("temporary memory backend dir already exists")
} }
be = backend.NewMemoryBackend() be = mem.New()
return be, nil return be, nil
} }

21
backend/readcloser.go Normal file
View file

@ -0,0 +1,21 @@
package backend
import "io"
// ReadCloser wraps a reader and adds a noop Close method if rd does not implement io.Closer.
func ReadCloser(rd io.Reader) io.ReadCloser {
return readCloser{rd}
}
// readCloser wraps a reader and adds a noop Close method if rd does not implement io.Closer.
type readCloser struct {
io.Reader
}
func (rd readCloser) Close() error {
if r, ok := rd.Reader.(io.Closer); ok {
return r.Close()
}
return nil
}

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
"github.com/restic/restic/backend/mem"
"github.com/restic/restic/backend/test" "github.com/restic/restic/backend/test"
) )
@ -17,7 +18,7 @@ func init() {
return nil, errors.New("temporary memory backend dir already exists") return nil, errors.New("temporary memory backend dir already exists")
} }
be = backend.NewMemoryBackend() be = mem.New()
return be, nil return be, nil
} }

View file

@ -9,6 +9,7 @@ import (
"github.com/restic/restic" "github.com/restic/restic"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
"github.com/restic/restic/backend/mem"
"github.com/restic/restic/checker" "github.com/restic/restic/checker"
"github.com/restic/restic/repository" "github.com/restic/restic/repository"
. "github.com/restic/restic/test" . "github.com/restic/restic/test"
@ -252,7 +253,7 @@ func (f faultReader) Read(p []byte) (int, error) {
} }
func TestCheckerModifiedData(t *testing.T) { func TestCheckerModifiedData(t *testing.T) {
be := backend.NewMemoryBackend() be := mem.New()
repo := repository.New(be) repo := repository.New(be)
OK(t, repo.Init(TestPassword)) OK(t, repo.Init(TestPassword))