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