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, len(snap.Participants))
65 for i, participant := range snap.Participants {
66 participantsIds[i] = participant.Id()
67 }
68
69 actorsIds := make([]entity.Id, 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
104/*
105 * Sorting
106 */
107
108type BugsById []*BugExcerpt
109
110func (b BugsById) Len() int {
111 return len(b)
112}
113
114func (b BugsById) Less(i, j int) bool {
115 return b[i].Id < b[j].Id
116}
117
118func (b BugsById) Swap(i, j int) {
119 b[i], b[j] = b[j], b[i]
120}
121
122type BugsByCreationTime []*BugExcerpt
123
124func (b BugsByCreationTime) Len() int {
125 return len(b)
126}
127
128func (b BugsByCreationTime) Less(i, j int) bool {
129 if b[i].CreateLamportTime < b[j].CreateLamportTime {
130 return true
131 }
132
133 if b[i].CreateLamportTime > b[j].CreateLamportTime {
134 return false
135 }
136
137 // When the logical clocks are identical, that means we had a concurrent
138 // edition. In this case we rely on the timestamp. While the timestamp might
139 // be incorrect due to a badly set clock, the drift in sorting is bounded
140 // by the first sorting using the logical clock. That means that if users
141 // synchronize their bugs regularly, the timestamp will rarely be used, and
142 // should still provide a kinda accurate sorting when needed.
143 return b[i].CreateUnixTime < b[j].CreateUnixTime
144}
145
146func (b BugsByCreationTime) Swap(i, j int) {
147 b[i], b[j] = b[j], b[i]
148}
149
150type BugsByEditTime []*BugExcerpt
151
152func (b BugsByEditTime) Len() int {
153 return len(b)
154}
155
156func (b BugsByEditTime) Less(i, j int) bool {
157 if b[i].EditLamportTime < b[j].EditLamportTime {
158 return true
159 }
160
161 if b[i].EditLamportTime > b[j].EditLamportTime {
162 return false
163 }
164
165 // When the logical clocks are identical, that means we had a concurrent
166 // edition. In this case we rely on the timestamp. While the timestamp might
167 // be incorrect due to a badly set clock, the drift in sorting is bounded
168 // by the first sorting using the logical clock. That means that if users
169 // synchronize their bugs regularly, the timestamp will rarely be used, and
170 // should still provide a kinda accurate sorting when needed.
171 return b[i].EditUnixTime < b[j].EditUnixTime
172}
173
174func (b BugsByEditTime) Swap(i, j int) {
175 b[i], b[j] = b[j], b[i]
176}