class Google::Cloud::Bigquery::Table::AsyncInserter::Batch

@private

Attributes

insert_ids[R]
json_rows[R]
max_bytes[R]
max_rows[R]
rows[R]

Public Class Methods

new(max_bytes: 10_000_000, max_rows: 500) click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 318
def initialize max_bytes: 10_000_000, max_rows: 500
  @max_bytes = max_bytes
  @max_rows = max_rows
  @rows = []
  @json_rows = []
  @insert_ids = []
  # The default request byte size overhead is 63.
  # "{\"rows\":[],\"ignoreUnknownValues\":false,
  # \"skipInvalidRows\":false}".bytesize #=> 63
  @current_bytes = 63
end

Public Instance Methods

insert(row, insert_id) click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 330
def insert row, insert_id
  insert_id ||= SecureRandom.uuid
  json_row = to_json_row row

  insert_rows_bytes row, json_row, insert_id, addl_bytes_for(json_row, insert_id)
end
ready?() click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 349
def ready?
  @current_bytes >= @max_bytes || rows.count >= @max_rows
end
try_insert(row, insert_id) click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 337
def try_insert row, insert_id
  insert_id ||= SecureRandom.uuid
  json_row = to_json_row row
  addl_bytes = addl_bytes_for json_row, insert_id

  return false if @current_bytes + addl_bytes >= @max_bytes
  return false if @rows.count + 1 >= @max_rows

  insert_rows_bytes row, json_row, insert_id, addl_bytes
  true
end

Private Instance Methods

addl_bytes_for(json_row, insert_id) click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 366
def addl_bytes_for json_row, insert_id
  if insert_id == :skip
    # "{\"json\":},".bytesize #=> 10
    10 + json_row.to_json.bytesize
  else
    # "{\"insertId\":\"\",\"json\":},".bytesize #=> 24
    24 + json_row.to_json.bytesize + insert_id.bytesize
  end
end
insert_rows_bytes(row, json_row, insert_id, addl_bytes) click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 355
def insert_rows_bytes row, json_row, insert_id, addl_bytes
  @rows << row
  @json_rows << json_row
  @insert_ids << insert_id if insert_id
  @current_bytes += addl_bytes
end
to_json_row(row) click to toggle source
# File lib/google/cloud/bigquery/table/async_inserter.rb, line 362
def to_json_row row
  Convert.to_json_row row
end