Class: Gcloud::Datastore::Service
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Service
- Defined in:
- lib/gcloud/datastore/service.rb
Overview
methods.
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#host ⇒ Object
Returns the value of attribute host.
-
#mocked_datastore ⇒ Object
Returns the value of attribute mocked_datastore.
-
#project ⇒ Object
Returns the value of attribute project.
-
#retries ⇒ Object
Returns the value of attribute retries.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#allocate_ids(*incomplete_keys) ⇒ Object
Allocate IDs for incomplete keys.
-
#begin_transaction ⇒ Object
Begin a new transaction.
-
#commit(mutations, transaction: nil) ⇒ Object
Commit a transaction, optionally creating, deleting or modifying some entities.
- #creds ⇒ Object
- #datastore ⇒ Object
-
#execute ⇒ Object
Performs backoff and error handling.
-
#initialize(project, credentials, host: nil, retries: nil, timeout: nil) ⇒ Service
constructor
Creates a new Service instance.
- #insecure? ⇒ Boolean
- #inspect ⇒ Object
-
#lookup(*keys, consistency: nil, transaction: nil) ⇒ Object
Look up entities by keys.
-
#rollback(transaction) ⇒ Object
Roll back a transaction.
-
#run_query(query, namespace = nil, consistency: nil, transaction: nil) ⇒ Object
Query for entities.
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
#credentials ⇒ Object
Returns the value of attribute credentials.
26 27 28 |
# File 'lib/gcloud/datastore/service.rb', line 26 def credentials @credentials end |
#host ⇒ Object
Returns the value of attribute host.
26 27 28 |
# File 'lib/gcloud/datastore/service.rb', line 26 def host @host end |
#mocked_datastore ⇒ Object
Returns the value of attribute mocked_datastore.
49 50 51 |
# File 'lib/gcloud/datastore/service.rb', line 49 def mocked_datastore @mocked_datastore end |
#project ⇒ Object
Returns the value of attribute project.
26 27 28 |
# File 'lib/gcloud/datastore/service.rb', line 26 def project @project end |
#retries ⇒ Object
Returns the value of attribute retries.
26 27 28 |
# File 'lib/gcloud/datastore/service.rb', line 26 def retries @retries end |
#timeout ⇒ Object
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_transaction ⇒ Object
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 |
#creds ⇒ Object
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 |
#datastore ⇒ Object
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 |
#execute ⇒ Object
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
51 52 53 |
# File 'lib/gcloud/datastore/service.rb', line 51 def insecure? credentials == :this_channel_is_insecure end |
#inspect ⇒ Object
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. = 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. = consistency, transaction run_req.partition_id = Google::Datastore::V1beta3::PartitionId.new( namespace_id: namespace) if namespace execute { datastore.run_query run_req } end |