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 ae1a9d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
22 changes: 11 additions & 11 deletions backend/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ 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 {
b.Grow(len(st.tpl) + (len(db)-len(ShardKeyVarDb))*st.dbCnt + (len(mm)-len(ShardKeyVarMm))*st.mmCnt)
for _, item := range st.parts {
if item == ShardKeyVarDb {
b.WriteString(db)
} else if item == ShardKeyVarMm {
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

0 comments on commit ae1a9d7

Please sign in to comment.