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