This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
multi.go
82 lines (75 loc) · 2.07 KB
/
multi.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
package appdash
// multiStore is like a normal store except all operations occur on the multiple
// underlying stores.
type multiStore struct {
// stores is the underlying set of stores that operations take place on.
stores []Store
}
// Collect implements the Collector interface by invoking Collect on each
// underlying store, returning the first error that occurs.
func (ms *multiStore) Collect(id SpanID, anns ...Annotation) error {
for _, s := range ms.stores {
if err := s.Collect(id, anns...); err != nil {
return err
}
}
return nil
}
// Trace implements the Store interface by returning the first trace found by
// asking each underlying store for it in consecutive order.
func (ms *multiStore) Trace(t ID) (*Trace, error) {
for _, s := range ms.stores {
trace, err := s.Trace(t)
if err == ErrTraceNotFound {
continue
} else if err != nil {
return nil, err
}
return trace, nil
}
return nil, ErrTraceNotFound
}
// MultiStore returns a Store whose operations occur on the multiple given
// stores.
func MultiStore(s ...Store) Store {
return &multiStore{
stores: s,
}
}
// multiStore is like a normal queryer except it queries from multiple
// underlying stores.
type multiQueryer struct {
// queryers is the underlying set of queryers that operations take place on.
queryers []Queryer
}
// Traces implements the Queryer interface by returning the union of all
// underlying stores.
//
// It panics if any underlying store does not implement the appdash Queryer
// interface.
func (mq *multiQueryer) Traces(opts TracesOpts) ([]*Trace, error) {
var (
union = make(map[ID]struct{})
all []*Trace
)
for _, q := range mq.queryers {
traces, err := q.Traces(TracesOpts{})
if err != nil {
return nil, err
}
for _, t := range traces {
if _, ok := union[t.ID.Trace]; !ok {
union[t.ID.Trace] = struct{}{}
all = append(all, t)
}
}
}
return all, nil
}
// MultiQueryer returns a Queryer whose Traces method returns a union of all
// traces across each queryer.
func MultiQueryer(q ...Queryer) Queryer {
return &multiQueryer{
queryers: q,
}
}