Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin api create/delete/disable/enable support tablename like ns:name #177

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions admin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
// AdminClient to perform admistrative operations with HMaster
type AdminClient interface {
CreateTable(t *hrpc.CreateTable) error
ModifyTable(t *hrpc.ModifyTable) error
GetTableDescriptors(t *hrpc.GetTableDescriptors) ([]*pb.TableSchema, error)
DeleteTable(t *hrpc.DeleteTable) error
EnableTable(t *hrpc.EnableTable) error
DisableTable(t *hrpc.DisableTable) error
Expand Down Expand Up @@ -100,6 +102,32 @@ func (c *client) CreateTable(t *hrpc.CreateTable) error {
return c.checkProcedureWithBackoff(t.Context(), r.GetProcId())
}

func (c *client) ModifyTable(t *hrpc.ModifyTable) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
return err
}

_, ok := pbmsg.(*pb.ModifyTableResponse)
if !ok {
return fmt.Errorf("sendRPC returned not a ModifyTableResponse")
}
return nil
}

func (c *client) GetTableDescriptors(t *hrpc.GetTableDescriptors) ([]*pb.TableSchema, error) {
pbmsg, err := c.SendRPC(t)
if err != nil {
return nil, err
}

r, ok := pbmsg.(*pb.GetTableDescriptorsResponse)
if !ok {
return nil, fmt.Errorf("sendRPC returned not a GetTableDescriptorsResponse")
}
return r.GetTableSchema(), nil
}

func (c *client) DeleteTable(t *hrpc.DeleteTable) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions hrpc/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package hrpc

import (
"bytes"
"context"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -153,6 +154,20 @@ func (b *base) Options() []func(Call) error {
return b.options
}

// return <namespace, tableName>
func (b *base) parseTableName() ([]byte, []byte) {
return parseTableName(b.table)
}

func parseTableName(table []byte) ([]byte, []byte) {
namespace := []byte("default")
if i := bytes.Index(table, []byte(":")); i > -1 {
namespace = table[:i]
table = table[i+1:]
}
return namespace, table
}

func applyOptions(call Call, options ...func(Call) error) error {
call.(withOptions).setOptions(options)
for _, option := range options {
Expand Down
52 changes: 46 additions & 6 deletions hrpc/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
type CreateTable struct {
base

families map[string]map[string]string
splitKeys [][]byte
families map[string]map[string]string
splitKeys [][]byte
attributes map[string]string
configuration map[string]string
}

var defaultAttributes = map[string]string{
Expand Down Expand Up @@ -46,7 +48,9 @@ func NewCreateTable(ctx context.Context, table []byte,
ctx: ctx,
resultch: make(chan RPCResult, 1),
},
families: make(map[string]map[string]string, len(families)),
families: make(map[string]map[string]string, len(families)),
attributes: make(map[string]string),
configuration: make(map[string]string),
}
for _, option := range options {
option(ct)
Expand All @@ -71,6 +75,20 @@ func SplitKeys(sk [][]byte) func(*CreateTable) {
}
}

// Attributes will add create table attributes params
func Attributes(key, value string) func(table *CreateTable) {
return func(ct *CreateTable) {
ct.attributes[key] = value
}
}

// Configuration will add create table conf
func Configuration(name, value string) func(table *CreateTable) {
return func(ct *CreateTable) {
ct.configuration[name] = value
}
}

// Name returns the name of this RPC call.
func (ct *CreateTable) Name() string {
return "CreateTable"
Expand All @@ -97,14 +115,36 @@ func (ct *CreateTable) ToProto() proto.Message {
}
pbFamilies = append(pbFamilies, f)
}

// Attributes
pbAttributes := make([]*pb.BytesBytesPair, 0, len(ct.attributes))
for k, v := range ct.attributes {
pbAttributes = append(pbAttributes, &pb.BytesBytesPair{
First: []byte(k),
Second: []byte(v),
})
}

// Configuration
pbConfiguration := make([]*pb.NameStringPair, 0, len(ct.configuration))
for k, v := range ct.configuration {
pbConfiguration = append(pbConfiguration, &pb.NameStringPair{
Name: proto.String(k),
Value: proto.String(v),
})
}

// TableName
namespace, table := ct.parseTableName()
return &pb.CreateTableRequest{
TableSchema: &pb.TableSchema{
TableName: &pb.TableName{
// TODO: handle namespaces
Namespace: []byte("default"),
Qualifier: ct.table,
Namespace: namespace,
Qualifier: table,
},
ColumnFamilies: pbFamilies,
Attributes: pbAttributes,
Configuration: pbConfiguration,
},
SplitKeys: ct.splitKeys,
}
Expand Down
6 changes: 3 additions & 3 deletions hrpc/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (dt *DeleteTable) Description() string {

// ToProto converts the RPC into a protobuf message
func (dt *DeleteTable) ToProto() proto.Message {
namespace, table := dt.parseTableName()
return &pb.DeleteTableRequest{
TableName: &pb.TableName{
// TODO: hadle namespaces properly
Namespace: []byte("default"),
Qualifier: dt.table,
Namespace: namespace,
Qualifier: table,
},
}
}
Expand Down
57 changes: 57 additions & 0 deletions hrpc/descriptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hrpc

import (
"context"

"github.com/tsuna/gohbase/pb"
"google.golang.org/protobuf/proto"
)

// GetTableDescriptors ...
type GetTableDescriptors struct {
base

tableNames [][]byte
}

// NewGetTableDescriptors ...
func NewGetTableDescriptors(ctx context.Context, tableNames [][]byte) *GetTableDescriptors {
tn := &GetTableDescriptors{
base: base{
ctx: ctx,
table: []byte{},
resultch: make(chan RPCResult, 1),
},
tableNames: tableNames,
}
return tn
}

// Name returns the name of this RPC call.
func (gd *GetTableDescriptors) Name() string {
return "GetTableDescriptors"
}

// Description returns the description of this RPC call.
func (gd *GetTableDescriptors) Description() string {
return gd.Name()
}

// ToProto converts the RPC into a protobuf message.
func (gd *GetTableDescriptors) ToProto() proto.Message {
req := &pb.GetTableDescriptorsRequest{}
for _, tableName := range gd.tableNames {
namespace, table := parseTableName(tableName)
req.TableNames = append(req.TableNames, &pb.TableName{
Namespace: namespace,
Qualifier: table,
})
}
return req
}

// NewResponse creates an empty protobuf message to read the response of this
// RPC.
func (gd *GetTableDescriptors) NewResponse() proto.Message {
return &pb.GetTableDescriptorsResponse{}
}
6 changes: 3 additions & 3 deletions hrpc/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (dt *DisableTable) Description() string {

// ToProto converts the RPC into a protobuf message
func (dt *DisableTable) ToProto() proto.Message {
namespace, table := dt.parseTableName()
return &pb.DisableTableRequest{
TableName: &pb.TableName{
// TODO: handle namespaces
Namespace: []byte("default"),
Qualifier: dt.table,
Namespace: namespace,
Qualifier: table,
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions hrpc/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (et *EnableTable) Description() string {

// ToProto converts the RPC into a protobuf message
func (et *EnableTable) ToProto() proto.Message {
namespace, table := et.parseTableName()
return &pb.EnableTableRequest{
TableName: &pb.TableName{
// TODO: handle namespaces
Namespace: []byte("default"),
Qualifier: et.table,
Namespace: namespace,
Qualifier: table,
},
}
}
Expand Down
56 changes: 56 additions & 0 deletions hrpc/modify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package hrpc

import (
"context"

"github.com/tsuna/gohbase/pb"
"google.golang.org/protobuf/proto"
)

type TableSchema = pb.TableSchema

// ModifyTable represents a ModifyTable HBase call
type ModifyTable struct {
base

schema *TableSchema
}

// NewModifyTable create new ModifyTable object
func NewModifyTable(ctx context.Context, table []byte, schema *TableSchema) *ModifyTable {
mt := &ModifyTable{
base: base{
table: table,
ctx: ctx,
resultch: make(chan RPCResult, 1),
},
schema: schema,
}
return mt
}

func (mt *ModifyTable) Name() string {
return "ModifyTable"
}

func (mt *ModifyTable) Description() string {
return mt.Name()
}

func (mt *ModifyTable) ToProto() proto.Message {

namespace, table := mt.parseTableName()
req := &pb.ModifyTableRequest{
TableName: &pb.TableName{
Namespace: namespace,
Qualifier: table,
},
}
mt.schema.TableName = req.TableName
req.TableSchema = mt.schema
return req
}

func (mt *ModifyTable) NewResponse() proto.Message {
return &pb.ModifyTableResponse{}
}