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