github.com/safing/portbase@v0.19.5/database/storage/storages.go (about) 1 package storage 2 3 import ( 4 "errors" 5 "fmt" 6 "sync" 7 ) 8 9 // A Factory creates a new database of it's type. 10 type Factory func(name, location string) (Interface, error) 11 12 var ( 13 storages = make(map[string]Factory) 14 storagesLock sync.Mutex 15 ) 16 17 // Register registers a new storage type. 18 func Register(name string, factory Factory) error { 19 storagesLock.Lock() 20 defer storagesLock.Unlock() 21 22 _, ok := storages[name] 23 if ok { 24 return errors.New("factory for this type already exists") 25 } 26 27 storages[name] = factory 28 return nil 29 } 30 31 // CreateDatabase starts a new database with the given name and storageType at location. 32 func CreateDatabase(name, storageType, location string) (Interface, error) { 33 return nil, nil 34 } 35 36 // StartDatabase starts a new database with the given name and storageType at location. 37 func StartDatabase(name, storageType, location string) (Interface, error) { 38 storagesLock.Lock() 39 defer storagesLock.Unlock() 40 41 factory, ok := storages[storageType] 42 if !ok { 43 return nil, fmt.Errorf("storage type %s not registered", storageType) 44 } 45 46 return factory(name, location) 47 }