restic/backend/mem/mem_backend.go

244 lines
4.8 KiB
Go
Raw Normal View History

2016-01-23 18:19:26 +00:00
package mem
2015-11-22 15:12:00 +00:00
import (
"bytes"
"errors"
"io"
"sort"
"sync"
2015-11-22 15:30:13 +00:00
2016-01-23 18:19:26 +00:00
"github.com/restic/restic/backend"
2015-11-22 15:30:13 +00:00
"github.com/restic/restic/debug"
2015-11-22 15:12:00 +00:00
)
type entry struct {
2016-01-23 18:19:26 +00:00
Type backend.Type
2015-11-22 15:12:00 +00:00
Name string
}
type memMap map[entry][]byte
// MemoryBackend is a mock backend that uses a map for storing all data in
// memory. This should only be used for tests.
type MemoryBackend struct {
data memMap
m sync.Mutex
2016-01-23 18:19:26 +00:00
backend.MockBackend
2015-11-22 15:12:00 +00:00
}
2016-01-23 18:19:26 +00:00
// New returns a new backend that saves all data in a map in memory.
func New() *MemoryBackend {
2015-11-22 15:12:00 +00:00
be := &MemoryBackend{
data: make(memMap),
}
2016-01-23 18:19:26 +00:00
be.MockBackend.TestFn = func(t backend.Type, name string) (bool, error) {
2015-11-22 15:12:00 +00:00
return memTest(be, t, name)
}
2016-01-23 18:19:26 +00:00
be.MockBackend.CreateFn = func() (backend.Blob, error) {
2015-11-22 15:12:00 +00:00
return memCreate(be)
}
2016-01-23 18:19:26 +00:00
be.MockBackend.GetReaderFn = func(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
2015-11-22 15:12:00 +00:00
return memGetReader(be, t, name, offset, length)
}
2016-01-23 18:19:26 +00:00
be.MockBackend.LoadFn = func(h backend.Handle, p []byte, off int64) (int, error) {
2016-01-23 16:19:55 +00:00
return memLoad(be, h, p, off)
}
2016-01-23 18:19:26 +00:00
be.MockBackend.RemoveFn = func(t backend.Type, name string) error {
2015-11-22 15:12:00 +00:00
return memRemove(be, t, name)
}
2016-01-23 18:19:26 +00:00
be.MockBackend.ListFn = func(t backend.Type, done <-chan struct{}) <-chan string {
2015-11-22 15:12:00 +00:00
return memList(be, t, done)
}
be.MockBackend.DeleteFn = func() error {
be.m.Lock()
defer be.m.Unlock()
be.data = make(memMap)
return nil
}
2016-01-23 16:19:55 +00:00
be.MockBackend.LocationFn = func() string {
return "Memory Backend"
}
2015-11-22 15:30:13 +00:00
debug.Log("MemoryBackend.New", "created new memory backend")
2015-11-22 15:12:00 +00:00
return be
}
2016-01-23 18:19:26 +00:00
func (be *MemoryBackend) insert(t backend.Type, name string, data []byte) error {
2015-11-22 15:12:00 +00:00
be.m.Lock()
defer be.m.Unlock()
if _, ok := be.data[entry{t, name}]; ok {
return errors.New("already present")
}
be.data[entry{t, name}] = data
return nil
}
2016-01-23 18:19:26 +00:00
func memTest(be *MemoryBackend, t backend.Type, name string) (bool, error) {
2015-11-22 15:12:00 +00:00
be.m.Lock()
defer be.m.Unlock()
2015-11-22 15:30:13 +00:00
debug.Log("MemoryBackend.Test", "test %v %v", t, name)
2015-11-22 15:12:00 +00:00
if _, ok := be.data[entry{t, name}]; ok {
return true, nil
}
return false, nil
}
// tempMemEntry temporarily holds data written to the memory backend before it
// is finalized.
type tempMemEntry struct {
be *MemoryBackend
data bytes.Buffer
}
func (e *tempMemEntry) Write(p []byte) (int, error) {
return e.data.Write(p)
}
func (e *tempMemEntry) Size() uint {
return uint(len(e.data.Bytes()))
}
2016-01-23 18:19:26 +00:00
func (e *tempMemEntry) Finalize(t backend.Type, name string) error {
if t == backend.Config {
name = ""
}
debug.Log("MemoryBackend", "save blob %p (%d bytes) as %v %v", e, len(e.data.Bytes()), t, name)
2015-11-22 15:12:00 +00:00
return e.be.insert(t, name, e.data.Bytes())
}
2016-01-23 18:19:26 +00:00
func memCreate(be *MemoryBackend) (backend.Blob, error) {
blob := &tempMemEntry{be: be}
debug.Log("MemoryBackend.Create", "create new blob %p", blob)
return blob, nil
2015-11-22 15:12:00 +00:00
}
2016-01-23 18:19:26 +00:00
func memGetReader(be *MemoryBackend, t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
2015-11-22 15:12:00 +00:00
be.m.Lock()
defer be.m.Unlock()
2016-01-23 18:19:26 +00:00
if t == backend.Config {
name = ""
}
2015-11-22 16:27:02 +00:00
debug.Log("MemoryBackend.GetReader", "get %v %v offset %v len %v", t, name, offset, length)
2015-11-22 15:30:13 +00:00
2015-11-22 15:12:00 +00:00
if _, ok := be.data[entry{t, name}]; !ok {
return nil, errors.New("no such data")
}
buf := be.data[entry{t, name}]
if offset > uint(len(buf)) {
return nil, errors.New("offset beyond end of file")
}
buf = buf[offset:]
2015-11-22 16:27:02 +00:00
if length > 0 {
if length > uint(len(buf)) {
length = uint(len(buf))
}
2015-11-22 15:12:00 +00:00
2015-11-22 16:27:02 +00:00
buf = buf[:length]
}
2015-11-22 15:12:00 +00:00
2016-01-23 18:19:26 +00:00
return backend.ReadCloser(bytes.NewReader(buf)), nil
2015-11-22 15:12:00 +00:00
}
2016-01-23 18:19:26 +00:00
func memLoad(be *MemoryBackend, h backend.Handle, p []byte, off int64) (int, error) {
2016-01-23 16:19:55 +00:00
be.m.Lock()
defer be.m.Unlock()
if err := h.Valid(); err != nil {
return 0, err
}
2016-01-23 18:19:26 +00:00
if h.Type == backend.Config {
2016-01-23 16:19:55 +00:00
h.Name = ""
}
debug.Log("MemoryBackend.Load", "get %v offset %v len %v", h, off, len(p))
if _, ok := be.data[entry{h.Type, h.Name}]; !ok {
return 0, errors.New("no such data")
}
buf := be.data[entry{h.Type, h.Name}]
if off > int64(len(buf)) {
return 0, errors.New("offset beyond end of file")
}
buf = buf[off:]
n := copy(p, buf)
if len(p) > len(buf) {
return n, io.ErrUnexpectedEOF
}
return n, nil
}
2016-01-23 18:19:26 +00:00
func memRemove(be *MemoryBackend, t backend.Type, name string) error {
2015-11-22 15:12:00 +00:00
be.m.Lock()
defer be.m.Unlock()
2015-11-22 15:30:13 +00:00
debug.Log("MemoryBackend.Remove", "get %v %v", t, name)
2015-11-22 15:12:00 +00:00
if _, ok := be.data[entry{t, name}]; !ok {
return errors.New("no such data")
}
delete(be.data, entry{t, name})
return nil
}
2016-01-23 18:19:26 +00:00
func memList(be *MemoryBackend, t backend.Type, done <-chan struct{}) <-chan string {
2015-11-22 15:12:00 +00:00
be.m.Lock()
defer be.m.Unlock()
ch := make(chan string)
var ids []string
for entry := range be.data {
if entry.Type != t {
continue
}
ids = append(ids, entry.Name)
}
sort.Strings(ids)
2015-11-22 15:30:13 +00:00
debug.Log("MemoryBackend.List", "list %v: %v", t, ids)
2015-11-22 15:12:00 +00:00
go func() {
defer close(ch)
for _, id := range ids {
select {
case ch <- id:
case <-done:
return
}
}
}()
return ch
}