Class: AWSCloudSearch::DocumentBatch

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_cloud_search/document_batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pref_bytesize = 1048576, max_bytesize = 5242880) ⇒ DocumentBatch

Constructor

Parameters:

  • pref_bytesize (Integer) (defaults to: 1048576)

    The preferred size of the batch in bytes. May be exceeded, if so batch is considered full.

  • max_bytesize (Integer) (defaults to: 5242880)

    The batch size in bytes must not exceed this number. Must be greater than pref_bytesize.

Raises:

  • (ArgumentError)

    If pref_bytesize is not less than max_bytesize


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

#bytesizeObject (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.

Parameters:

Raises:

  • (ArgumentError)

    If parameter is not an AWSCloudSearch::Document


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

#clearObject


79
80
81
82
83
# File 'lib/aws_cloud_search/document_batch.rb', line 79

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

Parameters:

  • doc (Document)

    The document to delete

Raises:

  • (ArgumentError)

    If parameter is not an AWSCloudSearch::Document


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

#empty?Boolean

Returns True if the batch contains no items

Returns:

  • (Boolean)

    True if the batch contains no items


65
66
67
# File 'lib/aws_cloud_search/document_batch.rb', line 65

def empty?
  size == 0
end

#full?Boolean

Returns True if the bytesize of the batch exceeds the preferred bytesize

Returns:

  • (Boolean)

    True if the bytesize of the batch exceeds the preferred bytesize


70
71
72
# File 'lib/aws_cloud_search/document_batch.rb', line 70

def full?
  @bytesize >= @pref_bytesize
end

#sizeInteger

Returns Number of items in the batch

Returns:

  • (Integer)

    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_jsonString

Returns The JSON string representation of the DocumentBatch

Returns:

  • (String)

    The JSON string representation of the DocumentBatch


75
76
77
# File 'lib/aws_cloud_search/document_batch.rb', line 75

def to_json
  (@batch_add + @batch_delete).map {|item| item.to_hash}.to_json
end