bug_excerpt.go

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