Skip to content

Commit

Permalink
Add streaming buffer attributes to Table
Browse files Browse the repository at this point in the history
[refs #1631]
  • Loading branch information
quartzmo authored and blowmage committed Oct 6, 2017
1 parent 3ae70ef commit aa81319
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions google-cloud-bigquery/acceptance/bigquery/table_test.rb
Expand Up @@ -85,6 +85,12 @@
fresh.time_partitioning_type.must_be_nil
fresh.time_partitioning_expiration.must_be_nil
#fresh.location.must_equal "US" TODO why nil? Set in dataset

# streaming buffer is transient, it seems it may or may not be present?
fresh.buffer_bytes.must_be_kind_of Integer if fresh.buffer_bytes
fresh.buffer_rows.must_be_kind_of Integer if fresh.buffer_rows
fresh.buffer_oldest_at.must_be_kind_of Time if fresh.buffer_oldest_at

fresh.schema.must_be_kind_of Google::Cloud::Bigquery::Schema
fresh.schema.wont_be :empty?
[:id, :breed, :name, :dob].each { |k| fresh.headers.must_include k }
Expand Down
44 changes: 44 additions & 0 deletions google-cloud-bigquery/lib/google/cloud/bigquery/table.rb
Expand Up @@ -479,6 +479,50 @@ def headers
schema.headers
end

##
# A lower-bound estimate of the number of bytes currently in this
# table's streaming buffer, if one is present. This field will be absent
# if the table is not being streamed to or if there is no data in the
# streaming buffer.
#
# @!group Attributes
#
def buffer_bytes
ensure_full_data!
@gapi.streaming_buffer.estimated_bytes if @gapi.streaming_buffer
end

##
# A lower-bound estimate of the number of rows currently in this
# table's streaming buffer, if one is present. This field will be absent
# if the table is not being streamed to or if there is no data in the
# streaming buffer.
#
# @!group Attributes
#
def buffer_rows
ensure_full_data!
@gapi.streaming_buffer.estimated_rows if @gapi.streaming_buffer
end

##
# The time of the oldest entry currently in this table's streaming
# buffer, if one is present. This field will be absent if the table is
# not being streamed to or if there is no data in the streaming buffer.
#
# @!group Attributes
#
def buffer_oldest_at
ensure_full_data!
return nil unless @gapi.streaming_buffer
oldest_entry_time = @gapi.streaming_buffer.oldest_entry_time
begin
::Time.at(Integer(oldest_entry_time) / 1000.0)
rescue
nil
end
end

##
# Retrieves data from the table.
#
Expand Down
Expand Up @@ -125,5 +125,7 @@ def self.attr_test attr, val
attr_test :bytes_count, 1000
attr_test :rows_count, 100
attr_test :location, "US"
attr_test :buffer_bytes, 2000
attr_test :buffer_rows, 200

end
Expand Up @@ -80,6 +80,12 @@
table.headers.must_equal [:name, :age, :score, :active, :avatar, :started_at, :duration, :target_end, :birthday]
end

it "knows its streaming buffer attributes" do
table.buffer_bytes.must_equal 2000
table.buffer_rows.must_equal 200
table.buffer_oldest_at.must_be_close_to ::Time.now, 1
end

it "can delete itself" do
mock = Minitest::Mock.new
mock.expect :delete_table, nil,
Expand Down
7 changes: 6 additions & 1 deletion google-cloud-bigquery/test/helper.rb
Expand Up @@ -229,7 +229,12 @@ def random_table_hash dataset, id = nil, name = nil, description = nil, project_
"expirationTime" => time_millis,
"lastModifiedTime" => time_millis,
"type" => "TABLE",
"location" => "US"
"location" => "US",
"streamingBuffer" => {
"estimatedBytes" => "2000", # String per google/google-api-ruby-client
"estimatedRows" => "200", # String per google/google-api-ruby-client
"oldestEntryTime" => time_millis
}
}
end

Expand Down

0 comments on commit aa81319

Please sign in to comment.