class Google::Cloud::Spanner::Partition

# Partition

Defines the segments of data to be read in a batch read/query context. A Partition instance can be serialized and used across several different machines or processes.

See {BatchSnapshot#partition_read}, {BatchSnapshot#partition_query}, and {BatchSnapshot#execute_partition}.

@example

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot
partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

results = batch_snapshot.execute_partition partition

Attributes

execute[R]

@ private

read[R]

Public Class Methods

from_execute_sql_grpc(grpc) click to toggle source

@private New Partition from a `Google::Cloud::Spanner::V1::ExecuteSqlRequest` object.

# File lib/google/cloud/spanner/partition.rb, line 201
def self.from_execute_sql_grpc grpc
  new.tap do |p|
    p.instance_variable_set :@execute, grpc
  end
end
from_read_grpc(grpc) click to toggle source

@private New Partition from a `Google::Cloud::Spanner::V1::ReadRequest` object.

# File lib/google/cloud/spanner/partition.rb, line 209
def self.from_read_grpc grpc
  new.tap do |p|
    p.instance_variable_set :@read, grpc
  end
end
load(data) click to toggle source

Returns a {Partition} from a serialized representation.

@param [String] data The serialized representation of an existing

batch partition. See {Partition#dump}.

@return [Google::Cloud::Spanner::Partition]

@example

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = Google::Cloud::Spanner::Partition.load \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
# File lib/google/cloud/spanner/partition.rb, line 170
def self.load data
  data = JSON.parse data, symbolize_names: true unless data.is_a? Hash

  # TODO: raise if hash[:execute_query].nil? && hash[:read].nil?
  new.tap do |p|
    if data[:execute]
      execute_sql_grpc = \
        V1::ExecuteSqlRequest.decode(
          Base64.decode64(data[:execute])
        )
      p.instance_variable_set :@execute, execute_sql_grpc
    end
    if data[:read]
      read_grpc = V1::ReadRequest.decode \
        Base64.decode64(data[:read])
      p.instance_variable_set :@read, read_grpc
    end
  end
end
new() click to toggle source

@private Creates a Partition object.

# File lib/google/cloud/spanner/partition.rb, line 53
def initialize
end

Public Instance Methods

dump() click to toggle source

Serializes the batch partition object so it can be recreated on another process. See {Partition.load} and {BatchClient#load_partition}.

@return [String] The serialized representation of the batch partition.

@example

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
# File lib/google/cloud/spanner/partition.rb, line 131
def dump
  JSON.dump to_h
end
Also aliased as: serialize
empty?() click to toggle source

@private Whether the partition does not have an execute_query or read operation. @return [Boolean]

# File lib/google/cloud/spanner/partition.rb, line 79
def empty?
  @execute.nil? && @read.nil?
end
execute?()
Alias for: execute_query?
execute_query?() click to toggle source

Whether the partition was created for an execute_query/query operation. @return [Boolean]

# File lib/google/cloud/spanner/partition.rb, line 60
def execute_query?
  !@execute.nil?
end
Also aliased as: execute?, execute_sql?, query?
execute_sql?()
Alias for: execute_query?
inspect() click to toggle source

@private

# File lib/google/cloud/spanner/partition.rb, line 191
def inspect
  status = "empty"
  status = "execute" if execute?
  status = "read" if read?
  "#<#{self.class.name} #{status}>"
end
query?()
Alias for: execute_query?
read?() click to toggle source

Whether the partition was created for a read operation. @return [Boolean]

# File lib/google/cloud/spanner/partition.rb, line 70
def read?
  !@read.nil?
end
serialize()
Alias for: dump
to_h() click to toggle source

@private Converts the the batch partition object to a Hash ready for serialization.

@return [Hash] A hash containing a representation of the batch

partition object.
# File lib/google/cloud/spanner/partition.rb, line 91
def to_h
  {}.tap do |h|
    h[:execute] = Base64.strict_encode64 @execute.to_proto if @execute
    h[:read]    = Base64.strict_encode64 @read.to_proto    if @read
  end
end