| 1 | package gitgraph |
| 2 | |
| 3 | import ( |
| 4 | "container/list" |
| 5 | "context" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | ) |
| 9 | |
| 10 | // DefaultMaxBytes is 8 MiB, ruim voor honderden grafieken. |
| 11 | const DefaultMaxBytes = 8 << 20 |
| 12 | |
| 13 | // Loader leest commits en refs uit git. Wordt alleen bij een miss aangeroepen. |
| 14 | type Loader func(ctx context.Context) ([]Commit, []Ref, error) |
| 15 | |
| 16 | // Cache bewaart gelegde grafieken in het geheugen. Geen schijf-I/O. |
| 17 | type Cache struct { |
| 18 | max int64 |
| 19 | |
| 20 | mu sync.Mutex |
| 21 | lru *list.List // voorkant = recent |
| 22 | items map[string]*list.Element |
| 23 | size int64 |
| 24 | hits int64 |
| 25 | miss int64 |
| 26 | } |
| 27 | |
| 28 | type cached struct { |
| 29 | key string |
| 30 | repo int64 |
| 31 | graph *Graph |
| 32 | bytes int64 |
| 33 | } |
| 34 | |
| 35 | // NewCache maakt een lege LRU-cache. max <= 0 gebruikt DefaultMaxBytes. |
| 36 | func NewCache(max int64) *Cache { |
| 37 | if max <= 0 { |
| 38 | max = DefaultMaxBytes |
| 39 | } |
| 40 | return &Cache{max: max, lru: list.New(), items: map[string]*list.Element{}} |
| 41 | } |
| 42 | |
| 43 | // Get geeft een cached grafiek, of legt er één vanuit load. load wordt |
| 44 | // bij een hit niet aangeroepen. |
| 45 | func (c *Cache) Get(ctx context.Context, key, defaultBranch string, load Loader) (*Graph, error) { |
| 46 | if c == nil { |
| 47 | commits, refs, err := load(ctx) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | return Layout(commits, refs, defaultBranch), nil |
| 52 | } |
| 53 | if g, ok := c.lookup(key); ok { |
| 54 | return g, nil |
| 55 | } |
| 56 | commits, refs, err := load(ctx) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | g := Layout(commits, refs, defaultBranch) |
| 61 | c.put(key, repoIDFromKey(key), g) |
| 62 | return g, nil |
| 63 | } |
| 64 | |
| 65 | func repoIDFromKey(key string) int64 { |
| 66 | var id int64 |
| 67 | for _, r := range key { |
| 68 | if r == ':' { |
| 69 | break |
| 70 | } |
| 71 | if r < '0' || r > '9' { |
| 72 | return 0 |
| 73 | } |
| 74 | id = id*10 + int64(r-'0') |
| 75 | } |
| 76 | return id |
| 77 | } |
| 78 | |
| 79 | func (c *Cache) lookup(key string) (*Graph, bool) { |
| 80 | c.mu.Lock() |
| 81 | defer c.mu.Unlock() |
| 82 | el, ok := c.items[key] |
| 83 | if !ok { |
| 84 | c.miss++ |
| 85 | return nil, false |
| 86 | } |
| 87 | c.lru.MoveToFront(el) |
| 88 | c.hits++ |
| 89 | return el.Value.(*cached).graph, true |
| 90 | } |
| 91 | |
| 92 | func (c *Cache) put(key string, repoID int64, g *Graph) { |
| 93 | if c == nil || g == nil { |
| 94 | return |
| 95 | } |
| 96 | n := estimateBytes(g) |
| 97 | c.mu.Lock() |
| 98 | defer c.mu.Unlock() |
| 99 | if el, ok := c.items[key]; ok { |
| 100 | c.size -= el.Value.(*cached).bytes |
| 101 | c.lru.Remove(el) |
| 102 | delete(c.items, key) |
| 103 | } |
| 104 | c.items[key] = c.lru.PushFront(&cached{key: key, repo: repoID, graph: g, bytes: n}) |
| 105 | c.size += n |
| 106 | c.evictLocked() |
| 107 | } |
| 108 | |
| 109 | func estimateBytes(g *Graph) int64 { |
| 110 | n := 256 |
| 111 | for _, r := range g.Rows { |
| 112 | n += 128 + len(r.SHA) + len(r.Subject) + len(r.Author) + len(r.Email) |
| 113 | for _, lb := range r.Labels { |
| 114 | n += 32 + len(lb.Name) |
| 115 | } |
| 116 | } |
| 117 | n += len(g.Edges) * 40 |
| 118 | return int64(n) |
| 119 | } |
| 120 | |
| 121 | func (c *Cache) evictLocked() { |
| 122 | for c.size > c.max && c.lru.Len() > 1 { |
| 123 | el := c.lru.Back() |
| 124 | e := el.Value.(*cached) |
| 125 | c.lru.Remove(el) |
| 126 | delete(c.items, e.key) |
| 127 | c.size -= e.bytes |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Drop vergeet alle grafieken van één repository (na een push of verwijdering). |
| 132 | func (c *Cache) Drop(repoID int64) { |
| 133 | if c == nil { |
| 134 | return |
| 135 | } |
| 136 | prefix := strings.TrimSpace(strings.SplitN(Key(repoID, nil, "", 0), ":", 2)[0]) + ":" |
| 137 | // Key always starts with "<id>:" |
| 138 | _ = prefix |
| 139 | c.mu.Lock() |
| 140 | defer c.mu.Unlock() |
| 141 | for key, el := range c.items { |
| 142 | if el.Value.(*cached).repo == repoID { |
| 143 | c.size -= el.Value.(*cached).bytes |
| 144 | c.lru.Remove(el) |
| 145 | delete(c.items, key) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Stats is hits, misses en huidig geheugengebruik, voor metingen. |
| 151 | func (c *Cache) Stats() (hits, misses, entries int64, bytes int64) { |
| 152 | if c == nil { |
| 153 | return 0, 0, 0, 0 |
| 154 | } |
| 155 | c.mu.Lock() |
| 156 | defer c.mu.Unlock() |
| 157 | return c.hits, c.miss, int64(len(c.items)), c.size |
| 158 | } |