This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git
The following commit(s) were added to refs/heads/master by this push:
new e083e75 #68 使用format字段,对唯一性进行校验 (#77)
e083e75 is described below
commit e083e7533b01d73bbc57d284da8d2b0fedb9d18e
Author: Shawn <[email protected]>
AuthorDate: Mon Jan 20 19:10:24 2020 +0800
#68 使用format字段,对唯一性进行校验 (#77)
* #68 使用format字段,对唯一性进行校验
* #68 使用format字段,对唯一性进行校验
* #68 使用format字段,对唯一性进行校验
* #68 使用format字段,对唯一性进行校验
* #68 使用format字段,对唯一性进行校验
* #68 使用format字段,对唯一性进行校验
---
{examples/dev => deployments}/db.js | 26 ++++++++++++++
deployments/docker/db.js | 51 ---------------------------
deployments/docker/docker-compose.yaml | 2 +-
examples/dev/docker-compose.yaml | 2 +-
pkg/model/db_schema.go | 1 +
pkg/stringutil/string_util.go | 45 ++++++++++++++++++++++++
pkg/stringutil/string_util_test.go | 39 +++++++++++++++++++++
server/pubsub/bus.go | 3 +-
server/pubsub/struct.go | 21 -----------
server/service/mongo/kv/kv_test.go | 1 +
server/service/mongo/label/label_dao.go | 11 ++----
server/service/mongo/label/label_dao_test.go | 52 ++++++++++++++++++++++++++++
12 files changed, 171 insertions(+), 83 deletions(-)
diff --git a/examples/dev/db.js b/deployments/db.js
similarity index 70%
rename from examples/dev/db.js
rename to deployments/db.js
index 918ee58..08cd781 100644
--- a/examples/dev/db.js
+++ b/deployments/db.js
@@ -46,6 +46,32 @@ db.createCollection( "kv", {
}
} }
} );
+
+db.createCollection( "label", {
+ validator: { $jsonSchema: {
+ bsonType: "object",
+ required: [ "id","domain","project","format" ],
+ properties: {
+ label_id: {
+ bsonType: "string",
+ },
+ domain: {
+ bsonType: "string"
+ },
+ project: {
+ bsonType: "string"
+ },
+ alias: {
+ bsonType: "string"
+ }
+ }
+ } }
+} );
+
+//index
db.kv.createIndex({"id": 1}, { unique: true } );
db.kv.createIndex({key: 1, label_id: 1,domain:1,project:1},{ unique: true });
+db.label.createIndex({"id": 1}, { unique: true } );
+db.label.createIndex({format: 1,domain:1,project:1},{ unique: true });
+//db config
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1} );
\ No newline at end of file
diff --git a/deployments/docker/db.js b/deployments/docker/db.js
deleted file mode 100644
index b051bfa..0000000
--- a/deployments/docker/db.js
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.
- */
-
-db.createUser(
- {
- user: "kie",
- pwd: "123",
- roles:[
- {
- role: "readWrite",
- db: "kie"
- }
- ]
- }
-);
-db.createCollection("counter");
-db.counter.insertOne( { name:"revision_counter",count: 1 } );
-db.createCollection( "kv", {
- validator: { $jsonSchema: {
- bsonType: "object",
- required: [
"key","domain","project","id","value","create_revision","update_revision","value_type","label_id"
],
- properties: {
- key: {
- bsonType: "string",
- description: "must be a string and is required"
- },
- value_type: {
- enum: [ "text", "yaml", "json", "properties", "ini" ]
- },
- labels: {
- bsonType: "object"
- }
- }
- } }
-} );
-db.kv.createIndex({"id": 1}, { unique: true } );
-db.kv.createIndex({key: 1, label_id: 1},{ unique: true });
\ No newline at end of file
diff --git a/deployments/docker/docker-compose.yaml
b/deployments/docker/docker-compose.yaml
index 2832c28..35cf47d 100644
--- a/deployments/docker/docker-compose.yaml
+++ b/deployments/docker/docker-compose.yaml
@@ -26,7 +26,7 @@ services:
MONGO_INITDB_ROOT_USERNAME: kie
MONGO_INITDB_ROOT_PASSWORD: 123
volumes:
- - ./db.js:/docker-entrypoint-initdb.d/db.js:ro
+ - ../db.js:/docker-entrypoint-initdb.d/db.js:ro
mongo-express:
image: mongo-express
restart: always
diff --git a/examples/dev/docker-compose.yaml b/examples/dev/docker-compose.yaml
index 9c5a1e4..8ca81b4 100644
--- a/examples/dev/docker-compose.yaml
+++ b/examples/dev/docker-compose.yaml
@@ -26,7 +26,7 @@ services:
MONGO_INITDB_ROOT_PASSWORD: 123
MONGO_INITDB_DATABASE: kie
volumes:
- - ./db.js:/docker-entrypoint-initdb.d/db.js:ro
+ - ../../deployments/db.js:/docker-entrypoint-initdb.d/db.js:ro
mongo-express:
image: mongo-express
restart: always
diff --git a/pkg/model/db_schema.go b/pkg/model/db_schema.go
index dae9461..4feaf42 100644
--- a/pkg/model/db_schema.go
+++ b/pkg/model/db_schema.go
@@ -21,6 +21,7 @@ package model
type LabelDoc struct {
ID string `json:"id,omitempty" bson:"id,omitempty"
yaml:"id,omitempty" swag:"string"`
Labels map[string]string `json:"labels,omitempty"
yaml:"labels,omitempty"`
+ Format string `bson:"format,omitempty"`
Domain string `json:"domain,omitempty"
yaml:"domain,omitempty"` //tenant info
Project string `json:"project,omitempty"
yaml:"project,omitempty"`
}
diff --git a/pkg/stringutil/string_util.go b/pkg/stringutil/string_util.go
new file mode 100644
index 0000000..6f01d14
--- /dev/null
+++ b/pkg/stringutil/string_util.go
@@ -0,0 +1,45 @@
+/*
+ * 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 stringutil
+
+import (
+ "sort"
+ "strings"
+)
+
+//FormatMap format map to string
+func FormatMap(m map[string]string) string {
+ if len(m) == 0 {
+ return "none"
+ }
+ sb := strings.Builder{}
+ s := make([]string, 0, len(m))
+ for k := range m {
+ s = append(s, k)
+ }
+ sort.Strings(s)
+ for i, k := range s {
+ sb.WriteString(k)
+ sb.WriteString("=")
+ sb.WriteString(m[k])
+ if i != (len(s) - 1) {
+ sb.WriteString("::")
+ }
+ }
+ return sb.String()
+}
diff --git a/pkg/stringutil/string_util_test.go
b/pkg/stringutil/string_util_test.go
new file mode 100644
index 0000000..7dcac58
--- /dev/null
+++ b/pkg/stringutil/string_util_test.go
@@ -0,0 +1,39 @@
+/*
+ * 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 stringutil_test
+
+import (
+ "github.com/apache/servicecomb-kie/pkg/stringutil"
+ "github.com/stretchr/testify/assert"
+ "testing"
+)
+
+func TestFormat(t *testing.T) {
+ s := stringutil.FormatMap(map[string]string{
+ "service": "a",
+ "version": "1",
+ })
+ s2 := stringutil.FormatMap(map[string]string{
+ "version": "1",
+ "service": "a",
+ })
+ t.Log(s)
+ assert.Equal(t, s, s2)
+ s3 := stringutil.FormatMap(nil)
+ assert.Equal(t, "none", s3)
+}
diff --git a/server/pubsub/bus.go b/server/pubsub/bus.go
index a67ca8f..7d5a7d0 100644
--- a/server/pubsub/bus.go
+++ b/server/pubsub/bus.go
@@ -19,6 +19,7 @@ package pubsub
import (
"encoding/json"
+ "github.com/apache/servicecomb-kie/pkg/stringutil"
"github.com/apache/servicecomb-kie/server/config"
"github.com/go-mesh/openlogging"
"github.com/hashicorp/serf/cmd/serf/command/agent"
@@ -104,7 +105,7 @@ func Publish(event *KVChangeEvent) error {
//ObserveOnce observe key changes by (key or labels) or (key and labels)
func ObserveOnce(o *Observer, topic *Topic) error {
- topic.Format()
+ topic.LabelsFormat = stringutil.FormatMap(topic.Labels)
b, err := json.Marshal(topic)
if err != nil {
return err
diff --git a/server/pubsub/struct.go b/server/pubsub/struct.go
index 3cb3449..3c56ba5 100644
--- a/server/pubsub/struct.go
+++ b/server/pubsub/struct.go
@@ -20,7 +20,6 @@ package pubsub
import (
"encoding/json"
"errors"
- "sort"
"strings"
)
@@ -88,26 +87,6 @@ func (t *Topic) Match(event *KVChangeEvent) bool {
return match
}
-//Format format to string
-func (t *Topic) Format() string {
- sb := strings.Builder{}
- s := make([]string, 0, len(t.Labels))
- for k := range t.Labels {
- s = append(s, k)
- }
- sort.Strings(s)
- for i, k := range s {
- sb.WriteString(k)
- sb.WriteString("=")
- sb.WriteString(t.Labels[k])
- if i != (len(s) - 1) {
- sb.WriteString("::")
- }
- }
- t.LabelsFormat = sb.String()
- return t.LabelsFormat
-}
-
//Observer represents a client polling request
type Observer struct {
UUID string
diff --git a/server/service/mongo/kv/kv_test.go
b/server/service/mongo/kv/kv_test.go
index cd52ea7..0416053 100644
--- a/server/service/mongo/kv/kv_test.go
+++ b/server/service/mongo/kv/kv_test.go
@@ -101,6 +101,7 @@ func TestService_CreateOrUpdate(t *testing.T) {
savedKV, err := kvsvc.Exist(context.Background(), "default",
"timeout", "test", service.WithLabels(map[string]string{
"app": "mall",
}))
+ assert.NoError(t, err)
assert.Equal(t, beforeKV.ID, savedKV.ID)
kvs, err := kvsvc.FindKV(context.Background(), "default",
"test",
service.WithKey("timeout"),
diff --git a/server/service/mongo/label/label_dao.go
b/server/service/mongo/label/label_dao.go
index ff1f40d..9decc40 100644
--- a/server/service/mongo/label/label_dao.go
+++ b/server/service/mongo/label/label_dao.go
@@ -21,6 +21,7 @@ import (
"context"
"fmt"
"github.com/apache/servicecomb-kie/pkg/model"
+ "github.com/apache/servicecomb-kie/pkg/stringutil"
"github.com/apache/servicecomb-kie/server/service/mongo/session"
"github.com/go-mesh/openlogging"
uuid "github.com/satori/go.uuid"
@@ -35,17 +36,10 @@ const (
//if map is empty. will return default labels doc which has no labels
func FindLabels(ctx context.Context, domain, project string, labels
map[string]string) (*model.LabelDoc, error) {
collection := session.GetDB().Collection(session.CollectionLabel)
-
filter := bson.M{"domain": domain, "project": project}
- for k, v := range labels {
- filter["labels."+k] = v
- }
- if len(labels) == 0 {
- filter["labels"] = defaultLabels //allow key without labels
- }
+ filter["format"] = stringutil.FormatMap(labels) //allow key without
labels
cur, err := collection.Find(ctx, filter)
if err != nil {
-
return nil, err
}
defer cur.Close(ctx)
@@ -94,6 +88,7 @@ func CreateLabel(ctx context.Context, domain string, labels
map[string]string, p
l := &model.LabelDoc{
Domain: domain,
Labels: labels,
+ Format: stringutil.FormatMap(labels),
Project: project,
ID: uuid.NewV4().String(),
}
diff --git a/server/service/mongo/label/label_dao_test.go
b/server/service/mongo/label/label_dao_test.go
new file mode 100644
index 0000000..4de4909
--- /dev/null
+++ b/server/service/mongo/label/label_dao_test.go
@@ -0,0 +1,52 @@
+/*
+ * 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 label_test
+
+import (
+ "context"
+ "github.com/apache/servicecomb-kie/server/config"
+ "github.com/apache/servicecomb-kie/server/service/mongo/label"
+ "github.com/apache/servicecomb-kie/server/service/mongo/session"
+ "github.com/stretchr/testify/assert"
+ "testing"
+)
+
+func TestCreateLabel(t *testing.T) {
+ var err error
+ config.Configurations = &config.Config{DB: config.DB{URI:
"mongodb://kie:[email protected]:27017/kie"}}
+ err = session.Init()
+ assert.NoError(t, err)
+ d, err := label.CreateLabel(context.TODO(), "default",
+ map[string]string{
+ "cluster": "a",
+ "role": "b",
+ "component": "c",
+ }, "default")
+ assert.NoError(t, err)
+ assert.NotEmpty(t, d.ID)
+ assert.Equal(t, "cluster=a::component=c::role=b", d.Format)
+ t.Log(d)
+
+ id, err := label.Exist(context.TODO(), "default", "default",
map[string]string{
+ "cluster": "a",
+ "role": "b",
+ "component": "c",
+ })
+ assert.NoError(t, err)
+ assert.NotEmpty(t, id)
+}