Skip to content

Commit

Permalink
optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
chengshiwen committed Aug 14, 2024
1 parent 35b1cbf commit d009d15
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
28 changes: 14 additions & 14 deletions backend/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,49 @@ import "strings"

type shardTpl struct {
tpl string
items []string
parts []string
dbCnt int
mmCnt int
}

func newShardTpl(tpl string) *shardTpl {
sTpl := &shardTpl{tpl: tpl}
st := &shardTpl{tpl: tpl}
for i := 0; i < len(tpl); {
for j := i; j < len(tpl); {
if j <= len(tpl)-3 && (tpl[j:j+3] == ShardKeyVarDb || tpl[j:j+3] == ShardKeyVarMm) {
if j > i {
sTpl.items = append(sTpl.items, tpl[i:j])
st.parts = append(st.parts, tpl[i:j])
}
sTpl.items = append(sTpl.items, tpl[j:j+3])
st.parts = append(st.parts, tpl[j:j+3])
if tpl[j:j+3] == ShardKeyVarDb {
sTpl.dbCnt++
st.dbCnt++
} else if tpl[j:j+3] == ShardKeyVarMm {
sTpl.mmCnt++
st.mmCnt++
}
i, j = j+3, j+3
continue
}
j++
if j == len(tpl) {
sTpl.items = append(sTpl.items, tpl[i:j])
st.parts = append(st.parts, tpl[i:j])
i = j
break
}
}
}
return sTpl
return st
}

func (sTpl *shardTpl) GetKey(db, mm string) string {
func (st *shardTpl) GetKey(db, mm string) string {
var b strings.Builder
b.Grow(len(sTpl.tpl) + (len(db)-len(ShardKeyVarDb))*sTpl.dbCnt + (len(mm)-len(ShardKeyVarMm))*sTpl.mmCnt)
for _, item := range sTpl.items {
if item == ShardKeyVarDb {
b.Grow(len(st.tpl) + (len(db)-len(ShardKeyVarDb))*st.dbCnt + (len(mm)-len(ShardKeyVarMm))*st.mmCnt)
for _, part := range st.parts {
if part == ShardKeyVarDb {
b.WriteString(db)
} else if item == ShardKeyVarMm {
} else if part == ShardKeyVarMm {
b.WriteString(mm)
} else {
b.WriteString(item)
b.WriteString(part)
}
}
return b.String()
Expand Down
34 changes: 17 additions & 17 deletions backend/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestShardTpl(t *testing.T) {
tpl string
db string
mm string
items []string
parts []string
dbCnt int
mmCnt int
render string
Expand All @@ -27,7 +27,7 @@ func TestShardTpl(t *testing.T) {
tpl: "%db,%mm",
db: "database",
mm: "measurement",
items: []string{"%db", ",", "%mm"},
parts: []string{"%db", ",", "%mm"},
dbCnt: 1,
mmCnt: 1,
render: "database,measurement",
Expand All @@ -37,7 +37,7 @@ func TestShardTpl(t *testing.T) {
tpl: "shard-%db-%mm",
db: "database",
mm: "measurement",
items: []string{"shard-", "%db", "-", "%mm"},
parts: []string{"shard-", "%db", "-", "%mm"},
dbCnt: 1,
mmCnt: 1,
render: "shard-database-measurement",
Expand All @@ -47,7 +47,7 @@ func TestShardTpl(t *testing.T) {
tpl: "%db-%mm-key",
db: "database",
mm: "measurement",
items: []string{"%db", "-", "%mm", "-key"},
parts: []string{"%db", "-", "%mm", "-key"},
dbCnt: 1,
mmCnt: 1,
render: "database-measurement-key",
Expand All @@ -57,7 +57,7 @@ func TestShardTpl(t *testing.T) {
tpl: "shard-%db-%mm-key",
db: "database",
mm: "measurement",
items: []string{"shard-", "%db", "-", "%mm", "-key"},
parts: []string{"shard-", "%db", "-", "%mm", "-key"},
dbCnt: 1,
mmCnt: 1,
render: "shard-database-measurement-key",
Expand All @@ -67,7 +67,7 @@ func TestShardTpl(t *testing.T) {
tpl: "shard-%mm-%db-%mm-%db-key",
db: "database",
mm: "measurement",
items: []string{"shard-", "%mm", "-", "%db", "-", "%mm", "-", "%db", "-key"},
parts: []string{"shard-", "%mm", "-", "%db", "-", "%mm", "-", "%db", "-key"},
dbCnt: 2,
mmCnt: 2,
render: "shard-measurement-database-measurement-database-key",
Expand All @@ -77,7 +77,7 @@ func TestShardTpl(t *testing.T) {
tpl: "%db%mm",
db: "database",
mm: "measurement",
items: []string{"%db", "%mm"},
parts: []string{"%db", "%mm"},
dbCnt: 1,
mmCnt: 1,
render: "databasemeasurement",
Expand All @@ -87,7 +87,7 @@ func TestShardTpl(t *testing.T) {
tpl: "shard%db%mm",
db: "database",
mm: "measurement",
items: []string{"shard", "%db", "%mm"},
parts: []string{"shard", "%db", "%mm"},
dbCnt: 1,
mmCnt: 1,
render: "sharddatabasemeasurement",
Expand All @@ -97,7 +97,7 @@ func TestShardTpl(t *testing.T) {
tpl: "%db%mmkey",
db: "database",
mm: "measurement",
items: []string{"%db", "%mm", "key"},
parts: []string{"%db", "%mm", "key"},
dbCnt: 1,
mmCnt: 1,
render: "databasemeasurementkey",
Expand All @@ -107,7 +107,7 @@ func TestShardTpl(t *testing.T) {
tpl: "shard%db%mmkey",
db: "database",
mm: "measurement",
items: []string{"shard", "%db", "%mm", "key"},
parts: []string{"shard", "%db", "%mm", "key"},
dbCnt: 1,
mmCnt: 1,
render: "sharddatabasemeasurementkey",
Expand All @@ -117,18 +117,18 @@ func TestShardTpl(t *testing.T) {
tpl: "shard%mm%db%mm%dbkey",
db: "database",
mm: "measurement",
items: []string{"shard", "%mm", "%db", "%mm", "%db", "key"},
parts: []string{"shard", "%mm", "%db", "%mm", "%db", "key"},
dbCnt: 2,
mmCnt: 2,
render: "shardmeasurementdatabasemeasurementdatabasekey",
},
}
for _, tt := range tests {
sTpl := newShardTpl(tt.tpl)
if !slices.Equal(sTpl.items, tt.items) || sTpl.dbCnt != tt.dbCnt || sTpl.mmCnt != tt.mmCnt {
t.Errorf("%v: got %+v, %d, %d, want %+v, %d, %d", tt.name, sTpl.items, sTpl.dbCnt, sTpl.mmCnt, tt.items, tt.dbCnt, tt.mmCnt)
st := newShardTpl(tt.tpl)
if !slices.Equal(st.parts, tt.parts) || st.dbCnt != tt.dbCnt || st.mmCnt != tt.mmCnt {
t.Errorf("%v: got %+v, %d, %d, want %+v, %d, %d", tt.name, st.parts, st.dbCnt, st.mmCnt, tt.parts, tt.dbCnt, tt.mmCnt)
}
if render := sTpl.GetKey(tt.db, tt.mm); render != tt.render {
if render := st.GetKey(tt.db, tt.mm); render != tt.render {
t.Errorf("%v: got %s, want %s", tt.name, render, tt.render)
}
}
Expand Down Expand Up @@ -162,10 +162,10 @@ func getKeyByBuilder(db, mm string) string {
}

func BenchmarkGetKeyByShardTpl(b *testing.B) {
sTpl := newShardTpl("%db,%mm")
st := newShardTpl("%db,%mm")
b.ResetTimer()
for i := 0; i < b.N; i++ {
sTpl.GetKey("database", "measurement")
st.GetKey("database", "measurement")
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/panjf2000/ants/v2 v2.10.0
github.com/prometheus/client_golang v1.19.1
github.com/spf13/viper v1.19.0
golang.org/x/sync v0.6.0
golang.org/x/sync v0.8.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
stathat.com/c/consistent v1.0.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit d009d15

Please sign in to comment.