Class: StatelyDB::CoreClient
- Inherits:
-
Object
- Object
- StatelyDB::CoreClient
- Defined in:
- lib/statelydb.rb
Overview
CoreClient is a low level client for interacting with the Stately Cloud API. This client shouldn’t be used directly in most cases. Instead, use the generated client for your schema.
Class Method Summary collapse
-
.make_endpoint(endpoint: nil, region: nil) ⇒ String
Construct the API endpoint from the region and endpoint.
Instance Method Summary collapse
-
#begin_list(prefix, limit: 100, sort_property: nil, sort_direction: :ascending, item_types: [], gt: nil, gte: nil, lt: nil, lte: nil) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
The list of Items and the token.
-
#begin_scan(limit: 0, item_types: [], total_segments: nil, segment_index: nil) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
Initiates a scan request which will scan over the entire store and apply the provided filters.
-
#close ⇒ void
Nil.
-
#continue_list(token) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
Continue listing Items from a StatelyDB Store using a token.
-
#continue_scan(token) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
continue_scan takes the token from a begin_scan call and returns more results based on the original request parameters and pagination options.
-
#delete(*key_paths) ⇒ void
Delete up to 50 Items from a StatelyDB Store at the given key_paths.
-
#get(key_path) ⇒ StatelyDB::Item, NilClass
Fetch a single Item from a StatelyDB Store at the given key_path.
-
#get_batch(*key_paths) ⇒ Array<StatelyDB::Item>, NilClass
Fetch a batch of up to 100 Items from a StatelyDB Store at the given key_paths.
-
#initialize(store_id:, schema:, token_provider: nil, endpoint: nil, region: nil, no_auth: false) ⇒ CoreClient
constructor
Initialize a new StatelyDB CoreClient.
-
#put(item, must_not_exist: false, overwrite_metadata_timestamps: false) ⇒ StatelyDB::Item
Put an Item into a StatelyDB Store at the given key_path.
-
#put_batch(*items) ⇒ Array<StatelyDB::Item>
Put a batch of up to 50 Items into a StatelyDB Store.
-
#sync_list(token) ⇒ StatelyDB::SyncResult
Sync a list of Items from a StatelyDB Store.
-
#transaction ⇒ StatelyDB::Transaction::Transaction::Result
Transaction takes a block and executes the block within a transaction.
-
#with_allow_stale(allow_stale) ⇒ self
Set whether to allow stale results for all operations with this client.
Constructor Details
#initialize(store_id:, schema:, token_provider: nil, endpoint: nil, region: nil, no_auth: false) ⇒ CoreClient
Initialize a new StatelyDB CoreClient
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/statelydb.rb', line 33 def initialize(store_id:, schema:, token_provider: nil, endpoint: nil, region: nil, no_auth: false) if store_id.nil? raise StatelyDB::Error.new("store_id is required", code: GRPC::Core::StatusCodes::INVALID_ARGUMENT, stately_code: "InvalidArgument") end if schema.nil? raise StatelyDB::Error.new("schema is required", code: GRPC::Core::StatusCodes::INVALID_ARGUMENT, stately_code: "InvalidArgument") end endpoint = self.class.make_endpoint(endpoint:, region:) @channel = Common::Net.new_channel(endpoint:) interceptors = [Common::ErrorInterceptor.new] # Make sure to use the correct endpoint for the default token provider unless no_auth @token_provider = token_provider || Common::Auth::AuthTokenProvider.new @token_provider.start(endpoint: endpoint) interceptors << Common::Auth::Interceptor.new(token_provider: @token_provider) end @stub = Stately::Db::DatabaseService::Stub.new(nil, nil, channel_override: @channel, interceptors:) @store_id = store_id.to_i @schema = schema @allow_stale = false end |
Class Method Details
.make_endpoint(endpoint: nil, region: nil) ⇒ String
Construct the API endpoint from the region and endpoint. If the endpoint is provided, it will be returned as-is. If the region is provided and the endpoint is not, then the region-specific endpoint will be returned. If neither the region nor the endpoint is provided, then the default endpoint will be returned.
417 418 419 420 421 422 423 424 |
# File 'lib/statelydb.rb', line 417 def self.make_endpoint(endpoint: nil, region: nil) return endpoint unless endpoint.nil? return "https://api.stately.cloud" if region.nil? region = region.sub("aws-", "") if region.start_with?("aws-") "https://#{region}.aws.api.stately.cloud" end |
Instance Method Details
#begin_list(prefix, limit: 100, sort_property: nil, sort_direction: :ascending, item_types: [], gt: nil, gte: nil, lt: nil, lte: nil) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
Returns the list of Items and the token.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/statelydb.rb', line 147 def begin_list(prefix, limit: 100, sort_property: nil, sort_direction: :ascending, item_types: [], gt: nil, gte: nil, lt: nil, lte: nil) sort_direction = sort_direction == :ascending ? 0 : 1 key_condition_params = [ [Stately::Db::Operator::OPERATOR_GREATER_THAN, gt.is_a?(String) ? gt : gt&.to_s], [Stately::Db::Operator::OPERATOR_GREATER_THAN_OR_EQUAL, gte.is_a?(String) ? gte : gte&.to_s], [Stately::Db::Operator::OPERATOR_LESS_THAN, lt.is_a?(String) ? lt : lt&.to_s], [Stately::Db::Operator::OPERATOR_LESS_THAN_OR_EQUAL, lte.is_a?(String) ? lte : lte&.to_s] ] key_conditions = key_condition_params .reject { |(_, value)| value.nil? } .map { |(operator, key_path)| Stately::Db::KeyCondition.new(operator: operator, key_path: key_path) } req = Stately::Db::BeginListRequest.new( store_id: @store_id, key_path_prefix: String(prefix), limit:, sort_property:, sort_direction:, filter_conditions: item_types.map do |item_type| Stately::Db::FilterCondition.new(item_type: item_type.respond_to?(:name) ? item_type.name.split("::").last : item_type) end, key_conditions: key_conditions, allow_stale: @allow_stale, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID ) resp = @stub.begin_list(req) process_list_response(resp) end |
#begin_scan(limit: 0, item_types: [], total_segments: nil, segment_index: nil) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
Initiates a scan request which will scan over the entire store and apply the provided filters. This API returns a token that you can pass to continue_scan to paginate through the result set. This can fail if the caller does not have permission to read Items.
WARNING: THIS API CAN BE EXTREMELY EXPENSIVE FOR STORES WITH A LARGE NUMBER OF ITEMS.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/statelydb.rb', line 224 def begin_scan(limit: 0, item_types: [], total_segments: nil, segment_index: nil) if total_segments.nil? != segment_index.nil? raise StatelyDB::Error.new("total_segments and segment_index must both be set or both be nil", code: GRPC::Core::StatusCodes::INVALID_ARGUMENT, stately_code: "InvalidArgument") end req = Stately::Db::BeginScanRequest.new( store_id: @store_id, limit:, filter_condition: item_types.map do |item_type| Stately::Db::FilterCondition.new(item_type: item_type.respond_to?(:name) ? item_type.name.split("::").last : item_type) end, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID ) resp = @stub.begin_scan(req) process_list_response(resp) end |
#close ⇒ void
This method returns an undefined value.
Returns nil.
68 69 70 71 |
# File 'lib/statelydb.rb', line 68 def close @channel&.close @token_provider&.close end |
#continue_list(token) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
Continue listing Items from a StatelyDB Store using a token.
192 193 194 195 196 197 198 199 200 |
# File 'lib/statelydb.rb', line 192 def continue_list(token) req = Stately::Db::ContinueListRequest.new( token_data: token.token_data, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID ) resp = @stub.continue_list(req) process_list_response(resp) end |
#continue_scan(token) ⇒ Array<StatelyDB::Item>, StatelyDB::Token
continue_scan takes the token from a begin_scan call and returns more results based on the original request parameters and pagination options.
WARNING: THIS API CAN BE EXTREMELY EXPENSIVE FOR STORES WITH A LARGE NUMBER OF ITEMS.
257 258 259 260 261 262 263 264 265 |
# File 'lib/statelydb.rb', line 257 def continue_scan(token) req = Stately::Db::ContinueScanRequest.new( token_data: token.token_data, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID ) resp = @stub.continue_scan(req) process_list_response(resp) end |
#delete(*key_paths) ⇒ void
This method returns an undefined value.
Delete up to 50 Items from a StatelyDB Store at the given key_paths.
362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/statelydb.rb', line 362 def delete(*key_paths) key_paths = Array(key_paths).flatten req = Stately::Db::DeleteRequest.new( store_id: @store_id, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID, deletes: key_paths.map { |key_path| Stately::Db::DeleteItem.new(key_path: String(key_path)) } ) @stub.delete(req) nil end |
#get(key_path) ⇒ StatelyDB::Item, NilClass
Fetch a single Item from a StatelyDB Store at the given key_path.
93 94 95 96 97 98 |
# File 'lib/statelydb.rb', line 93 def get(key_path) resp = get_batch(key_path) # Always return a single Item. resp.first end |
#get_batch(*key_paths) ⇒ Array<StatelyDB::Item>, NilClass
Fetch a batch of up to 100 Items from a StatelyDB Store at the given key_paths.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/statelydb.rb', line 109 def get_batch(*key_paths) key_paths = Array(key_paths).flatten req = Stately::Db::GetRequest.new( store_id: @store_id, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID, gets: key_paths.map { |key_path| Stately::Db::GetItem.new(key_path: String(key_path)) }, allow_stale: @allow_stale ) resp = @stub.get(req) resp.items.map do |result| @schema.unmarshal_item(stately_item: result) end end |
#put(item, must_not_exist: false, overwrite_metadata_timestamps: false) ⇒ StatelyDB::Item
Put an Item into a StatelyDB Store at the given key_path.
305 306 307 308 309 310 311 312 |
# File 'lib/statelydb.rb', line 305 def put(item, must_not_exist: false, overwrite_metadata_timestamps: false) resp = put_batch({ item:, must_not_exist:, overwrite_metadata_timestamps: }) # Always return a single Item. resp.first end |
#put_batch(*items) ⇒ Array<StatelyDB::Item>
Put a batch of up to 50 Items into a StatelyDB Store.
Max 50 items.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/statelydb.rb', line 324 def put_batch(*items) puts = Array(items).flatten.map do |input| if input.is_a?(Hash) item = input[:item] Stately::Db::PutItem.new( item: item.send("marshal_stately"), overwrite_metadata_timestamps: input[:overwrite_metadata_timestamps], must_not_exist: input[:must_not_exist] ) else Stately::Db::PutItem.new( item: input.send("marshal_stately") ) end end req = Stately::Db::PutRequest.new( store_id: @store_id, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID, puts: ) resp = @stub.put(req) resp.items.map do |result| @schema.unmarshal_item(stately_item: result) end end |
#sync_list(token) ⇒ StatelyDB::SyncResult
Sync a list of Items from a StatelyDB Store.
275 276 277 278 279 280 281 282 283 |
# File 'lib/statelydb.rb', line 275 def sync_list(token) req = Stately::Db::SyncListRequest.new( token_data: token.token_data, schema_id: @schema::SCHEMA_ID, schema_version_id: @schema::SCHEMA_VERSION_ID ) resp = @stub.sync_list(req) process_sync_response(resp) end |
#transaction ⇒ StatelyDB::Transaction::Transaction::Result
Transaction takes a block and executes the block within a transaction. If the block raises an exception, the transaction is rolled back. If the block completes successfully, the transaction is committed.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/statelydb.rb', line 388 def transaction txn = StatelyDB::Transaction::Transaction.new(stub: @stub, store_id: @store_id, schema: @schema) txn.begin yield txn txn.commit rescue StatelyDB::Error raise # Handle any other exceptions and abort the transaction. We're rescuing Exception here # because we want to catch all exceptions, including those that don't inherit from StandardError. rescue Exception => e txn.abort # All gRPC errors inherit from GRPC::BadStatus. We wrap these in a StatelyDB::Error. raise StatelyDB::Error.from(e) if e.is_a? GRPC::BadStatus # Calling raise with no parameters re-raises the original exception raise end |
#with_allow_stale(allow_stale) ⇒ self
Set whether to allow stale results for all operations with this client. This produces a new client with the allow_stale flag set.
79 80 81 82 83 |
# File 'lib/statelydb.rb', line 79 def with_allow_stale(allow_stale) new_client = clone new_client.instance_variable_set(:@allow_stale, allow_stale) new_client end |