aboutsummaryrefslogtreecommitdiffstats
path: root/cache/resolvers.go
blob: e53c3660a0b3b87ac2088ae0c4ebe858471063e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cache

import (
	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/identity"
)

var _ identity.Resolver = &identityCacheResolver{}

// identityCacheResolver is an identity Resolver that retrieve identities from
// the cache
type identityCacheResolver struct {
	cache *RepoCache
}

func newIdentityCacheResolver(cache *RepoCache) *identityCacheResolver {
	return &identityCacheResolver{cache: cache}
}

func (i *identityCacheResolver) ResolveIdentity(id entity.Id) (identity.Interface, error) {
	return i.cache.ResolveIdentity(id)
}

var _ identity.Resolver = &identityCacheResolverNoLock{}

// identityCacheResolverNoLock is an identity Resolver that retrieve identities from
// the cache, without locking it.
type identityCacheResolverNoLock struct {
	cache *RepoCache
}

func newIdentityCacheResolverNoLock(cache *RepoCache) *identityCacheResolverNoLock {
	return &identityCacheResolverNoLock{cache: cache}
}

func (ir *identityCacheResolverNoLock) ResolveIdentity(id entity.Id) (identity.Interface, error) {
	cached, ok := ir.cache.identities[id]
	if ok {
		return cached, nil
	}

	i, err := identity.ReadLocal(ir.cache.repo, id)
	if err != nil {
		return nil, err
	}

	cached = NewIdentityCache(ir.cache, i)
	ir.cache.identities[id] = cached

	return cached, nil
}