Class: AWSCloudSearch::DocumentBatch
- Inherits:
-
Object
- Object
- AWSCloudSearch::DocumentBatch
- Defined in:
- lib/aws_cloud_search/document_batch.rb
Instance Attribute Summary collapse
-
#bytesize ⇒ Object
readonly
Returns the value of attribute bytesize.
Instance Method Summary collapse
-
#add_document(doc) ⇒ Object
Adds a document with the add operation to the batch.
- #clear ⇒ Object
-
#delete_document(doc) ⇒ Object
Adds a delete document operation to the batch.
-
#full? ⇒ Boolean
True if the bytesize of the batch exceeds the preferred bytesize.
-
#initialize(pref_bytesize = 1048576, max_bytesize = 5242880) ⇒ DocumentBatch
constructor
Constructor.
-
#size ⇒ Integer
Number of items in the batch.
-
#to_json ⇒ String
The JSON string representation of the DocumentBatch.
Constructor Details
#initialize(pref_bytesize = 1048576, max_bytesize = 5242880) ⇒ DocumentBatch
Constructor
13 14 15 16 17 18 19 20 21 |
# File 'lib/aws_cloud_search/document_batch.rb', line 13 def initialize(pref_bytesize=1048576, max_bytesize= 5242880) raise ArgumentError.new("pref_bytesize must be less than max_bytesize") if pref_bytesize >= max_bytesize @pref_bytesize = pref_bytesize @max_bytesize = max_bytesize @batch_add = [] @batch_delete = [] @bytesize = 0 end |
Instance Attribute Details
#bytesize ⇒ Object (readonly)
Returns the value of attribute bytesize.
7 8 9 |
# File 'lib/aws_cloud_search/document_batch.rb', line 7 def bytesize @bytesize end |
Instance Method Details
#add_document(doc) ⇒ Object
Adds a document with the add operation to the batch.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aws_cloud_search/document_batch.rb', line 26 def add_document(doc) raise ArgumentError.new("Invalid Type") unless doc.is_a? Document doc.type = 'add' json = doc.to_json doc_bytesize = json.bytesize raise Exception.new("Max batch size exceeded, document add was not added to batch.") if (doc_bytesize + @bytesize) > @max_bytesize raise ArgumentError.new("Found invalid XML 1.0 unicode characters.") if json =~ INVALID_CHAR_XML10 @bytesize += doc_bytesize @batch_add << doc end |
#clear ⇒ Object
74 75 76 77 78 |
# File 'lib/aws_cloud_search/document_batch.rb', line 74 def clear @batch_add.clear @batch_delete.clear @bytesize = 0 end |
#delete_document(doc) ⇒ Object
Adds a delete document operation to the batch. Removes lang and fields from the object as they are not required for delete operations. TODO: refactor to only use the required fields, hide the document construction from the user
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/aws_cloud_search/document_batch.rb', line 45 def delete_document(doc) raise ArgumentError.new("Invalid Type") unless doc.is_a? Document doc.type = 'delete' doc.lang = nil doc.clear_fields doc_bytesize = doc.to_json.bytesize raise Exception.new("Max batch size exceeded, document delete was not added to batch.") if (doc_bytesize + @bytesize) > @max_bytesize @bytesize += doc_bytesize @batch_delete << doc end |
#full? ⇒ Boolean
Returns True if the bytesize of the batch exceeds the preferred bytesize.
65 66 67 |
# File 'lib/aws_cloud_search/document_batch.rb', line 65 def full? @bytesize >= @pref_bytesize end |
#size ⇒ Integer
Returns Number of items in the batch.
60 61 62 |
# File 'lib/aws_cloud_search/document_batch.rb', line 60 def size @batch_add.size + @batch_delete.size end |
#to_json ⇒ String
Returns The JSON string representation of the DocumentBatch.
70 71 72 |
# File 'lib/aws_cloud_search/document_batch.rb', line 70 def to_json (@batch_add + @batch_delete).map {|item| item.to_hash}.to_json end |