Class: Google::Cloud::Datastore::Commit

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

Overview

# Commit

Object yielded from ‘commit` methods to allow multiple changes to be made in a single commit.

See Dataset#commit and Transaction#commit.

Examples:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.save task1, task2
  c.delete entity1, entity2
end

Instance Method Summary collapse

Constructor Details

#initializeCommit

Returns a new instance of Commit.



37
38
39
40
41
42
# File 'lib/google/cloud/datastore/commit.rb', line 37

def initialize
  @shared_upserts = []
  @shared_inserts = []
  @shared_updates = []
  @shared_deletes = []
end

Instance Method Details

#delete(*entities_or_keys) ⇒ Object

Remove entities from the Datastore.

Examples:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.delete task1, task2
end

Parameters:

  • entities_or_keys (Entity, Key)

    One or more Entity or Key objects to remove.



111
112
113
114
115
116
117
118
# File 'lib/google/cloud/datastore/commit.rb', line 111

def delete *entities_or_keys
  keys = Array(entities_or_keys).flatten.map do |e_or_k|
    e_or_k.respond_to?(:key) ? e_or_k.key : e_or_k
  end
  @shared_deletes += keys unless keys.empty?
  # Do not delete yet
  true
end

#entitiesObject



139
140
141
# File 'lib/google/cloud/datastore/commit.rb', line 139

def entities
  @shared_upserts + @shared_inserts + @shared_updates
end

#insert(*entities) ⇒ Object

Inserts entities to the Datastore.

Examples:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.insert task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to insert.



74
75
76
77
78
79
# File 'lib/google/cloud/datastore/commit.rb', line 74

def insert *entities
  entities = Array(entities).flatten
  @shared_inserts += entities unless entities.empty?
  # Do not insert yet
  entities
end

#mutationsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/google/cloud/datastore/commit.rb', line 121

def mutations
  mutations = []
  mutations += @shared_upserts.map do |entity|
    Google::Datastore::V1::Mutation.new upsert: entity.to_grpc
  end
  mutations += @shared_inserts.map do |entity|
    Google::Datastore::V1::Mutation.new insert: entity.to_grpc
  end
  mutations += @shared_updates.map do |entity|
    Google::Datastore::V1::Mutation.new update: entity.to_grpc
  end
  mutations += @shared_deletes.map do |key|
    Google::Datastore::V1::Mutation.new delete: key.to_grpc
  end
  mutations
end

#save(*entities) ⇒ Object Also known as: upsert

Saves entities to the Datastore.

Examples:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.save task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to save.



55
56
57
58
59
60
# File 'lib/google/cloud/datastore/commit.rb', line 55

def save *entities
  entities = Array(entities).flatten
  @shared_upserts += entities unless entities.empty?
  # Do not save yet
  entities
end

#update(*entities) ⇒ Object

Updates entities to the Datastore.

Examples:

datastore = Google::Cloud::Datastore.new
datastore.commit do |c|
  c.update task1, task2
end

Parameters:

  • entities (Entity)

    One or more Entity objects to update.



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

def update *entities
  entities = Array(entities).flatten
  @shared_updates += entities unless entities.empty?
  # Do not update yet
  entities
end