Class: LogStash::Outputs::Application_insights::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/application_insights/block.rb

Constant Summary collapse

@@Block_number =
0
@@semaphore =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_separator = "") ⇒ Block

Returns a new instance of Block.



52
53
54
55
56
# File 'lib/logstash/outputs/application_insights/block.rb', line 52

def initialize ( event_separator = "" )
  dispose
  @event_separator = event_separator
  @event_separator_bytesize = @event_separator.bytesize
end

Instance Attribute Details

#block_numbersObject

Returns the value of attribute block_numbers.



29
30
31
# File 'lib/logstash/outputs/application_insights/block.rb', line 29

def block_numbers
  @block_numbers
end

#bufferObject

Returns the value of attribute buffer.



26
27
28
# File 'lib/logstash/outputs/application_insights/block.rb', line 26

def buffer
  @buffer
end

#bytesObject

Returns the value of attribute bytes.



25
26
27
# File 'lib/logstash/outputs/application_insights/block.rb', line 25

def bytes
  @bytes
end

#bytesizeObject

Returns the value of attribute bytesize.



27
28
29
# File 'lib/logstash/outputs/application_insights/block.rb', line 27

def bytesize
  @bytesize
end

#done_timeObject

Returns the value of attribute done_time.



30
31
32
# File 'lib/logstash/outputs/application_insights/block.rb', line 30

def done_time
  @done_time
end

#events_countObject

Returns the value of attribute events_count.



28
29
30
# File 'lib/logstash/outputs/application_insights/block.rb', line 28

def events_count
  @events_count
end

#oldest_event_timeObject

Returns the value of attribute oldest_event_time.



31
32
33
# File 'lib/logstash/outputs/application_insights/block.rb', line 31

def oldest_event_time
  @oldest_event_time
end

Class Method Details

.generate_block_numberObject



39
40
41
# File 'lib/logstash/outputs/application_insights/block.rb', line 39

def self.generate_block_number
  @@semaphore.synchronize { @@Block_number = ( @@Block_number + 1 ) % 1000000 }
end

.generate_block_numbers(count) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/logstash/outputs/application_insights/block.rb', line 43

def self.generate_block_numbers ( count )
  @@semaphore.synchronize { 
    firstNumber = ( @@Block_number + 1 ) % 1000000 
    @@Block_number = ( @@Block_number + count ) % 1000000
    firstNumber
  }
end

Instance Method Details

#<<(data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/logstash/outputs/application_insights/block.rb', line 74

def << (data)
  @bytesize += data.bytesize + @event_separator_bytesize

  # if first data, it will accept even it overflows
  if is_overflowed? && @events_count > 0
    @bytesize -= data.bytesize + @event_separator_bytesize
    raise BlockTooSmallError if is_empty?
    raise BlockOverflowError
  end

  @oldest_event_time ||= Time.now.utc
  @events_count += 1
  @buffer << data
end

#concat(other) ⇒ Object

concatenate two blocks into one



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/logstash/outputs/application_insights/block.rb', line 59

def concat ( other )
  if @bytesize + other.bytesize <= BLOB_BLOCK_MAX_BYTESIZE
    if @block_numbers
      @block_numbers.concat( other.block_numbers )
      @bytes += other.bytes
      @done_time = other.done_time if other.done_time > @done_time
    else
      @buffer.concat( other.buffer )
    end
    @events_count += other.events_count
    @oldest_event_time = other.oldest_event_time if other.oldest_event_time < @oldest_event_time
    @bytesize += other.bytesize
  end
end

#disposeObject



89
90
91
92
93
94
95
96
97
# File 'lib/logstash/outputs/application_insights/block.rb', line 89

def dispose
  @bytes = nil
  @buffer = [  ]
  @bytesize = 0
  @events_count = 0
  @done_time = nil
  @oldest_event_time = nil
  @block_numbers = nil
end

#is_full?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/logstash/outputs/application_insights/block.rb', line 117

def is_full?
  @bytesize >= BLOB_BLOCK_MAX_BYTESIZE
end

#partial_sealObject



100
101
102
103
104
105
106
107
# File 'lib/logstash/outputs/application_insights/block.rb', line 100

def partial_seal
  if @done_time.nil?
    @done_time = Time.now.utc
    @buffer << "" # required to add eol after last event
    @bytes = @buffer.join( @event_separator )
    @buffer = nil # release the memory of the array
  end
end

#sealObject



110
111
112
113
114
115
# File 'lib/logstash/outputs/application_insights/block.rb', line 110

def seal
  if @done_time.nil?
    @block_numbers = [ Block.generate_block_number ]
    partial_seal
  end
end