-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearcher.go
345 lines (290 loc) · 8.47 KB
/
searcher.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
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
340
341
342
343
344
345
package stuber
import (
"errors"
"iter"
"maps"
"sync"
"github.com/google/uuid"
)
// ErrServiceNotFound is returned when the service is not found.
var ErrServiceNotFound = errors.New("service not found")
// ErrMethodNotFound is returned when the method is not found.
var ErrMethodNotFound = errors.New("method not found")
// ErrStubNotFound is returned when the stub is not found.
var ErrStubNotFound = errors.New("stub not found")
// searcher is a struct that manages the storage of search results.
//
// It contains a mutex for concurrent access, a map to store and retrieve
// used stubs by their UUID, and a pointer to the storage struct.
type searcher struct {
mu sync.RWMutex // mutex for concurrent access
stubUsed map[uuid.UUID]struct{}
// map to store and retrieve used stubs by their UUID
storage *storage // pointer to the storage struct
}
// newSearcher creates a new instance of the searcher struct.
//
// It initializes the stubUsed map and the storage pointer.
//
// Returns a pointer to the newly created searcher struct.
func newSearcher() *searcher {
return &searcher{
storage: newStorage(),
stubUsed: make(map[uuid.UUID]struct{}),
}
}
// Result represents the result of a search operation.
//
// It contains two fields: found and similar. Found represents the exact
// match found in the search, while similar represents the most similar match
// found.
type Result struct {
found *Stub // The exact match found in the search
similar *Stub // The most similar match found
}
// Found returns the exact match found in the search.
//
// Returns a pointer to the Stub struct representing the found match.
func (r *Result) Found() *Stub {
return r.found
}
// Similar returns the most similar match found in the search.
//
// Returns a pointer to the Stub struct representing the similar match.
func (r *Result) Similar() *Stub {
return r.similar
}
// upsert inserts the given stub values into the searcher. If a stub value
// already exists with the same key, it is updated.
//
// The function returns a slice of UUIDs representing the keys of the
// inserted or updated values.
func (s *searcher) upsert(values ...*Stub) []uuid.UUID {
return s.storage.upsert(s.castToValue(values)...)
}
// del deletes the stub values with the given UUIDs from the searcher.
//
// Returns the number of stub values that were successfully deleted.
func (s *searcher) del(ids ...uuid.UUID) int {
return s.storage.del(ids...)
}
// findByID retrieves the stub value associated with the given ID from the
// searcher.
//
// Returns a pointer to the Stub struct associated with the given ID, or nil
// if not found.
func (s *searcher) findByID(id uuid.UUID) *Stub {
if v, ok := s.storage.findByID(id).(*Stub); ok {
return v
}
return nil
}
// findBy retrieves all Stub values that match the given service and method
// from the searcher.
//
// Parameters:
// - service: The service field used to search for Stub values.
// - method: The method field used to search for Stub values.
//
// Returns:
// - []*Stub: The Stub values that match the given service and method, or nil if not found.
// - error: An error if the search fails.
func (s *searcher) findBy(service, method string) ([]*Stub, error) {
seq, err := s.storage.findAll(service, method)
if err != nil {
return nil, s.wrap(err)
}
return collectStubs(seq), nil
}
// clear resets the searcher.
//
// It clears the stubUsed map and calls the storage clear method.
func (s *searcher) clear() {
s.mu.Lock()
defer s.mu.Unlock()
// Clear the stubUsed map.
s.stubUsed = make(map[uuid.UUID]struct{})
// Clear the storage.
s.storage.clear()
}
// all returns all Stub values stored in the searcher.
//
// Returns:
// - []*Stub: The Stub values stored in the searcher.
func (s *searcher) all() []*Stub {
return collectStubs(s.storage.values())
}
// used returns all Stub values that have been used by the searcher.
//
// Returns:
// - []*Stub: The Stub values that have been used by the searcher.
func (s *searcher) used() []*Stub {
s.mu.RLock()
defer s.mu.RUnlock()
return collectStubs(s.storage.findByIDs(maps.Keys(s.stubUsed)))
}
// unused returns all Stub values that have not been used by the searcher.
//
// Returns:
// - []*Stub: The Stub values that have not been used by the searcher.
func (s *searcher) unused() []*Stub {
s.mu.RLock()
defer s.mu.RUnlock()
unused := make([]*Stub, 0)
for stub := range s.iterAll() {
if _, exists := s.stubUsed[stub.ID]; !exists {
unused = append(unused, stub)
}
}
return unused
}
// find retrieves the Stub value associated with the given Query from the searcher.
//
// Parameters:
// - query: The Query used to search for a Stub value.
//
// Returns:
// - *Result: The Result containing the found Stub value (if any), or nil.
// - error: An error if the search fails.
func (s *searcher) find(query Query) (*Result, error) {
// Check if the Query has an ID field.
if query.ID != nil {
// Search for the Stub value with the given ID.
return s.searchByID(query)
}
// Search for the Stub value with the given service and method.
return s.search(query)
}
// searchByID retrieves the Stub value associated with the given ID from the searcher.
//
// Parameters:
// - query: The Query used to search for a Stub value.
//
// Returns:
// - *Result: The Result containing the found Stub value (if any), or nil.
// - error: An error if the search fails.
func (s *searcher) searchByID(query Query) (*Result, error) {
// Check if the given service and method are valid.
_, err := s.storage.posByPN(query.Service, query.Method)
if err != nil {
return nil, s.wrap(err)
}
// Search for the Stub value with the given ID.
if found := s.findByID(*query.ID); found != nil {
// Mark the Stub value as used.
s.mark(query, *query.ID)
// Return the found Stub value.
return &Result{found: found}, nil
}
// Return an error if the Stub value is not found.
return nil, ErrServiceNotFound
}
// search retrieves the Stub value associated with the given Query from the searcher.
//
// Parameters:
// - query: The Query used to search for a Stub value.
//
// Returns:
// - *Result: The Result containing the found Stub value (if any), or nil.
// - error: An error if the search fails.
func (s *searcher) search(query Query) (*Result, error) {
var (
found *Stub
foundRank float64
similar *Stub
similarRank float64
)
seq, err := s.storage.findAll(query.Service, query.Method)
if err != nil {
return nil, s.wrap(err)
}
for v := range seq {
stub, ok := v.(*Stub)
if !ok {
continue
}
current := rankMatch(query, stub)
if current > similarRank {
similar, similarRank = stub, current
}
if match(query, stub) && current > foundRank {
found, foundRank = stub, current
}
}
if found != nil {
s.mark(query, found.ID)
return &Result{found: found}, nil
}
if similar != nil {
return &Result{similar: similar}, nil
}
return nil, ErrStubNotFound
}
// mark marks the given Stub value as used in the searcher.
//
// If the query's RequestInternal flag is set, the mark is skipped.
//
// Parameters:
// - query: The query used to mark the Stub value.
// - id: The UUID of the Stub value to mark.
func (s *searcher) mark(query Query, id uuid.UUID) {
// If the query's RequestInternal flag is set, skip the mark.
if query.RequestInternal() {
return
}
// Lock the mutex to ensure concurrent access.
s.mu.Lock()
defer s.mu.Unlock()
// Mark the Stub value as used by adding it to the stubUsed map.
s.stubUsed[id] = struct{}{}
}
func collectStubs(seq iter.Seq[Value]) []*Stub {
result := make([]*Stub, 0)
for v := range seq {
if stub, ok := v.(*Stub); ok {
result = append(result, stub)
}
}
return result
}
func (s *searcher) iterAll() iter.Seq[*Stub] {
return func(yield func(*Stub) bool) {
for v := range s.storage.values() {
if stub, ok := v.(*Stub); ok {
if !yield(stub) {
return
}
}
}
}
}
// castToValue converts a slice of *Stub values to a slice of Value any.
//
// Parameters:
// - values: A slice of *Stub values to convert.
//
// Returns:
// - A slice of Value any containing the converted values.
func (s *searcher) castToValue(values []*Stub) []Value {
result := make([]Value, len(values))
for i, v := range values {
result[i] = v
}
return result
}
// wrap wraps an error with specific error types.
//
// Parameters:
// - err: The error to wrap.
//
// Returns:
// - The wrapped error.
func (s *searcher) wrap(err error) error {
if errors.Is(err, ErrLeftNotFound) {
return ErrServiceNotFound
}
if errors.Is(err, ErrRightNotFound) {
return ErrMethodNotFound
}
return err
}