bug_excerpt.go

 1package cache
 2
 3import (
 4	"github.com/MichaelMure/git-bug/bug"
 5	"github.com/MichaelMure/git-bug/util"
 6)
 7
 8// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs
 9// efficiently without having to read and compile each raw bugs.
10type BugExcerpt struct {
11	Id string
12
13	CreateLamportTime util.LamportTime
14	EditLamportTime   util.LamportTime
15	CreateUnixTime    int64
16	EditUnixTime      int64
17
18	Status bug.Status
19	Author bug.Person
20}
21
22func NewBugExcerpt(b *bug.Bug, snap bug.Snapshot) BugExcerpt {
23	return BugExcerpt{
24		Id:                b.Id(),
25		CreateLamportTime: b.CreateLamportTime(),
26		EditLamportTime:   b.EditLamportTime(),
27		CreateUnixTime:    b.FirstOp().UnixTime(),
28		EditUnixTime:      snap.LastEditUnix(),
29		Status:            snap.Status,
30		Author:            snap.Author,
31	}
32}
33
34/*
35 * Sorting
36 */
37
38type BugsByCreationTime []*BugExcerpt
39
40func (b BugsByCreationTime) Len() int {
41	return len(b)
42}
43
44func (b BugsByCreationTime) Less(i, j int) bool {
45	if b[i].CreateLamportTime < b[j].CreateLamportTime {
46		return true
47	}
48
49	if b[i].CreateLamportTime > b[j].CreateLamportTime {
50		return false
51	}
52
53	// When the logical clocks are identical, that means we had a concurrent
54	// edition. In this case we rely on the timestamp. While the timestamp might
55	// be incorrect due to a badly set clock, the drift in sorting is bounded
56	// by the first sorting using the logical clock. That means that if users
57	// synchronize their bugs regularly, the timestamp will rarely be used, and
58	// should still provide a kinda accurate sorting when needed.
59	return b[i].CreateUnixTime < b[j].CreateUnixTime
60}
61
62func (b BugsByCreationTime) Swap(i, j int) {
63	b[i], b[j] = b[j], b[i]
64}
65
66type BugsByEditTime []*BugExcerpt
67
68func (b BugsByEditTime) Len() int {
69	return len(b)
70}
71
72func (b BugsByEditTime) Less(i, j int) bool {
73	if b[i].EditLamportTime < b[j].EditLamportTime {
74		return true
75	}
76
77	if b[i].EditLamportTime > b[j].EditLamportTime {
78		return false
79	}
80
81	// When the logical clocks are identical, that means we had a concurrent
82	// edition. In this case we rely on the timestamp. While the timestamp might
83	// be incorrect due to a badly set clock, the drift in sorting is bounded
84	// by the first sorting using the logical clock. That means that if users
85	// synchronize their bugs regularly, the timestamp will rarely be used, and
86	// should still provide a kinda accurate sorting when needed.
87	return b[i].EditUnixTime < b[j].EditUnixTime
88}
89
90func (b BugsByEditTime) Swap(i, j int) {
91	b[i], b[j] = b[j], b[i]
92}