Class: UU::BinaryStore::BinaryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/uu_binarystore/binary_store.rb,
lib/uu_binarystore/binary_store/uu_binary.rb,
lib/uu_binarystore/binary_store/result_list.rb,
lib/uu_binarystore/binary_store/uu_binary_query.rb,
lib/uu_binarystore/binary_store/concurrency_strategy.rb

Overview

Binary store component provides external storage for data required by uuApps.

Examples:

Basic usage:

binary_store = UU::BinaryStore::BinaryStore.init(binary_store_uri, :credentials => "access")
begin
  binary = binary_store.load(:dataKey => data_key)
  binary.data.read #get binary data

  file_name = 'pluto.png'
  f = File.open(file_name, 'rb')
  begin
    binary = UU::BinaryStore::BinaryStore::UUBinary.new(:data => f)
    binary_store.save(binary)
  ensure
    f.close
  end
  pluto_list = binary_store.query(:query => "fileName = '#{file_name}'")

  File.open(file_name, 'wb') do |f|
   f << pluto_list.first.data.read
  end
ensure
  binary_store.close
end

Defined Under Namespace

Classes: ConcurrencyStrategy, ResultList, UUBinary, UUBinaryQuery

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.init(binary_store_uri, parameters = {}) ⇒ Object

Creates new instance of binary store.

Examples:

Basic Usage:

binary_store = UU::BinaryStore::BinaryStore.init(storage_uri, :credentials => "access")
 begin
  # binary store operations here
 ensure
   binary_store.close
 end

Without credentials:

UU::OS::Security::Session.("access")
begin
  binary_store = UU::BinaryStore::BinaryStore.init(storage_uri)
  begin
    # binary store operations here
  ensure
    binary_store.close
  end
ensure
  UU::OS::Security::Session.logout
end

Without credentials and without login, it will logged app_identity

binary_store = UU::BinaryStore::BinaryStore.init(storage_uri)
   begin
     # binary store operations here
   ensure
     binary_store.close
   end

With session uses logged user for binary store operations

UU::OS::Security::Session.('uni355','password')
sess = UU::OS::Security::Session.current_session
binary_store = UU::BinaryStore::BinaryStore.init(storage_uri,:session => sess)
   begin
     # binary store operations here
   ensure
     binary_store.close
   end

Parameters:

  • binary_store_uri (String, UU::OS::UESURI)

    URI of binary store

  • parameters (Hash) (defaults to: {})

    Additional initialization parameters

Options Hash (parameters):

  • :concurrencyStrategy (ConcurrencyStrategy)

    Concurrency strategy to be used (optional, defaults to ConcurrencyStrategy::VERSION_BASE)

  • :credentials (String, File)

    Authentication token to be used for binary store operations (optional, see Session.login for possible values)

  • :session (UU::OS::Security::Session)

    Instance of session to be used for binary store operations (optional, alternative to using credentials parameter)



105
106
107
# File 'lib/uu_binarystore/binary_store.rb', line 105

def self.init(binary_store_uri, parameters = {})
  self.new(binary_store_uri, parameters)
end

Instance Method Details

#closeObject

Method closes instance of binary store. Once instance of binary store is closed, it cannot be used (Throws exception for each invoked operation. New instance must be created to access binary store).



350
351
352
# File 'lib/uu_binarystore/binary_store.rb', line 350

def close
  @closed = true
end

#delete(parameters) ⇒ Object

Method deletes existing uuBinary.

Examples:

Delete Binary:

binary_store = UU::BinaryStore::BinaryStore.init(storage_uri, :credentials => "access")
begin
  binary_store.delete(:dataKey => "TEST")
  binary_store.delete('ues:TERR:STORE:BINARY_ID')
  list = binary_store.query
  binary_store.delete(list.first) if list.first
ensure
  binary_store.close
end

Parameters:

  • parameters (UUBinary)

    Instance of loaded uuBinary

  • parameters (String, UU::OS::UESURI)

    URI of uuBinary

  • parameters (Hash)

    Unique uuBinary identification data

Options Hash (parameters):

  • :uri (String, UU::OS::UESURI)

    URI of uuBinary

  • :dataKey (String)

    Unique identifier of uuBinary (must be set if :uri parameter is not used)



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/uu_binarystore/binary_store.rb', line 252

def delete(parameters)
  closed?
  params = {}
  if parameters.kind_of?(UUBinary)
    # Parameter was instance of uuBinary
    params[:uri] = parameters.uri
    return if params[:uri].nil?
  elsif (parameters.kind_of? String) || (parameters.kind_of? UU::OS::UESURI)
    # Parameter was uuBinary URI
    params[:uri] = parameters
  elsif parameters.kind_of? Hash
    if parameters.has_key?(:uri)
      # Parameter was Hash with uuBinary URI
      params[:uri] = parameters[:uri]
    elsif parameters.has_key?(:dataKey) || parameters.has_key?(:data_key)
      # We have to find uuBinary URI via query (using dataKey and schema)
      params[:dataKey] = parameters[:dataKey] || parameters[:data_key]
    else
      raise ArgumentError.new('Parameters must contain uuBinary URI or dataKey.')
    end
  else
    raise ArgumentError.new('Parameters must contain uuBinary URI or dataKey.')
  end
  cmd = UU::OS::CMD::CommandClient.new("uu-binarystore", @session)
  cmd.invoke('BinaryStore/deleteBinary', @binary_store_uri, parameters: params)
  nil
end

#load(parameters) ⇒ UUBinary

Loads binary according to given parameters.

Parameters:

  • parameters (String, UU::OS::UESURI)

    URI of uuBinary

  • parameters (Hash)

    Unique uuBinary identification data

Options Hash (parameters):

  • :uri (String, UU::OS::UESURI)

    URI of uuBinary

  • :dataKey (String)

    Unique identifier of uuBinary within (must be set if :uri parameter is not used)

Returns:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/uu_binarystore/binary_store.rb', line 214

def load(parameters)
  closed?
  params = {}
  if (parameters.kind_of? String) || (parameters.kind_of? UU::OS::UESURI)
    # Parameter was uuBinary URI
    params[:uri] = parameters
  elsif (parameters.kind_of? Hash)
    params[:uri] = parameters[:uri] if parameters[:uri]
    data_key = parameters[:dataKey] || parameters[:data_key]
    params[:dataKey] = data_key if data_key
    raise ArgumentError.new('Parameters must contain uuBinary URI or dataKey.') if params.empty?
  else
    raise ArgumentError.new('Parameters must contain uuBinary URI or dataKey.')
  end
  cmd = UU::OS::CMD::CommandClient.new("uu-binarystore", @session)
  result = cmd.invoke('BinaryStore/getBinaryAttributes', @binary_store_uri, parameters: params)
  UU::BinaryStore::BinaryStore::UUBinary.new(result, @session)
end

#presign(parameters) ⇒ UUBinary

Preauthorize (create token) access role to download/upload uuBinary.

Examples:

Presign Binary:


binary_store = UU::BinaryStore::BinaryStore.init(storage_uri, :credentials => "access")
begin
  binary_store.presign(:uri => 'ues:TERR:STORE:BINARY_ID', :access_role_uri => "ues:TERR:ACR")
   list = binary_store.query
   binary_store.presign(:access_role_uri => "ues:TERR:ACR") # creates new object
  ensure
    binary_store.close
  end

Parameters:

  • parameters (Hash)

    Unique uuBinary identification data

Options Hash (parameters):

  • :uri (String, UU::OS::UESURI)

    URI of uuBinary

  • :access_role_uri (String, UU::OS::UESURI)

    URI of access role which will be preauthorized via token. It can be from any territory. Parameter is mandatory.

Returns:



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/uu_binarystore/binary_store.rb', line 300

def presign(parameters)
  closed?
  params = {}
  if parameters.kind_of? Hash
    if parameters.has_key?(:uri)
      # Parameter was Hash with uuBinary URI
      params[:uri] = parameters[:uri]
    end
    acr_uri = parameters[:accessRoleUri] || parameters[:access_role_uri]
    if acr_uri
      params[:accessRoleUri] = acr_uri
    else
      raise ArgumentError.new('Access Role have to be set.')
    end
  else
    raise ArgumentError.new('Parameters must be hash.')
  end
  cmd = UU::OS::CMD::CommandClient.new("uu-binarystore", @session)
  result = cmd.invoke('BinaryStore/presign', @binary_store_uri, parameters: params)
  UU::BinaryStore::BinaryStore::UUBinary.new(result)
end

#query(query = nil) ⇒ UU::BinaryStore::BinaryStore::ResultList<UUBinaryQuery, UUBinary>

Method finds all uuBinaries matching given criteria.

Examples:

binary_store = UU::BinaryStore::BinaryStore.init(storage_uri, :credentials => "access")
  begin
   binary_store.query(:query => "fileName = 'pluto.png'")
   binary_store.query(:query => "contentType = 'image/png'")
  ensure
    binary_store.close
  end

Parameters:

  • query (String, BinaryStore::UUBinaryQuery) (defaults to: nil)

    Actual uuBinary query for filtering and ordering of the result list (more information in uuQuery - Guideline). It is possible to filter the result list by fileName, contentType, size, dataKey, version, modificationStamp, creationStamp, creationAccessRoleUri (canonical), modificationAccessRoleUri (canonical)

Returns:



339
340
341
342
343
344
345
# File 'lib/uu_binarystore/binary_store.rb', line 339

def query(query = nil)
  closed?
  dto = BinaryStore::UUBinaryQuery.new(query)
  cmd = UU::OS::CMD::CommandClient.new("uu-binarystore", @session)
  result = cmd.invoke('BinaryStore/getBinaryList', @binary_store_uri, :parameters => dto.to_json)
  list = UU::BinaryStore::BinaryStore::ResultList.new(BinaryStore::UUBinaryQuery, BinaryStore::UUBinary, @session, result)
end

#save(binary) ⇒ UU::OS::UESURI

Method saves given uuBinary (updates existing or creates new). If the uri is set method will update existing uuBinary otherwise it will create new uuBinary. Method may throw concurrent modification exception in case current binary state violates constraints required by used Concurrency strategy. Each binary can be saved only once, it has to be re-loaded before next modification (with exception of concurrency strategy VERSION_BASED). Method will throw exception on repeated binary save.

Examples:

Create new Binary:

binary_store = UU::BinaryStore::BinaryStore.init(storage_uri, :credentials => "access")
begin
  #create new binary
  binary = UU::BinaryStore::BinaryStore::UUBinary.new(:data => f )
  binary_store.save(binary)

  #update binary
  binary = binary_store.load(:dataKey => 'DATA_KEY')
  f = File.open(file_name, 'rb')
   begin
     binary.data = f
     binary_store.save(binary)
   ensure
     f.close
   end

  binary_store.save(binary)

ensure
  binary_store.close
end

Parameters:

  • binary (UUBinary)

    Instance of uuBinary

Returns:

  • (UU::OS::UESURI)

    URI of saved uuBinary



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/uu_binarystore/binary_store.rb', line 168

def save(binary)
  closed?
  if binary.send(:saved)
    raise 'Instance of uuBinary was already saved. Re-load uuBinary before next modification.'

  end
  cmd = UU::OS::CMD::CommandClient.new("uu-binarystore", @session)

  binary_client = UU::OS::CMD::CommandClient.new("uu-binarystore-data", @session,
                                                 :request_timeout => 0)

  bv = UU::OS::Lang::BinaryValue.new(content: binary.data.data, content_type: binary.content_type, name: binary.file_name)

  if binary.uri
    params = {:uri => binary.uri}
    params[:size]
    params[:attributes] = binary.to_hash
    if binary.send(:data_key_changed)
      params[:originalDataKey] = binary.send(:orig_data_key)
    end

    result = cmd.invoke('BinaryStore/setBinaryAttributes', @binary_store_uri, parameters: params)
    params = binary.to_hash
    params[:version] += 1
  else
    params = binary.to_hash
    result = cmd.invoke('BinaryStore/createBinary', @binary_store_uri, parameters: params)
  end
  params[:data] = bv
  params[:concurrencyStrategy] = @concurrency_strategy
  params[:prevalidToken] = result[:token]
  result = binary_client.invoke('BinaryStore/setBinaryData', result[:uri], parameters: params)
  binary.send(:saved=, true)
  UU::OS::UESURI.new(result)
end