diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-06 20:31:20 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-06 20:31:20 +0200 |
commit | 435be2b693aee89ed34a2d1e7291b3b141b19717 (patch) | |
tree | 89244a9dcb995c27002995e9f25f9be631101713 /util/persisted_lamport.go | |
parent | 593891b8e01fd89866b30854a60aece1dad5f6ab (diff) | |
download | git-bug-435be2b693aee89ed34a2d1e7291b3b141b19717.tar.gz |
bug: add a Lamport logical clock to be able to sort bugs by creation time and edit time without having to rely on a timestamp
Diffstat (limited to 'util/persisted_lamport.go')
-rw-r--r-- | util/persisted_lamport.go | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/util/persisted_lamport.go b/util/persisted_lamport.go index 43a2863c..c8c898e2 100644 --- a/util/persisted_lamport.go +++ b/util/persisted_lamport.go @@ -14,7 +14,8 @@ type PersistedLamport struct { func NewPersistedLamport(filePath string) *PersistedLamport { clock := &PersistedLamport{ - filePath: filePath, + LamportClock: NewLamportClock(), + filePath: filePath, } return clock } @@ -32,21 +33,17 @@ func LoadPersistedLamport(filePath string) (*PersistedLamport, error) { 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) Time() LamportTime { - // Equivalent to: - // - // res = c.LamportClock.Time() - // bugClock.Increment() - // - // ... but thread safe - return c.Increment() - 1 -} - func (c *PersistedLamport) read() error { content, err := ioutil.ReadFile(c.filePath) if err != nil { @@ -76,6 +73,6 @@ func (c *PersistedLamport) Write() error { return err } - data := []byte(fmt.Sprintf("%d", c.LamportClock.Time())) + data := []byte(fmt.Sprintf("%d", c.counter)) return ioutil.WriteFile(c.filePath, data, 0644) } |