1package cache
2
3import (
4 "encoding/gob"
5 "fmt"
6 "time"
7
8 "github.com/MichaelMure/git-bug/bug"
9 "github.com/MichaelMure/git-bug/entity"
10 "github.com/MichaelMure/git-bug/identity"
11 "github.com/MichaelMure/git-bug/util/lamport"
12)
13
14// Package initialisation used to register the type for (de)serialization
15func init() {
16 gob.Register(BugExcerpt{})
17}
18
19// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs
20// efficiently without having to read and compile each raw bugs.
21type BugExcerpt struct {
22 Id entity.Id
23
24 CreateLamportTime lamport.Time
25 EditLamportTime lamport.Time
26 CreateUnixTime int64
27 EditUnixTime int64
28
29 AuthorId entity.Id
30 Status bug.Status
31 Labels []bug.Label
32 Title string
33 LenComments int
34 Actors []entity.Id
35 Participants []entity.Id
36
37 CreateMetadata map[string]string
38}
39
40// identity.Bare data are directly embedded in the bug excerpt
41type LegacyAuthorExcerpt struct {
42 Name string
43 Login string
44}
45
46func (l LegacyAuthorExcerpt) DisplayName() string {
47 switch {
48 case l.Name == "" && l.Login != "":
49 return l.Login
50 case l.Name != "" && l.Login == "":
51 return l.Name
52 case l.Name != "" && l.Login != "":
53 return fmt.Sprintf("%s (%s)", l.Name, l.Login)
54 }
55
56 panic("invalid person data")
57}
58
59func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
60 participantsIds := make([]entity.Id, 0, len(snap.Participants))
61 for _, participant := range snap.Participants {
62 if _, ok := participant.(*identity.Identity); ok {
63 participantsIds = append(participantsIds, participant.Id())
64 }
65 }
66
67 actorsIds := make([]entity.Id, 0, len(snap.Actors))
68 for _, actor := range snap.Actors {
69 if _, ok := actor.(*identity.Identity); ok {
70 actorsIds = append(actorsIds, actor.Id())
71 }
72 }
73
74 e := &BugExcerpt{
75 Id: b.Id(),
76 CreateLamportTime: b.CreateLamportTime(),
77 EditLamportTime: b.EditLamportTime(),
78 CreateUnixTime: b.FirstOp().Time().Unix(),
79 EditUnixTime: snap.EditTime().Unix(),
80 Status: snap.Status,
81 Labels: snap.Labels,
82 Actors: actorsIds,
83 Participants: participantsIds,
84 Title: snap.Title,
85 LenComments: len(snap.Comments),
86 CreateMetadata: b.FirstOp().AllMetadata(),
87 }
88
89 switch snap.Author.(type) {
90 case *identity.Identity, *identity.IdentityStub, *IdentityCache:
91 e.AuthorId = snap.Author.Id()
92 default:
93 panic("unhandled identity type")
94 }
95
96 return e
97}
98
99func (b *BugExcerpt) CreateTime() time.Time {
100 return time.Unix(b.CreateUnixTime, 0)
101}
102
103func (b *BugExcerpt) EditTime() time.Time {
104 return time.Unix(b.EditUnixTime, 0)
105}
106
107/*
108 * Sorting
109 */
110
111type BugsById []*BugExcerpt
112
113func (b BugsById) Len() int {
114 return len(b)
115}
116
117func (b BugsById) Less(i, j int) bool {
118 return b[i].Id < b[j].Id
119}
120
121func (b BugsById) Swap(i, j int) {
122 b[i], b[j] = b[j], b[i]
123}
124
125type BugsByCreationTime []*BugExcerpt
126
127func (b BugsByCreationTime) Len() int {
128 return len(b)
129}
130
131func (b BugsByCreationTime) Less(i, j int) bool {
132 if b[i].CreateLamportTime < b[j].CreateLamportTime {
133 return true
134 }
135
136 if b[i].CreateLamportTime > b[j].CreateLamportTime {
137 return false
138 }
139
140 // When the logical clocks are identical, that means we had a concurrent
141 // edition. In this case we rely on the timestamp. While the timestamp might
142 // be incorrect due to a badly set clock, the drift in sorting is bounded
143 // by the first sorting using the logical clock. That means that if users
144 // synchronize their bugs regularly, the timestamp will rarely be used, and
145 // should still provide a kinda accurate sorting when needed.
146 return b[i].CreateUnixTime < b[j].CreateUnixTime
147}
148
149func (b BugsByCreationTime) Swap(i, j int) {
150 b[i], b[j] = b[j], b[i]
151}
152
153type BugsByEditTime []*BugExcerpt
154
155func (b BugsByEditTime) Len() int {
156 return len(b)
157}
158
159func (b BugsByEditTime) Less(i, j int) bool {
160 if b[i].EditLamportTime < b[j].EditLamportTime {
161 return true
162 }
163
164 if b[i].EditLamportTime > b[j].EditLamportTime {
165 return false
166 }
167
168 // When the logical clocks are identical, that means we had a concurrent
169 // edition. In this case we rely on the timestamp. While the timestamp might
170 // be incorrect due to a badly set clock, the drift in sorting is bounded
171 // by the first sorting using the logical clock. That means that if users
172 // synchronize their bugs regularly, the timestamp will rarely be used, and
173 // should still provide a kinda accurate sorting when needed.
174 return b[i].EditUnixTime < b[j].EditUnixTime
175}
176
177func (b BugsByEditTime) Swap(i, j int) {
178 b[i], b[j] = b[j], b[i]
179}