Skip to content

Commit

Permalink
refactor: refact topology to support sharding db (#234)
Browse files Browse the repository at this point in the history
* refactor: refact topology to support sharding db

* Update pkg/topo/topo.go

Co-authored-by: hehe.bu <hehebu33@gmail.com>

Co-authored-by: hehe.bu <hehebu33@gmail.com>
  • Loading branch information
dk-lockdown and bohehe committed Aug 15, 2022
1 parent df533f7 commit a8fe90d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
17 changes: 17 additions & 0 deletions pkg/misc/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
33 changes: 33 additions & 0 deletions pkg/misc/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
51 changes: 35 additions & 16 deletions pkg/topo/topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/cznic/mathutil"
"github.com/pkg/errors"

"github.com/cectc/dbpack/pkg/misc"
)

const (
Expand Down Expand Up @@ -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 be 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
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/topo/topo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a8fe90d

Please sign in to comment.