Class: Gcloud::Datastore::Connection

Inherits:
Object
  • Object
show all
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 =

:nodoc:

"v1beta2"
API_URL =
"https://www.googleapis.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset_id, credentials) ⇒ Connection

Create a new Connection instance.

conn = Gcloud::Datastore.Connection.new "my-todo-project",
  Gcloud::Datastore::Credentials.new("/path/to/keyfile.json")


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

def initialize dataset_id, credentials
  @dataset_id = dataset_id
  @credentials = credentials
end

Instance Attribute Details

#credentialsObject

The Credentials object for signing HTTP requests.



36
37
38
# File 'lib/gcloud/datastore/connection.rb', line 36

def credentials
  @credentials
end

#dataset_idObject

The project/dataset_id connected to.



32
33
34
# File 'lib/gcloud/datastore/connection.rb', line 32

def dataset_id
  @dataset_id
end

#default_http_headersObject

The default HTTP headers to be sent on all API calls.



115
116
117
118
119
# File 'lib/gcloud/datastore/connection.rb', line 115

def default_http_headers #:nodoc:
  @default_http_headers ||= {
    "User-Agent"   => "gcloud-node/#{Gcloud::VERSION}",
    "Content-Type" => "application/x-protobuf" }
end

#httpObject

The HTTP object that makes calls to Datastore. This must be a Faraday object.



127
128
129
# File 'lib/gcloud/datastore/connection.rb', line 127

def http #:nodoc:
  @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.)



51
52
53
54
55
56
57
58
# File 'lib/gcloud/datastore/connection.rb', line 51

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_transactionObject

Begin a new transaction.



80
81
82
83
84
85
# File 'lib/gcloud/datastore/connection.rb', line 80

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.



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gcloud/datastore/connection.rb', line 90

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_hostObject

The Datastore API URL.



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

def http_host
  @http_host || ENV["DATASTORE_HOST"] || API_URL
end

#http_host=(new_http_host) ⇒ Object

Update the Datastore API URL.



142
143
144
145
# File 'lib/gcloud/datastore/connection.rb', line 142

def http_host= new_http_host
  @http = nil # Reset the HTTP connection when host is set
  @http_host = new_http_host
end

#lookup(*keys) ⇒ Object

Look up entities by keys.



62
63
64
65
66
67
# File 'lib/gcloud/datastore/connection.rb', line 62

def lookup *keys
  lookup = Proto::LookupRequest.new
  lookup.key = keys

  Proto::LookupResponse.decode rpc("lookup", lookup)
end

#rollback(transaction) ⇒ Object

Roll back a transaction.



105
106
107
108
109
110
111
# File 'lib/gcloud/datastore/connection.rb', line 105

def rollback transaction
  rollback = Proto::RollbackRequest.new.tap do |r|
    r.transaction = transaction
  end

  Proto::RollbackResponse.decode rpc("rollback", rollback)
end

#run_query(query) ⇒ Object

Query for entities.



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

def run_query query
  run_query = Proto::RunQueryRequest.new.tap do |rq|
    rq.query = query
  end

  Proto::RunQueryResponse.decode rpc("runQuery", run_query)
end