Class: Google::Cloud::Datastore::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/service.rb

Overview

methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials, host: nil, timeout: nil, client_config: nil) ⇒ Service

Creates a new Service instance.



33
34
35
36
37
38
39
40
# File 'lib/google/cloud/datastore/service.rb', line 33

def initialize project, credentials, host: nil, timeout: nil,
               client_config: nil
  @project = project
  @credentials = credentials
  @host = host || V1::DatastoreApi::SERVICE_ADDRESS
  @timeout = timeout
  @client_config = client_config || {}
end

Instance Attribute Details

#client_configObject

Returns the value of attribute client_config.



29
30
31
# File 'lib/google/cloud/datastore/service.rb', line 29

def client_config
  @client_config
end

#credentialsObject

Returns the value of attribute credentials.



29
30
31
# File 'lib/google/cloud/datastore/service.rb', line 29

def credentials
  @credentials
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/google/cloud/datastore/service.rb', line 29

def host
  @host
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



64
65
66
# File 'lib/google/cloud/datastore/service.rb', line 64

def mocked_service
  @mocked_service
end

#projectObject

Returns the value of attribute project.



29
30
31
# File 'lib/google/cloud/datastore/service.rb', line 29

def project
  @project
end

#timeoutObject

Returns the value of attribute timeout.



29
30
31
# File 'lib/google/cloud/datastore/service.rb', line 29

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



73
74
75
76
77
78
# File 'lib/google/cloud/datastore/service.rb', line 73

def allocate_ids *incomplete_keys
  execute do
    service.allocate_ids project, incomplete_keys,
                         options: default_options
  end
end

#begin_transactionObject

Begin a new transaction.



113
114
115
# File 'lib/google/cloud/datastore/service.rb', line 113

def begin_transaction
  execute { service.begin_transaction project }
end

#chan_credsObject



47
48
49
50
51
52
# File 'lib/google/cloud/datastore/service.rb', line 47

def chan_creds
  return credentials if insecure?
  require "grpc"
  GRPC::Core::ChannelCredentials.new.compose \
    GRPC::Core::CallCredentials.new credentials.client.updater_proc
end

#channelObject



42
43
44
45
# File 'lib/google/cloud/datastore/service.rb', line 42

def channel
  require "grpc"
  GRPC::Core::Channel.new host, nil, chan_creds
end

#commit(mutations, transaction: nil) ⇒ Object

Commit a transaction, optionally creating, deleting or modifying some entities.



120
121
122
123
124
125
126
# File 'lib/google/cloud/datastore/service.rb', line 120

def commit mutations, transaction: nil
  mode =  transaction.nil? ? :NON_TRANSACTIONAL : :TRANSACTIONAL
  execute do
    service.commit project, mode, mutations, transaction: transaction,
                                             options: default_options
  end
end

#insecure?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/google/cloud/datastore/service.rb', line 66

def insecure?
  credentials == :this_channel_is_insecure
end

#inspectObject



136
137
138
# File 'lib/google/cloud/datastore/service.rb', line 136

def inspect
  "#{self.class}(#{@project})"
end

#lookup(*keys, consistency: nil, transaction: nil) ⇒ Object

Look up entities by keys.



82
83
84
85
86
87
88
# File 'lib/google/cloud/datastore/service.rb', line 82

def lookup *keys, consistency: nil, transaction: nil
  read_options = generate_read_options consistency, transaction

  execute do
    service.lookup project, read_options, keys, options: default_options
  end
end

#rollback(transaction) ⇒ Object

Roll back a transaction.



130
131
132
133
134
# File 'lib/google/cloud/datastore/service.rb', line 130

def rollback transaction
  execute do
    service.rollback project, transaction, options: default_options
  end
end

#run_query(query, namespace = nil, consistency: nil, transaction: nil) ⇒ Object

Query for entities.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/google/cloud/datastore/service.rb', line 91

def run_query query, namespace = nil, consistency: nil, transaction: nil
  gql_query = nil
  if query.is_a? Google::Datastore::V1::GqlQuery
    gql_query = query
    query = nil
  end
  read_options = generate_read_options consistency, transaction
  partition_id = Google::Datastore::V1::PartitionId.new(
    namespace_id: namespace) if namespace

  execute do
    service.run_query project,
                      partition_id,
                      read_options,
                      query: query,
                      gql_query: gql_query,
                      options: default_options
  end
end

#serviceObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/google/cloud/datastore/service.rb', line 54

def service
  return mocked_service if mocked_service
  @service ||= V1::DatastoreApi.new(
    service_path: host,
    channel: channel,
    timeout: timeout,
    client_config: client_config,
    app_name: "gcloud-ruby",
    app_version: Google::Cloud::Datastore::VERSION)
end