1package cache
2
3import (
4 "encoding/gob"
5
6 "github.com/MichaelMure/git-bug/bug"
7 "github.com/MichaelMure/git-bug/util/lamport"
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 lamport.Time
16 EditLamportTime lamport.Time
17 CreateUnixTime int64
18 EditUnixTime int64
19
20 Status bug.Status
21 AuthorId string
22 Labels []bug.Label
23
24 CreateMetadata map[string]string
25}
26
27func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
28 return &BugExcerpt{
29 Id: b.Id(),
30 CreateLamportTime: b.CreateLamportTime(),
31 EditLamportTime: b.EditLamportTime(),
32 CreateUnixTime: b.FirstOp().GetUnixTime(),
33 EditUnixTime: snap.LastEditUnix(),
34 Status: snap.Status,
35 AuthorId: snap.Author.Id(),
36 Labels: snap.Labels,
37 CreateMetadata: b.FirstOp().AllMetadata(),
38 }
39}
40
41// Package initialisation used to register the type for (de)serialization
42func init() {
43 gob.Register(BugExcerpt{})
44}
45
46/*
47 * Sorting
48 */
49
50type BugsById []*BugExcerpt
51
52func (b BugsById) Len() int {
53 return len(b)
54}
55
56func (b BugsById) Less(i, j int) bool {
57 return b[i].Id < b[j].Id
58}
59
60func (b BugsById) Swap(i, j int) {
61 b[i], b[j] = b[j], b[i]
62}
63
64type BugsByCreationTime []*BugExcerpt
65
66func (b BugsByCreationTime) Len() int {
67 return len(b)
68}
69
70func (b BugsByCreationTime) Less(i, j int) bool {
71 if b[i].CreateLamportTime < b[j].CreateLamportTime {
72 return true
73 }
74
75 if b[i].CreateLamportTime > b[j].CreateLamportTime {
76 return false
77 }
78
79 // When the logical clocks are identical, that means we had a concurrent
80 // edition. In this case we rely on the timestamp. While the timestamp might
81 // be incorrect due to a badly set clock, the drift in sorting is bounded
82 // by the first sorting using the logical clock. That means that if users
83 // synchronize their bugs regularly, the timestamp will rarely be used, and
84 // should still provide a kinda accurate sorting when needed.
85 return b[i].CreateUnixTime < b[j].CreateUnixTime
86}
87
88func (b BugsByCreationTime) Swap(i, j int) {
89 b[i], b[j] = b[j], b[i]
90}
91
92type BugsByEditTime []*BugExcerpt
93
94func (b BugsByEditTime) Len() int {
95 return len(b)
96}
97
98func (b BugsByEditTime) Less(i, j int) bool {
99 if b[i].EditLamportTime < b[j].EditLamportTime {
100 return true
101 }
102
103 if b[i].EditLamportTime > b[j].EditLamportTime {
104 return false
105 }
106
107 // When the logical clocks are identical, that means we had a concurrent
108 // edition. In this case we rely on the timestamp. While the timestamp might
109 // be incorrect due to a badly set clock, the drift in sorting is bounded
110 // by the first sorting using the logical clock. That means that if users
111 // synchronize their bugs regularly, the timestamp will rarely be used, and
112 // should still provide a kinda accurate sorting when needed.
113 return b[i].EditUnixTime < b[j].EditUnixTime
114}
115
116func (b BugsByEditTime) Swap(i, j int) {
117 b[i], b[j] = b[j], b[i]
118}