-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
141 lines (124 loc) · 3.28 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"encoding/gob"
"fmt"
"os"
"slices"
"sync"
"time"
)
// GHPullRequest represents the metadata of a GitHub pull request. It only
// includes relevant fields, and is used in the cache.
type GHPullRequest struct {
ID int
Title string
AuthorUsername string
MergeBranch string
CommitHash string
CachedAt time.Time
}
// Cache represents the cache of the application. It includes the commit map,
// the last cached commits of the branches, and the pull request cache.
// It includes methods that are concurrency-safe.
type Cache struct {
Version int
CommitMap map[string][]string
mapLock sync.Mutex
Built bool
LastBranchHeads map[string]string
PRCache map[int]GHPullRequest
prCacheLock sync.Mutex
}
func (c *Cache) SetLastBranchHead(branch, hash string) {
c.mapLock.Lock()
defer c.mapLock.Unlock()
c.LastBranchHeads[branch] = hash
}
func (c *Cache) GetLastBranchHead(branch string) string {
c.mapLock.Lock()
defer c.mapLock.Unlock()
return c.LastBranchHeads[branch]
}
func NewCache() *Cache {
return &Cache{
Version: 1,
CommitMap: make(map[string][]string),
LastBranchHeads: make(map[string]string),
PRCache: make(map[int]GHPullRequest),
}
}
func (c *Cache) GetBranchesForCommit(hash string) []string {
c.mapLock.Lock()
defer c.mapLock.Unlock()
return c.CommitMap[hash]
}
func (c *Cache) CommitExistsInBranch(hash, branch string) bool {
c.mapLock.Lock()
defer c.mapLock.Unlock()
return slices.Contains(c.CommitMap[hash], branch)
}
func (c *Cache) AddCommitToBranch(hash, branch string) {
c.mapLock.Lock()
defer c.mapLock.Unlock()
if !slices.Contains(c.CommitMap[hash], branch) {
c.CommitMap[hash] = append(c.CommitMap[hash], branch)
}
}
func (c *Cache) getGHPR(prId int) (GHPullRequest, error) {
c.prCacheLock.Lock()
if pr, ok := c.PRCache[prId]; ok {
c.prCacheLock.Unlock()
return pr, nil
}
// Don't let the request block accessing the cache
c.prCacheLock.Unlock()
pr, _, err := client.PullRequests.Get(context.Background(), "NixOS", "nixpkgs", prId)
if err != nil {
return GHPullRequest{}, err
}
ghpr := GHPullRequest{
ID: prId,
Title: *pr.Title,
AuthorUsername: *pr.User.Login,
MergeBranch: *pr.Base.Ref,
CommitHash: *pr.Head.SHA,
CachedAt: time.Now(),
}
c.prCacheLock.Lock()
c.PRCache[prId] = ghpr
c.prCacheLock.Unlock()
return ghpr, nil
}
func (c *Cache) SaveToFile(filename string) error {
c.mapLock.Lock()
defer c.mapLock.Unlock()
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating file: %v", err)
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(c)
if err != nil {
return fmt.Errorf("error encoding data: %v", err)
}
return nil
}
// LoadFromFile loads the Cache struct from a file using gob encoding
func (c *Cache) LoadFromFile(filename string) error {
c.mapLock.Lock()
defer c.mapLock.Unlock()
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
// Create a new gob decoder and read the Cache from the file
decoder := gob.NewDecoder(file)
err = decoder.Decode(c)
if err != nil {
return fmt.Errorf("error decoding data: %v", err)
}
return nil
}