1package cache
  2
  3import (
  4	"fmt"
  5	"io"
  6	"os"
  7	"strconv"
  8	"sync"
  9
 10	"github.com/MichaelMure/git-bug/entities/bug"
 11	"github.com/MichaelMure/git-bug/entity"
 12	"github.com/MichaelMure/git-bug/repository"
 13	"github.com/MichaelMure/git-bug/util/multierr"
 14	"github.com/MichaelMure/git-bug/util/process"
 15)
 16
 17// 1: original format
 18// 2: added cache for identities with a reference in the bug cache
 19// 3: no more legacy identity
 20// 4: entities make their IDs from data, not git commit
 21const formatVersion = 4
 22
 23// The maximum number of bugs loaded in memory. After that, eviction will be done.
 24const defaultMaxLoadedBugs = 1000
 25
 26var _ repository.RepoCommon = &RepoCache{}
 27var _ repository.RepoConfig = &RepoCache{}
 28var _ repository.RepoKeyring = &RepoCache{}
 29
 30// cacheMgmt is the expected interface for a sub-cache.
 31type cacheMgmt interface {
 32	Typename() string
 33	Load() error
 34	Build() <-chan BuildEvent
 35	SetCacheSize(size int)
 36	RemoveAll() error
 37	MergeAll(remote string) <-chan entity.MergeResult
 38	GetNamespace() string
 39	Close() error
 40}
 41
 42// RepoCache is a cache for a Repository. This cache has multiple functions:
 43//
 44//  1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
 45//     access later.
 46//  2. The cache maintain in memory and on disk a pre-digested excerpt for each bug,
 47//     allowing for fast querying the whole set of bugs without having to load
 48//     them individually.
 49//  3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
 50//     loss of data that we could have with multiple copies in the same process.
 51//  4. The same way, the cache maintain in memory a single copy of the loaded identities.
 52//
 53// The cache also protect the on-disk data by locking the git repository for its
 54// own usage, by writing a lock file. Of course, normal git operations are not
 55// affected, only git-bug related one.
 56type RepoCache struct {
 57	// the underlying repo
 58	repo repository.ClockedRepo
 59
 60	// the name of the repository, as defined in the MultiRepoCache
 61	name string
 62
 63	// resolvers for all known entities and excerpts
 64	resolvers entity.Resolvers
 65
 66	bugs       *RepoCacheBug
 67	identities *RepoCacheIdentity
 68
 69	subcaches []cacheMgmt
 70
 71	// the user identity's id, if known
 72	muUserIdentity sync.RWMutex
 73	userIdentityId entity.Id
 74}
 75
 76// NewRepoCache create or open a cache on top of a raw repository.
 77// The caller is expected to read all returned events before the cache is considered
 78// ready to use.
 79func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent) {
 80	return NewNamedRepoCache(r, defaultRepoName)
 81}
 82
 83// NewNamedRepoCache create or open a named cache on top of a raw repository.
 84// The caller is expected to read all returned events before the cache is considered
 85// ready to use.
 86func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent) {
 87	c := &RepoCache{
 88		repo: r,
 89		name: name,
 90	}
 91
 92	c.identities = NewRepoCacheIdentity(r, c.getResolvers, c.GetUserIdentity)
 93	c.subcaches = append(c.subcaches, c.identities)
 94
 95	c.bugs = NewRepoCacheBug(r, c.getResolvers, c.GetUserIdentity)
 96	c.subcaches = append(c.subcaches, c.bugs)
 97
 98	c.resolvers = entity.Resolvers{
 99		&IdentityCache{}:   entity.ResolverFunc[*IdentityCache](c.identities.Resolve),
100		&IdentityExcerpt{}: entity.ResolverFunc[*IdentityExcerpt](c.identities.ResolveExcerpt),
101
102		bug.Interface(nil): entity.ResolverFunc[*BugCache](c.bugs.Resolve),
103		&BugCache{}:        entity.ResolverFunc[*BugCache](c.bugs.Resolve),
104		&BugExcerpt{}:      entity.ResolverFunc[*BugExcerpt](c.bugs.ResolveExcerpt),
105	}
106
107	// small buffer so that below functions can emit an event without blocking
108	events := make(chan BuildEvent)
109
110	go func() {
111		defer close(events)
112
113		err := c.lock(events)
114		if err != nil {
115			events <- BuildEvent{Err: err}
116			return
117		}
118
119		err = c.load()
120		if err == nil {
121			return
122		}
123
124		// Cache is either missing, broken or outdated. Rebuilding.
125		c.buildCache(events)
126	}()
127
128	return c, events
129}
130
131func NewRepoCacheNoEvents(r repository.ClockedRepo) (*RepoCache, error) {
132	cache, events := NewRepoCache(r)
133	for event := range events {
134		if event.Err != nil {
135			for range events {
136			}
137			return nil, event.Err
138		}
139	}
140	return cache, nil
141}
142
143// Bugs gives access to the Bug entities
144func (c *RepoCache) Bugs() *RepoCacheBug {
145	return c.bugs
146}
147
148// Identities gives access to the Identity entities
149func (c *RepoCache) Identities() *RepoCacheIdentity {
150	return c.identities
151}
152
153func (c *RepoCache) getResolvers() entity.Resolvers {
154	return c.resolvers
155}
156
157// setCacheSize change the maximum number of loaded bugs
158func (c *RepoCache) setCacheSize(size int) {
159	for _, subcache := range c.subcaches {
160		subcache.SetCacheSize(size)
161	}
162}
163
164// load will try to read from the disk all the cache files
165func (c *RepoCache) load() error {
166	var errWait multierr.ErrWaitGroup
167	for _, mgmt := range c.subcaches {
168		errWait.Go(mgmt.Load)
169	}
170	return errWait.Wait()
171}
172
173func (c *RepoCache) lock(events chan BuildEvent) error {
174	err := repoIsAvailable(c.repo, events)
175	if err != nil {
176		return err
177	}
178
179	f, err := c.repo.LocalStorage().Create(lockfile)
180	if err != nil {
181		return err
182	}
183
184	pid := fmt.Sprintf("%d", os.Getpid())
185	_, err = f.Write([]byte(pid))
186	if err != nil {
187		_ = f.Close()
188		return err
189	}
190
191	return f.Close()
192}
193
194func (c *RepoCache) Close() error {
195	var errWait multierr.ErrWaitGroup
196	for _, mgmt := range c.subcaches {
197		errWait.Go(mgmt.Close)
198	}
199	err := errWait.Wait()
200	if err != nil {
201		return err
202	}
203
204	err = c.repo.Close()
205	if err != nil {
206		return err
207	}
208
209	return c.repo.LocalStorage().Remove(lockfile)
210}
211
212type BuildEventType int
213
214const (
215	_ BuildEventType = iota
216	// BuildEventCacheIsBuilt signal that the cache is being built (aka, not skipped)
217	BuildEventCacheIsBuilt
218	// BuildEventRemoveLock signal that an old repo lock has been cleaned
219	BuildEventRemoveLock
220	// BuildEventStarted signal the beginning of a cache build for an entity
221	BuildEventStarted
222	// BuildEventProgress signal progress in the cache building for an entity
223	BuildEventProgress
224	// BuildEventFinished signal the end of a cache build for an entity
225	BuildEventFinished
226)
227
228// BuildEvent carry an event happening during the cache build process.
229type BuildEvent struct {
230	// Err carry an error if the build process failed. If set, no other field matter.
231	Err error
232	// Typename is the name of the entity of which the event relate to. Can be empty if not particular entity is involved.
233	Typename string
234	// Event is the type of the event.
235	Event BuildEventType
236	// Total is the total number of element being built. Set if Event is BuildEventStarted.
237	Total int64
238	// Progress is the current count of processed element. Set if Event is BuildEventProgress.
239	Progress int64
240}
241
242func (c *RepoCache) buildCache(events chan BuildEvent) {
243	events <- BuildEvent{Event: BuildEventCacheIsBuilt}
244
245	var wg sync.WaitGroup
246	for _, subcache := range c.subcaches {
247		wg.Add(1)
248		go func(subcache cacheMgmt) {
249			defer wg.Done()
250
251			buildEvents := subcache.Build()
252			for buildEvent := range buildEvents {
253				events <- buildEvent
254				if buildEvent.Err != nil {
255					return
256				}
257			}
258		}(subcache)
259	}
260	wg.Wait()
261}
262
263// repoIsAvailable check is the given repository is locked by a Cache.
264// Note: this is a smart function that will clean the lock file if the
265// corresponding process is not there anymore.
266// If no error is returned, the repo is free to edit.
267func repoIsAvailable(repo repository.RepoStorage, events chan BuildEvent) error {
268	// Todo: this leave way for a racey access to the repo between the test
269	// if the file exist and the actual write. It's probably not a problem in
270	// practice because using a repository will be done from user interaction
271	// or in a context where a single instance of git-bug is already guaranteed
272	// (say, a server with the web UI running). But still, that might be nice to
273	// have a mutex or something to guard that.
274
275	// Todo: this will fail if somehow the filesystem is shared with another
276	// computer. Should add a configuration that prevent the cleaning of the
277	// lock file
278
279	f, err := repo.LocalStorage().Open(lockfile)
280	if err != nil && !os.IsNotExist(err) {
281		return err
282	}
283
284	if err == nil {
285		// lock file already exist
286		buf, err := io.ReadAll(io.LimitReader(f, 10))
287		if err != nil {
288			_ = f.Close()
289			return err
290		}
291
292		err = f.Close()
293		if err != nil {
294			return err
295		}
296
297		if len(buf) >= 10 {
298			return fmt.Errorf("the lock file should be < 10 bytes")
299		}
300
301		pid, err := strconv.Atoi(string(buf))
302		if err != nil {
303			return err
304		}
305
306		if process.IsRunning(pid) {
307			return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
308		}
309
310		// The lock file is just laying there after a crash, clean it
311
312		events <- BuildEvent{Event: BuildEventRemoveLock}
313
314		err = repo.LocalStorage().Remove(lockfile)
315		if err != nil {
316			return err
317		}
318	}
319
320	return nil
321}