/
kv_service.go
215 lines (202 loc) · 5.96 KB
/
kv_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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 kv
import (
"context"
"errors"
"time"
"github.com/apache/servicecomb-kie/pkg/model"
"github.com/apache/servicecomb-kie/pkg/util"
"github.com/apache/servicecomb-kie/server/service"
"github.com/apache/servicecomb-kie/server/service/mongo/label"
"github.com/apache/servicecomb-kie/server/service/mongo/session"
"github.com/go-mesh/openlogging"
)
//const of kv service
const (
MsgFindKvFailed = "find kv failed, deadline exceeded"
MsgFindOneKey = "find one key"
MsgFindOneKeyByID = "find one key by id"
MsgFindMoreKey = "find more"
MsgHitExactLabels = "hit exact labels"
FmtErrFindKvFailed = "can not find kv in %s"
)
//Service operate data in mongodb
type Service struct {
timeout time.Duration
}
//CreateOrUpdate will create or update a key value record
//it first check label exists or not, and create labels if labels is first posted.
//if label exists, then get its latest revision, and update current revision,
//save the current label and its all key values to history collection
//then check key exists or not, then create or update it
func (s *Service) CreateOrUpdate(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
ctx, _ = context.WithTimeout(ctx, session.Timeout)
if kv.Domain == "" {
return nil, session.ErrMissingDomain
}
//check whether the project has certain labels or not
labelID, err := label.Exist(ctx, kv.Domain, kv.Project, kv.Labels)
if err != nil {
if err == session.ErrLabelNotExists {
l := &model.LabelDoc{
Domain: kv.Domain,
Labels: kv.Labels,
Project: kv.Project,
}
l, err = label.CreateLabel(ctx, l)
if err != nil {
openlogging.Error("create label failed", openlogging.WithTags(openlogging.Tags{
"k": kv.Key,
"domain": kv.Domain,
}))
return nil, err
}
labelID = l.ID
} else {
return nil, err
}
}
kv.LabelID = labelID
if kv.ValueType == "" {
kv.ValueType = session.DefaultValueType
}
oldKV, err := s.Exist(ctx, kv.Domain, kv.Key, kv.Project, service.WithLabelID(kv.LabelID))
if err != nil {
if err != service.ErrKeyNotExists {
openlogging.Error(err.Error())
return nil, err
}
kv, err := createKey(ctx, kv)
if err != nil {
openlogging.Error(err.Error())
return nil, err
}
kv.Domain = ""
kv.Project = ""
return kv, nil
}
kv.ID = oldKV.ID
kv.CreateRevision = oldKV.CreateRevision
err = updateKeyValue(ctx, kv)
if err != nil {
return nil, err
}
kv.Domain = ""
kv.Project = ""
return kv, nil
}
//Exist supports you query a key value by label map or labels id
func (s *Service) Exist(ctx context.Context, domain, key string, project string, options ...service.FindOption) (*model.KVDoc, error) {
ctx, _ = context.WithTimeout(context.Background(), session.Timeout)
opts := service.FindOptions{}
for _, o := range options {
o(&opts)
}
if opts.LabelID != "" {
kvs, err := findKVByLabelID(ctx, domain, opts.LabelID, key, project)
if err != nil {
openlogging.Error(err.Error())
return nil, err
}
return kvs[0], nil
}
kvs, err := s.List(ctx, domain, project,
service.WithExactLabels(),
service.WithLabels(opts.Labels),
service.WithKey(key))
if err != nil {
openlogging.Error("check kv exist: " + err.Error())
return nil, err
}
if len(kvs.Data) != 1 {
return nil, session.ErrTooMany
}
return kvs.Data[0], nil
}
//Delete delete kv,If the labelID is "", query the collection kv to get it
//domain=tenant
func (s *Service) Delete(ctx context.Context, kvID string, domain string, project string) error {
ctx, _ = context.WithTimeout(context.Background(), session.Timeout)
if domain == "" {
return session.ErrMissingDomain
}
if project == "" {
return session.ErrMissingProject
}
if kvID == "" {
return errors.New("key id is empty")
}
//delete kv
err := deleteKV(ctx, kvID, project, domain)
if err != nil {
openlogging.Error("can not delete key, err:" + err.Error())
return errors.New("can not delete key")
}
return nil
}
//List get kv list by key and criteria
func (s *Service) List(ctx context.Context, domain, project string, options ...service.FindOption) (*model.KVResponse, error) {
opts := service.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
cur, total, err := findKV(ctx, domain, project, opts)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
result := &model.KVResponse{
Data: []*model.KVDoc{},
}
for cur.Next(ctx) {
curKV := &model.KVDoc{}
if err := cur.Decode(curKV); err != nil {
openlogging.Error("decode to KVs error: " + err.Error())
return nil, err
}
if opts.ExactLabels {
if !util.IsEquivalentLabel(opts.Labels, curKV.Labels) {
continue
}
}
clearPart(curKV)
result.Data = append(result.Data, curKV)
}
result.Total = total
return result, nil
}
//Get get kvs by id
func (s *Service) Get(ctx context.Context, domain, project, id string, options ...service.FindOption) (*model.KVResponse, error) {
opts := service.FindOptions{}
for _, o := range options {
o(&opts)
}
if opts.Timeout == 0 {
opts.Timeout = session.DefaultTimeout
}
if domain == "" {
return nil, session.ErrMissingDomain
}
if project == "" {
return nil, session.ErrMissingProject
}
if id == "" {
return nil, session.ErrIDIsNil
}
return findKVByID(ctx, domain, project, id)
}