Skip to content

Commit

Permalink
Merge pull request #65 from safing/feature/make-db-registry-persistan…
Browse files Browse the repository at this point in the history
…ce-optional

Make DB registry persistence optional
  • Loading branch information
ppacher authored Jul 12, 2020
2 parents fc31a72 + 6fcd1d3 commit 91f759d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 0 additions & 1 deletion database/location.go

This file was deleted.

11 changes: 5 additions & 6 deletions database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 13 additions & 4 deletions database/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,}$")
Expand Down Expand Up @@ -67,7 +68,7 @@ func Register(new *Database) (*Database, error) {
save = true
}

if save {
if save && registryPersistence.IsSet() {
if ok {
registeredDB.Updated()
}
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 91f759d

Please sign in to comment.