1package cache
2
3import (
4 "fmt"
5
6 "github.com/git-bug/git-bug/repository"
7)
8
9const lockfile = "lock"
10const defaultRepoName = "__default"
11
12// MultiRepoCache is the root cache, holding multiple RepoCache.
13type MultiRepoCache struct {
14 repos map[string]*RepoCache
15}
16
17func NewMultiRepoCache() *MultiRepoCache {
18 return &MultiRepoCache{
19 repos: make(map[string]*RepoCache),
20 }
21}
22
23// RegisterRepository registers a named repository. Use this for multi-repo setup
24func (c *MultiRepoCache) RegisterRepository(repo repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent) {
25 r, events := NewNamedRepoCache(repo, name)
26
27 // intercept events to make sure the cache building process succeeds properly
28 out := make(chan BuildEvent)
29 go func() {
30 defer close(out)
31
32 for event := range events {
33 out <- event
34 if event.Err != nil {
35 return
36 }
37 }
38
39 c.repos[name] = r
40 }()
41
42 return r, out
43}
44
45// RegisterDefaultRepository registers an unnamed repository. Use this for single-repo setup
46func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.ClockedRepo) (*RepoCache, chan BuildEvent) {
47 return c.RegisterRepository(repo, defaultRepoName)
48}
49
50// DefaultRepo retrieves the default repository
51func (c *MultiRepoCache) DefaultRepo() (*RepoCache, error) {
52 if len(c.repos) != 1 {
53 return nil, fmt.Errorf("repository is not unique")
54 }
55
56 for _, r := range c.repos {
57 return r, nil
58 }
59
60 panic("unreachable")
61}
62
63// ResolveRepo retrieves a repository by name
64func (c *MultiRepoCache) ResolveRepo(name string) (*RepoCache, error) {
65 r, ok := c.repos[name]
66 if !ok {
67 return nil, fmt.Errorf("unknown repo")
68 }
69 return r, nil
70}
71
72// RegisterObserver registers an Observer on repo and entity, according to nameFilter and typename.
73// - if nameFilter is empty, the observer is registered on all available repo
74// - if nameFilter is not empty, the observer is registered on the repo with the matching name
75// - if typename is empty, the observer is registered on all available entities
76// - if typename is not empty, the observer is registered on the matching entity type only
77func (c *MultiRepoCache) RegisterObserver(observer Observer, nameFilter string, typename string) error {
78 if nameFilter == "" {
79 for repoName, repo := range c.repos {
80 if typename == "" {
81 repo.registerAllObservers(repoName, observer)
82 } else {
83 if err := repo.registerObserver(repoName, typename, observer); err != nil {
84 return err
85 }
86 }
87 }
88 return nil
89 }
90
91 r, err := c.ResolveRepo(nameFilter)
92 if err != nil {
93 return err
94 }
95 if typename == "" {
96 r.registerAllObservers(r.Name(), observer)
97 } else {
98 if err := r.registerObserver(r.Name(), typename, observer); err != nil {
99 return err
100 }
101 }
102 return nil
103}
104
105// UnregisterObserver deregisters the observer from all repos and all entity types.
106func (c *MultiRepoCache) UnregisterObserver(observer Observer) {
107 for _, repo := range c.repos {
108 repo.unregisterAllObservers(observer)
109 }
110}
111
112// Close will do anything that is needed to close the cache properly
113func (c *MultiRepoCache) Close() error {
114 for _, cachedRepo := range c.repos {
115 err := cachedRepo.Close()
116 if err != nil {
117 return err
118 }
119 }
120 return nil
121}