list.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package hrpc
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/tsuna/gohbase/pb"
  7. )
  8. // ListTableNames models a ListTableNames pb call
  9. type ListTableNames struct {
  10. base
  11. regex string
  12. includeSysTables bool
  13. namespace string
  14. }
  15. // ListRegex sets a regex for ListTableNames
  16. func ListRegex(regex string) func(Call) error {
  17. return func(c Call) error {
  18. l, ok := c.(*ListTableNames)
  19. if !ok {
  20. return errors.New("ListRegex option can only be used with ListTableNames")
  21. }
  22. l.regex = regex
  23. return nil
  24. }
  25. }
  26. // ListNamespace sets a namespace for ListTableNames
  27. func ListNamespace(ns string) func(Call) error {
  28. return func(c Call) error {
  29. l, ok := c.(*ListTableNames)
  30. if !ok {
  31. return errors.New("ListNamespace option can only be used with ListTableNames")
  32. }
  33. l.namespace = ns
  34. return nil
  35. }
  36. }
  37. // ListSysTables includes sys tables for ListTableNames
  38. func ListSysTables(b bool) func(Call) error {
  39. return func(c Call) error {
  40. l, ok := c.(*ListTableNames)
  41. if !ok {
  42. return errors.New("ListSysTables option can only be used with ListTableNames")
  43. }
  44. l.includeSysTables = b
  45. return nil
  46. }
  47. }
  48. // NewListTableNames creates a new GetTableNames request that will list tables in hbase.
  49. //
  50. // By default matchs all tables. Use the options (ListRegex, ListNamespace, ListSysTables) to
  51. // set non default behaviour.
  52. func NewListTableNames(ctx context.Context, opts ...func(Call) error) (*ListTableNames, error) {
  53. tn := &ListTableNames{
  54. base: base{
  55. ctx: ctx,
  56. resultch: make(chan RPCResult, 1),
  57. },
  58. regex: ".*",
  59. }
  60. if err := applyOptions(tn, opts...); err != nil {
  61. return nil, err
  62. }
  63. return tn, nil
  64. }
  65. // Name returns the name of this RPC call.
  66. func (tn *ListTableNames) Name() string {
  67. return "GetTableNames"
  68. }
  69. // ToProto converts the RPC into a protobuf message.
  70. func (tn *ListTableNames) ToProto() proto.Message {
  71. return &pb.GetTableNamesRequest{
  72. Regex: proto.String(tn.regex),
  73. IncludeSysTables: proto.Bool(tn.includeSysTables),
  74. Namespace: proto.String(tn.namespace),
  75. }
  76. }
  77. // NewResponse creates an empty protobuf message to read the response of this
  78. // RPC.
  79. func (tn *ListTableNames) NewResponse() proto.Message {
  80. return &pb.GetTableNamesResponse{}
  81. }