Skip to content

Commit

Permalink
cherry-pick pingcap#342
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu committed May 14, 2020
1 parent 7d70d54 commit 96896c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
28 changes: 19 additions & 9 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type Table struct {
Name string `toml:"tbl-name" json:"tbl-name" yaml:"tbl-name"`
}

// Clone clones a new filter.Table
func (t *Table) Clone() *Table {
return &Table{
Schema: t.Schema,
Name: t.Name,
}
}

// String implements the fmt.Stringer interface.
func (t *Table) String() string {
if len(t.Name) > 0 {
Expand Down Expand Up @@ -277,13 +285,14 @@ func (f *Filter) ApplyOn(stbs []*Table) []*Table {

var tbs []*Table
for _, tb := range stbs {
newTb := tb.Clone()
if !f.caseSensitive {
tb.Schema = strings.ToLower(tb.Schema)
tb.Name = strings.ToLower(tb.Name)
newTb.Schema = strings.ToLower(newTb.Schema)
newTb.Name = strings.ToLower(newTb.Name)
}

if f.Match(tb) {
tbs = append(tbs, tb)
if f.Match(newTb) {
tbs = append(tbs, newTb)
}
}

Expand All @@ -295,16 +304,17 @@ func (f *Filter) Match(tb *Table) bool {
if f == nil || f.rules == nil {
return true
}
newTb := tb.Clone()
if !f.caseSensitive {
tb.Schema = strings.ToLower(tb.Schema)
tb.Name = strings.ToLower(tb.Name)
newTb.Schema = strings.ToLower(newTb.Schema)
newTb.Name = strings.ToLower(newTb.Name)
}

name := tb.String()
name := newTb.String()
do, exist := f.c.query(name)
if !exist {
do = ActionType(f.filterOnSchemas(tb) && f.filterOnTables(tb))
f.c.set(tb.String(), do)
do = ActionType(f.filterOnSchemas(newTb) && f.filterOnTables(newTb))
f.c.set(newTb.String(), do)
}
return do == Do
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import (
. "github.com/pingcap/check"
)

func cloneTables(tbs []*Table) []*Table {
if tbs == nil {
return nil
}
newTbs := make([]*Table, 0, len(tbs))
for _, tb := range tbs {
newTbs = append(newTbs, tb.Clone())
}
return newTbs
}

func (s *testFilterSuite) TestFilterOnSchema(c *C) {
cases := []struct {
rules *Rules
Expand Down Expand Up @@ -148,8 +159,10 @@ func (s *testFilterSuite) TestFilterOnSchema(c *C) {
for _, t := range cases {
ft, err := New(t.caseSensitive, t.rules)
c.Assert(err, IsNil)
originInput := cloneTables(t.Input)
got := ft.ApplyOn(t.Input)
c.Logf("got %+v, expected %+v", got, t.Output)
c.Assert(originInput, DeepEquals, t.Input)
c.Assert(got, DeepEquals, t.Output)
}
}
Expand Down Expand Up @@ -196,13 +209,16 @@ func (s *testFilterSuite) TestCaseSensitive(c *C) {
}

r, err = New(false, rules)
c.Assert(err, IsNil)
inputTable = &Table{"bar", "a"}
c.Assert(r.Match(inputTable), IsTrue)

c.Assert(err, IsNil)

inputTable = &Table{"BAR", "a"}
originInputTable := inputTable.Clone()
c.Assert(r.Match(inputTable), IsTrue)
c.Assert(originInputTable, DeepEquals, inputTable)
}

func (s *testFilterSuite) TestInvalidRegex(c *C) {
Expand Down

0 comments on commit 96896c0

Please sign in to comment.