diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-23 19:19:16 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-23 19:19:16 +0200 |
commit | e7648996c8f278d061fe03a5c4d255049da765e5 (patch) | |
tree | 2917891d130853b24a375ef00b68fe14c8228296 /cache | |
parent | 16f55e3f4d560330a638986130d27fd067300169 (diff) | |
download | git-bug-e7648996c8f278d061fe03a5c4d255049da765e5.tar.gz |
bug: add a new BugExerpt that hold a subset of a bug state for efficient sorting and retrieval
Diffstat (limited to 'cache')
-rw-r--r-- | cache/bug_excerpt.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/cache/bug_excerpt.go b/cache/bug_excerpt.go new file mode 100644 index 00000000..23ac459b --- /dev/null +++ b/cache/bug_excerpt.go @@ -0,0 +1,92 @@ +package cache + +import ( + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/util" +) + +// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs +// efficiently without having to read and compile each raw bugs. +type BugExcerpt struct { + Id string + + CreateLamportTime util.LamportTime + EditLamportTime util.LamportTime + CreateUnixTime int64 + EditUnixTime int64 + + Status bug.Status + Author bug.Person +} + +func NewBugExcerpt(b *bug.Bug, snap bug.Snapshot) BugExcerpt { + return BugExcerpt{ + Id: b.Id(), + CreateLamportTime: b.CreateLamportTime(), + EditLamportTime: b.EditLamportTime(), + CreateUnixTime: b.FirstOp().UnixTime(), + EditUnixTime: snap.LastEditUnix(), + Status: snap.Status, + Author: snap.Author, + } +} + +/* + * Sorting + */ + +type BugsByCreationTime []*BugExcerpt + +func (b BugsByCreationTime) Len() int { + return len(b) +} + +func (b BugsByCreationTime) Less(i, j int) bool { + if b[i].CreateLamportTime < b[j].CreateLamportTime { + return true + } + + if b[i].CreateLamportTime > b[j].CreateLamportTime { + return false + } + + // When the logical clocks are identical, that means we had a concurrent + // edition. In this case we rely on the timestamp. While the timestamp might + // be incorrect due to a badly set clock, the drift in sorting is bounded + // by the first sorting using the logical clock. That means that if users + // synchronize their bugs regularly, the timestamp will rarely be used, and + // should still provide a kinda accurate sorting when needed. + return b[i].CreateUnixTime < b[j].CreateUnixTime +} + +func (b BugsByCreationTime) Swap(i, j int) { + b[i], b[j] = b[j], b[i] +} + +type BugsByEditTime []*BugExcerpt + +func (b BugsByEditTime) Len() int { + return len(b) +} + +func (b BugsByEditTime) Less(i, j int) bool { + if b[i].EditLamportTime < b[j].EditLamportTime { + return true + } + + if b[i].EditLamportTime > b[j].EditLamportTime { + return false + } + + // When the logical clocks are identical, that means we had a concurrent + // edition. In this case we rely on the timestamp. While the timestamp might + // be incorrect due to a badly set clock, the drift in sorting is bounded + // by the first sorting using the logical clock. That means that if users + // synchronize their bugs regularly, the timestamp will rarely be used, and + // should still provide a kinda accurate sorting when needed. + return b[i].EditUnixTime < b[j].EditUnixTime +} + +func (b BugsByEditTime) Swap(i, j int) { + b[i], b[j] = b[j], b[i] +} |