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")


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

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

Instance Attribute Details

#credentialsObject

The Credentials object for signing HTTP requests.



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

def credentials
  @credentials
end

#dataset_idObject

The project/dataset_id connected to.



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

def dataset_id
  @dataset_id
end

#default_http_headersObject

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



113
114
115
116
117
# File 'lib/gcloud/datastore/connection.rb', line 113

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.



125
126
127
# File 'lib/gcloud/datastore/connection.rb', line 125

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.)



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

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.



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

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.



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

def commit mutation, transaction = nil
  mode = transaction ? Proto::CommitRequest::Mode::TRANSACTIONAL :
                       Proto::CommitRequest::Mode::NON_TRANSACTIONAL
  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.



134
135
136
# File 'lib/gcloud/datastore/connection.rb', line 134

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

#http_host=(new_http_host) ⇒ Object

Update the Datastore API URL.



140
141
142
143
# File 'lib/gcloud/datastore/connection.rb', line 140

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.



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

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

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

#rollback(transaction) ⇒ Object

Roll back a transaction.



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

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.



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

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

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