From bf11c08f5ad0894e494228c312235d674b58af6a Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 13 Sep 2018 16:05:27 +0200 Subject: lamport: better perf by ensuring that the folder is created only once --- repository/git.go | 36 ++++++++++++++++++++++++++++-------- util/lamport/persisted_lamport.go | 17 +++++++++-------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/repository/git.go b/repository/git.go index 3b5a3a1a..29417dc7 100644 --- a/repository/git.go +++ b/repository/git.go @@ -90,7 +90,10 @@ func NewGitRepo(path string, witnesser func(repo *GitRepo) error) (*GitRepo, err if err != nil { // No clock yet, trying to initialize them - repo.createClocks() + err = repo.createClocks() + if err != nil { + return nil, err + } err = witnesser(repo) if err != nil { @@ -111,9 +114,12 @@ func NewGitRepo(path string, witnesser func(repo *GitRepo) error) (*GitRepo, err // InitGitRepo create a new empty git repo at the given path func InitGitRepo(path string) (*GitRepo, error) { repo := &GitRepo{Path: path} - repo.createClocks() + err := repo.createClocks() + if err != nil { + return nil, err + } - _, err := repo.runGitCommand("init", path) + _, err = repo.runGitCommand("init", path) if err != nil { return nil, err } @@ -124,9 +130,12 @@ func InitGitRepo(path string) (*GitRepo, error) { // InitBareGitRepo create a new --bare empty git repo at the given path func InitBareGitRepo(path string) (*GitRepo, error) { repo := &GitRepo{Path: path} - repo.createClocks() + err := repo.createClocks() + if err != nil { + return nil, err + } - _, err := repo.runGitCommand("init", "--bare", path) + _, err = repo.runGitCommand("init", "--bare", path) if err != nil { return nil, err } @@ -336,12 +345,23 @@ func (repo *GitRepo) AddRemote(name string, url string) error { return err } -func (repo *GitRepo) createClocks() { +func (repo *GitRepo) createClocks() error { createPath := path.Join(repo.Path, createClockFile) - repo.createClock = lamport.NewPersisted(createPath) + createClock, err := lamport.NewPersisted(createPath) + if err != nil { + return err + } editPath := path.Join(repo.Path, editClockFile) - repo.editClock = lamport.NewPersisted(editPath) + editClock, err := lamport.NewPersisted(editPath) + if err != nil { + return err + } + + repo.createClock = createClock + repo.editClock = editClock + + return nil } func (repo *GitRepo) LoadClocks() error { diff --git a/util/lamport/persisted_lamport.go b/util/lamport/persisted_lamport.go index 4f12dd1b..22b23dcb 100644 --- a/util/lamport/persisted_lamport.go +++ b/util/lamport/persisted_lamport.go @@ -12,12 +12,19 @@ type Persisted struct { filePath string } -func NewPersisted(filePath string) *Persisted { +func NewPersisted(filePath string) (*Persisted, error) { clock := &Persisted{ Clock: NewClock(), filePath: filePath, } - return clock + + dir := filepath.Dir(filePath) + err := os.MkdirAll(dir, 0777) + if err != nil { + return nil, err + } + + return clock, nil } func LoadPersisted(filePath string) (*Persisted, error) { @@ -67,12 +74,6 @@ func (c *Persisted) read() error { } func (c *Persisted) Write() error { - dir := filepath.Dir(c.filePath) - err := os.MkdirAll(dir, 0777) - if err != nil { - return err - } - data := []byte(fmt.Sprintf("%d", c.counter)) return ioutil.WriteFile(c.filePath, data, 0644) } -- cgit