diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-11 22:04:16 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-11 22:14:46 +0200 |
commit | 3605887345792d2f981f971c6c4a2cb7f86a343e (patch) | |
tree | afd525b6e3a638e4c619a5a986fcb2811c297444 /util/persisted_lamport.go | |
parent | 7b05983c19af4da70f2a9a5062913f4e4f5d5faa (diff) | |
download | git-bug-3605887345792d2f981f971c6c4a2cb7f86a343e.tar.gz |
reorganize package for a more idomatic go
Diffstat (limited to 'util/persisted_lamport.go')
-rw-r--r-- | util/persisted_lamport.go | 78 |
1 files changed, 0 insertions, 78 deletions
diff --git a/util/persisted_lamport.go b/util/persisted_lamport.go deleted file mode 100644 index c8c898e2..00000000 --- a/util/persisted_lamport.go +++ /dev/null @@ -1,78 +0,0 @@ -package util - -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" -) - -type PersistedLamport struct { - LamportClock - filePath string -} - -func NewPersistedLamport(filePath string) *PersistedLamport { - clock := &PersistedLamport{ - LamportClock: NewLamportClock(), - filePath: filePath, - } - return clock -} - -func LoadPersistedLamport(filePath string) (*PersistedLamport, error) { - clock := &PersistedLamport{ - filePath: filePath, - } - - err := clock.read() - if err != nil { - return nil, err - } - - return clock, nil -} - -func (c *PersistedLamport) Increment() (LamportTime, error) { - time := c.LamportClock.Increment() - return time, c.Write() -} - -func (c *PersistedLamport) Witness(time LamportTime) error { - // TODO: rework so that we write only when the clock was actually updated - c.LamportClock.Witness(time) - return c.Write() -} - -func (c *PersistedLamport) read() error { - content, err := ioutil.ReadFile(c.filePath) - if err != nil { - return err - } - - var value uint64 - n, err := fmt.Sscanf(string(content), "%d", &value) - - if err != nil { - return err - } - - if n != 1 { - return fmt.Errorf("could not read the clock") - } - - c.LamportClock = NewLamportClockWithTime(value) - - return nil -} - -func (c *PersistedLamport) 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) -} |