Skip to content

Commit

Permalink
router: add the interface about partition type radondb#491
Browse files Browse the repository at this point in the history
  • Loading branch information
andyli029 committed Oct 22, 2019
1 parent d4340f9 commit 375d09f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/router/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 375d09f

Please sign in to comment.