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 | |
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')
-rw-r--r-- | util/lamport.go | 4 | ||||
-rw-r--r-- | util/lamport_test.go | 2 | ||||
-rw-r--r-- | util/persisted_lamport.go | 21 |
3 files changed, 12 insertions, 15 deletions
diff --git a/util/lamport.go b/util/lamport.go index c75e1d15..4d5dcf5b 100644 --- a/util/lamport.go +++ b/util/lamport.go @@ -58,9 +58,9 @@ func (l *LamportClock) Time() LamportTime { return LamportTime(atomic.LoadUint64(&l.counter)) } -// Increment is used to increment and return the value of the lamport clock +// Increment is used to return the value of the lamport clock and increment it afterwards func (l *LamportClock) Increment() LamportTime { - return LamportTime(atomic.AddUint64(&l.counter, 1)) + return LamportTime(atomic.AddUint64(&l.counter, 1) - 1) } // Witness is called to update our local clock if necessary after diff --git a/util/lamport_test.go b/util/lamport_test.go index 0bac7d51..de7f1822 100644 --- a/util/lamport_test.go +++ b/util/lamport_test.go @@ -38,7 +38,7 @@ func TestLamportClock(t *testing.T) { t.Fatalf("bad time value") } - if l.Increment() != 1 { + if l.Increment() != 0 { t.Fatalf("bad time value") } 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) } |