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.



117
118
119
120
121
# File 'lib/gcloud/datastore/connection.rb', line 117

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.



129
130
131
# File 'lib/gcloud/datastore/connection.rb', line 129

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.



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

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.



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

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.



138
139
140
# File 'lib/gcloud/datastore/connection.rb', line 138

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

#http_host=(new_http_host) ⇒ Object

Update the Datastore API URL.



144
145
146
147
# File 'lib/gcloud/datastore/connection.rb', line 144

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

#inspectObject

:nodoc:



149
150
151
# File 'lib/gcloud/datastore/connection.rb', line 149

def inspect #:nodoc:
  "#{self.class}(#{@dataset_id})"
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.



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

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) ⇒ Object

Query for entities.



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

def run_query query, partition = nil
  run_query = Proto::RunQueryRequest.new.tap do |rq|
    rq.query = query
    rq.partition_id = partition if partition
    rq
  end

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