/
search.go
287 lines (272 loc) · 7.74 KB
/
search.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storetesting
import (
"sort"
"github.com/juju/charm/v8"
"github.com/juju/charmrepo/v6/csclient/params"
"gopkg.in/juju/charmstore.v5/internal/mongodoc"
"gopkg.in/juju/charmstore.v5/internal/router"
"gopkg.in/juju/charmstore.v5/internal/series"
)
// SearchSeries contains the list of charm series that will be indexed in
// the search in descending order of preference.
var SearchSeries = func() []string {
var ss []string
for k, v := range series.Series {
if v.Distribution != series.Ubuntu || !v.SearchIndex {
continue
}
ss = append(ss, k)
}
sort.Slice(ss, func(i, j int) bool {
return series.Series[ss[i]].SearchBoost > series.Series[ss[j]].SearchBoost
})
return ss
}()
// A SearchEntity is an entity that is used in search tests.
type SearchEntity struct {
URL *charm.URL
PromulgatedRevision int
SupportedSeries []string
Charm *Charm
Bundle *Bundle
ACL []string
Downloads int
}
func (e SearchEntity) Entity() *mongodoc.Entity {
var purl *charm.URL
if e.PromulgatedRevision > -1 {
purl = new(charm.URL)
*purl = *e.URL
purl.User = ""
purl.Revision = e.PromulgatedRevision
}
supportedSeries := e.SupportedSeries
if e.URL.Series == "bundle" {
supportedSeries = nil
} else if e.URL.Series != "" {
supportedSeries = []string{e.URL.Series}
}
return &mongodoc.Entity{
URL: e.URL,
PromulgatedURL: purl,
PromulgatedRevision: e.PromulgatedRevision,
SupportedSeries: supportedSeries,
}
}
func (e SearchEntity) ResolvedURL() *router.ResolvedURL {
return &router.ResolvedURL{
URL: *e.URL,
PromulgatedRevision: e.PromulgatedRevision,
}
}
var SearchEntities = map[string]SearchEntity{
"wordpress": {
URL: charm.MustParseURL("cs:~charmers/" + SearchSeries[0] + "/wordpress-23"),
PromulgatedRevision: 23,
Charm: NewCharm(&charm.Meta{
Description: "blog",
Provides: map[string]charm.Relation{
"url": {
Name: "url",
Interface: "http",
Scope: charm.ScopeGlobal,
},
"monitoring-port": {
Name: "monitoring-port",
Interface: "monitoring",
Scope: charm.ScopeContainer,
},
},
Requires: map[string]charm.Relation{
"mysql": {
Name: "mysql",
Interface: "mysql",
Scope: charm.ScopeGlobal,
},
"cache": {
Name: "cache",
Interface: "varnish",
Scope: charm.ScopeGlobal,
},
},
Categories: []string{"wordpress", "wordpressCAT"},
Tags: []string{"wordpressTAG"},
}),
ACL: []string{params.Everyone},
},
"mysql": {
URL: charm.MustParseURL("cs:~openstack-charmers/" + SearchSeries[2] + "/mysql-7"),
PromulgatedRevision: 7,
Charm: NewCharm(&charm.Meta{
Summary: "Database Engine",
Description: "database",
Provides: map[string]charm.Relation{
"mysql": {
Name: "mysql",
Interface: "mysql",
Scope: charm.ScopeGlobal,
},
},
Categories: []string{"mysql"},
Tags: []string{"mysqlTAG", "bar"},
}),
ACL: []string{params.Everyone},
Downloads: 3,
},
"varnish": {
URL: charm.MustParseURL("cs:~foo/" + SearchSeries[2] + "/varnish-1"),
PromulgatedRevision: -1,
Charm: NewCharm(&charm.Meta{
Summary: "Database Engine",
Description: "database",
Categories: []string{"varnish"},
Tags: []string{"varnishTAG"},
}),
ACL: []string{params.Everyone},
Downloads: 5,
},
"riak": {
URL: charm.MustParseURL("cs:~charmers/" + SearchSeries[2] + "/riak-67"),
PromulgatedRevision: 67,
Charm: NewCharm(&charm.Meta{
Categories: []string{"riak"},
Tags: []string{"riakTAG"},
}),
ACL: []string{"charmers"},
},
"wordpress-simple": {
URL: charm.MustParseURL("cs:~charmers/bundle/wordpress-simple-4"),
PromulgatedRevision: 4,
Bundle: NewBundle(&charm.BundleData{
Applications: map[string]*charm.ApplicationSpec{
"wordpress": {
Charm: "wordpress",
NumUnits: 1,
},
},
Tags: []string{"wordpress"},
}),
ACL: []string{params.Everyone},
Downloads: 1,
},
// Note: "squid-forwardproxy" shares a trigram "dpr" with "wordpress".
"squid-forwardproxy": {
URL: charm.MustParseURL("cs:~charmers/" + SearchSeries[2] + "/squid-forwardproxy-3"),
PromulgatedRevision: 3,
Charm: NewCharm(&charm.Meta{}),
ACL: []string{params.Everyone},
Downloads: 2,
},
// Note: "cloud-controller-worker-v2" shares a trigram "wor" with "wordpress".
"cloud-controller-worker-v2": {
URL: charm.MustParseURL("cs:~cf-charmers/" + SearchSeries[1] + "/cloud-controller-worker-v2-7"),
PromulgatedRevision: -1,
Charm: NewCharm(&charm.Meta{}),
ACL: []string{params.Everyone},
Downloads: 4,
},
"multi-series": {
URL: charm.MustParseURL("cs:~charmers/multi-series-0"),
PromulgatedRevision: 0,
Charm: NewCharm(&charm.Meta{
Series: SearchSeries,
Provides: map[string]charm.Relation{
"url": {
Name: "url",
Interface: "http",
Scope: charm.ScopeGlobal,
},
"monitoring-port": {
Name: "monitoring-port",
Interface: "monitoring",
Scope: charm.ScopeContainer,
},
},
Requires: map[string]charm.Relation{
"mysql": {
Name: "mysql",
Interface: "mysql",
Scope: charm.ScopeGlobal,
},
"cache": {
Name: "cache",
Interface: "varnish",
Scope: charm.ScopeGlobal,
},
},
Categories: []string{"multi-series", "multi-seriesCAT"},
Tags: []string{"multi-seriesTAG"},
}),
ACL: []string{params.Everyone},
},
}
// SortBySeries sorts the given slice of SearchEntities by series name
// and returns the slice.
func SortBySeries(es []SearchEntity, desc bool) []SearchEntity {
f := func(i, j int) bool { return lowestSeries(es[i]) < lowestSeries(es[j]) }
if desc {
f = func(i, j int) bool { return highestSeries(es[j]) < highestSeries(es[i]) }
}
sort.SliceStable(es, f)
return es
}
func lowestSeries(e SearchEntity) string {
if e.URL.Series != "" {
return e.URL.Series
}
s := e.Charm.Meta().Series[0]
for _, t := range e.Charm.Meta().Series[1:] {
if t < s {
s = t
}
}
return s
}
func highestSeries(e SearchEntity) string {
if e.URL.Series != "" {
return e.URL.Series
}
s := e.Charm.Meta().Series[0]
for _, t := range e.Charm.Meta().Series[1:] {
if t > s {
s = t
}
}
return s
}
// ResolvedURLs creates a list of ResolvedURLs for the given
// SearchEntities. If expand is true then all the URLs will be expanded
// to include all possible series. This if for v4 compatibility.
func ResolvedURLs(es []SearchEntity, expand bool) []*router.ResolvedURL {
urls := make([]*router.ResolvedURL, 0, len(es))
for _, e := range es {
rurl := e.ResolvedURL()
if !expand || rurl.URL.Series != "" {
urls = append(urls, rurl)
continue
}
for _, s := range e.Charm.Meta().Series {
urls = append(urls, ResolvedURLWithSeries(rurl, s))
}
}
return urls
}
// ResolvedURLWithSeries creates a copy of the given ResolvedURL with the
// given series.
func ResolvedURLWithSeries(rurl *router.ResolvedURL, series string) *router.ResolvedURL {
rurl1 := *rurl
rurl1.URL.Series = series
return &rurl1
}
// SortResolvedURLsBySeries sorts the given slice of ResolvedURLs in
// series order.
func SortResolvedURLsBySeries(urls []*router.ResolvedURL, desc bool) []*router.ResolvedURL {
f := func(i, j int) bool { return urls[i].URL.Series < urls[j].URL.Series }
if desc {
f = func(i, j int) bool { return urls[j].URL.Series < urls[i].URL.Series }
}
sort.SliceStable(urls, f)
return urls
}