Class: Google::Cloud::Datastore::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/key.rb

Overview

# Key

Every Datastore record has an identifying key, which includes the record’s entity kind and a unique identifier. The identifier may be either a key name string, assigned explicitly by the application, or an integer numeric ID, assigned automatically by Datastore.

Examples:

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind = nil, id_or_name = nil) ⇒ Google::Cloud::Datastore::Dataset::Key

Create a new Key instance.

Examples:

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"

Parameters:

  • kind (String) (defaults to: nil)

    The kind of the Key. This is optional.

  • id_or_name (Integer, String) (defaults to: nil)

    The id or name of the Key. This is optional.



91
92
93
94
95
96
97
98
# File 'lib/google/cloud/datastore/key.rb', line 91

def initialize kind = nil, id_or_name = nil
  @kind = kind
  if id_or_name.is_a? Integer
    @id = id_or_name
  else
    @name = id_or_name
  end
end

Instance Attribute Details

#idInteger?

The id of the Key.

Examples:

task_key = Google::Cloud::Datastore::Key.new "Task", 123456
task_key.id #=> 123456

Returns:

  • (Integer, nil)


128
129
130
# File 'lib/google/cloud/datastore/key.rb', line 128

def id
  @id
end

#kindString

The kind of the Key.

Examples:

key = Google::Cloud::Datastore::Key.new "TaskList"
key.kind #=> "TaskList"
key.kind = "Task"

Returns:

  • (String)


41
42
43
# File 'lib/google/cloud/datastore/key.rb', line 41

def kind
  @kind
end

#nameString?

The name of the Key.

Examples:

task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"
task_key.name #=> "sampleTask"

Returns:

  • (String, nil)


158
159
160
# File 'lib/google/cloud/datastore/key.rb', line 158

def name
  @name
end

#namespaceString?

The namespace of the Key.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new "my-todo-project",
                    "/path/to/keyfile.json"

datastore = gcloud.datastore
task = datastore.find "Task", "sampleTask"
task.key.namespace #=> "ns~todo-project"

Returns:

  • (String, nil)


77
78
79
# File 'lib/google/cloud/datastore/key.rb', line 77

def namespace
  @namespace
end

#parentKey?

The parent of the Key.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
datastore = gcloud.datastore

task_list = datastore.find "TaskList", "default"
query = datastore.query("Task").
  ancestor(task_list)
lists = datastore.run query
lists.first.key.parent #=> Key("TaskList", "default")

Returns:



200
201
202
# File 'lib/google/cloud/datastore/key.rb', line 200

def parent
  @parent
end

#projectString Also known as: dataset_id

The project of the Key.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new "my-todo-project",
                    "/path/to/keyfile.json"

datastore = gcloud.datastore
task = datastore.find "Task", "sampleTask"
task.key.project #=> "my-todo-project"

Returns:

  • (String)


58
59
60
# File 'lib/google/cloud/datastore/key.rb', line 58

def project
  @project
end

Class Method Details

.from_grpc(grpc) ⇒ Object

object.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/google/cloud/datastore/key.rb', line 267

def self.from_grpc grpc
  return nil if grpc.nil?
  key_grpc = grpc.dup
  key = Key.new
  path_grpc = key_grpc.path.pop
  if path_grpc
    id_or_name =
      (path_grpc.id_type == :id ? path_grpc.id : path_grpc.name)
    key = Key.new path_grpc.kind, id_or_name
  end
  if key_grpc.partition_id
    key.project = key_grpc.partition_id.project_id
    key.namespace = key_grpc.partition_id.namespace_id
  end
  key.parent = Key.from_grpc(key_grpc) if key_grpc.path.count > 0
  # Freeze the key to make it immutable.
  key.freeze
  key
end

Instance Method Details

#complete?Boolean

Determine if the key is complete. A complete key has either an id or a name.

Inverse of #incomplete?

Returns:

  • (Boolean)


225
226
227
# File 'lib/google/cloud/datastore/key.rb', line 225

def complete?
  !incomplete?
end

#incomplete?Boolean

Determine if the key is incomplete. An incomplete key has neither an id nor a name.

Inverse of #complete?

Returns:

  • (Boolean)


234
235
236
# File 'lib/google/cloud/datastore/key.rb', line 234

def incomplete?
  kind.nil? || (id.nil? && (name.nil? || name.empty?))
end

#pathArray<Array<(String, String)>>

Represent the Key’s path (including parent) as an array of arrays. Each inner array contains two values, the kind and the id or name. If neither an id or name exist then nil will be returned.

Examples:

parent_key = Google::Cloud::Datastore::Key.new "TaskList", "default"
task_key = Google::Cloud::Datastore::Key.new "Task", "sampleTask"
task_key.parent = parent_key
task_key.path #=> [["TaskList", "default"], ["Task", "sampleTask"]]

Returns:

  • (Array<Array<(String, String)>>)


215
216
217
218
# File 'lib/google/cloud/datastore/key.rb', line 215

def path
  new_path = parent ? parent.path : []
  new_path << [kind, (id || name)]
end

#serialized_sizeObject

The number of bytes the Key will take to serialize during API calls.



240
241
242
# File 'lib/google/cloud/datastore/key.rb', line 240

def serialized_size
  to_grpc.to_proto.length
end

#to_grpcObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/google/cloud/datastore/key.rb', line 246

def to_grpc
  grpc_path = path.map do |pe_kind, pe_id_or_name|
    path_args = { kind: pe_kind }
    if pe_id_or_name.is_a? Integer
      path_args[:id] = pe_id_or_name
    elsif pe_id_or_name.is_a? String
      path_args[:name] = pe_id_or_name unless pe_id_or_name.empty?
    end
    Google::Datastore::V1::Key::PathElement.new(path_args)
  end
  grpc = Google::Datastore::V1::Key.new(path: grpc_path)
  if project || namespace
    grpc.partition_id = Google::Datastore::V1::PartitionId.new(
      project_id: project.to_s, namespace_id: namespace.to_s)
  end
  grpc
end