From 412861badd4988d696d4e847a04040267f9935c9 Mon Sep 17 00:00:00 2001 From: scott lewis Date: Tue, 9 Aug 2022 10:52:06 +0800 Subject: [PATCH 1/2] refactor: refact topology to support sharding db --- pkg/misc/strings.go | 17 ++++++++++++++ pkg/misc/strings_test.go | 33 ++++++++++++++++++++++++++ pkg/topo/topo.go | 51 +++++++++++++++++++++++++++------------- pkg/topo/topo_test.go | 10 ++++++++ 4 files changed, 95 insertions(+), 16 deletions(-) diff --git a/pkg/misc/strings.go b/pkg/misc/strings.go index a87237db..96d8e61c 100644 --- a/pkg/misc/strings.go +++ b/pkg/misc/strings.go @@ -17,10 +17,19 @@ package misc import ( + "regexp" "strings" "unicode" ) +const ( + Numeric = "^[0-9]+$" +) + +var ( + rxNumeric = regexp.MustCompile(Numeric) +) + func FirstNonEmptyString(first string, second string, others ...string) string { if len(first) > 0 { return first @@ -148,3 +157,11 @@ func IsBlank(s string) bool { return !unicode.IsSpace(r) }) == -1 } + +// IsNumeric checks if the string contains only numbers. Empty string is valid. +func IsNumeric(str string) bool { + if len(str) == 0 { + return true + } + return rxNumeric.MatchString(str) +} diff --git a/pkg/misc/strings_test.go b/pkg/misc/strings_test.go index 893ddbf8..7d01de66 100644 --- a/pkg/misc/strings_test.go +++ b/pkg/misc/strings_test.go @@ -441,3 +441,36 @@ func TestIsBlank(t *testing.T) { }) } } + +func TestIsNumeric(t *testing.T) { + cases := map[string]struct { + input string + expected bool + }{ + "success-1": { + input: "", + expected: true, + }, + "success-2": { + input: "1", + expected: true, + }, + "false-3": { + input: "a", + expected: false, + }, + "false-4": { + input: "1-9", + expected: false, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + actual := IsNumeric(tc.input) + if actual != tc.expected { + t.Errorf("expected %t, got %t", tc.expected, actual) + } + }) + } +} diff --git a/pkg/topo/topo.go b/pkg/topo/topo.go index 41412b89..e399c004 100644 --- a/pkg/topo/topo.go +++ b/pkg/topo/topo.go @@ -23,6 +23,8 @@ import ( "github.com/cznic/mathutil" "github.com/pkg/errors" + + "github.com/cectc/dbpack/pkg/misc" ) const ( @@ -55,31 +57,48 @@ func ParseTopology(dbName, tableName string, topology map[int]string) (*Topology for i := 0; i < dbLen; i++ { tp := topology[i] - realDB := fmt.Sprintf("%s_%d", dbName, i) - params := topologyRegexp.FindStringSubmatch(tp) - if len(params) != 3 { + if tp == "" { return nil, errors.Errorf("incorrect topology format") } - begin, err := strconv.Atoi(params[1]) - if err != nil { - return nil, err - } - end, err := strconv.Atoi(params[2]) - if err != nil { - return nil, err - } - if begin >= end { - return nil, errors.Errorf("incorrect topology, begin index must less than end index") - } + realDB := fmt.Sprintf("%s_%d", dbName, i) tableSlice := make([]string, 0) - for j := begin; j <= end; j++ { - index := j + + if misc.IsNumeric(tp) { + index, err := strconv.Atoi(tp) + if err != nil { + return nil, err + } realTable := fmt.Sprintf("%s_%d", tableName, index) tables[realTable] = realDB tableIndexMap[index] = realTable tableIndexSlice = append(tableIndexSlice, index) tableSlice = append(tableSlice, realTable) max = mathutil.Max(max, index) + } else { + params := topologyRegexp.FindStringSubmatch(tp) + if len(params) != 3 { + return nil, errors.Errorf("incorrect topology format") + } + begin, err := strconv.Atoi(params[1]) + if err != nil { + return nil, err + } + end, err := strconv.Atoi(params[2]) + if err != nil { + return nil, err + } + if begin >= end { + return nil, errors.Errorf("incorrect topology, begin index must less than end index") + } + for j := begin; j <= end; j++ { + index := j + realTable := fmt.Sprintf("%s_%d", tableName, index) + tables[realTable] = realDB + tableIndexMap[index] = realTable + tableIndexSlice = append(tableIndexSlice, index) + tableSlice = append(tableSlice, realTable) + max = mathutil.Max(max, index) + } } dbs[realDB] = tableSlice } diff --git a/pkg/topo/topo_test.go b/pkg/topo/topo_test.go index 7d1b86c9..7a876a50 100644 --- a/pkg/topo/topo_test.go +++ b/pkg/topo/topo_test.go @@ -38,4 +38,14 @@ func Test_ParseTopology(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 100, tp.TableSliceLen) + + tp, err = ParseTopology("school", "student", map[int]string{ + 0: "0", + 1: "1", + 2: "2", + 3: "3", + }) + + assert.Nil(t, err) + assert.Equal(t, 4, tp.TableSliceLen) } From 5a093803d4142c2a72b65fc2e37ecef00ddcc7df Mon Sep 17 00:00:00 2001 From: scott lewis <33612882+dk-lockdown@users.noreply.github.com> Date: Tue, 9 Aug 2022 21:36:21 +0800 Subject: [PATCH 2/2] Update pkg/topo/topo.go Co-authored-by: hehe.bu --- pkg/topo/topo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/topo/topo.go b/pkg/topo/topo.go index e399c004..06acdaad 100644 --- a/pkg/topo/topo.go +++ b/pkg/topo/topo.go @@ -88,7 +88,7 @@ func ParseTopology(dbName, tableName string, topology map[int]string) (*Topology return nil, err } if begin >= end { - return nil, errors.Errorf("incorrect topology, begin index must less than end index") + return nil, errors.Errorf("incorrect topology, begin index must be less than end index") } for j := begin; j <= end; j++ { index := j