1package cache
2
3import (
4 "encoding/gob"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/bug"
8 "github.com/MichaelMure/git-bug/identity"
9 "github.com/MichaelMure/git-bug/util/lamport"
10)
11
12// Package initialisation used to register the type for (de)serialization
13func init() {
14 gob.Register(BugExcerpt{})
15}
16
17// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs
18// efficiently without having to read and compile each raw bugs.
19type BugExcerpt struct {
20 Id string
21
22 CreateLamportTime lamport.Time
23 EditLamportTime lamport.Time
24 CreateUnixTime int64
25 EditUnixTime int64
26
27 Status bug.Status
28 Labels []bug.Label
29 Title string
30 LenComments int
31 Actors []string
32 Participants []string
33
34 // If author is identity.Bare, LegacyAuthor is set
35 // If author is identity.Identity, AuthorId is set and data is deported
36 // in a IdentityExcerpt
37 LegacyAuthor LegacyAuthorExcerpt
38 AuthorId string
39
40 CreateMetadata map[string]string
41}
42
43// identity.Bare data are directly embedded in the bug excerpt
44type LegacyAuthorExcerpt struct {
45 Name string
46 Login string
47}
48
49func (l LegacyAuthorExcerpt) DisplayName() string {
50 switch {
51 case l.Name == "" && l.Login != "":
52 return l.Login
53 case l.Name != "" && l.Login == "":
54 return l.Name
55 case l.Name != "" && l.Login != "":
56 return fmt.Sprintf("%s (%s)", l.Name, l.Login)
57 }
58
59 panic("invalid person data")
60}
61
62func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
63 participantsIds := make([]string, len(snap.Participants))
64 for i, participant := range snap.Participants {
65 participantsIds[i] = participant.Id()
66 }
67
68 actorsIds := make([]string, len(snap.Actors))
69 for i, actor := range snap.Actors {
70 actorsIds[i] = actor.Id()
71 }
72
73 e := &BugExcerpt{
74 Id: b.Id(),
75 CreateLamportTime: b.CreateLamportTime(),
76 EditLamportTime: b.EditLamportTime(),
77 CreateUnixTime: b.FirstOp().GetUnixTime(),
78 EditUnixTime: snap.LastEditUnix(),
79 Status: snap.Status,
80 Labels: snap.Labels,
81 Actors: actorsIds,
82 Participants: participantsIds,
83 Title: snap.Title,
84 LenComments: len(snap.Comments),
85 CreateMetadata: b.FirstOp().AllMetadata(),
86 }
87
88 switch snap.Author.(type) {
89 case *identity.Identity:
90 e.AuthorId = snap.Author.Id()
91 case *identity.Bare:
92 e.LegacyAuthor = LegacyAuthorExcerpt{
93 Login: snap.Author.Login(),
94 Name: snap.Author.Name(),
95 }
96 default:
97 panic("unhandled identity type")
98 }
99
100 return e
101}
102
103func (b *BugExcerpt) HumanId() string {
104 return bug.FormatHumanID(b.Id)
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}