Class: EventMeter::Stores::Stream::File

Inherits:
Object
  • Object
show all
Includes:
FileHelpers
Defined in:
lib/event_meter/stores/stream/file.rb

Constant Summary collapse

SYNC_MODES =
%i[none flush fsync].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, sync: :flush, **stream_options) ⇒ File

Returns a new instance of File.



15
16
17
18
19
20
21
22
# File 'lib/event_meter/stores/stream/file.rb', line 15

def initialize(path:, sync: :flush, **stream_options)
  @path = normalize_file_store_path(path)
  @sync = sync.respond_to?(:to_sym) ? sync.to_sym : sync
  @stream_options = stream_options
  validate_sync!
  @streams = {}
  @read_ids = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/event_meter/stores/stream/file.rb', line 13

def path
  @path
end

#stream_optionsObject (readonly)

Returns the value of attribute stream_options.



13
14
15
# File 'lib/event_meter/stores/stream/file.rb', line 13

def stream_options
  @stream_options
end

#syncObject (readonly)

Returns the value of attribute sync.



13
14
15
# File 'lib/event_meter/stores/stream/file.rb', line 13

def sync
  @sync
end

Instance Method Details

#append(payload) ⇒ Object



24
25
26
27
# File 'lib/event_meter/stores/stream/file.rb', line 24

def append(payload)
  hash = payload.to_h
  stream_for(hash.fetch("name")).append(hash)
end

#closeObject



56
57
58
59
# File 'lib/event_meter/stores/stream/file.rb', line 56

def close
  release
  @streams.each_value(&:close)
end

#deleteObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/event_meter/stores/stream/file.rb', line 37

def delete
  return false unless @batch

  @batch.delete
  deleted_claims?
ensure
  @read_ids = []
  @read_name = nil
  @batch = nil
end

#read(name:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/event_meter/stores/stream/file.rb', line 29

def read(name:)
  release
  @read_name = name.to_s
  @batch = stream_for(name).read
  @read_ids = @batch.entries.map(&:first)
  @batch.entries
end

#releaseObject



48
49
50
51
52
53
54
# File 'lib/event_meter/stores/stream/file.rb', line 48

def release
  @batch&.release
ensure
  @read_ids = []
  @read_name = nil
  @batch = nil
end