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 Name: snap.Author.Name(),
99 }
100 default:
101 panic("unhandled identity type")
102 }
103
104 return e
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}