Class: Gcloud::Datastore::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/datastore/service.rb

Overview

methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials) ⇒ Service

Creates a new Service instance.



30
31
32
33
34
# File 'lib/gcloud/datastore/service.rb', line 30

def initialize project, credentials
  @project = project
  @credentials = credentials
  @host = "datastore.googleapis.com"
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



26
27
28
# File 'lib/gcloud/datastore/service.rb', line 26

def credentials
  @credentials
end

#hostObject

Returns the value of attribute host.



26
27
28
# File 'lib/gcloud/datastore/service.rb', line 26

def host
  @host
end

#mocked_datastoreObject

Returns the value of attribute mocked_datastore.



47
48
49
# File 'lib/gcloud/datastore/service.rb', line 47

def mocked_datastore
  @mocked_datastore
end

#projectObject

Returns the value of attribute project.



26
27
28
# File 'lib/gcloud/datastore/service.rb', line 26

def project
  @project
end

Instance Method Details

#allocate_ids(*incomplete_keys) ⇒ Object

Allocate IDs for incomplete keys. (This is useful for referencing an entity before it is inserted.)



56
57
58
59
60
61
62
63
# File 'lib/gcloud/datastore/service.rb', line 56

def allocate_ids *incomplete_keys
  allocate_req = Google::Datastore::V1beta3::AllocateIdsRequest.new(
    project_id: project,
    keys: incomplete_keys
  )

  backoff { datastore.allocate_ids allocate_req }
end

#backoff(options = {}) ⇒ Object



138
139
140
141
142
# File 'lib/gcloud/datastore/service.rb', line 138

def backoff options = {}
  Gcloud::Backoff.new(options).execute_grpc do
    yield
  end
end

#begin_transactionObject

Begin a new transaction.



98
99
100
101
102
103
104
# File 'lib/gcloud/datastore/service.rb', line 98

def begin_transaction
  tx_req = Google::Datastore::V1beta3::BeginTransactionRequest.new(
    project_id: project
  )

  backoff { datastore.begin_transaction tx_req }
end

#commit(mutations, transaction: nil) ⇒ Object

Commit a transaction, optionally creating, deleting or modifying some entities.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gcloud/datastore/service.rb', line 109

def commit mutations, transaction: nil
  commit_req = Google::Datastore::V1beta3::CommitRequest.new(
    project_id: project,
    mode: :NON_TRANSACTIONAL,
    mutations: mutations
  )
  if transaction
    commit_req.mode = :TRANSACTIONAL
    commit_req.transaction = transaction
  end

  backoff { datastore.commit commit_req }
end

#credsObject



36
37
38
39
40
# File 'lib/gcloud/datastore/service.rb', line 36

def creds
  return credentials if insecure?
  GRPC::Core::ChannelCredentials.new.compose \
    GRPC::Core::CallCredentials.new credentials.client.updater_proc
end

#datastoreObject



42
43
44
45
46
# File 'lib/gcloud/datastore/service.rb', line 42

def datastore
  return mocked_datastore if mocked_datastore
  @datastore ||= Google::Datastore::V1beta3::Datastore::Stub.new(
    host, creds)
end

#insecure?Boolean



49
50
51
# File 'lib/gcloud/datastore/service.rb', line 49

def insecure?
  credentials == :this_channel_is_insecure
end

#inspectObject



134
135
136
# File 'lib/gcloud/datastore/service.rb', line 134

def inspect
  "#{self.class}(#{@project})"
end

#lookup(*keys, consistency: nil, transaction: nil) ⇒ Object

Look up entities by keys.



67
68
69
70
71
72
73
74
75
# File 'lib/gcloud/datastore/service.rb', line 67

def lookup *keys, consistency: nil, transaction: nil
  lookup_req = Google::Datastore::V1beta3::LookupRequest.new(
    project_id: project,
    keys: keys
  )
  lookup_req.read_options = generate_read_options consistency, transaction

  backoff { datastore.lookup lookup_req }
end

#rollback(transaction) ⇒ Object

Roll back a transaction.



125
126
127
128
129
130
131
132
# File 'lib/gcloud/datastore/service.rb', line 125

def rollback transaction
  rb_req = Google::Datastore::V1beta3::RollbackRequest.new(
    project_id: project,
    transaction: transaction
  )

  backoff { datastore.rollback rb_req }
end

#run_query(query, namespace = nil, consistency: nil, transaction: nil) ⇒ Object

Query for entities.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gcloud/datastore/service.rb', line 78

def run_query query, namespace = nil, consistency: nil, transaction: nil
  run_req = Google::Datastore::V1beta3::RunQueryRequest.new(
    project_id: project)
  if query.is_a? Google::Datastore::V1beta3::Query
    run_req["query"] = query
  elsif query.is_a? Google::Datastore::V1beta3::GqlQuery
    run_req["gql_query"] = query
  else
    fail ArgumentError, "Unable to query with a #{query.class} object."
  end
  run_req.read_options = generate_read_options consistency, transaction

  run_req.partition_id = Google::Datastore::V1beta3::PartitionId.new(
    namespace_id: namespace) if namespace

  backoff { datastore.run_query run_req }
end