create.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2015 The GoHBase Authors. All rights reserved.
  2. // This file is part of GoHBase.
  3. // Use of this source code is governed by the Apache License 2.0
  4. // that can be found in the COPYING file.
  5. package hrpc
  6. import (
  7. "context"
  8. "github.com/golang/protobuf/proto"
  9. "github.com/tsuna/gohbase/pb"
  10. )
  11. // CreateTable represents a CreateTable HBase call
  12. type CreateTable struct {
  13. base
  14. families map[string]map[string]string
  15. splitKeys [][]byte
  16. }
  17. var defaultAttributes = map[string]string{
  18. "BLOOMFILTER": "ROW",
  19. "VERSIONS": "3",
  20. "IN_MEMORY": "false",
  21. "KEEP_DELETED_CELLS": "false",
  22. "DATA_BLOCK_ENCODING": "FAST_DIFF",
  23. "TTL": "2147483647",
  24. "COMPRESSION": "NONE",
  25. "MIN_VERSIONS": "0",
  26. "BLOCKCACHE": "true",
  27. "BLOCKSIZE": "65536",
  28. "REPLICATION_SCOPE": "0",
  29. }
  30. // NewCreateTable creates a new CreateTable request that will create the given
  31. // table in HBase. 'families' is a map of column family name to its attributes.
  32. // For use by the admin client.
  33. func NewCreateTable(ctx context.Context, table []byte,
  34. families map[string]map[string]string,
  35. options ...func(*CreateTable)) *CreateTable {
  36. ct := &CreateTable{
  37. base: base{
  38. table: table,
  39. ctx: ctx,
  40. resultch: make(chan RPCResult, 1),
  41. },
  42. families: make(map[string]map[string]string, len(families)),
  43. }
  44. for _, option := range options {
  45. option(ct)
  46. }
  47. for family, attrs := range families {
  48. ct.families[family] = make(map[string]string, len(defaultAttributes))
  49. for k, dv := range defaultAttributes {
  50. if v, ok := attrs[k]; ok {
  51. ct.families[family][k] = v
  52. } else {
  53. ct.families[family][k] = dv
  54. }
  55. }
  56. }
  57. return ct
  58. }
  59. // SplitKeys will return an option that will set the split keys for the created table
  60. func SplitKeys(sk [][]byte) func(*CreateTable) {
  61. return func(ct *CreateTable) {
  62. ct.splitKeys = sk
  63. }
  64. }
  65. // Name returns the name of this RPC call.
  66. func (ct *CreateTable) Name() string {
  67. return "CreateTable"
  68. }
  69. // ToProto converts the RPC into a protobuf message
  70. func (ct *CreateTable) ToProto() proto.Message {
  71. pbFamilies := make([]*pb.ColumnFamilySchema, 0, len(ct.families))
  72. for family, attrs := range ct.families {
  73. f := &pb.ColumnFamilySchema{
  74. Name: []byte(family),
  75. Attributes: make([]*pb.BytesBytesPair, 0, len(attrs)),
  76. }
  77. for k, v := range attrs {
  78. f.Attributes = append(f.Attributes, &pb.BytesBytesPair{
  79. First: []byte(k),
  80. Second: []byte(v),
  81. })
  82. }
  83. pbFamilies = append(pbFamilies, f)
  84. }
  85. return &pb.CreateTableRequest{
  86. TableSchema: &pb.TableSchema{
  87. TableName: &pb.TableName{
  88. // TODO: handle namespaces
  89. Namespace: []byte("default"),
  90. Qualifier: ct.table,
  91. },
  92. ColumnFamilies: pbFamilies,
  93. },
  94. SplitKeys: ct.splitKeys,
  95. }
  96. }
  97. // NewResponse creates an empty protobuf message to read the response of this
  98. // RPC.
  99. func (ct *CreateTable) NewResponse() proto.Message {
  100. return &pb.CreateTableResponse{}
  101. }