1package cache
2
3import (
4 "encoding/gob"
5
6 "github.com/MichaelMure/git-bug/bug"
7 "github.com/MichaelMure/git-bug/util"
8)
9
10// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs
11// efficiently without having to read and compile each raw bugs.
12type BugExcerpt struct {
13 Id string
14
15 CreateLamportTime util.LamportTime
16 EditLamportTime util.LamportTime
17 CreateUnixTime int64
18 EditUnixTime int64
19
20 Status bug.Status
21 Author bug.Person
22}
23
24func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) BugExcerpt {
25 return BugExcerpt{
26 Id: b.Id(),
27 CreateLamportTime: b.CreateLamportTime(),
28 EditLamportTime: b.EditLamportTime(),
29 CreateUnixTime: b.FirstOp().UnixTime(),
30 EditUnixTime: snap.LastEditUnix(),
31 Status: snap.Status,
32 Author: snap.Author,
33 }
34}
35
36// Package initialisation used to register the type for (de)serialization
37func init() {
38 gob.Register(BugExcerpt{})
39}
40
41/*
42 * Sorting
43 */
44
45type BugsByCreationTime []*BugExcerpt
46
47func (b BugsByCreationTime) Len() int {
48 return len(b)
49}
50
51func (b BugsByCreationTime) Less(i, j int) bool {
52 if b[i].CreateLamportTime < b[j].CreateLamportTime {
53 return true
54 }
55
56 if b[i].CreateLamportTime > b[j].CreateLamportTime {
57 return false
58 }
59
60 // When the logical clocks are identical, that means we had a concurrent
61 // edition. In this case we rely on the timestamp. While the timestamp might
62 // be incorrect due to a badly set clock, the drift in sorting is bounded
63 // by the first sorting using the logical clock. That means that if users
64 // synchronize their bugs regularly, the timestamp will rarely be used, and
65 // should still provide a kinda accurate sorting when needed.
66 return b[i].CreateUnixTime < b[j].CreateUnixTime
67}
68
69func (b BugsByCreationTime) Swap(i, j int) {
70 b[i], b[j] = b[j], b[i]
71}
72
73type BugsByEditTime []*BugExcerpt
74
75func (b BugsByEditTime) Len() int {
76 return len(b)
77}
78
79func (b BugsByEditTime) Less(i, j int) bool {
80 if b[i].EditLamportTime < b[j].EditLamportTime {
81 return true
82 }
83
84 if b[i].EditLamportTime > b[j].EditLamportTime {
85 return false
86 }
87
88 // When the logical clocks are identical, that means we had a concurrent
89 // edition. In this case we rely on the timestamp. While the timestamp might
90 // be incorrect due to a badly set clock, the drift in sorting is bounded
91 // by the first sorting using the logical clock. That means that if users
92 // synchronize their bugs regularly, the timestamp will rarely be used, and
93 // should still provide a kinda accurate sorting when needed.
94 return b[i].EditUnixTime < b[j].EditUnixTime
95}
96
97func (b BugsByEditTime) Swap(i, j int) {
98 b[i], b[j] = b[j], b[i]
99}