-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsession.go
180 lines (159 loc) · 4.35 KB
/
session.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package oci
import (
"encoding/json"
"reflect"
"github.com/mandelsoft/goutils/errors"
"ocm.software/ocm/api/datacontext"
)
type NamespaceContainer interface {
LookupNamespace(name string) (NamespaceAccess, error)
}
type ArtifactContainer interface {
GetArtifact(version string) (ArtifactAccess, error)
}
type EvaluationResult struct {
Ref RefSpec
Repository Repository
Namespace NamespaceAccess
Artifact ArtifactAccess
}
type Session interface {
datacontext.Session
LookupRepository(Context, RepositorySpec) (Repository, error)
LookupNamespace(NamespaceContainer, string) (NamespaceAccess, error)
GetArtifact(ArtifactContainer, string) (ArtifactAccess, error)
EvaluateRef(ctx Context, ref string) (*EvaluationResult, error)
DetermineRepository(ctx Context, ref string) (Repository, UniformRepositorySpec, error)
DetermineRepositoryBySpec(ctx Context, spec *UniformRepositorySpec) (Repository, error)
}
type session struct {
datacontext.Session
base datacontext.SessionBase
repositories map[datacontext.ObjectKey]Repository
namespaces map[datacontext.ObjectKey]NamespaceAccess
artifacts map[datacontext.ObjectKey]ArtifactAccess
}
var key = reflect.TypeOf(session{})
func NewSession(s datacontext.Session) Session {
return datacontext.GetOrCreateSubSession(s, key, newSession).(Session)
}
func newSession(s datacontext.SessionBase) datacontext.Session {
return &session{
Session: s.Session(),
base: s,
repositories: map[datacontext.ObjectKey]Repository{},
namespaces: map[datacontext.ObjectKey]NamespaceAccess{},
artifacts: map[datacontext.ObjectKey]ArtifactAccess{},
}
}
func (s *session) Close() error {
return s.Session.Close()
// TODO: cleanup cache
}
func (s *session) LookupRepository(ctx Context, spec RepositorySpec) (Repository, error) {
spec, err := ctx.RepositoryTypes().Convert(spec)
if err != nil {
return nil, err
}
data, err := json.Marshal(spec)
if err != nil {
return nil, err
}
key := datacontext.ObjectKey{
Object: ctx,
Name: string(data),
}
s.base.Lock()
defer s.base.Unlock()
if s.base.IsClosed() {
return nil, errors.ErrClosed("session")
}
if r := s.repositories[key]; r != nil {
return r, nil
}
repo, err := ctx.RepositoryForSpec(spec)
if err != nil {
return nil, err
}
s.repositories[key] = repo
s.base.AddCloser(repo)
return repo, err
}
func (s *session) LookupNamespace(c NamespaceContainer, name string) (NamespaceAccess, error) {
key := datacontext.ObjectKey{
Object: c,
Name: name,
}
s.base.Lock()
defer s.base.Unlock()
if s.base.IsClosed() {
return nil, errors.ErrClosed("session")
}
if ns := s.namespaces[key]; ns != nil {
return ns, nil
}
ns, err := c.LookupNamespace(name)
if err != nil {
return nil, err
}
s.namespaces[key] = ns
s.base.AddCloser(ns)
return ns, err
}
func (s *session) GetArtifact(c ArtifactContainer, version string) (ArtifactAccess, error) {
key := datacontext.ObjectKey{
Object: c,
Name: version,
}
s.base.Lock()
defer s.base.Unlock()
if s.base.IsClosed() {
return nil, errors.ErrClosed("session")
}
if obj := s.artifacts[key]; obj != nil {
return obj, nil
}
obj, err := c.GetArtifact(version)
if err != nil {
return nil, err
}
s.artifacts[key] = obj
s.base.AddCloser(obj)
return obj, err
}
func (s *session) EvaluateRef(ctx Context, ref string) (*EvaluationResult, error) {
var err error
result := &EvaluationResult{}
result.Ref, err = ParseRef(ref)
if err != nil {
return nil, err
}
result.Repository, err = s.DetermineRepositoryBySpec(ctx, &result.Ref.UniformRepositorySpec)
if err != nil {
return nil, err
}
if result.Ref.Repository == "" {
return result, nil
}
result.Namespace, err = s.LookupNamespace(result.Repository, result.Ref.Repository)
if !result.Ref.IsVersion() {
return result, err
}
result.Artifact, err = s.GetArtifact(result.Namespace, result.Ref.Version())
return result, err
}
func (s *session) DetermineRepository(ctx Context, ref string) (Repository, UniformRepositorySpec, error) {
spec, err := ParseRepo(ref)
if err != nil {
return nil, spec, err
}
r, err := s.DetermineRepositoryBySpec(ctx, &spec)
return r, spec, err
}
func (s *session) DetermineRepositoryBySpec(ctx Context, spec *UniformRepositorySpec) (Repository, error) {
rspec, err := ctx.MapUniformRepositorySpec(spec)
if err != nil {
return nil, err
}
return s.LookupRepository(ctx, rspec)
}