2014-10-22 00:02:20 +02:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
2014-10-29 02:15:40 +01:00
|
|
|
"github.com/docker/docker-registry/storagedriver/factory"
|
2014-10-22 00:02:20 +02:00
|
|
|
)
|
|
|
|
|
2014-10-29 02:15:40 +01:00
|
|
|
const DriverName = "inmemory"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
factory.Register(DriverName, &inMemoryDriverFactory{})
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// inMemoryDriverFacotry implements the factory.StorageDriverFactory interface
|
2014-10-29 02:15:40 +01:00
|
|
|
type inMemoryDriverFactory struct{}
|
|
|
|
|
|
|
|
func (factory *inMemoryDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) {
|
|
|
|
return New(), nil
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// InMemoryDriver is a storagedriver.StorageDriver implementation backed by a local map
|
2014-10-29 02:15:40 +01:00
|
|
|
// Intended solely for example and testing purposes
|
2014-10-22 00:02:20 +02:00
|
|
|
type InMemoryDriver struct {
|
|
|
|
storage map[string][]byte
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// New constructs a new InMemoryDriver
|
2014-10-29 02:15:40 +01:00
|
|
|
func New() *InMemoryDriver {
|
2014-10-22 00:02:20 +02:00
|
|
|
return &InMemoryDriver{storage: make(map[string][]byte)}
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:14:19 +01:00
|
|
|
// Implement the storagedriver.StorageDriver interface
|
|
|
|
|
2014-10-22 00:02:20 +02:00
|
|
|
func (d *InMemoryDriver) GetContent(path string) ([]byte, error) {
|
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
contents, ok := d.storage[path]
|
|
|
|
if !ok {
|
|
|
|
return nil, storagedriver.PathNotFoundError{path}
|
|
|
|
}
|
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *InMemoryDriver) PutContent(path string, contents []byte) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
d.storage[path] = contents
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *InMemoryDriver) ReadStream(path string, offset uint64) (io.ReadCloser, error) {
|
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
contents, err := d.GetContent(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(contents) < int(offset) {
|
|
|
|
return nil, storagedriver.InvalidOffsetError{path, offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
src := contents[offset:]
|
|
|
|
buf := make([]byte, len(src))
|
|
|
|
copy(buf, src)
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(buf)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *InMemoryDriver) WriteStream(path string, offset, size uint64, reader io.ReadCloser) error {
|
|
|
|
defer reader.Close()
|
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
|
2014-11-07 21:58:48 +01:00
|
|
|
resumableOffset, err := d.CurrentSize(path)
|
2014-10-22 00:02:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset > resumableOffset {
|
|
|
|
return storagedriver.InvalidOffsetError{path, offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset > 0 {
|
|
|
|
contents = append(d.storage[path][0:offset], contents...)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.storage[path] = contents
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-07 21:58:48 +01:00
|
|
|
func (d *InMemoryDriver) CurrentSize(path string) (uint64, error) {
|
2014-10-22 00:02:20 +02:00
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
contents, ok := d.storage[path]
|
|
|
|
if !ok {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return uint64(len(contents)), nil
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:52:24 +01:00
|
|
|
func (d *InMemoryDriver) List(path string) ([]string, error) {
|
|
|
|
subPathMatcher, err := regexp.Compile(fmt.Sprintf("^%s/[^/]+", path))
|
2014-10-22 00:02:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
// we use map to collect uniq keys
|
|
|
|
keySet := make(map[string]struct{})
|
|
|
|
for k := range d.storage {
|
|
|
|
if key := subPathMatcher.FindString(k); key != "" {
|
|
|
|
keySet[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(keySet))
|
|
|
|
for k := range keySet {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *InMemoryDriver) Move(sourcePath string, destPath string) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
contents, ok := d.storage[sourcePath]
|
|
|
|
if !ok {
|
|
|
|
return storagedriver.PathNotFoundError{sourcePath}
|
|
|
|
}
|
|
|
|
d.storage[destPath] = contents
|
|
|
|
delete(d.storage, sourcePath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *InMemoryDriver) Delete(path string) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
subPaths := make([]string, 0)
|
|
|
|
for k := range d.storage {
|
|
|
|
if strings.HasPrefix(k, path) {
|
|
|
|
subPaths = append(subPaths, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(subPaths) == 0 {
|
|
|
|
return storagedriver.PathNotFoundError{path}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subPath := range subPaths {
|
|
|
|
delete(d.storage, subPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|