Skip to content

Commit

Permalink
Merge f4b7d45 into c943c7c
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed Jan 20, 2020
2 parents c943c7c + f4b7d45 commit ddb1404
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 83 deletions.
26 changes: 26 additions & 0 deletions examples/dev/db.js → deployments/db.js
Expand Up @@ -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} );
51 changes: 0 additions & 51 deletions deployments/docker/db.js

This file was deleted.

2 changes: 1 addition & 1 deletion deployments/docker/docker-compose.yaml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/dev/docker-compose.yaml
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/model/db_schema.go
Expand Up @@ -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"`
}
Expand Down
45 changes: 45 additions & 0 deletions 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()
}
39 changes: 39 additions & 0 deletions 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)
}
3 changes: 2 additions & 1 deletion server/pubsub/bus.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions server/pubsub/struct.go
Expand Up @@ -20,7 +20,6 @@ package pubsub
import (
"encoding/json"
"errors"
"sort"
"strings"
)

Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions server/service/mongo/label/label_dao.go
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
}
Expand Down

0 comments on commit ddb1404

Please sign in to comment.