restic/backend/mem/mem_backend.go

224 lines
4.2 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 (
"errors"
"io"
"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.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-24 15:59:38 +00:00
be.MockBackend.SaveFn = func(h backend.Handle, p []byte) error {
return memSave(be, h, p)
}
2016-01-23 16:19:55 +00:00
2016-01-23 22:27:58 +00:00
be.MockBackend.StatFn = func(h backend.Handle) (backend.BlobInfo, error) {
return memStat(be, h)
}
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
}
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
if err := h.Valid(); err != nil {
return 0, err
}
2016-01-24 00:15:35 +00:00
be.m.Lock()
defer be.m.Unlock()
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-24 00:15:35 +00:00
func memSave(be *MemoryBackend, h backend.Handle, p []byte) error {
if err := h.Valid(); err != nil {
return err
}
be.m.Lock()
defer be.m.Unlock()
if h.Type == backend.Config {
h.Name = ""
}
2016-01-24 19:23:50 +00:00
if _, ok := be.data[entry{h.Type, h.Name}]; ok {
return errors.New("file already exists")
}
2016-01-24 00:15:35 +00:00
debug.Log("MemoryBackend.Save", "save %v bytes at %v", len(p), h)
buf := make([]byte, len(p))
copy(buf, p)
be.data[entry{h.Type, h.Name}] = buf
return nil
}
2016-01-23 22:27:58 +00:00
func memStat(be *MemoryBackend, h backend.Handle) (backend.BlobInfo, error) {
be.m.Lock()
defer be.m.Unlock()
if err := h.Valid(); err != nil {
return backend.BlobInfo{}, err
}
if h.Type == backend.Config {
h.Name = ""
}
debug.Log("MemoryBackend.Stat", "stat %v", h)
e, ok := be.data[entry{h.Type, h.Name}]
if !ok {
return backend.BlobInfo{}, errors.New("no such data")
}
return backend.BlobInfo{Size: int64(len(e))}, 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)
}
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
}