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 entity.Id
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([]entity.Id, 0, len(snap.Participants))
65 for _, participant := range snap.Participants {
66 if _, ok := participant.(*identity.Identity); ok {
67 participantsIds = append(participantsIds, participant.Id())
68 }
69 }
70
71 actorsIds := make([]entity.Id, 0, len(snap.Actors))
72 for _, actor := range snap.Actors {
73 if _, ok := actor.(*identity.Identity); ok {
74 actorsIds = append(actorsIds, actor.Id())
75 }
76 }
77
78 e := &BugExcerpt{
79 Id: b.Id(),
80 CreateLamportTime: b.CreateLamportTime(),
81 EditLamportTime: b.EditLamportTime(),
82 CreateUnixTime: b.FirstOp().GetUnixTime(),
83 EditUnixTime: snap.LastEditUnix(),
84 Status: snap.Status,
85 Labels: snap.Labels,
86 Actors: actorsIds,
87 Participants: participantsIds,
88 Title: snap.Title,
89 LenComments: len(snap.Comments),
90 CreateMetadata: b.FirstOp().AllMetadata(),
91 }
92
93 switch snap.Author.(type) {
94 case *identity.Identity:
95 e.AuthorId = snap.Author.Id()
96 case *identity.Bare:
97 e.LegacyAuthor = LegacyAuthorExcerpt{
98 Login: snap.Author.Login(),
99 Name: snap.Author.Name(),
100 }
101 default:
102 panic("unhandled identity type")
103 }
104
105 return e
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}