mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Merge pull request #65 from safing/feature/make-db-registry-persistance-optional
Make DB registry persistence optional
This commit is contained in:
commit
91f759d148
3 changed files with 18 additions and 11 deletions
|
@ -1 +0,0 @@
|
|||
package database
|
|
@ -40,14 +40,13 @@ func Initialize(dirStructureRoot *utils.DirStructure) error {
|
|||
return fmt.Errorf("could not create/open database directory (%s): %s", rootStructure.Path, err)
|
||||
}
|
||||
|
||||
err = loadRegistry()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootStructure.Path, registryFileName), err)
|
||||
if registryPersistence.IsSet() {
|
||||
err = loadRegistry()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootStructure.Path, registryFileName), err)
|
||||
}
|
||||
}
|
||||
|
||||
// start registry writer
|
||||
go registryWriter()
|
||||
|
||||
return nil
|
||||
}
|
||||
return errors.New("database already initialized")
|
||||
|
|
|
@ -19,9 +19,10 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
writeRegistrySoon = abool.NewBool(false)
|
||||
registryPersistence = abool.NewBool(false)
|
||||
writeRegistrySoon = abool.NewBool(false)
|
||||
|
||||
registry map[string]*Database
|
||||
registry = make(map[string]*Database)
|
||||
registryLock sync.Mutex
|
||||
|
||||
nameConstraint = regexp.MustCompile("^[A-Za-z0-9_-]{4,}$")
|
||||
|
@ -67,7 +68,7 @@ func Register(new *Database) (*Database, error) {
|
|||
save = true
|
||||
}
|
||||
|
||||
if save {
|
||||
if save && registryPersistence.IsSet() {
|
||||
if ok {
|
||||
registeredDB.Updated()
|
||||
}
|
||||
|
@ -99,6 +100,15 @@ func getDatabase(name string) (*Database, error) {
|
|||
return registeredDB, nil
|
||||
}
|
||||
|
||||
// EnableRegistryPersistence enables persistence of the database registry.
|
||||
func EnableRegistryPersistence() {
|
||||
if registryPersistence.SetToIf(false, true) {
|
||||
// start registry writer
|
||||
go registryWriter()
|
||||
// TODO: make an initial write if database system is already initialized
|
||||
}
|
||||
}
|
||||
|
||||
func loadRegistry() error {
|
||||
registryLock.Lock()
|
||||
defer registryLock.Unlock()
|
||||
|
@ -108,7 +118,6 @@ func loadRegistry() error {
|
|||
data, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
registry = make(map[string]*Database)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
|
Loading…
Add table
Reference in a new issue