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