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