From 375d09f1951ee6402e5f204f1f5ff0d9a2ae3cc0 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 22 Oct 2019 12:02:50 +0800 Subject: [PATCH] router: add the interface about partition type #491 --- src/router/partition.go | 1 + src/router/router.go | 11 ++++++++++- src/router/router_test.go | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/router/partition.go b/src/router/partition.go index cd485548..435c3186 100644 --- a/src/router/partition.go +++ b/src/router/partition.go @@ -45,6 +45,7 @@ type Segment struct { // Partition interface. type Partition interface { Build() error + Type() MethodType Lookup(start *sqlparser.SQLVal, end *sqlparser.SQLVal) ([]Segment, error) GetIndex(sqlval *sqlparser.SQLVal) (int, error) GetSegments() []Segment diff --git a/src/router/router.go b/src/router/router.go index a02a66cd..8d3ef527 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -239,7 +239,7 @@ func (r *Router) getTable(database string, tableName string) (*Table, error) { return table, nil } -// ShardKey used to lookup shardkey from given database and table name +// ShardKey used to lookup shardkey from given database and table name. func (r *Router) ShardKey(database string, tableName string) (string, error) { table, err := r.getTable(database, tableName) if err != nil { @@ -248,6 +248,15 @@ func (r *Router) ShardKey(database string, tableName string) (string, error) { return table.ShardKey, nil } +// PartitionType used to get PartitionType from given database and table name. +func (r *Router) PartitionType(database string, tableName string) (MethodType, error) { + table, err := r.getTable(database, tableName) + if err != nil { + return "", err + } + return table.Partition.Type(), nil +} + // TableConfig returns the config by database and tableName. func (r *Router) TableConfig(database string, tableName string) (*config.TableConfig, error) { table, err := r.getTable(database, tableName) diff --git a/src/router/router_test.go b/src/router/router_test.go index c73b02f3..bcf58c95 100644 --- a/src/router/router_test.go +++ b/src/router/router_test.go @@ -312,6 +312,23 @@ func TestRouterShardKey(t *testing.T) { } } +func TestRouterPartitionType(t *testing.T) { + log := xlog.NewStdLog(xlog.Level(xlog.PANIC)) + router, cleanup := MockNewRouter(log) + defer cleanup() + assert.NotNil(t, router) + + // add router of sbtest.A + { + err := router.addTable("sbtest", MockTableAConfig()) + assert.Nil(t, err) + + partitionType, err := router.PartitionType("sbtest", "A") + assert.Nil(t, err) + assert.EqualValues(t, methodTypeHash, partitionType) + } +} + func TestRouterShardKeyError(t *testing.T) { log := xlog.NewStdLog(xlog.Level(xlog.PANIC)) router, cleanup := MockNewRouter(log)