Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core, api: store labels case-insensitive #794

Merged
merged 4 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions server/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package api
import (
"net/http"
"regexp"
"strings"

"github.com/juju/errors"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -47,8 +48,8 @@ func (h *labelsHandler) Get(w http.ResponseWriter, r *http.Request) {
for _, s := range stores {
ls := s.GetLabels()
for _, l := range ls {
if _, ok := m[l.Key+l.Value]; !ok {
m[l.Key+l.Value] = struct{}{}
if _, ok := m[strings.ToLower(l.Key+l.Value)]; !ok {
m[strings.ToLower(l.Key+l.Value)] = struct{}{}
labels = append(labels, l)
}
}
Expand Down Expand Up @@ -100,11 +101,12 @@ type storesLabelFilter struct {
}

func newStoresLabelFilter(name, value string) (*storesLabelFilter, error) {
keyPattern, err := regexp.Compile(name)
// add (?i) to set a case-insensitive flag
keyPattern, err := regexp.Compile("(?i)" + name)
if err != nil {
return nil, errors.Trace(err)
}
valuePattern, err := regexp.Compile(value)
valuePattern, err := regexp.Compile("(?i)" + value)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
14 changes: 7 additions & 7 deletions server/api/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *testLabelsStoreSuite) TestStoresLabelFilter(c *C) {
want []*metapb.Store
}{
{
name: "zone",
name: "Zone",
want: s.stores[:],
},
{
Expand All @@ -137,22 +137,22 @@ func (s *testLabelsStoreSuite) TestStoresLabelFilter(c *C) {
},
{
name: "zone",
value: "us-west-1",
value: "Us-west-1",
want: s.stores[:1],
},
{
name: "zone",
name: "Zone",
value: "west",
want: s.stores[:2],
},
{
name: "zo",
value: "beijing",
name: "Zo",
value: "Beijing",
want: s.stores[2:3],
},
{
name: "zone",
value: "ssd",
name: "ZONE",
value: "SSD",
want: []*metapb.Store{},
},
}
Expand Down
7 changes: 4 additions & 3 deletions server/core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package core

import (
"strings"
"time"

log "github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -193,8 +194,8 @@ func (s *StoreInfo) IsDisconnected() bool {
// GetLabelValue returns a label's value (if exists).
func (s *StoreInfo) GetLabelValue(key string) string {
for _, label := range s.GetLabels() {
if label.GetKey() == key {
return label.GetValue()
if strings.EqualFold(label.GetKey(), key) {
return strings.ToLower(label.GetValue())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer returning label.GetValue() here, use strings.EqualFold outside to compare.

}
}
return ""
Expand All @@ -220,7 +221,7 @@ func (s *StoreInfo) MergeLabels(labels []*metapb.StoreLabel) {
L:
for _, newLabel := range labels {
for _, label := range s.Labels {
if label.Key == newLabel.Key {
if strings.EqualFold(label.Key, newLabel.Key) {
label.Value = newLabel.Value
continue L
}
Expand Down