Class: TableStoreClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tablestore/table_store_client.rb

Instance Method Summary collapse

Instance Method Details

#decode_batch_get_row(body) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/tablestore/table_store_client.rb', line 173

def decode_batch_get_row(body)
  proto = BatchGetRowResponse.new
  proto.parse_from_string(body)
  rows = []
  proto.tables.each do |table_item|
    rows << parse_get_row_item(table_item.rows)
  end
  rows
end

#decode_get_range_request(body) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tablestore/table_store_client.rb', line 126

def decode_get_range_request(body)
  proto = GetRangeResponse.new
  proto.parse_from_string(body)

  next_start_pk = nil
  row_list = []

  if proto.next_start_primary_key.length != 0
    inputStream = PlainBufferInputStream.new(proto.next_start_primary_key)
    codedInputStream = PlainBufferCodedInputStream.new(inputStream)
    next_start_pk, att = codedInputStream.read_row
  end

  if proto.rows.length != 0
    inputStream = PlainBufferInputStream.new(proto.rows)
    codedInputStream = PlainBufferCodedInputStream.new(inputStream)
    row_list = codedInputStream.read_rows
  end

  return next_start_pk, row_list
end

#decode_get_row(body) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tablestore/table_store_client.rb', line 148

def decode_get_row(body)
  proto = GetRowResponse.new
  proto.parse_from_string(body)

  return_row = nil
  if proto.row.length > 0
    inputStream = PlainBufferInputStream.new(proto.row)
    codedInputStream = PlainBufferCodedInputStream.new(inputStream)
    return_row = codedInputStream.read_row
  end
  return_row
end

#decode_list_table(body) ⇒ Object

Decode



119
120
121
122
123
124
# File 'lib/tablestore/table_store_client.rb', line 119

def decode_list_table(body)
  proto = ListTableResponse.new
  proto.parse_from_string(body)
  names = proto.table_names
  names
end

#decode_put_row(body) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/tablestore/table_store_client.rb', line 161

def decode_put_row(body)
  proto = PutRowResponse.new
  proto.parse_from_string(body)
  return_row = nil
  if proto.row.length != 0
    inputStream = PlainBufferInputStream.new(proto.row)
    codedInputStream = PlainBufferCodedInputStream.new(inputStream)
    return_row = codedInputStream.read_row
  end
  return_row
end

#encode_create_table(table_meta, table_options, reserved_throughput) ⇒ Object

Encode



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tablestore/table_store_client.rb', line 13

def encode_create_table(table_meta, table_options, reserved_throughput)
  proto = CreateTableRequest.new

  meta_proto = TableMeta.new
  meta_proto.table_name = table_meta.table_name
  meta_proto.table_name = table_meta.table_name
  table_meta.schema_of_primary_key.each do |primary_key|
    meta_proto.primary_key << make_schemas_with_list(primary_key)
  end
  proto.table_meta = meta_proto

  proto.table_options = make_table_options(table_options)

  rt_proto = ReservedThroughput.new
  rt_proto.capacity_unit = make_capacity_unit(reserved_throughput.capacity_unit)
  proto.reserved_throughput = rt_proto
  proto.serialize_to_string
end

#encode_delete_row(table_name, row, condition) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/tablestore/table_store_client.rb', line 108

def encode_delete_row(table_name, row, condition)
  proto = DeleteRowRequest.new
  proto.table_name = table_name
  condition = Condition.new(RowExistenceExpectation::IGNORE) if condition.nil?
  condition_proto = Condition.new
  proto.condition = make_condition(condition_proto, condition)
  proto.primary_key = serialize_for_delete_row(row.primary_key)
  proto.serialize_to_string
end

#encode_delete_table(table_name) ⇒ Object



51
52
53
54
55
# File 'lib/tablestore/table_store_client.rb', line 51

def encode_delete_table(table_name)
  proto = DeleteTableRequest.new
  proto.table_name = table_name
  proto.serialize_to_string
end

#encode_get_range_request(request) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tablestore/table_store_client.rb', line 57

def encode_get_range_request(request)
  proto = GetRangeRequest.new
  proto.table_name = request[:table_name]
  proto.direction = request[:direction]
  proto.inclusive_start_primary_key = serialize_primary_key(request[:inclusive_start_primary_key])
  proto.exclusive_end_primary_key = serialize_primary_key(request[:exclusive_end_primary_key])
  proto.max_versions = request[:max_version]
  proto.limit = request[:limit]
  if request[:column_filter]
    proto.filter = make_column_condition(request[:column_filter]).serialize_to_string
  end
  proto.serialize_to_string
end

#encode_get_row(table_name, primary_key, columns_to_get, column_filter, max_version) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tablestore/table_store_client.rb', line 81

def encode_get_row(table_name, primary_key, columns_to_get, column_filter, max_version)
  proto = GetRowRequest.new
  proto.table_name = table_name
  make_repeated_column_names(proto.columns_to_get, columns_to_get)
  if column_filter
    proto.filter = make_column_condition(column_filter).serialize_to_string
  end
  proto.primary_key = serialize_primary_key(primary_key)
  proto.max_versions = max_version if max_version
  proto.serialize_to_string
end

#encode_list_tableObject



32
33
34
35
# File 'lib/tablestore/table_store_client.rb', line 32

def encode_list_table
  proto = ListTableRequest.new
  proto.serialize_to_string
end

#encode_put_row(table_name, row, condition) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/tablestore/table_store_client.rb', line 71

def encode_put_row(table_name, row, condition)
  proto = PutRowRequest.new
  proto.table_name = table_name
  condition = Condition.new(RowExistenceExpectation::IGNORE) if condition.nil?
  contion_proto = Condition.new
  proto.condition = make_condition(contion_proto, condition)
  proto.row = serialize_for_put_row(row.primary_key, row.attribute_columns)
  proto.serialize_to_string
end

#encode_update_row(teble_name, row, condition) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tablestore/table_store_client.rb', line 93

def encode_update_row(teble_name, row, condition)
  proto = UpdateRowRequest.new
  proto.table_name = teble_name
  condition = Condition.new(RowExistenceExpectation::IGNORE) if condition.nil?
  condition_proto = Condition.new
  proto.condition = make_condition(condition_proto, condition)
  if return_type == ReturnType::RT_PK
    return_content = ReturnContent.new
    return_content.return_type = RT_PK
    proto.return_content = return_content
  end
  proto.row_change = serialize_for_update_row(row.primary_key, row.attribute_columns)
  proto.serialize_to_string
end

#encode_update_table(table_name, table_options, reserved_throughput) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tablestore/table_store_client.rb', line 37

def encode_update_table(table_name, table_options, reserved_throughput)
  proto = UpdateTableRequest.new
  proto.table_name = table_name
  if reserved_throughput
    rt_proto = ReservedThroughput.new
    rt_proto.capacity_unit = make_capacity_unit(reserved_throughput.capacity_unit)
    proto.reserved_throughput = rt_proto
  end
  if table_options
    proto.table_options = make_table_options(table_options)
  end
  proto.serialize_to_string
end

#make_batch_get_row(request) ⇒ Object

Make



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
# File 'lib/tablestore/table_store_client.rb', line 184

def make_batch_get_row(request)
  proto = BatchGetRowRequest.new
  request.items.each do |item|
    table_value = item[1]
    table_item = TableInBatchGetRowRequest.new
    table_item.table_name = table_value.table_name
    make_repeated_column_names(table_item.columns_to_get, table_value.columns_to_get)

    if table_value.column_filter
      table_item.filter = make_column_condition(table_value.column_filter).serialize_to_string
    end

    table_value.primary_keys.each do |pk|
      table_item.primary_key << serialize_primary_key(pk)
    end

    if table_value.max_version
        table_item.max_versions = table_value.max_version
    end
    if table_value.time_range
      if table_value.time_range.is_a?(Array)
        table_item.time_range.start_time = table_value.time_range[0]
        table_item.time_range.end_time = table_value.time_range[1]
      elsif table_value.is_a?(Fixnum)
        table_item.time_range.specific_time = table_value.time_range
      end
    end
    if table_value.start_column
      table_item.start_column = table_value.start_column
    end
    if table_value.end_column
        table_item.end_column = table_value.end_column
    end
    proto.tables << table_item
  end
  proto.serialize_to_string
end

#make_batch_write_row(request) ⇒ Object



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
# File 'lib/tablestore/table_store_client.rb', line 222

def make_batch_write_row(request)
  proto = BatchWriteRowRequest.new
  request.items.each do |item|
    table_value = item[1]
    table_item = TableInBatchWriteRowRequest.new
    table_item.table_name = table_value.table_name

    table_value.row_items.each do |row_item|
      if row_item.type == Metadata::BatchWriteRowType::PUT
        row = RowInBatchWriteRowRequest.new
        table_item.rows << make_put_row_item(row, row_item)
      end
      if row_item.type == Metadata::BatchWriteRowType::UPDATE
        row = RowInBatchWriteRowRequest.new
        table_item.rows << make_update_row_item(row, row_item)
      end
      if row_item.type == Metadata::BatchWriteRowType::DELETE
        row = RowInBatchWriteRowRequest.new
        table_item.rows << make_delete_row_item(row, row_item)
      end
    end
    proto.tables << table_item
  end
  proto.serialize_to_string
end

#make_capacity_unit(capacity_unit) ⇒ Object



401
402
403
404
405
406
# File 'lib/tablestore/table_store_client.rb', line 401

def make_capacity_unit(capacity_unit)
  proto = CapacityUnit.new
  proto.read = capacity_unit.read if capacity_unit.read
  proto.write = capacity_unit.write if capacity_unit.write
  proto
end

#make_column_condition(column_condition) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/tablestore/table_store_client.rb', line 327

def make_column_condition(column_condition)
  return if column_condition.nil?
  proto = Filter.new
  proto.type = column_condition.get_type

  # condition
  if column_condition.is_a?(Metadata::CompositeColumnCondition)
    proto.filter = make_composite_condition(column_condition)
  elsif column_condition.is_a?(Metadata::SingleColumnCondition)
    proto.filter = make_relation_condition(column_condition)
  else
    raise TableStoreClientError.new("expect CompositeColumnCondition, SingleColumnCondition but not #{column_condition.class}")
  end
  proto
end

#make_composite_condition(condition) ⇒ Object



343
344
345
346
347
348
349
350
351
352
# File 'lib/tablestore/table_store_client.rb', line 343

def make_composite_condition(condition)
  proto = CompositeColumnValueFilter.new
  proto.combinator = condition.get_combinator

  condition.sub_conditions.each do |sub|
    proto.sub_filters << make_column_condition(sub)
  end

  proto.serialize_to_string
end

#make_condition(proto, condition) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
# File 'lib/tablestore/table_store_client.rb', line 315

def make_condition(proto, condition)
  raise TableStoreClientError.new("condition should be an instance of Condition, not #{condition.class}") unless condition.is_a?(Metadata::Condition)
  expectation_str = condition.row_existence_expectation
  proto.row_existence = expectation_str
  raise TableStoreClientError.new("row_existence_expectation should be one of [#{join(', ')}], not #{expectation_str}") if proto.row_existence.nil?

  if condition.column_condition
    proto.column_condition = make_column_condition(condition.column_condition).serialize_to_string
  end
  proto
end

#make_delete_row_item(proto, delete_row_item) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/tablestore/table_store_client.rb', line 285

def make_delete_row_item(proto, delete_row_item)
  condition = delete_row_item.condition
  if condition.nil?
    condition = Metadata::Condition.new(RowExistenceExpectation::IGNORE)
  end
  condition_proto = Condition.new
  proto.condition = make_condition(condition_proto, condition)

  if delete_row_item.return_type == ReturnType::RT_PK
    return_content = ReturnContent.new
    return_content.return_type = RT_PK
    proto.return_content = return_content
  end

  proto.row_change = serialize_for_delete_row(delete_row_item.row.primary_key)
  proto.type = DELETE
  proto

end

#make_put_row_item(proto, put_row_item) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/tablestore/table_store_client.rb', line 248

def make_put_row_item(proto, put_row_item)
  condition = put_row_item.condition
  if condition.nil?
    condition = Condition.new(Metadata::RowExistenceExpectation::IGNORE)
  end
  condition_proto = Condition.new
  proto.condition = make_condition(condition_proto, condition)
  if put_row_item.return_type == ReturnType::RT_PK
    return_content = ReturnContent.new
    return_content.return_type = RT_PK
    proto.return_content = return_content
  end

  proto.row_change = serialize_for_put_row(put_row_item.row.primary_key, put_row_item.row.attribute_columns)
  proto.type = PUT
  proto
end

#make_relation_condition(condition) ⇒ Object



354
355
356
357
358
359
360
361
362
363
# File 'lib/tablestore/table_store_client.rb', line 354

def make_relation_condition(condition)
  proto = SingleColumnValueFilter.new
  proto.comparator = condition.get_comparator

  proto.column_name = condition.get_column_name
  proto.column_value = serialize_column_value(condition.get_column_value)
  proto.filter_if_missing = !condition.pass_if_missing
  proto.latest_version_only = condition.latest_version_only
  proto.serialize_to_string
end

#make_repeated_column_names(proto, columns_to_get) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'lib/tablestore/table_store_client.rb', line 305

def make_repeated_column_names(proto, columns_to_get)
  if columns_to_get.nil?
    return
  end

  columns_to_get.each do |column_name|
    proto << column_name
  end
end

#make_schemas_with_list(schema) ⇒ Object



365
366
367
368
369
370
371
372
373
# File 'lib/tablestore/table_store_client.rb', line 365

def make_schemas_with_list(schema)
  schema_proto = PrimaryKeySchema.new
  schema_proto.name = schema[0]
  schema_proto.type = schema[1]
  if schema.size == 3
    schema_proto.option = 1
  end
  schema_proto
end

#make_table_options(options) ⇒ Object



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
# File 'lib/tablestore/table_store_client.rb', line 375

def make_table_options(options)
  option_proto = TableOptions.new
  unless options.is_a?(Metadata::TableOptions)
    raise TableStoreClientError.new("table_option should be an instance of Meta::TableOptions, not #{options.class}" )
  end
  if options.time_to_live
    unless options.time_to_live.is_a?(Fixnum)
      raise TableStoreClientError("time_to_live should be an instance of int, not #{options.time_to_live.class}")
    end
    option_proto.time_to_live = options.time_to_live
  end
  if options.max_version
    unless options.max_version.is_a?(Fixnum)
      raise TableStoreClientError("max_version should be an instance of int, not #{options.max_version.class}")
    end
    option_proto.max_versions = options.max_version
  end
  if options.max_time_deviation
    unless options.max_time_deviation.is_a?(Fixnum)
      raise TableStoreClientError("max_time_deviation should be an instance of int, not #{options.max_version.class}")
    end
    option_proto.deviation_cell_version_in_sec = options.max_time_deviation
  end
  option_proto
end

#make_update_row_item(proto, update_row_item) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/tablestore/table_store_client.rb', line 266

def make_update_row_item(proto, update_row_item)
  condition = update_row_item.condition
  if condition.nil?
    condition = Condition.new(RowExistenceExpectation::IGNORE)
  end
  condition_proto = Condition.new
  proto.condition = make_condition(condition_proto, condition)

  if update_row_item.return_type == ReturnType::RT_PK
    return_content = ReturnContent.new
    return_content.return_type = RT_PK
    proto.return_content = return_content
  end
  update_row_item.row.attribute_columns
  proto.row_change = serialize_for_update_row(update_row_item.row.primary_key, update_row_item.row.attribute_columns)
  proto.type = UPDATE
  proto
end

#parse_batch_write_row(proto) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/tablestore/table_store_client.rb', line 440

def parse_batch_write_row(proto)
  result_list = {}
  proto.each do |table_item|
    table_name = table_item.table_name
    result_list[table_name] = []

    table_item.rows.each do |row_item|
      row = parse_write_row_item(row_item)
      result_list[table_name] << row
    end
  end

  result_list
end

#parse_get_row_item(proto) ⇒ Object

Parse



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
# File 'lib/tablestore/table_store_client.rb', line 409

def parse_get_row_item(proto)
  row_list = []
  proto.each do |row_item|
    primary_key_columns = nil
    attribute_columns = nil

    if row_item.is_ok
      # error_code = nil
      # error_message = nil
      # capacity_unit = parse_capacity_unit(row_item.consumed.capacity_unit)

      if row_item.row.length != 0
        inputStream = PlainBufferInputStream.new(row_item.row)
        codedInputStream = PlainBufferCodedInputStream.new(inputStream)
        primary_key_columns, attribute_columns = codedInputStream.read_row
      end
    else
      # error_code = row_item.error.code
      # error_message = row_item.error.HasField('message') ? row_item.error.message : ''
      # if row_item.HasField('consumed')
      #   capacity_unit = parse_capacity_unit(row_item.consumed.capacity_unit)
      # else
      #   capacity_unit = nil
      # end
    end

    row_list << {pk: primary_key_columns, attr: attribute_columns} if primary_key_columns
  end
  row_list
end

#parse_write_row_item(row_item) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/tablestore/table_store_client.rb', line 455

def parse_write_row_item(row_item)
  primary_key_columns = nil

  if row_item.is_ok
    error_code = nil
    error_message = nil

    if row_item.row.length != 0
      inputStream = PlainBufferInputStream.new(row_item.row)
      codedInputStream = PlainBufferCodedInputStream.new(inputStream)
      primary_key_columns, attribute_columns = codedInputStream.read_row
    end
  end
  primary_key_columns

  #BatchWriteRowResponseItem.new(row_item.is_ok, error_code, error_message, consumed, primary_key_columns)
end