forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbrp_mapping_service.go
158 lines (131 loc) · 4.31 KB
/
dbrp_mapping_service.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
package inmem
import (
"context"
"fmt"
"path"
"github.com/influxdata/influxdb/v2"
)
var (
errDBRPMappingNotFound = &influxdb.Error{
Code: influxdb.ENotFound,
Msg: "dbrp mapping not found",
}
)
func encodeDBRPMappingKey(cluster, db, rp string) string {
return path.Join(cluster, db, rp)
}
func (s *Service) loadDBRPMapping(ctx context.Context, cluster, db, rp string) (*influxdb.DBRPMapping, error) {
i, ok := s.dbrpMappingKV.Load(encodeDBRPMappingKey(cluster, db, rp))
if !ok {
return nil, errDBRPMappingNotFound
}
m, ok := i.(influxdb.DBRPMapping)
if !ok {
return nil, fmt.Errorf("type %T is not a dbrp mapping", i)
}
return &m, nil
}
// FindBy returns a single dbrp mapping by cluster, db and rp.
func (s *Service) FindBy(ctx context.Context, cluster, db, rp string) (*influxdb.DBRPMapping, error) {
return s.loadDBRPMapping(ctx, cluster, db, rp)
}
func (s *Service) forEachDBRPMapping(ctx context.Context, fn func(m *influxdb.DBRPMapping) bool) error {
var err error
s.dbrpMappingKV.Range(func(k, v interface{}) bool {
m, ok := v.(influxdb.DBRPMapping)
if !ok {
err = fmt.Errorf("type %T is not a dbrp mapping", v)
return false
}
return fn(&m)
})
return err
}
func (s *Service) filterDBRPMappings(ctx context.Context, fn func(m *influxdb.DBRPMapping) bool) ([]*influxdb.DBRPMapping, error) {
mappings := []*influxdb.DBRPMapping{}
err := s.forEachDBRPMapping(ctx, func(m *influxdb.DBRPMapping) bool {
if fn(m) {
mappings = append(mappings, m)
}
return true
})
if err != nil {
return nil, err
}
return mappings, nil
}
// Find returns the first dbrp mapping that matches filter.
func (s *Service) Find(ctx context.Context, filter influxdb.DBRPMappingFilter) (*influxdb.DBRPMapping, error) {
if filter.Cluster == nil && filter.Database == nil && filter.RetentionPolicy == nil {
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "no filter parameters provided",
}
}
// filter by dbrpMapping id
if filter.Cluster != nil && filter.Database != nil && filter.RetentionPolicy != nil {
return s.FindBy(ctx, *filter.Cluster, *filter.Database, *filter.RetentionPolicy)
}
mappings, n, err := s.FindMany(ctx, filter)
if err != nil {
return nil, err
}
if n < 1 {
return nil, errDBRPMappingNotFound
}
return mappings[0], nil
}
// FindMany returns a list of dbrpMappings that match filter and the total count of matching dbrp mappings.
// Additional options provide pagination & sorting.
func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) {
// filter by dbrpMapping id
if filter.Cluster != nil && filter.Database != nil && filter.RetentionPolicy != nil {
m, err := s.FindBy(ctx, *filter.Cluster, *filter.Database, *filter.RetentionPolicy)
if err != nil {
return nil, 0, err
}
return []*influxdb.DBRPMapping{m}, 1, nil
}
filterFunc := func(mapping *influxdb.DBRPMapping) bool {
return (filter.Cluster == nil || (*filter.Cluster) == mapping.Cluster) &&
(filter.Database == nil || (*filter.Database) == mapping.Database) &&
(filter.RetentionPolicy == nil || (*filter.RetentionPolicy) == mapping.RetentionPolicy) &&
(filter.Default == nil || (*filter.Default) == mapping.Default)
}
mappings, err := s.filterDBRPMappings(ctx, filterFunc)
if err != nil {
return nil, 0, err
}
return mappings, len(mappings), nil
}
// Create creates a new dbrp mapping.
func (s *Service) Create(ctx context.Context, m *influxdb.DBRPMapping) error {
if err := m.Validate(); err != nil {
return nil
}
existing, err := s.loadDBRPMapping(ctx, m.Cluster, m.Database, m.RetentionPolicy)
if err != nil {
if err == errDBRPMappingNotFound {
return s.PutDBRPMapping(ctx, m)
}
return err
}
if !existing.Equal(m) {
return &influxdb.Error{
Code: influxdb.EConflict,
Msg: "dbrp mapping already exists",
}
}
return s.PutDBRPMapping(ctx, m)
}
// PutDBRPMapping sets dbrpMapping with the current ID.
func (s *Service) PutDBRPMapping(ctx context.Context, m *influxdb.DBRPMapping) error {
k := encodeDBRPMappingKey(m.Cluster, m.Database, m.RetentionPolicy)
s.dbrpMappingKV.Store(k, *m)
return nil
}
// Delete removes a dbrp mapping
func (s *Service) Delete(ctx context.Context, cluster, db, rp string) error {
s.dbrpMappingKV.Delete(encodeDBRPMappingKey(cluster, db, rp))
return nil
}