Class: App42::Storage::StorageService

Inherits:
Object
  • Object
show all
Defined in:
lib/storage/StorageService.rb

Overview

Storage service on cloud provides the way to store the JSON document in NoSQL database running on cloud. One can store the JSON document, update it , search it and can apply the map-reduce search on stored documents. Example : If one will store JSON doc “date”:“5Feb” it will be stored with unique Object Id and stored JSON object will look like { “date” : “5Feb” , “_id” : { “$oid” : “4f423dcce1603b3f0bd560cf”}}. This oid can further be used to access/search the document.

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ StorageService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


34
35
36
37
38
39
40
41
# File 'lib/storage/StorageService.rb', line 34

def initialize(api_key, secret_key, base_url)
  puts "StorageService->initialize"
  @api_key = api_key
  @secret_key = secret_key
  @base_url = base_url
  @resource = "storage"
  @version = "1.0"
end

Instance Method Details

#delete_document_by_id(dbName, collectionName, docId) ⇒ Object

Delete target document using Object Id from given db and collection. The Object Id will be searched in the JSON doc stored on the cloud and matching Doc will be deleted.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • docId
    • Unique Object Id handler.

Returns:

  • App42Response object if deleted successfully

Raises:

  • App42Exception



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/storage/StorageService.rb', line 424

def delete_document_by_id(dbName, collectionName, docId)
  puts "Delete Document By ID Called "
  puts "Base url #{@base_url}"
  response = nil
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(docId, "DocumentId");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    util = Util.new
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    puts params
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    params.store("docId", docId)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/deleteDocById/dbName/#{dbName}/collectionName/#{collectionName}/docId/#{docId}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#find_all_documents(dbName, collectionName) ⇒ Object

Find all documents stored in given database and collection.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

Returns:

  • Storage object

Raises:

  • App42Exception



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/storage/StorageService.rb', line 110

def find_all_documents(dbName, collectionName)
  puts "Find All Documents Called "
  puts "Base url #{@base_url}"
  response = nil
  storage = nil
  storage = Storage.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findAll/dbName/#{dbName}/collectionName/#{collectionName}"
    response = connection.get(signature, resource_url, query_params)
    storage = StorageResponseBuilder.new().buildResponse(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storage
end

#find_all_documents_by_paging(dbName, collectionName, max, offset) ⇒ Object

Find all documents stored in given database and collection.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • max
    • Maximum number of records to be fetched

  • offset
    • From where the records are to be fetched

Returns:

  • Storage object

Raises:

  • App42Exception



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/storage/StorageService.rb', line 206

def find_all_documents_by_paging(dbName, collectionName, max, offset)
  puts "Find All Documents Called "
  puts "Base url #{@base_url}"
  response = nil
  storageObj = nil
  storageObj = Storage.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    params.store("max", "" + (max.to_i).to_s);
    params.store("offset", "" + (offset.to_i).to_s);
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findAll/dbName/#{dbName}/collectionName/#{collectionName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}"
    response = connection.get(signature, resource_url, query_params)
    storageObj = StorageResponseBuilder.new().buildResponse(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storageObj
end

#find_all_documents_count(dbName, collectionName) ⇒ Object

Gets the count of all documents stored in given database and collection.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

Returns:

  • App42Response object

Raises:

  • App42Exception



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
183
184
185
186
187
# File 'lib/storage/StorageService.rb', line 155

def find_all_documents_count(dbName, collectionName)
  puts "Find All Documents Count Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findAll/count/dbName/#{dbName}/collectionName/#{collectionName}"
    response = connection.get(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
    responseObj = StorageResponseBuilder.new()
    responseObj.getTotalRecords(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#find_docs_with_query_paging_order_by(dbName, collectionName, query, max, offset, orderByKey, type) ⇒ Object

Find target document using Custom Query with paging and orderby.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched

  • Query
    • Query Object containing custom query for searching docs

  • max
    • max result parameter

  • offset
    • offset result parameter

Returns:

  • Storage object

Raises:

  • App42Exception



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/storage/StorageService.rb', line 730

def find_docs_with_query_paging_order_by(dbName, collectionName, query, max, offset, orderByKey, type)
  puts "findDocsWithQueryPagingOrderBy Called "
  puts "Base url #{@base_url}"
  response = nil
  storageObj = nil
  storageObj = Storage.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(query, "query");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
      'jsonQuery' => query.getStr()
    }
    if (orderByKey != nil)
      query_params.store("orderByKey", orderByKey);
    end
    if (type != nil)
      query_params.store("orderByType", type);
    end
    params.store("dbName", dbName);
    params.store("collectionName", collectionName);
    params.store("max", "" + (max.to_i).to_s)
    params.store("offset", "" + (offset.to_i).to_s)
    query_params = params.clone
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findDocsByQuery/dbName/#{dbName}/collectionName/#{collectionName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}"
    response = connection.get(signature, resource_url, query_params)
    storage = StorageResponseBuilder.new
    storageObj = storage.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storageObj
end

#find_document_by_id(dbName, collectionName, docId) ⇒ Object

Find target document by given unique object id.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • docId
    • Unique Object Id handler.

Returns:

  • Storage object

    @raise App42Exception



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/storage/StorageService.rb', line 255

def find_document_by_id(dbName, collectionName, docId)
  puts "Final Document By ID Called "
  puts "Base url #{@base_url}"
  response = nil
  storage = nil
  storage = Storage.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(docId, "DocumentId");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    params.store("docId", docId)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findDocById/dbName/#{dbName}/collectionName/#{collectionName}/docId/#{docId}"
    response = connection.get(signature, resource_url, query_params)
    storage = StorageResponseBuilder.new().buildResponse(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storage
end

#find_document_by_key_value(dbName, collectionName, key, value) ⇒ Object

Find target document using key value search parameter. This key value pair will be searched in the JSON doc stored on the cloud and matching Doc will be returned as a result of this method.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • key
    • Key to be searched for target JSON doc.

  • value
    • Value to be searched for target JSON doc.

Returns:

  • Storage object

Raises:

  • App42Exception



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/storage/StorageService.rb', line 307

def find_document_by_key_value(dbName, collectionName, key, value)
  puts "Find Document By Key Value Called"
  puts "Base url #{@base_url}"
  response = nil
  storage = nil
  storage = Storage.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(key, "Key");
  util.throwExceptionIfNullOrBlank(value, "Value");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    params.store("key", key)
    params.store("value", value)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findDocByKV/dbName/#{dbName}/collectionName/#{collectionName}/#{key}/#{value}"
    response = connection.get(signature, resource_url, query_params)
    storage = StorageResponseBuilder.new().buildResponse(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storage
end

#find_document_by_query(dbName, collectionName, query) ⇒ Object

Find target document using Custom Query.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched

  • Query
    • Query Object containing custom query for searching docs

Returns:

  • Storage object

Raises:

  • App42Exception



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/storage/StorageService.rb', line 620

def find_document_by_query(dbName, collectionName, query)
  puts "find_document_by_query Called "
  puts "Base url #{@base_url}"
  response = nil
  storageObj = nil
  storageObj = Storage.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(query, "query");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
      'jsonQuery' => query.getStr()
    }
    params.store("dbName", dbName);
    params.store("collectionName", collectionName);
    query_params = params.clone
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findDocsByQuery/dbName/#{dbName}/collectionName/#{collectionName}"
    response = connection.get(signature, resource_url, query_params)
    storage = StorageResponseBuilder.new
    storageObj = storage.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storageObj
end

#find_documents_by_query_with_paging(dbName, collectionName, query, max, offset) ⇒ Object

Find target document using Custom Query with paging.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched

  • Query
    • Query Object containing custom query for searching docs

  • max
    • max result parameter

  • offset
    • offset result parameter

Returns:

  • Storage object

Raises:

  • App42Exception



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/storage/StorageService.rb', line 674

def find_documents_by_query_with_paging(dbName, collectionName, query, max, offset)
  puts "findDocumentsByQueryWithPaging Called "
  puts "Base url #{@base_url}"
  response = nil
  storageObj = nil
  storageObj = Storage.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(query, "query");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
      'jsonQuery' => query.getStr()
    }
    params.store("dbName", dbName);
    params.store("collectionName", collectionName);
    params.store("max", "" + (max.to_i).to_s)
    params.store("offset", "" + (offset.to_i).to_s)
    query_params = params.clone
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/findDocsByQuery/dbName/#{dbName}/collectionName/#{collectionName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}"
    response = connection.get(signature, resource_url, query_params)
    storage = StorageResponseBuilder.new
    storageObj = storage.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storageObj
end

#get_json_from_map(map) ⇒ Object

Parameters:

  • map

Returns:



467
468
469
# File 'lib/storage/StorageService.rb', line 467

def get_json_from_map(map)
  return map.to_json
end

#insert_json_doc_using_map(dbName, collectionName, map) ⇒ Object

Save the JSON document in given database name and collection name. It accepts the HashMap containing key-value and convert it into JSON. Converted JSON doc further saved on the cloud using given db name and collection name.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc has to be saved.

  • map
    • HashMap containing key-value pairs

Returns:

  • Storage object

Raises:

  • App42Exception



487
488
489
490
491
492
# File 'lib/storage/StorageService.rb', line 487

def insert_json_doc_using_map(dbName,collectionName,map)
  puts "insert_json_doc_using_map Called "
  puts "Base url #{@base_url}"
  jsonBody = get_json_from_map(map);
  return insert_json_document(dbName, collectionName, jsonBody)
end

#insert_json_document(dbName, collectionName, json) ⇒ Object

Save the JSON document in given database name and collection name.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc has to be saved.

  • json
    • Target JSON document to be saved

Returns:

  • Storage object

Raises:

  • App42Exception



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/storage/StorageService.rb', line 58

def insert_json_document(dbName, collectionName, json)
  puts "Create Insert JSON Document Called "
  puts "Base url #{@base_url}"
  response = nil
  storage = nil
  storage = Storage.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName")
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName")
  util.throwExceptionIfNullOrBlank(json, "Json")
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"storage"=> {
      "jsonDoc" => json
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc
    }
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    query_params = params.clone
    params.store("body", body)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/insert/dbName/#{dbName}/collectionName/#{collectionName}"
    response = connection.post(signature, resource_url, query_params, body)
    storageObj = StorageResponseBuilder.new
    storage = storageObj.buildResponse(response)
  rescue  App42Exception => e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storage
end

#map_reduce(dbName, collectionName, mapFunction, reduceFunction) ⇒ Object

Map reduce function to search the target document.

Please see detail information on map-reduce en.wikipedia.org/wiki/MapReduce

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • mapFunction
    • Map function to be used to search the document

  • reduceFunction
    • Reduce function to be used to search the document

Returns:

  • Returns the target JSON document.

Raises:

  • App42Exception



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/storage/StorageService.rb', line 513

def map_reduce(dbName,collectionName,mapFunction,reduceFunction)
  response = nil
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(mapFunction, "MapFunction");
  util.throwExceptionIfNullOrBlank(reduceFunction, "ReduceFunction");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"storage"=> {
      "map" => mapFunction,
      "reduce" => reduceFunction
      }}}.to_json
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    query_params = params.clone
    params.store("body", body)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/mapReduce/dbName/#{dbName}/collectionName/#{collectionName}"
    response = connection.post(signature, resource_url, query_params, body)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return response
end

#update_document_by_doc_id(dbName, collectionName, docId, newJsonDoc) ⇒ Object

Update target document using the document id.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • docId
    • Id of the document to be searched for target JSON doc.

  • newJsonDoc
    • New Json document to be added.

Returns:

  • Storage object

Raises:

  • App42Exception



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/storage/StorageService.rb', line 564

def update_document_by_doc_id(dbName, collectionName, docId, newJsonDoc)
  puts "Update Document By Document Id Called "
  puts "Base url #{@base_url}"
  response = nil
  storage = nil
  storage = Storage.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName")
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName")
  util.throwExceptionIfNullOrBlank(docId, "DocId");
  util.throwExceptionIfNullOrBlank(newJsonDoc, "NewJsonDocument");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"storage"=> {
      "jsonDoc" => newJsonDoc
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc
    }
    params.store("body", body)
    params.store("docId", docId)
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    query_params = params.clone
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/updateByDocId/dbName/#{dbName}/collectionName/#{collectionName}/docId/#{docId}"
    response = connection.put(signature, resource_url, query_params, body)
    storageObj = StorageResponseBuilder.new
    storage = storageObj.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storage
end

#update_document_by_key_value(dbName, collectionName, key, value, newJsonDoc) ⇒ Object

Update target document using key value search parameter.

This key value pair will be searched in the JSON doc stored in the cloud and matching Doc will be updated with new value passed.

Parameters:

  • dbName
    • Unique handler for storage name

  • collectionName
    • Name of collection under which JSON doc needs to be searched.

  • key
    • Key to be searched for target JSON doc.

  • value
    • Value to be searched for target JSON doc.

  • newJsonDoc
    • New Json document to be added.

Returns:

  • Storage object

Raises:

  • App42Exception



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/storage/StorageService.rb', line 365

def update_document_by_key_value(dbName, collectionName, key, value, newJsonDoc)
  puts "Update Document By Key Value Called"
  puts "Base url #{@base_url}"
  response = nil
  storageObj = nil
  storageObj = Storage.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(dbName, "DataBaseName");
  util.throwExceptionIfNullOrBlank(collectionName, "CollectionName");
  util.throwExceptionIfNullOrBlank(key, "Key");
  util.throwExceptionIfNullOrBlank(value, "Value");
  util.throwExceptionIfNullOrBlank(newJsonDoc, "NewJsonDocument");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"storage"=> {
      "jsonDoc" => newJsonDoc
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    params.store("dbName", dbName)
    params.store("collectionName", collectionName)
    params.store("key", key)
    params.store("value", value)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/update/dbName/#{dbName}/collectionName/#{collectionName}/#{key}/#{value}"
    response = connection.put(signature, resource_url, query_params,body)
    storage = StorageResponseBuilder.new().buildResponse(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return storage
end