Class: Utility::BulkQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/utility/bulk_queue.rb

Defined Under Namespace

Classes: QueueOverflowError

Instance Method Summary collapse

Constructor Details

#initialize(operation_count_threshold = Utility::Constants::DEFAULT_MAX_INGESTION_QUEUE_SIZE, size_threshold = Utility::Constants::DEFAULT_MAX_INGESTION_QUEUE_BYTES) ⇒ BulkQueue

500 items or 5MB



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/utility/bulk_queue.rb', line 16

def initialize(operation_count_threshold = Utility::Constants::DEFAULT_MAX_INGESTION_QUEUE_SIZE, size_threshold = Utility::Constants::DEFAULT_MAX_INGESTION_QUEUE_BYTES)
  @operation_count_threshold = operation_count_threshold.freeze
  @size_threshold = size_threshold.freeze

  @buffer = ''

  @current_operation_count = 0

  @current_buffer_size = 0
  @current_data_size = 0
end

Instance Method Details

#add(operation, payload = nil) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/utility/bulk_queue.rb', line 36

def add(operation, payload = nil)
  raise QueueOverflowError unless will_fit?(operation, payload)

  operation_size = get_size(operation)
  payload_size = get_size(payload)

  @current_operation_count += 1
  @current_buffer_size += operation_size
  @current_buffer_size += payload_size
  @current_data_size += payload_size

  @buffer << operation
  @buffer << "\n"

  if payload
    @buffer << payload
    @buffer << "\n"
  end
end

#current_statsObject



65
66
67
68
69
70
# File 'lib/utility/bulk_queue.rb', line 65

def current_stats
  {
    :current_operation_count => @current_operation_count,
    :current_buffer_size => @current_buffer_size
  }
end

#pop_allObject



28
29
30
31
32
33
34
# File 'lib/utility/bulk_queue.rb', line 28

def pop_all
  result = @buffer

  reset

  result
end

#will_fit?(operation, payload = nil) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/utility/bulk_queue.rb', line 56

def will_fit?(operation, payload = nil)
  return false if @current_operation_count + 1 > @operation_count_threshold

  operation_size = get_size(operation)
  payload_size = get_size(payload)

  @current_buffer_size + operation_size + payload_size < @size_threshold
end