Adds StorageDriverFactory, unifying creation of StorageDrivers

Custom storage drivers can register a factory to create the driver by
name, similar to the database/sql package's Register and Open
factory.Create returns an in-process driver if registered or an IPC
driver if one can be found, erroring otherwise
This standardizes parameter passing for creation of storage drivers

Also adds documentation for storagedriver package and children
This commit is contained in:
Brian Bland 2014-10-28 18:15:40 -07:00
parent ff81f3a719
commit ca0084fad1
15 changed files with 290 additions and 79 deletions

View file

@ -10,14 +10,31 @@ import (
"sync"
"github.com/docker/docker-registry/storagedriver"
"github.com/docker/docker-registry/storagedriver/factory"
)
const DriverName = "inmemory"
func init() {
factory.Register(DriverName, &inMemoryDriverFactory{})
}
// Implements the factory.StorageDriverFactory interface
type inMemoryDriverFactory struct{}
func (factory *inMemoryDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) {
return New(), nil
}
// InMemory Storage Driver backed by a map
// Intended solely for example and testing purposes
type InMemoryDriver struct {
storage map[string][]byte
mutex sync.RWMutex
}
func NewDriver() *InMemoryDriver {
// Constructs a new InMemoryDriver
func New() *InMemoryDriver {
return &InMemoryDriver{storage: make(map[string][]byte)}
}

View file

@ -13,8 +13,8 @@ func Test(t *testing.T) { TestingT(t) }
func init() {
inmemoryDriverConstructor := func() (storagedriver.StorageDriver, error) {
return NewDriver(), nil
return New(), nil
}
testsuites.RegisterInProcessSuite(inmemoryDriverConstructor, testsuites.NeverSkip)
testsuites.RegisterIPCSuite("inmemory", nil, testsuites.NeverSkip)
testsuites.RegisterIPCSuite(DriverName, nil, testsuites.NeverSkip)
}