2018-08-21 19:46:53 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-08-31 14:18:03 +03:00
|
|
|
"io/ioutil"
|
2018-08-23 22:24:57 +03:00
|
|
|
"os"
|
2018-08-31 14:18:03 +03:00
|
|
|
"strconv"
|
2020-02-08 19:12:49 +03:00
|
|
|
"sync"
|
2019-08-12 17:12:14 +03:00
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2019-03-27 23:44:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-08-21 19:46:53 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/process"
|
2018-08-21 19:46:53 +03:00
|
|
|
)
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
// 1: original format
|
|
|
|
// 2: added cache for identities with a reference in the bug cache
|
2020-07-01 21:00:53 +03:00
|
|
|
// 3: no more legacy identity
|
2020-11-08 21:18:44 +03:00
|
|
|
// 4: entities make their IDs from data, not git commit
|
|
|
|
const formatVersion = 4
|
2018-09-13 18:59:02 +03:00
|
|
|
|
2020-08-25 16:26:23 +03:00
|
|
|
// The maximum number of bugs loaded in memory. After that, eviction will be done.
|
2020-08-26 04:04:40 +03:00
|
|
|
const defaultMaxLoadedBugs = 1000
|
2020-07-26 10:52:29 +03:00
|
|
|
|
2019-05-27 22:18:46 +03:00
|
|
|
var _ repository.RepoCommon = &RepoCache{}
|
2020-09-27 01:54:14 +03:00
|
|
|
var _ repository.RepoConfig = &RepoCache{}
|
|
|
|
var _ repository.RepoKeyring = &RepoCache{}
|
2019-05-27 22:18:46 +03:00
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// RepoCache is a cache for a Repository. This cache has multiple functions:
|
|
|
|
//
|
|
|
|
// 1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
|
|
|
|
// access later.
|
2019-04-18 03:48:27 +03:00
|
|
|
// 2. The cache maintain in memory and on disk a pre-digested excerpt for each bug,
|
2019-01-19 18:01:06 +03:00
|
|
|
// allowing for fast querying the whole set of bugs without having to load
|
|
|
|
// them individually.
|
|
|
|
// 3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
|
|
|
|
// loss of data that we could have with multiple copies in the same process.
|
|
|
|
// 4. The same way, the cache maintain in memory a single copy of the loaded identities.
|
|
|
|
//
|
|
|
|
// The cache also protect the on-disk data by locking the git repository for its
|
|
|
|
// own usage, by writing a lock file. Of course, normal git operations are not
|
|
|
|
// affected, only git-bug related one.
|
2018-08-21 19:46:53 +03:00
|
|
|
type RepoCache struct {
|
2018-09-11 20:28:32 +03:00
|
|
|
// the underlying repo
|
2018-09-21 19:18:51 +03:00
|
|
|
repo repository.ClockedRepo
|
2019-02-18 16:11:37 +03:00
|
|
|
|
2020-02-12 23:03:20 +03:00
|
|
|
// the name of the repository, as defined in the MultiRepoCache
|
|
|
|
name string
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
// resolvers for all known entities
|
|
|
|
resolvers entity.Resolvers
|
|
|
|
|
2020-08-25 16:26:23 +03:00
|
|
|
// maximum number of loaded bugs
|
|
|
|
maxLoadedBugs int
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
muBug sync.RWMutex
|
2018-09-11 20:28:32 +03:00
|
|
|
// excerpt of bugs data for all bugs
|
2019-08-12 17:12:14 +03:00
|
|
|
bugExcerpts map[entity.Id]*BugExcerpt
|
2018-09-11 20:28:32 +03:00
|
|
|
// bug loaded in memory
|
2019-08-12 17:12:14 +03:00
|
|
|
bugs map[entity.Id]*BugCache
|
2020-08-25 16:26:23 +03:00
|
|
|
// loadedBugs is an LRU cache that records which bugs the cache has loaded in
|
|
|
|
loadedBugs *LRUIdCache
|
2020-07-26 10:52:29 +03:00
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
muIdentity sync.RWMutex
|
2019-02-18 16:11:37 +03:00
|
|
|
// excerpt of identities data for all identities
|
2019-08-12 17:12:14 +03:00
|
|
|
identitiesExcerpts map[entity.Id]*IdentityExcerpt
|
2019-01-17 05:09:08 +03:00
|
|
|
// identities loaded in memory
|
2019-08-12 17:12:14 +03:00
|
|
|
identities map[entity.Id]*IdentityCache
|
2019-02-18 16:11:37 +03:00
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
// the user identity's id, if known
|
2019-08-12 17:12:14 +03:00
|
|
|
userIdentityId entity.Id
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:18:51 +03:00
|
|
|
func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
|
2020-02-12 23:03:20 +03:00
|
|
|
return NewNamedRepoCache(r, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) {
|
2018-08-23 22:24:57 +03:00
|
|
|
c := &RepoCache{
|
2020-08-25 16:26:23 +03:00
|
|
|
repo: r,
|
|
|
|
name: name,
|
|
|
|
maxLoadedBugs: defaultMaxLoadedBugs,
|
|
|
|
bugs: make(map[entity.Id]*BugCache),
|
|
|
|
loadedBugs: NewLRUIdCache(),
|
|
|
|
identities: make(map[entity.Id]*IdentityCache),
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
c.resolvers = makeResolvers(c)
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
err := c.lock()
|
|
|
|
if err != nil {
|
|
|
|
return &RepoCache{}, err
|
|
|
|
}
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
err = c.load()
|
2018-08-23 22:24:57 +03:00
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
2020-06-26 00:18:17 +03:00
|
|
|
|
2020-06-26 01:58:38 +03:00
|
|
|
// Cache is either missing, broken or outdated. Rebuilding.
|
2018-09-19 00:36:45 +03:00
|
|
|
err = c.buildCache()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-09-13 18:59:02 +03:00
|
|
|
return c, c.write()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:26:23 +03:00
|
|
|
// setCacheSize change the maximum number of loaded bugs
|
|
|
|
func (c *RepoCache) setCacheSize(size int) {
|
|
|
|
c.maxLoadedBugs = size
|
|
|
|
c.evictIfNeeded()
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:31:48 +03:00
|
|
|
// load will try to read from the disk all the cache files
|
|
|
|
func (c *RepoCache) load() error {
|
|
|
|
err := c.loadBugCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-01 15:16:31 +03:00
|
|
|
|
2020-07-03 19:31:48 +03:00
|
|
|
return c.loadIdentityCache()
|
2020-06-21 23:12:04 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:31:48 +03:00
|
|
|
// write will serialize on disk all the cache files
|
|
|
|
func (c *RepoCache) write() error {
|
|
|
|
err := c.writeBugCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.writeIdentityCache()
|
2020-06-21 23:12:04 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
func (c *RepoCache) lock() error {
|
|
|
|
err := repoIsAvailable(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
f, err := c.repo.LocalStorage().Create(lockfile)
|
2018-08-31 14:18:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pid := fmt.Sprintf("%d", os.Getpid())
|
2020-12-05 05:08:54 +03:00
|
|
|
_, err = f.Write([]byte(pid))
|
2018-08-31 14:18:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) Close() error {
|
2020-02-08 19:12:49 +03:00
|
|
|
c.muBug.Lock()
|
|
|
|
defer c.muBug.Unlock()
|
|
|
|
c.muIdentity.Lock()
|
|
|
|
defer c.muIdentity.Unlock()
|
|
|
|
|
2019-08-13 20:51:14 +03:00
|
|
|
c.identities = make(map[entity.Id]*IdentityCache)
|
|
|
|
c.identitiesExcerpts = nil
|
2020-08-26 04:04:40 +03:00
|
|
|
c.bugs = make(map[entity.Id]*BugCache)
|
2019-08-13 20:51:14 +03:00
|
|
|
c.bugExcerpts = nil
|
2019-03-25 23:58:27 +03:00
|
|
|
|
2020-12-08 16:42:13 +03:00
|
|
|
err := c.repo.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-01 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
return c.repo.LocalStorage().Remove(lockfile)
|
2018-08-31 14:18:03 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:36:45 +03:00
|
|
|
func (c *RepoCache) buildCache() error {
|
2019-02-19 01:16:47 +03:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
c.identitiesExcerpts = make(map[entity.Id]*IdentityExcerpt)
|
2019-02-19 01:16:47 +03:00
|
|
|
|
2020-09-16 17:22:02 +03:00
|
|
|
allIdentities := identity.ReadAllLocal(c.repo)
|
2019-02-19 01:16:47 +03:00
|
|
|
|
|
|
|
for i := range allIdentities {
|
|
|
|
if i.Err != nil {
|
|
|
|
return i.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.identitiesExcerpts[i.Identity.Id()] = NewIdentityExcerpt(i.Identity)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Done.")
|
|
|
|
|
2019-02-20 23:58:29 +03:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
|
2018-09-09 21:21:49 +03:00
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
c.bugExcerpts = make(map[entity.Id]*BugExcerpt)
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2022-08-13 13:08:48 +03:00
|
|
|
allBugs := bug.ReadAllWithResolver(c.repo, c.resolvers)
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2020-12-08 16:42:13 +03:00
|
|
|
// wipe the index just to be sure
|
|
|
|
err := c.repo.ClearBleveIndex("bug")
|
2020-10-01 15:16:31 +03:00
|
|
|
if err != nil {
|
2020-12-08 16:42:13 +03:00
|
|
|
return err
|
2020-10-01 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
2020-08-25 05:43:42 +03:00
|
|
|
for b := range allBugs {
|
2018-09-19 00:36:45 +03:00
|
|
|
if b.Err != nil {
|
|
|
|
return b.Err
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
snap := b.Bug.Compile()
|
2022-07-31 15:38:32 +03:00
|
|
|
c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, snap)
|
2020-10-01 15:16:31 +03:00
|
|
|
|
2022-07-31 15:38:32 +03:00
|
|
|
if err := c.addBugToSearchIndex(snap); err != nil {
|
2020-10-01 15:16:31 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
2018-09-02 17:36:48 +03:00
|
|
|
|
2019-02-20 23:58:29 +03:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Done.")
|
2020-10-01 15:16:31 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
// repoIsAvailable check is the given repository is locked by a Cache.
|
2022-07-31 15:38:32 +03:00
|
|
|
// Note: this is a smart function that will clean the lock file if the
|
2018-08-31 14:18:03 +03:00
|
|
|
// corresponding process is not there anymore.
|
|
|
|
// If no error is returned, the repo is free to edit.
|
2020-12-05 05:08:54 +03:00
|
|
|
func repoIsAvailable(repo repository.RepoStorage) error {
|
2018-08-31 14:18:03 +03:00
|
|
|
// Todo: this leave way for a racey access to the repo between the test
|
|
|
|
// if the file exist and the actual write. It's probably not a problem in
|
|
|
|
// practice because using a repository will be done from user interaction
|
|
|
|
// or in a context where a single instance of git-bug is already guaranteed
|
|
|
|
// (say, a server with the web UI running). But still, that might be nice to
|
|
|
|
// have a mutex or something to guard that.
|
|
|
|
|
|
|
|
// Todo: this will fail if somehow the filesystem is shared with another
|
|
|
|
// computer. Should add a configuration that prevent the cleaning of the
|
|
|
|
// lock file
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
f, err := repo.LocalStorage().Open(lockfile)
|
2018-08-31 14:18:03 +03:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// lock file already exist
|
|
|
|
buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(buf) == 10 {
|
2018-09-10 13:47:05 +03:00
|
|
|
return fmt.Errorf("the lock file should be < 10 bytes")
|
2018-08-31 14:18:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(string(buf))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
if process.IsRunning(pid) {
|
2018-09-10 13:47:05 +03:00
|
|
|
return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
|
2018-08-31 14:18:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The lock file is just laying there after a crash, clean it
|
|
|
|
|
|
|
|
fmt.Println("A lock file is present but the corresponding process is not, removing it.")
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
err = repo.LocalStorage().Remove(lockfile)
|
2018-08-31 14:18:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|