Class: Gcloud::Datastore::Connection
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Connection
- Defined in:
- lib/gcloud/datastore/connection.rb
Overview
Represent the HTTP connection to the Datastore, as well as the Datastore API calls.
This class only deals with Protocol Buffer objects, and is not part of the public API.
Constant Summary collapse
- API_VERSION =
"v1beta2"- API_URL =
"https://www.googleapis.com"
Instance Attribute Summary collapse
-
#credentials ⇒ Object
The Credentials object for signing HTTP requests.
-
#dataset_id ⇒ Object
The project/dataset_id connected to.
-
#default_http_headers ⇒ Object
The default HTTP headers to be sent on all API calls.
-
#http ⇒ Object
The HTTP object that makes calls to Datastore.
Instance Method Summary collapse
-
#allocate_ids(*incomplete_keys) ⇒ Object
Allocate IDs for incomplete keys.
-
#begin_transaction ⇒ Object
Begin a new transaction.
-
#commit(mutation, transaction = nil) ⇒ Object
Commit a transaction, optionally creating, deleting or modifying some entities.
-
#http_host ⇒ Object
The Datastore API URL.
-
#http_host=(new_http_host) ⇒ Object
Update the Datastore API URL.
-
#initialize(dataset_id, credentials) ⇒ Connection
constructor
Create a new Connection instance.
- #inspect ⇒ Object
-
#lookup(*keys, consistency: nil, transaction: nil) ⇒ Object
Look up entities by keys.
-
#rollback(transaction) ⇒ Object
Roll back a transaction.
-
#run_query(query, partition = nil, consistency: nil, transaction: nil) ⇒ Object
Query for entities.
Constructor Details
#initialize(dataset_id, credentials) ⇒ Connection
Create a new Connection instance.
47 48 49 50 |
# File 'lib/gcloud/datastore/connection.rb', line 47 def initialize dataset_id, credentials @dataset_id = dataset_id @credentials = credentials end |
Instance Attribute Details
#credentials ⇒ Object
The Credentials object for signing HTTP requests.
38 39 40 |
# File 'lib/gcloud/datastore/connection.rb', line 38 def credentials @credentials end |
#dataset_id ⇒ Object
The project/dataset_id connected to.
34 35 36 |
# File 'lib/gcloud/datastore/connection.rb', line 34 def dataset_id @dataset_id end |
#default_http_headers ⇒ Object
The default HTTP headers to be sent on all API calls.
135 136 137 138 139 |
# File 'lib/gcloud/datastore/connection.rb', line 135 def default_http_headers @default_http_headers ||= { "User-Agent" => "gcloud-node/#{Gcloud::VERSION}", "Content-Type" => "application/x-protobuf" } end |
#http ⇒ Object
The HTTP object that makes calls to Datastore. This must be a Faraday object.
147 148 149 |
# File 'lib/gcloud/datastore/connection.rb', line 147 def http @http ||= Faraday.new url: http_host 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.)
55 56 57 58 59 60 61 62 |
# File 'lib/gcloud/datastore/connection.rb', line 55 def allocate_ids *incomplete_keys allocate_ids = Proto::AllocateIdsRequest.new.tap do |ai| ai.key = incomplete_keys end rpc_response = rpc("allocateIds", allocate_ids) Proto::AllocateIdsResponse.decode rpc_response end |
#begin_transaction ⇒ Object
Begin a new transaction.
100 101 102 103 104 105 |
# File 'lib/gcloud/datastore/connection.rb', line 100 def begin_transaction tx_request = Proto::BeginTransactionRequest.new response_rpc = rpc "beginTransaction", tx_request Proto::BeginTransactionResponse.decode response_rpc end |
#commit(mutation, transaction = nil) ⇒ Object
Commit a transaction, optionally creating, deleting or modifying some entities.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/gcloud/datastore/connection.rb', line 110 def commit mutation, transaction = nil mode = Proto::CommitRequest::Mode::NON_TRANSACTIONAL mode = Proto::CommitRequest::Mode::TRANSACTIONAL if transaction commit = Proto::CommitRequest.new.tap do |c| c.mutation = mutation c.mode = mode c.transaction = transaction end Proto::CommitResponse.decode rpc("commit", commit) end |
#http_host ⇒ Object
The Datastore API URL.
156 157 158 |
# File 'lib/gcloud/datastore/connection.rb', line 156 def http_host @http_host || ENV["DATASTORE_HOST"] || API_URL end |
#http_host=(new_http_host) ⇒ Object
Update the Datastore API URL.
162 163 164 165 |
# File 'lib/gcloud/datastore/connection.rb', line 162 def http_host= new_http_host @http = nil # Reset the HTTP connection when host is set @http_host = new_http_host end |
#inspect ⇒ Object
167 168 169 |
# File 'lib/gcloud/datastore/connection.rb', line 167 def inspect "#{self.class}(#{@dataset_id})" end |
#lookup(*keys, consistency: nil, transaction: nil) ⇒ Object
Look up entities by keys.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gcloud/datastore/connection.rb', line 66 def lookup *keys, consistency: nil, transaction: nil lookup = Proto::LookupRequest.new key: keys if consistency == :eventual lookup. = Proto::ReadOptions.new(read_consistency: 2) elsif consistency == :strong lookup. = Proto::ReadOptions.new(read_consistency: 1) elsif transaction lookup. = Proto::ReadOptions.new( transaction: transaction) end Proto::LookupResponse.decode rpc("lookup", lookup) end |
#rollback(transaction) ⇒ Object
Roll back a transaction.
125 126 127 128 129 130 131 |
# File 'lib/gcloud/datastore/connection.rb', line 125 def rollback transaction rollback = Proto::RollbackRequest.new.tap do |r| r.transaction = transaction end Proto::RollbackResponse.decode rpc("rollback", rollback) end |
#run_query(query, partition = nil, consistency: nil, transaction: nil) ⇒ Object
Query for entities.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gcloud/datastore/connection.rb', line 81 def run_query query, partition = nil, consistency: nil, transaction: nil run_query = Proto::RunQueryRequest.new.tap do |rq| rq.query = query rq.partition_id = partition if partition end if consistency == :eventual run_query. = Proto::ReadOptions.new(read_consistency: 2) elsif consistency == :strong run_query. = Proto::ReadOptions.new(read_consistency: 1) elsif transaction run_query. = Proto::ReadOptions.new( transaction: transaction) end Proto::RunQueryResponse.decode rpc("runQuery", run_query) end |