add compare function to timedversion (#1422)

* add compare function to timedversion

* cleanup
This commit is contained in:
Paul Wells
2023-02-14 21:05:55 -08:00
committed by GitHub
parent c133b9f2e3
commit 24d8ad0da9
2 changed files with 29 additions and 0 deletions

View File

@@ -56,6 +56,12 @@ func NewTimedVersionFromProto(ptv *livekit.TimedVersion) *TimedVersion {
}
}
func NewTimedVersionFromTime(t time.Time) *TimedVersion {
return &TimedVersion{
ts: t.UnixMicro(),
}
}
func (t *TimedVersion) Update(other *TimedVersion) {
t.lock.Lock()
if other.After(t) {
@@ -76,6 +82,25 @@ func (t *TimedVersion) After(other *TimedVersion) bool {
return t.ts > other.ts
}
func (t *TimedVersion) Compare(other *TimedVersion) int {
t.lock.RLock()
defer t.lock.RUnlock()
if t.ts == other.ts {
if t.ticks == other.ticks {
return 0
}
if t.ticks > other.ticks {
return 1
}
return -1
}
if t.ts > other.ts {
return 1
}
return -1
}
func (t *TimedVersion) ToProto() *livekit.TimedVersion {
t.lock.RLock()
defer t.lock.RUnlock()

View File

@@ -36,4 +36,8 @@ func TestTimedVersion(t *testing.T) {
assert.True(t, tv2.After(tv1))
// tv3 and tv2 are equivalent after update
assert.False(t, tv3.After(tv2))
assert.Equal(t, 0, tv1.Compare(tv1))
assert.Equal(t, -1, tv1.Compare(tv2))
assert.Equal(t, 1, tv2.Compare(tv1))
}