/
mutate.go
569 lines (507 loc) · 15.7 KB
/
mutate.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// 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"
"encoding/binary"
"errors"
"math"
"time"
"github.com/tsuna/gohbase/pb"
"google.golang.org/protobuf/proto"
)
var attributeNameTTL = "_ttl"
// DurabilityType is used to set durability for Durability option
type DurabilityType int32
const (
// UseDefault is USER_DEFAULT
UseDefault DurabilityType = iota
// SkipWal is SKIP_WAL
SkipWal
// AsyncWal is ASYNC_WAL
AsyncWal
// SyncWal is SYNC_WAL
SyncWal
// FsyncWal is FSYNC_WAL
FsyncWal
)
const (
putType = 4
deleteType = 8
deleteFamilyVersionType = 10
deleteColumnType = 12
deleteFamilyType = 14
)
var emptyQualifier = map[string][]byte{"": nil}
// Mutate represents a mutation on HBase.
type Mutate struct {
base
mutationType pb.MutationProto_MutationType //*int32
// values is a map of column families to a map of column qualifiers to bytes
values map[string]map[string][]byte
ttl []byte
timestamp uint64
durability DurabilityType
deleteOneVersion bool
skipbatch bool
}
// TTL sets a time-to-live for mutation queries.
// The value will be in millisecond resolution.
func TTL(t time.Duration) func(Call) error {
return func(o Call) error {
m, ok := o.(*Mutate)
if !ok {
return errors.New("'TTL' option can only be used with mutation queries")
}
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(t.Nanoseconds()/1e6))
m.ttl = buf
return nil
}
}
// Timestamp sets timestamp for mutation queries.
// The time object passed will be rounded to a millisecond resolution, as by default,
// if no timestamp is provided, HBase sets it to current time in milliseconds.
// In order to have custom time precision, use TimestampUint64 call option for
// mutation requests and corresponding TimeRangeUint64 for retrieval requests.
func Timestamp(ts time.Time) func(Call) error {
return func(o Call) error {
m, ok := o.(*Mutate)
if !ok {
return errors.New("'Timestamp' option can only be used with mutation queries")
}
m.timestamp = uint64(ts.UnixNano() / 1e6)
return nil
}
}
// TimestampUint64 sets timestamp for mutation queries.
func TimestampUint64(ts uint64) func(Call) error {
return func(o Call) error {
m, ok := o.(*Mutate)
if !ok {
return errors.New("'TimestampUint64' option can only be used with mutation queries")
}
m.timestamp = ts
return nil
}
}
// Durability sets durability for mutation queries.
func Durability(d DurabilityType) func(Call) error {
return func(o Call) error {
m, ok := o.(*Mutate)
if !ok {
return errors.New("'Durability' option can only be used with mutation queries")
}
if d < UseDefault || d > FsyncWal {
return errors.New("invalid durability value")
}
m.durability = d
return nil
}
}
// DeleteOneVersion is a delete option that can be passed in order to delete only
// one latest version of the specified qualifiers. Without timestamp specified,
// it will have no effect for delete specific column families request.
// If a Timestamp option is passed along, only the version at that timestamp will be removed
// for delete specific column families and/or qualifier request.
// This option cannot be used for delete entire row request.
func DeleteOneVersion() func(Call) error {
return func(o Call) error {
m, ok := o.(*Mutate)
if !ok {
return errors.New("'DeleteOneVersion' option can only be used with mutation queries")
}
m.deleteOneVersion = true
return nil
}
}
// baseMutate returns a Mutate struct without the mutationType filled in.
func baseMutate(ctx context.Context, table, key []byte, values map[string]map[string][]byte,
options ...func(Call) error) (*Mutate, error) {
m := &Mutate{
base: base{
table: table,
key: key,
ctx: ctx,
resultch: make(chan RPCResult, 1),
},
values: values,
timestamp: MaxTimestamp,
}
err := applyOptions(m, options...)
if err != nil {
return nil, err
}
return m, nil
}
// NewPut creates a new Mutation request to insert the given
// family-column-values in the given row key of the given table.
func NewPut(ctx context.Context, table, key []byte,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
m, err := baseMutate(ctx, table, key, values, options...)
if err != nil {
return nil, err
}
m.mutationType = pb.MutationProto_PUT
return m, nil
}
// NewPutStr is just like NewPut but takes table and key as strings.
func NewPutStr(ctx context.Context, table, key string,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
return NewPut(ctx, []byte(table), []byte(key), values, options...)
}
// NewDel is used to perform Delete operations on a single row.
// To delete entire row, values should be nil.
//
// To delete specific families, qualifiers map should be nil:
//
// map[string]map[string][]byte{
// "cf1": nil,
// "cf2": nil,
// }
//
// To delete specific qualifiers:
//
// map[string]map[string][]byte{
// "cf": map[string][]byte{
// "q1": nil,
// "q2": nil,
// },
// }
//
// To delete all versions before and at a timestamp, pass hrpc.Timestamp() option.
// By default all versions will be removed.
//
// To delete only a specific version at a timestamp, pass hrpc.DeleteOneVersion() option
// along with a timestamp. For delete specific qualifiers request, if timestamp is not
// passed, only the latest version will be removed. For delete specific families request,
// the timestamp should be passed or it will have no effect as it's an expensive
// operation to perform.
func NewDel(ctx context.Context, table, key []byte,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
m, err := baseMutate(ctx, table, key, values, options...)
if err != nil {
return nil, err
}
if len(m.values) == 0 && m.deleteOneVersion {
return nil, errors.New(
"'DeleteOneVersion' option cannot be specified for delete entire row request")
}
m.mutationType = pb.MutationProto_DELETE
return m, nil
}
// NewDelStr is just like NewDel but takes table and key as strings.
func NewDelStr(ctx context.Context, table, key string,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
return NewDel(ctx, []byte(table), []byte(key), values, options...)
}
// NewApp creates a new Mutation request to append the given
// family-column-values into the existing cells in HBase (or create them if
// needed), in given row key of the given table.
func NewApp(ctx context.Context, table, key []byte,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
m, err := baseMutate(ctx, table, key, values, options...)
if err != nil {
return nil, err
}
m.mutationType = pb.MutationProto_APPEND
return m, nil
}
// NewAppStr is just like NewApp but takes table and key as strings.
func NewAppStr(ctx context.Context, table, key string,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
return NewApp(ctx, []byte(table), []byte(key), values, options...)
}
// NewIncSingle creates a new Mutation request that will increment the given value
// by amount in HBase under the given table, key, family and qualifier.
func NewIncSingle(ctx context.Context, table, key []byte, family, qualifier string,
amount int64, options ...func(Call) error) (*Mutate, error) {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(amount))
value := map[string]map[string][]byte{family: map[string][]byte{qualifier: buf}}
return NewInc(ctx, table, key, value, options...)
}
// NewIncStrSingle is just like NewIncSingle but takes table and key as strings.
func NewIncStrSingle(ctx context.Context, table, key, family, qualifier string,
amount int64, options ...func(Call) error) (*Mutate, error) {
return NewIncSingle(ctx, []byte(table), []byte(key), family, qualifier, amount, options...)
}
// NewInc creates a new Mutation request that will increment the given values
// in HBase under the given table and key.
func NewInc(ctx context.Context, table, key []byte,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
m, err := baseMutate(ctx, table, key, values, options...)
if err != nil {
return nil, err
}
m.mutationType = pb.MutationProto_INCREMENT
return m, nil
}
// NewIncStr is just like NewInc but takes table and key as strings.
func NewIncStr(ctx context.Context, table, key string,
values map[string]map[string][]byte, options ...func(Call) error) (*Mutate, error) {
return NewInc(ctx, []byte(table), []byte(key), values, options...)
}
// Name returns the name of this RPC call.
func (m *Mutate) Name() string {
return "Mutate"
}
// Description returns the string of type of mutation being performed
func (m *Mutate) Description() string {
return pb.MutationProto_MutationType_name[int32(m.mutationType)]
}
// SkipBatch returns true if the Mutate request shouldn't be batched,
// but should be sent to Region Server right away.
func (m *Mutate) SkipBatch() bool {
return m.skipbatch
}
// Values returns the internal values object
// which should be treated as read-only.
// This would typically be used for calculations
// related to metrics and workloads
func (m *Mutate) Values() map[string]map[string][]byte {
return m.values
}
func (m *Mutate) setSkipBatch(v bool) {
m.skipbatch = v
}
var (
MutationProtoDeleteFamilyVersion = pb.MutationProto_DELETE_FAMILY_VERSION.Enum()
MutationProtoDeleteFamily = pb.MutationProto_DELETE_FAMILY.Enum()
MutationProtoDeleteOneVersion = pb.MutationProto_DELETE_ONE_VERSION.Enum()
MutationProtoDeleteMultipleVersions = pb.MutationProto_DELETE_MULTIPLE_VERSIONS.Enum()
)
func (m *Mutate) valuesToProto(ts *uint64) []*pb.MutationProto_ColumnValue {
cvs := make([]*pb.MutationProto_ColumnValue, len(m.values))
i := 0
for k, v := range m.values {
// And likewise, each item in each column needs to be converted to a
// protobuf QualifierValue
// if it's a delete, figure out the type
var dt *pb.MutationProto_DeleteType
if m.mutationType == pb.MutationProto_DELETE {
if len(v) == 0 {
// delete the whole column family
if m.deleteOneVersion {
dt = MutationProtoDeleteFamilyVersion
} else {
dt = MutationProtoDeleteFamily
}
// add empty qualifier
if v == nil {
v = emptyQualifier
}
} else {
// delete specific qualifiers
if m.deleteOneVersion {
dt = MutationProtoDeleteOneVersion
} else {
dt = MutationProtoDeleteMultipleVersions
}
}
}
qvs := make([]*pb.MutationProto_ColumnValue_QualifierValue, len(v))
j := 0
for k1, v1 := range v {
qvs[j] = &pb.MutationProto_ColumnValue_QualifierValue{
Qualifier: []byte(k1),
Value: v1,
Timestamp: ts,
DeleteType: dt,
}
j++
}
cvs[i] = &pb.MutationProto_ColumnValue{
Family: []byte(k),
QualifierValue: qvs,
}
i++
}
return cvs
}
func cellblockLen(rowLen, familyLen, qualifierLen, valueLen int) int {
keyLength := 2 + rowLen + 1 + familyLen + qualifierLen + 8 + 1
keyValueLength := 4 + 4 + keyLength + valueLen
return 4 + keyValueLength
}
func appendCellblock(row []byte, family, qualifier string, value []byte, ts uint64, typ byte,
cbs []byte) []byte {
// cellblock layout:
//
// Header:
// 4 byte length of key + value
// 4 byte length of key
// 4 byte length of value
//
// Key:
// 2 byte length of row
// <row>
// 1 byte length of row family
// <family>
// <qualifier>
// 8 byte timestamp
// 1 byte type
//
// Value:
// <value>
keylength := 2 + len(row) + 1 + len(family) + len(qualifier) + 8 + 1
valuelength := len(value)
keyvaluelength := 4 + 4 + keylength + valuelength
i := len(cbs)
cbs = append(cbs, make([]byte,
cellblockLen(len(row), len(family), len(qualifier), len(value)))...)
// Header:
binary.BigEndian.PutUint32(cbs[i:], uint32(keyvaluelength))
i += 4
binary.BigEndian.PutUint32(cbs[i:], uint32(keylength))
i += 4
binary.BigEndian.PutUint32(cbs[i:], uint32(valuelength))
i += 4
// Key:
binary.BigEndian.PutUint16(cbs[i:], uint16(len(row)))
i += 2
i += copy(cbs[i:], row)
cbs[i] = byte(len(family))
i++
i += copy(cbs[i:], family)
i += copy(cbs[i:], qualifier)
binary.BigEndian.PutUint64(cbs[i:], ts)
i += 8
cbs[i] = typ
i++
// Value:
copy(cbs[i:], value)
return cbs
}
func (m *Mutate) valuesToCellblocks() ([]byte, int32, uint32) {
if len(m.values) == 0 {
return nil, 0, 0
}
var cbsLen int
var count int
for family, v := range m.values {
if v == nil {
v = emptyQualifier
}
count += len(v)
for k1, v1 := range v {
cbsLen += cellblockLen(len(m.key), len(family), len(k1), len(v1))
}
}
cbs := make([]byte, 0, cbsLen)
var ts uint64
if m.timestamp == MaxTimestamp {
ts = math.MaxInt64 // Java's Long.MAX_VALUE use for HBase's LATEST_TIMESTAMP
} else {
ts = m.timestamp
}
for family, v := range m.values {
// figure out mutation type
var mt byte
if m.mutationType == pb.MutationProto_DELETE {
if len(v) == 0 {
// delete the whole column family
if m.deleteOneVersion {
mt = deleteFamilyVersionType
} else {
mt = deleteFamilyType
}
// add empty qualifier
if v == nil {
v = emptyQualifier
}
} else {
// delete specific qualifiers
if m.deleteOneVersion {
mt = deleteType
} else {
mt = deleteColumnType
}
}
} else {
mt = putType
}
for k1, v1 := range v {
cbs = appendCellblock(m.key, family, k1, v1, ts, mt, cbs)
}
}
if len(cbs) != cbsLen {
panic("cellblocks len mismatch")
}
return cbs, int32(count), uint32(len(cbs))
}
var durabilities = []*pb.MutationProto_Durability{
pb.MutationProto_Durability(UseDefault).Enum(),
pb.MutationProto_Durability(SkipWal).Enum(),
pb.MutationProto_Durability(AsyncWal).Enum(),
pb.MutationProto_Durability(SyncWal).Enum(),
pb.MutationProto_Durability(FsyncWal).Enum(),
}
func (m *Mutate) toProto(isCellblocks bool, cbs [][]byte) (*pb.MutateRequest, [][]byte, uint32) {
var ts *uint64
if m.timestamp != MaxTimestamp {
ts = &m.timestamp
}
var size uint32
mProto := &pb.MutationProto{
Row: m.key,
MutateType: &m.mutationType,
Durability: durabilities[m.durability],
Timestamp: ts,
}
if isCellblocks {
// if cellblocks we only add associated cell count as the actual
// data will be sent after protobuf
cellblocks, count, sz := m.valuesToCellblocks()
mProto.AssociatedCellCount = &count
size = sz
if size > 0 {
cbs = append(cbs, cellblocks)
}
} else {
// otherwise, convert the values to protobuf
mProto.ColumnValue = m.valuesToProto(ts)
}
if len(m.ttl) > 0 {
mProto.Attribute = append(mProto.Attribute, &pb.NameBytesPair{
Name: &attributeNameTTL,
Value: m.ttl,
})
}
return &pb.MutateRequest{
Region: m.regionSpecifier(),
Mutation: mProto,
}, cbs, size
}
// ToProto converts this mutate RPC into a protobuf message
func (m *Mutate) ToProto() proto.Message {
p, _, _ := m.toProto(false, nil)
return p
}
// NewResponse creates an empty protobuf message to read the response of this RPC.
func (m *Mutate) NewResponse() proto.Message {
return &pb.MutateResponse{}
}
// DeserializeCellBlocks deserializes mutate result from cell blocks
func (m *Mutate) DeserializeCellBlocks(pm proto.Message, b []byte) (uint32, error) {
resp := pm.(*pb.MutateResponse)
if resp.Result == nil {
// TODO: is this possible?
return 0, nil
}
cells, read, err := deserializeCellBlocks(b, uint32(resp.Result.GetAssociatedCellCount()))
if err != nil {
return 0, err
}
resp.Result.Cell = append(resp.Result.Cell, cells...)
return read, nil
}
func (m *Mutate) SerializeCellBlocks(cbs [][]byte) (proto.Message, [][]byte, uint32) {
return m.toProto(true, cbs)
}
func (m *Mutate) CellBlocksEnabled() bool {
// TODO: maybe have some global client option
return true
}