Class: Google::Cloud::Spanner::Partition

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/spanner/partition.rb

Overview

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.

Examples:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#executeObject (readonly)

@ private



48
49
50
# File 'lib/google/cloud/spanner/partition.rb', line 48

def execute
  @execute
end

#readObject (readonly)

@ private



48
49
50
# File 'lib/google/cloud/spanner/partition.rb', line 48

def read
  @read
end

Class Method Details

.load(data) ⇒ Google::Cloud::Spanner::Partition

Returns a Google::Cloud::Spanner::Partition from a serialized representation.

Examples:

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

Parameters:

  • data (String)

    The serialized representation of an existing batch partition. See #dump.

Returns:



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/google/cloud/spanner/partition.rb', line 168

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 = \
        Google::Spanner::V1::ExecuteSqlRequest.decode(
          Base64.decode64(data[:execute])
        )
      p.instance_variable_set :@execute, execute_sql_grpc
    end
    if data[:read]
      read_grpc = Google::Spanner::V1::ReadRequest.decode \
        Base64.decode64(data[:read])
      p.instance_variable_set :@read, read_grpc
    end
  end
end

Instance Method Details

#dumpString Also known as: serialize

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

Examples:

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

Returns:

  • (String)

    The serialized representation of the batch partition.



129
130
131
# File 'lib/google/cloud/spanner/partition.rb', line 129

def dump
  JSON.dump to_h
end

#execute_query?Boolean Also known as: execute?, execute_sql?, query?

Whether the partition was created for an execute_query/query operation.

Returns:

  • (Boolean)


58
59
60
# File 'lib/google/cloud/spanner/partition.rb', line 58

def execute_query?
  !@execute.nil?
end

#read?Boolean

Whether the partition was created for a read operation.

Returns:

  • (Boolean)


68
69
70
# File 'lib/google/cloud/spanner/partition.rb', line 68

def read?
  !@read.nil?
end