diff --git a/google-cloud-spanner/.rubocop.yml b/google-cloud-spanner/.rubocop.yml index 71b696e..36df750 100644 --- a/google-cloud-spanner/.rubocop.yml +++ b/google-cloud-spanner/.rubocop.yml @@ -16,6 +16,7 @@ Documentation: Metrics/BlockLength: Exclude: - "acceptance/**/*" + - ".toys.rb" Metrics/MethodLength: Exclude: - "acceptance/**/*" diff --git a/google-cloud-spanner/.toys.rb b/google-cloud-spanner/.toys.rb new file mode 100644 index 0000000..62d936b --- /dev/null +++ b/google-cloud-spanner/.toys.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +toys_version! ">= 0.15.6" + +if ENV["RUBY_COMMON_TOOLS"] + common_tools_dir = File.expand_path ENV["RUBY_COMMON_TOOLS"] + load File.join(common_tools_dir, "toys", "gapic") +else + load_git remote: "https://github.com/googleapis/ruby-common-tools.git", + path: "toys/gapic", + update: true +end + +tool "benchmark" do + tool "gen-protos" do + include :exec, e: true + include :gems + include :git_cache + + def run + setup + generate_spanner_bench + end + + def setup + gem "grpc-tools", "~> 1.65" + @googleapis_dir = git_cache.get "https://github.com/googleapis/googleapis.git", update: true + Dir.chdir context_directory + end + + def generate_spanner_bench + Dir.chdir "benchmark/benchwrapper" do + cmd = [ + "grpc_tools_ruby_protoc", + "--ruby_out", ".", + "--grpc_out", ".", + "-I", ".", + "-I", @googleapis_dir, + "spanner_bench/spanner.proto" + ] + exec cmd + end + end + end +end diff --git a/google-cloud-spanner/benchmark/benchwrapper/benchwrapper.rb b/google-cloud-spanner/benchmark/benchwrapper/benchwrapper.rb index b3c701e..9a627af 100755 --- a/google-cloud-spanner/benchmark/benchwrapper/benchwrapper.rb +++ b/google-cloud-spanner/benchmark/benchwrapper/benchwrapper.rb @@ -20,7 +20,7 @@ require 'optparse' require 'grpc' -require 'spanner_services_pb' +require 'spanner_bench/spanner_services_pb' require "google/cloud/spanner" include SpannerBench diff --git a/google-cloud-spanner/benchmark/benchwrapper/spanner.proto b/google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner.proto similarity index 100% rename from google-cloud-spanner/benchmark/benchwrapper/spanner.proto rename to google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner.proto diff --git a/google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner_pb.rb b/google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner_pb.rb new file mode 100644 index 0000000..abb33ac --- /dev/null +++ b/google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner_pb.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: spanner_bench/spanner.proto + +require 'google/protobuf' + + +descriptor_data = "\n\x1bspanner_bench/spanner.proto\x12\rspanner_bench\"P\n\x06Singer\x12\n\n\x02id\x18\x01 \x01(\x03\x12\x12\n\nfirst_name\x18\x02 \x01(\t\x12\x11\n\tlast_name\x18\x03 \x01(\t\x12\x13\n\x0bsinger_info\x18\x04 \x01(\t\";\n\x05\x41lbum\x12\n\n\x02id\x18\x01 \x01(\x03\x12\x11\n\tsinger_id\x18\x02 \x01(\x03\x12\x13\n\x0b\x61lbum_title\x18\x03 \x01(\t\"\x1a\n\tReadQuery\x12\r\n\x05query\x18\x01 \x01(\t\"[\n\x0bInsertQuery\x12&\n\x07singers\x18\x01 \x03(\x0b\x32\x15.spanner_bench.Singer\x12$\n\x06\x61lbums\x18\x02 \x03(\x0b\x32\x14.spanner_bench.Album\"\x1e\n\x0bUpdateQuery\x12\x0f\n\x07queries\x18\x01 \x03(\t\"\x0f\n\rEmptyResponse2\xe3\x01\n\x13SpannerBenchWrapper\x12@\n\x04Read\x12\x18.spanner_bench.ReadQuery\x1a\x1c.spanner_bench.EmptyResponse\"\x00\x12\x44\n\x06Insert\x12\x1a.spanner_bench.InsertQuery\x1a\x1c.spanner_bench.EmptyResponse\"\x00\x12\x44\n\x06Update\x12\x1a.spanner_bench.UpdateQuery\x1a\x1c.spanner_bench.EmptyResponse\"\x00\x62\x06proto3" + +pool = Google::Protobuf::DescriptorPool.generated_pool +pool.add_serialized_file(descriptor_data) + +module SpannerBench + Singer = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.Singer").msgclass + Album = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.Album").msgclass + ReadQuery = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.ReadQuery").msgclass + InsertQuery = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.InsertQuery").msgclass + UpdateQuery = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.UpdateQuery").msgclass + EmptyResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.EmptyResponse").msgclass +end diff --git a/google-cloud-spanner/benchmark/benchwrapper/spanner_services_pb.rb b/google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner_services_pb.rb similarity index 95% rename from google-cloud-spanner/benchmark/benchwrapper/spanner_services_pb.rb rename to google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner_services_pb.rb index 0ada3f0..c840d56 100644 --- a/google-cloud-spanner/benchmark/benchwrapper/spanner_services_pb.rb +++ b/google-cloud-spanner/benchmark/benchwrapper/spanner_bench/spanner_services_pb.rb @@ -1,5 +1,5 @@ # Generated by the protocol buffer compiler. DO NOT EDIT! -# Source: spanner.proto for package 'spanner_bench' +# Source: spanner_bench/spanner.proto for package 'spanner_bench' # Original file comments: # Copyright 2020 Google LLC # @@ -17,7 +17,7 @@ # require 'grpc' -require 'spanner_pb' +require 'spanner_bench/spanner_pb' module SpannerBench module SpannerBenchWrapper diff --git a/google-cloud-spanner/benchmark/benchwrapper/spanner_pb.rb b/google-cloud-spanner/benchmark/benchwrapper/spanner_pb.rb deleted file mode 100644 index c88f047..0000000 --- a/google-cloud-spanner/benchmark/benchwrapper/spanner_pb.rb +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: spanner.proto - -require 'google/protobuf' - -Google::Protobuf::DescriptorPool.generated_pool.build do - add_file("spanner.proto", :syntax => :proto3) do - add_message "spanner_bench.Singer" do - optional :id, :int64, 1 - optional :first_name, :string, 2 - optional :last_name, :string, 3 - optional :singer_info, :string, 4 - end - add_message "spanner_bench.Album" do - optional :id, :int64, 1 - optional :singer_id, :int64, 2 - optional :album_title, :string, 3 - end - add_message "spanner_bench.ReadQuery" do - optional :query, :string, 1 - end - add_message "spanner_bench.InsertQuery" do - repeated :singers, :message, 1, "spanner_bench.Singer" - repeated :albums, :message, 2, "spanner_bench.Album" - end - add_message "spanner_bench.UpdateQuery" do - repeated :queries, :string, 1 - end - add_message "spanner_bench.EmptyResponse" do - end - end -end - -module SpannerBench - Singer = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.Singer").msgclass - Album = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.Album").msgclass - ReadQuery = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.ReadQuery").msgclass - InsertQuery = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.InsertQuery").msgclass - UpdateQuery = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.UpdateQuery").msgclass - EmptyResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("spanner_bench.EmptyResponse").msgclass -end diff --git a/google-cloud-spanner/lib/google/cloud/spanner/instance.rb b/google-cloud-spanner/lib/google/cloud/spanner/instance.rb index 094a8b9..93d54fc 100644 --- a/google-cloud-spanner/lib/google/cloud/spanner/instance.rb +++ b/google-cloud-spanner/lib/google/cloud/spanner/instance.rb @@ -258,12 +258,13 @@ def save ensure_service! field_mask = [] - @current_values.each do |field, value| - field_mask << field unless @grpc[field.to_s] == value + new_values = @grpc.to_h + (@current_values.keys + new_values.keys).uniq.each do |field| + field_mask << field unless new_values[field] == @current_values[field] end job_grpc = service.update_instance @grpc, field_mask: field_mask - @current_values = @grpc.to_h + @current_values = new_values Instance::Job.from_grpc job_grpc, service end alias update save