board_excerpt.go

 1package cache
 2
 3import (
 4	"encoding/gob"
 5	"time"
 6
 7	"github.com/MichaelMure/git-bug/entity"
 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(BoardExcerpt{})
14}
15
16var _ Excerpt = &BoardExcerpt{}
17
18// BoardExcerpt hold a subset of the board values to be able to sort and filter boards
19// efficiently without having to read and compile each raw boards.
20type BoardExcerpt struct {
21	id entity.Id
22
23	CreateLamportTime lamport.Time
24	EditLamportTime   lamport.Time
25	CreateUnixTime    int64
26	EditUnixTime      int64
27
28	Title       string
29	Description string
30	ItemCount   int
31	Actors      []entity.Id
32
33	CreateMetadata map[string]string
34}
35
36func NewBoardExcerpt(b *BoardCache) *BoardExcerpt {
37	snap := b.Snapshot()
38
39	actorsIds := make([]entity.Id, 0, len(snap.Actors))
40	for _, actor := range snap.Actors {
41		actorsIds = append(actorsIds, actor.Id())
42	}
43
44	return &BoardExcerpt{
45		id:                b.Id(),
46		CreateLamportTime: b.CreateLamportTime(),
47		EditLamportTime:   b.EditLamportTime(),
48		CreateUnixTime:    b.FirstOp().Time().Unix(),
49		EditUnixTime:      snap.EditTime().Unix(),
50		Title:             snap.Title,
51		Description:       snap.Description,
52		ItemCount:         snap.ItemCount(),
53		Actors:            actorsIds,
54		CreateMetadata:    b.FirstOp().AllMetadata(),
55	}
56}
57
58func (b *BoardExcerpt) Id() entity.Id {
59	return b.id
60}
61
62func (b *BoardExcerpt) setId(id entity.Id) {
63	b.id = id
64}
65
66func (b *BoardExcerpt) CreateTime() time.Time {
67	return time.Unix(b.CreateUnixTime, 0)
68}
69
70func (b *BoardExcerpt) EditTime() time.Time {
71	return time.Unix(b.EditUnixTime, 0)
72}