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, host: nil, retries: nil, timeout: nil) ⇒ Service

Creates a new Service instance.



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

def initialize project, credentials, host: nil, retries: nil, timeout: nil
  @project = project
  @credentials = credentials
  @host = host || "datastore.googleapis.com"
  @retries = retries
  @timeout = timeout
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.



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

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

#retriesObject

Returns the value of attribute retries.



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

def retries
  @retries
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
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.)



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

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

  execute { datastore.allocate_ids allocate_req }
end

#begin_transactionObject

Begin a new transaction.



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

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

  execute { datastore.begin_transaction tx_req }
end

#commit(mutations, transaction: nil) ⇒ Object

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



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

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

  execute { datastore.commit commit_req }
end

#credsObject



38
39
40
41
42
# File 'lib/gcloud/datastore/service.rb', line 38

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

#datastoreObject



44
45
46
47
48
# File 'lib/gcloud/datastore/service.rb', line 44

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

#executeObject

Performs backoff and error handling



142
143
144
145
146
147
148
# File 'lib/gcloud/datastore/service.rb', line 142

def execute
  Gcloud::Backoff.new(retries: retries).execute_grpc do
    yield
  end
rescue GRPC::BadStatus => e
  raise Gcloud::Error.from_error(e)
end

#insecure?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/gcloud/datastore/service.rb', line 51

def insecure?
  credentials == :this_channel_is_insecure
end

#inspectObject



136
137
138
# File 'lib/gcloud/datastore/service.rb', line 136

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

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

Look up entities by keys.



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

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

  execute { datastore.lookup lookup_req }
end

#rollback(transaction) ⇒ Object

Roll back a transaction.



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

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

  execute { datastore.rollback rb_req }
end

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

Query for entities.



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

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

  execute { datastore.run_query run_req }
end