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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package identity
import (
"fmt"
"sync"
"github.com/gohugoio/hugo/compare"
)
// NewFinder creates a new Finder.
// This is a thread safe implementation with a cache.
func NewFinder(cfg FinderConfig) *Finder {
return &Finder{cfg: cfg, answers: make(map[ManagerIdentity]FinderResult), seenFindOnce: make(map[Identity]bool)}
}
var searchIDPool = sync.Pool{
New: func() interface{} {
return &searchID{seen: make(map[Manager]bool)}
},
}
func getSearchID() *searchID {
return searchIDPool.Get().(*searchID)
}
func putSearchID(sid *searchID) {
sid.id = nil
sid.isDp = false
sid.isPeq = false
sid.hasEqer = false
sid.maxDepth = 0
sid.dp = nil
sid.peq = nil
sid.eqer = nil
for k := range sid.seen {
delete(sid.seen, k)
}
searchIDPool.Put(sid)
}
// GetSearchID returns a searchID from the pool.
// Finder finds identities inside another.
type Finder struct {
cfg FinderConfig
answers map[ManagerIdentity]FinderResult
muAnswers sync.RWMutex
seenFindOnce map[Identity]bool
muSeenFindOnce sync.RWMutex
}
type FinderResult int
const (
FinderNotFound FinderResult = iota
FinderFoundOneOfManyRepetition
FinderFoundOneOfMany
FinderFound
)
// Contains returns whether in contains id.
func (f *Finder) Contains(id, in Identity, maxDepth int) FinderResult {
if id == Anonymous || in == Anonymous {
return FinderNotFound
}
if id == GenghisKhan && in == GenghisKhan {
return FinderNotFound
}
if id == GenghisKhan {
return FinderFound
}
if id == in {
return FinderFound
}
if id == nil || in == nil {
return FinderNotFound
}
var (
isDp bool
isPeq bool
dp IsProbablyDependentProvider
peq compare.ProbablyEqer
)
if !f.cfg.Exact {
dp, isDp = id.(IsProbablyDependentProvider)
peq, isPeq = id.(compare.ProbablyEqer)
}
eqer, hasEqer := id.(compare.Eqer)
sid := getSearchID()
sid.id = id
sid.isDp = isDp
sid.isPeq = isPeq
sid.hasEqer = hasEqer
sid.dp = dp
sid.peq = peq
sid.eqer = eqer
sid.maxDepth = maxDepth
defer putSearchID(sid)
r := FinderNotFound
if i := f.checkOne(sid, in, 0); i > r {
r = i
}
if r == FinderFound {
return r
}
m := GetDependencyManager(in)
if m != nil {
if i := f.checkManager(sid, m, 0); i > r {
r = i
}
}
return r
}
func (f *Finder) checkMaxDepth(sid *searchID, level int) FinderResult {
if sid.maxDepth >= 0 && level > sid.maxDepth {
return FinderNotFound
}
if level > 100 {
// This should never happen, but some false positives are probably better than a panic.
if !f.cfg.Exact {
return FinderFound
}
panic("too many levels")
}
return -1
}
func (f *Finder) checkManager(sid *searchID, m Manager, level int) FinderResult {
if r := f.checkMaxDepth(sid, level); r >= 0 {
return r
}
if m == nil {
return FinderNotFound
}
if sid.seen[m] {
return FinderNotFound
}
sid.seen[m] = true
f.muAnswers.RLock()
r, ok := f.answers[ManagerIdentity{Manager: m, Identity: sid.id}]
f.muAnswers.RUnlock()
if ok {
return r
}
r = f.search(sid, m, level)
if r == FinderFoundOneOfMany {
// Don't cache this one.
return r
}
f.muAnswers.Lock()
f.answers[ManagerIdentity{Manager: m, Identity: sid.id}] = r
f.muAnswers.Unlock()
return r
}
func (f *Finder) checkOne(sid *searchID, v Identity, depth int) (r FinderResult) {
if ff, ok := v.(FindFirstManagerIdentityProvider); ok {
f.muSeenFindOnce.RLock()
mi := ff.FindFirstManagerIdentity()
seen := f.seenFindOnce[mi.Identity]
f.muSeenFindOnce.RUnlock()
if seen {
return FinderFoundOneOfManyRepetition
}
r = f.doCheckOne(sid, mi.Identity, depth)
if r == 0 {
r = f.checkManager(sid, mi.Manager, depth)
}
if r > FinderFoundOneOfManyRepetition {
f.muSeenFindOnce.Lock()
// Double check.
if f.seenFindOnce[mi.Identity] {
f.muSeenFindOnce.Unlock()
return FinderFoundOneOfManyRepetition
}
f.seenFindOnce[mi.Identity] = true
f.muSeenFindOnce.Unlock()
r = FinderFoundOneOfMany
}
return r
} else {
return f.doCheckOne(sid, v, depth)
}
}
func (f *Finder) doCheckOne(sid *searchID, v Identity, depth int) FinderResult {
id2 := Unwrap(v)
if id2 == Anonymous {
return FinderNotFound
}
id := sid.id
if sid.hasEqer {
if sid.eqer.Eq(id2) {
return FinderFound
}
} else if id == id2 {
return FinderFound
}
if f.cfg.Exact {
return FinderNotFound
}
if id2 == nil {
return FinderNotFound
}
if id2 == GenghisKhan {
return FinderFound
}
if id.IdentifierBase() == id2.IdentifierBase() {
return FinderFound
}
if sid.isDp && sid.dp.IsProbablyDependent(id2) {
return FinderFound
}
if sid.isPeq && sid.peq.ProbablyEq(id2) {
return FinderFound
}
if pdep, ok := id2.(IsProbablyDependencyProvider); ok && pdep.IsProbablyDependency(id) {
return FinderFound
}
if peq, ok := id2.(compare.ProbablyEqer); ok && peq.ProbablyEq(id) {
return FinderFound
}
return FinderNotFound
}
// search searches for id in ids.
func (f *Finder) search(sid *searchID, m Manager, depth int) FinderResult {
id := sid.id
if id == Anonymous {
return FinderNotFound
}
if !f.cfg.Exact && id == GenghisKhan {
return FinderNotFound
}
var r FinderResult
m.forEeachIdentity(
func(v Identity) bool {
i := f.checkOne(sid, v, depth)
if i > r {
r = i
}
if r == FinderFound {
return true
}
m := GetDependencyManager(v)
if i := f.checkManager(sid, m, depth+1); i > r {
r = i
}
if r == FinderFound {
return true
}
return false
},
)
return r
}
// FinderConfig provides configuration for the Finder.
// Note that we by default will use a strategy where probable matches are
// good enough. The primary use case for this is to identity the change set
// for a given changed identity (e.g. a template), and we don't want to
// have any false negatives there, but some false positives are OK. Also, speed is important.
type FinderConfig struct {
// Match exact matches only.
Exact bool
}
// ManagerIdentity wraps a pair of Identity and Manager.
type ManagerIdentity struct {
Identity
Manager
}
func (p ManagerIdentity) String() string {
return fmt.Sprintf("%s:%s", p.Identity.IdentifierBase(), p.Manager.IdentifierBase())
}
type searchID struct {
id Identity
isDp bool
isPeq bool
hasEqer bool
maxDepth int
seen map[Manager]bool
dp IsProbablyDependentProvider
peq compare.ProbablyEqer
eqer compare.Eqer
}
|