bug_excerpt.go

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