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 959a309  topic support none label cases (#107)
959a309 is described below

commit 959a30975b44388cfdfbf96e10743013ab8b2f9c
Author: zhulijian <[email protected]>
AuthorDate: Tue Mar 3 09:48:12 2020 +0800

    topic support none label cases (#107)
---
 pkg/stringutil/string_util.go                      |  7 +++-
 pkg/{stringutil/string_util.go => util/util.go}    | 32 +++++-------------
 .../string_util.go => util/util_test.go}           | 39 +++++++++++-----------
 server/pubsub/event_handler.go                     |  2 +-
 server/pubsub/struct.go                            | 16 +++++++--
 server/service/mongo/kv/kv_service.go              |  4 +--
 6 files changed, 50 insertions(+), 50 deletions(-)

diff --git a/pkg/stringutil/string_util.go b/pkg/stringutil/string_util.go
index 6f01d14..0e0ab86 100644
--- a/pkg/stringutil/string_util.go
+++ b/pkg/stringutil/string_util.go
@@ -22,10 +22,15 @@ import (
        "strings"
 )
 
+const (
+       // LabelNone is the format string when the map is none
+       LabelNone = "none"
+)
+
 //FormatMap format map to string
 func FormatMap(m map[string]string) string {
        if len(m) == 0 {
-               return "none"
+               return LabelNone
        }
        sb := strings.Builder{}
        s := make([]string, 0, len(m))
diff --git a/pkg/stringutil/string_util.go b/pkg/util/util.go
similarity index 64%
copy from pkg/stringutil/string_util.go
copy to pkg/util/util.go
index 6f01d14..29eb39e 100644
--- a/pkg/stringutil/string_util.go
+++ b/pkg/util/util.go
@@ -15,31 +15,15 @@
  * limitations under the License.
  */
 
-package stringutil
+package util
 
-import (
-       "sort"
-       "strings"
-)
+import "reflect"
 
-//FormatMap format map to string
-func FormatMap(m map[string]string) string {
-       if len(m) == 0 {
-               return "none"
+//IsEquivalentLabel compares whether two labels are equal.
+//In particular, if one is nil and another is an empty map, it return true
+func IsEquivalentLabel(x, y map[string]string) bool {
+       if len(x) == 0 && len(y) == 0 {
+               return true
        }
-       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()
+       return reflect.DeepEqual(x, y)
 }
diff --git a/pkg/stringutil/string_util.go b/pkg/util/util_test.go
similarity index 57%
copy from pkg/stringutil/string_util.go
copy to pkg/util/util_test.go
index 6f01d14..a98e0ed 100644
--- a/pkg/stringutil/string_util.go
+++ b/pkg/util/util_test.go
@@ -15,31 +15,30 @@
  * limitations under the License.
  */
 
-package stringutil
+package util_test
 
 import (
-       "sort"
-       "strings"
+       "testing"
+
+       "github.com/apache/servicecomb-kie/pkg/util"
+       "github.com/stretchr/testify/assert"
 )
 
-//FormatMap format map to string
-func FormatMap(m map[string]string) string {
-       if len(m) == 0 {
-               return "none"
+func TestIsEquivalentLabel(t *testing.T) {
+       var m1 map[string]string
+       m2 := make(map[string]string)
+       m3 := map[string]string{
+               "foo": "bar",
        }
-       sb := strings.Builder{}
-       s := make([]string, 0, len(m))
-       for k := range m {
-               s = append(s, k)
+       m4 := map[string]string{
+               "foo": "bar",
        }
-       sort.Strings(s)
-       for i, k := range s {
-               sb.WriteString(k)
-               sb.WriteString("=")
-               sb.WriteString(m[k])
-               if i != (len(s) - 1) {
-                       sb.WriteString("::")
-               }
+       m5 := map[string]string{
+               "bar": "foo",
        }
-       return sb.String()
+       assert.Equal(t, util.IsEquivalentLabel(m1, m1), true)
+       assert.Equal(t, util.IsEquivalentLabel(m1, m2), true)
+       assert.Equal(t, util.IsEquivalentLabel(m2, m3), false)
+       assert.Equal(t, util.IsEquivalentLabel(m3, m4), true)
+       assert.Equal(t, util.IsEquivalentLabel(m3, m5), false)
 }
diff --git a/server/pubsub/event_handler.go b/server/pubsub/event_handler.go
index 941e143..7b6ebca 100644
--- a/server/pubsub/event_handler.go
+++ b/server/pubsub/event_handler.go
@@ -49,7 +49,7 @@ func handleKVEvent(e serf.Event) {
        topics.Range(func(key, value interface{}) bool { //range all topics
                t, err := ParseTopicString(key.(string))
                if err != nil {
-                       openlogging.Error("can not parse topic:" + key.(string))
+                       openlogging.Error("can not parse topic " + key.(string) 
+ ": " + err.Error())
                        return true
                }
                if t.Match(ke) {
diff --git a/server/pubsub/struct.go b/server/pubsub/struct.go
index df67116..dd7d156 100644
--- a/server/pubsub/struct.go
+++ b/server/pubsub/struct.go
@@ -20,10 +20,11 @@ package pubsub
 import (
        "encoding/json"
        "errors"
-       "reflect"
        "strings"
 
        "github.com/apache/servicecomb-kie/pkg/common"
+       "github.com/apache/servicecomb-kie/pkg/stringutil"
+       "github.com/apache/servicecomb-kie/pkg/util"
 )
 
 // const
@@ -67,6 +68,9 @@ func ParseTopicString(s string) (*Topic, error) {
        if err != nil {
                return nil, err
        }
+       if t.LabelsFormat == stringutil.LabelNone {
+               return t, nil
+       }
        ls := strings.Split(t.LabelsFormat, "::")
        if len(ls) != 0 {
                for _, l := range ls {
@@ -81,6 +85,11 @@ func ParseTopicString(s string) (*Topic, error) {
 }
 
 //Match compare event with topic
+//If the match type is set to exact in long pulling request, only update 
request with exactly
+//the same label of pulling request will match the request and will trigger an 
immediate return.
+//
+//If the match type is not set, it will be matched when pulling request labels 
is equal to
+//update request labels or a subset of it.
 func (t *Topic) Match(event *KVChangeEvent) bool {
        match := false
        if t.Key != "" {
@@ -89,10 +98,13 @@ func (t *Topic) Match(event *KVChangeEvent) bool {
                }
        }
        if t.MatchType == common.PatternExact {
-               if !reflect.DeepEqual(t.Labels, event.Labels) {
+               if !util.IsEquivalentLabel(t.Labels, event.Labels) {
                        return false
                }
        }
+       if len(t.Labels) == 0 {
+               return true
+       }
        for k, v := range t.Labels {
                if event.Labels[k] != v {
                        return false
diff --git a/server/service/mongo/kv/kv_service.go 
b/server/service/mongo/kv/kv_service.go
index f828caa..99a11c8 100644
--- a/server/service/mongo/kv/kv_service.go
+++ b/server/service/mongo/kv/kv_service.go
@@ -20,10 +20,10 @@ package kv
 import (
        "context"
        "errors"
-       "reflect"
        "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"
@@ -180,7 +180,7 @@ func (s *Service) List(ctx context.Context, domain, project 
string, options ...s
                        return nil, err
                }
                if opts.ExactLabels {
-                       if !reflect.DeepEqual(opts.Labels, curKV.Labels) {
+                       if !util.IsEquivalentLabel(opts.Labels, curKV.Labels) {
                                continue
                        }
                }

Reply via email to