repo_cache.go

  1package cache
  2
  3import (
  4	"fmt"
  5	"io"
  6	"io/ioutil"
  7	"os"
  8	"path"
  9	"path/filepath"
 10	"strconv"
 11	"sync"
 12
 13	"github.com/MichaelMure/git-bug/bug"
 14	"github.com/MichaelMure/git-bug/entity"
 15	"github.com/MichaelMure/git-bug/identity"
 16	"github.com/MichaelMure/git-bug/repository"
 17	"github.com/MichaelMure/git-bug/util/process"
 18)
 19
 20// 1: original format
 21// 2: added cache for identities with a reference in the bug cache
 22// 3: no more legacy identity
 23const formatVersion = 3
 24
 25// The maximum number of bugs loaded in memory. After that, eviction will be done.
 26const defaultMaxLoadedBugs = 1000
 27
 28var _ repository.RepoCommon = &RepoCache{}
 29var _ repository.RepoConfig = &RepoCache{}
 30var _ repository.RepoKeyring = &RepoCache{}
 31
 32// RepoCache is a cache for a Repository. This cache has multiple functions:
 33//
 34// 1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
 35// 		access later.
 36// 2. The cache maintain in memory and on disk a pre-digested excerpt for each bug,
 37// 		allowing for fast querying the whole set of bugs without having to load
 38//		them individually.
 39// 3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
 40// 		loss of data that we could have with multiple copies in the same process.
 41// 4. The same way, the cache maintain in memory a single copy of the loaded identities.
 42//
 43// The cache also protect the on-disk data by locking the git repository for its
 44// own usage, by writing a lock file. Of course, normal git operations are not
 45// affected, only git-bug related one.
 46type RepoCache struct {
 47	// the underlying repo
 48	repo repository.ClockedRepo
 49
 50	// the name of the repository, as defined in the MultiRepoCache
 51	name string
 52
 53	// maximum number of loaded bugs
 54	maxLoadedBugs int
 55
 56	muBug sync.RWMutex
 57	// excerpt of bugs data for all bugs
 58	bugExcerpts map[entity.Id]*BugExcerpt
 59	// bug loaded in memory
 60	bugs map[entity.Id]*BugCache
 61	// loadedBugs is an LRU cache that records which bugs the cache has loaded in
 62	loadedBugs *LRUIdCache
 63
 64	muIdentity sync.RWMutex
 65	// excerpt of identities data for all identities
 66	identitiesExcerpts map[entity.Id]*IdentityExcerpt
 67	// identities loaded in memory
 68	identities map[entity.Id]*IdentityCache
 69
 70	// the user identity's id, if known
 71	userIdentityId entity.Id
 72}
 73
 74func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
 75	return NewNamedRepoCache(r, "")
 76}
 77
 78func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) {
 79	c := &RepoCache{
 80		repo:          r,
 81		name:          name,
 82		maxLoadedBugs: defaultMaxLoadedBugs,
 83		bugs:          make(map[entity.Id]*BugCache),
 84		loadedBugs:    NewLRUIdCache(),
 85		identities:    make(map[entity.Id]*IdentityCache),
 86	}
 87
 88	err := c.lock()
 89	if err != nil {
 90		return &RepoCache{}, err
 91	}
 92
 93	err = c.load()
 94	if err == nil {
 95		return c, nil
 96	}
 97
 98	// Cache is either missing, broken or outdated. Rebuilding.
 99	err = c.buildCache()
100	if err != nil {
101		return nil, err
102	}
103
104	return c, c.write()
105}
106
107// setCacheSize change the maximum number of loaded bugs
108func (c *RepoCache) setCacheSize(size int) {
109	c.maxLoadedBugs = size
110	c.evictIfNeeded()
111}
112
113// load will try to read from the disk all the cache files
114func (c *RepoCache) load() error {
115	err := c.loadBugCache()
116	if err != nil {
117		return err
118	}
119	return c.loadIdentityCache()
120}
121
122// write will serialize on disk all the cache files
123func (c *RepoCache) write() error {
124	err := c.writeBugCache()
125	if err != nil {
126		return err
127	}
128	return c.writeIdentityCache()
129}
130
131func (c *RepoCache) lock() error {
132	lockPath := repoLockFilePath(c.repo)
133
134	err := repoIsAvailable(c.repo)
135	if err != nil {
136		return err
137	}
138
139	err = os.MkdirAll(filepath.Dir(lockPath), 0777)
140	if err != nil {
141		return err
142	}
143
144	f, err := os.Create(lockPath)
145	if err != nil {
146		return err
147	}
148
149	pid := fmt.Sprintf("%d", os.Getpid())
150	_, err = f.WriteString(pid)
151	if err != nil {
152		return err
153	}
154
155	return f.Close()
156}
157
158func (c *RepoCache) Close() error {
159	c.muBug.Lock()
160	defer c.muBug.Unlock()
161	c.muIdentity.Lock()
162	defer c.muIdentity.Unlock()
163
164	c.identities = make(map[entity.Id]*IdentityCache)
165	c.identitiesExcerpts = nil
166	c.bugs = make(map[entity.Id]*BugCache)
167	c.bugExcerpts = nil
168
169	lockPath := repoLockFilePath(c.repo)
170	return os.Remove(lockPath)
171}
172
173func (c *RepoCache) buildCache() error {
174	c.muBug.Lock()
175	defer c.muBug.Unlock()
176	c.muIdentity.Lock()
177	defer c.muIdentity.Unlock()
178
179	_, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
180
181	c.identitiesExcerpts = make(map[entity.Id]*IdentityExcerpt)
182
183	allIdentities := identity.ReadAllLocal(c.repo)
184
185	for i := range allIdentities {
186		if i.Err != nil {
187			return i.Err
188		}
189
190		c.identitiesExcerpts[i.Identity.Id()] = NewIdentityExcerpt(i.Identity)
191	}
192
193	_, _ = fmt.Fprintln(os.Stderr, "Done.")
194
195	_, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
196
197	c.bugExcerpts = make(map[entity.Id]*BugExcerpt)
198
199	allBugs := bug.ReadAllLocal(c.repo)
200
201	for b := range allBugs {
202		if b.Err != nil {
203			return b.Err
204		}
205
206		snap := b.Bug.Compile()
207		c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
208	}
209
210	_, _ = fmt.Fprintln(os.Stderr, "Done.")
211	return nil
212}
213
214func repoLockFilePath(repo repository.Repo) string {
215	return path.Join(repo.GetPath(), "git-bug", lockfile)
216}
217
218// repoIsAvailable check is the given repository is locked by a Cache.
219// Note: this is a smart function that will cleanup the lock file if the
220// corresponding process is not there anymore.
221// If no error is returned, the repo is free to edit.
222func repoIsAvailable(repo repository.Repo) error {
223	lockPath := repoLockFilePath(repo)
224
225	// Todo: this leave way for a racey access to the repo between the test
226	// if the file exist and the actual write. It's probably not a problem in
227	// practice because using a repository will be done from user interaction
228	// or in a context where a single instance of git-bug is already guaranteed
229	// (say, a server with the web UI running). But still, that might be nice to
230	// have a mutex or something to guard that.
231
232	// Todo: this will fail if somehow the filesystem is shared with another
233	// computer. Should add a configuration that prevent the cleaning of the
234	// lock file
235
236	f, err := os.Open(lockPath)
237
238	if err != nil && !os.IsNotExist(err) {
239		return err
240	}
241
242	if err == nil {
243		// lock file already exist
244		buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
245		if err != nil {
246			return err
247		}
248		if len(buf) == 10 {
249			return fmt.Errorf("the lock file should be < 10 bytes")
250		}
251
252		pid, err := strconv.Atoi(string(buf))
253		if err != nil {
254			return err
255		}
256
257		if process.IsRunning(pid) {
258			return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
259		}
260
261		// The lock file is just laying there after a crash, clean it
262
263		fmt.Println("A lock file is present but the corresponding process is not, removing it.")
264		err = f.Close()
265		if err != nil {
266			return err
267		}
268
269		err = os.Remove(lockPath)
270		if err != nil {
271			return err
272		}
273	}
274
275	return nil
276}