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