Skip to content

Commit

Permalink
fix docs and updated test cases, reformat service code
Browse files Browse the repository at this point in the history
  • Loading branch information
jiren committed Jan 23, 2020
1 parent af73fe2 commit 13eb5cd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 69 deletions.
2 changes: 1 addition & 1 deletion google-cloud-spanner/acceptance/spanner/client_test.rb
Expand Up @@ -29,7 +29,7 @@
client = spanner.client instance_id, database_id, enable_resource_based_routing: true
client.resource_based_routing_enabled?.must_equal true
instance = spanner.instance instance_id, fields: ["endpoint_uris"]
# currently instance not returning endpoint uris, set to default if no endpoint uri present.
# Set to default if no endpoint uri present.
host = instance.endpoint_uris.first || Google::Cloud::Spanner::V1::SpannerClient::SERVICE_ADDRESS
client.service.host.must_equal host
end
Expand Down
4 changes: 2 additions & 2 deletions google-cloud-spanner/lib/google/cloud/spanner/project.rb
Expand Up @@ -136,7 +136,7 @@ def instances token: nil, max: nil
#
# @param [String] instance_id The unique identifier for the instance.
# @param [Array<String>] fields Specifies the subset of fields that
# should be returned. dIf fields not provided then all fields will
# should be returned. If fields not provided then all fields will be
# returned. Optional.
# @return [Google::Cloud::Spanner::Instance, nil] The instance, or `nil`
# if the instance does not exist.
Expand All @@ -153,7 +153,7 @@ def instances token: nil, max: nil
# spanner = Google::Cloud::Spanner.new
# instance = spanner.instance "non-existing" # nil
#
# @example Get instance detail with specified fields.
# @example Get instance details with specified fields.
# require "google/cloud/spanner"
#
# spanner = Google::Cloud::Spanner.new
Expand Down
Expand Up @@ -53,14 +53,14 @@ def resource_based_routing_service
client_config: @project.service.client_config
rescue Google::Cloud::PermissionDeniedError
warn <<~WARN
Warning: The client library attempted to connect to an endpoint
The client library attempted to connect to an endpoint
closer to your Cloud Spanner data but was unable to do so.
The client library will fall back and route requests to the global
Spanner endpoint (spanner.googleapis.com), which may result in
The client library will fallback and route requests to the
endpoint given in the client options, which may result in
increased latency. We recommend including the scope
https://www.googleapis.com/auth/spanner.admin so that the client
library can get an instance-specific endpoint and efficiently route
requests.
https://www.googleapis.com/auth/spanner.admin so that
the client library can get an instance-specific endpoint
and efficiently route requests.
WARN
end
end
Expand Down
72 changes: 18 additions & 54 deletions google-cloud-spanner/lib/google/cloud/spanner/service.rb
Expand Up @@ -277,14 +277,11 @@ def create_session database_name, labels: nil
session = Google::Spanner::V1::Session.new labels: labels if labels
execute do
service.create_session database_name, session: session,
options: opts
options: opts
end
end

def batch_create_sessions \
database_name,
session_count,
labels: nil
def batch_create_sessions database_name, session_count, labels: nil
opts = default_options_from_session database_name
session = Google::Spanner::V1::Session.new labels: labels if labels
execute do
Expand All @@ -304,14 +301,9 @@ def delete_session session_name
end
end

def execute_streaming_sql \
session_name, sql,
transaction: nil,
params: nil,
types: nil,
resume_token: nil,
partition_token: nil,
seqno: nil
def execute_streaming_sql session_name, sql, transaction: nil,
params: nil, types: nil, resume_token: nil,
partition_token: nil, seqno: nil
opts = default_options_from_session session_name
execute do
service.execute_streaming_sql \
Expand All @@ -325,11 +317,7 @@ def execute_streaming_sql \
end
end

def execute_batch_dml \
session_name,
transaction,
statements,
seqno
def execute_batch_dml session_name, transaction, statements, seqno
opts = default_options_from_session session_name
statements = statements.map(&:to_grpc)
results = execute do
Expand All @@ -351,16 +339,9 @@ def execute_batch_dml \
end
end

def streaming_read_table \
session_name,
table_name,
columns,
keys: nil,
index: nil,
transaction: nil,
limit: nil,
resume_token: nil,
partition_token: nil
def streaming_read_table session_name, table_name, columns, keys: nil,
index: nil, transaction: nil, limit: nil,
resume_token: nil, partition_token: nil
opts = default_options_from_session session_name
execute do
service.streaming_read \
Expand All @@ -371,15 +352,9 @@ def streaming_read_table \
end
end

def partition_read \
session_name,
table_name,
columns,
transaction,
keys: nil,
index: nil,
partition_size_bytes: nil,
max_partitions: nil
def partition_read session_name, table_name, columns, transaction,
keys: nil, index: nil, partition_size_bytes: nil,
max_partitions: nil
partition_opts = partition_options partition_size_bytes,
max_partitions

Expand All @@ -393,14 +368,9 @@ def partition_read \
end
end

def partition_query \
session_name,
sql,
transaction,
params: nil,
types: nil,
partition_size_bytes: nil,
max_partitions: nil
def partition_query session_name, sql, transaction, params: nil,
types: nil, partition_size_bytes: nil,
max_partitions: nil
partition_opts = partition_options partition_size_bytes,
max_partitions

Expand All @@ -415,10 +385,7 @@ def partition_query \
end
end

def commit \
session_name,
mutations = [],
transaction_id: nil
def commit session_name, mutations = [], transaction_id: nil
tx_opts = nil
if transaction_id.nil?
tx_opts = Google::Spanner::V1::TransactionOptions.new(
Expand Down Expand Up @@ -452,11 +419,8 @@ def begin_transaction session_name
end
end

def create_snapshot \
session_name,
strong: nil,
timestamp: nil,
staleness: nil
def create_snapshot session_name, strong: nil, timestamp: nil,
staleness: nil
tx_opts = Google::Spanner::V1::TransactionOptions.new(
read_only: Google::Spanner::V1::TransactionOptions::ReadOnly.new(
{
Expand Down
Expand Up @@ -51,9 +51,9 @@
client.resource_based_routing_enabled?.must_equal false
end

it "set service host to instance endpoint url if resource based routing enabled" do
it "set service host to instance endpoint uri if resource based routing enabled" do
get_res = Google::Spanner::Admin::Instance::V1::Instance.new \
name: instance_path(instance_id), endpoint_uris: ["test.host.com"]
name: instance_path(instance_id), endpoint_uris: ["test1.host.com", "test2.host.com"]
mock = Minitest::Mock.new
mock.expect :get_instance, get_res, [
instance_path(instance_id),
Expand All @@ -65,17 +65,17 @@
client = Google::Cloud::Spanner::Client.new \
spanner, instance_id, database_id, enable_resource_based_routing: true
client.resource_based_routing_enabled?.must_equal true
client.service.host.must_equal "test.host.com"
client.service.host.must_equal "test1.host.com"
end

mock.verify
end

it "set service host with instance endpoint url if resource based routing enabled using an environment variable" do
it "set service host with instance endpoint uri if resource based routing enabled using an environment variable" do
ENV["GOOGLE_CLOUD_ENABLE_RESOURCE_BASED_ROUTING"] = "TRUE"

get_res = Google::Spanner::Admin::Instance::V1::Instance.new \
name: instance_path(instance_id), endpoint_uris: ["nam1-test.host.com"]
name: instance_path(instance_id), endpoint_uris: ["test1.host.com", "test2.host.com"]
mock = Minitest::Mock.new
mock.expect :get_instance, get_res, [
instance_path(instance_id),
Expand All @@ -87,7 +87,7 @@
client = Google::Cloud::Spanner::Client.new \
spanner, instance_id, database_id, enable_resource_based_routing: true
client.resource_based_routing_enabled?.must_equal true
client.service.host.must_equal "nam1-test.host.com"
client.service.host.must_equal "test1.host.com"
end

mock.verify
Expand Down

0 comments on commit 13eb5cd

Please sign in to comment.