repo_cache.go

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