diff --git a/pkg/filter/filter.go b/pkg/filter/filter.go index 590dccaa5..c1e60e0e3 100644 --- a/pkg/filter/filter.go +++ b/pkg/filter/filter.go @@ -46,6 +46,14 @@ func (t *Table) String() string { return fmt.Sprintf("`%s`", t.Schema) } +// Clone clones a new filter.Table +func (t *Table) Clone() *Table { + return &Table{ + Schema: t.Schema, + Name: t.Name, + } +} + type cache struct { sync.RWMutex items map[string]ActionType // `schema`.`table` => do/ignore @@ -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) } } @@ -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 } diff --git a/pkg/filter/filter_test.go b/pkg/filter/filter_test.go index 9e31d20a9..e6590ac41 100644 --- a/pkg/filter/filter_test.go +++ b/pkg/filter/filter_test.go @@ -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 @@ -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) } } @@ -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) {