class Aggro::FileStore
Public: Stores and retrieves events by serializing them to flat files.
Constants
- EVENT_DIRECTORY
- INDEX_DIRECTORY
- REGISTRY_FILE
Public Class Methods
new(directory)
click to toggle source
# File lib/aggro/file_store.rb, line 11 def initialize(directory) @event_directory = [directory, EVENT_DIRECTORY].join('/') @index_directory = [directory, INDEX_DIRECTORY].join('/') FileUtils.mkdir_p @event_directory FileUtils.mkdir_p @index_directory @registry_file = [directory, REGISTRY_FILE].join('/') initialize_registry if File.exist? @registry_file end
Public Instance Methods
all()
click to toggle source
# File lib/aggro/file_store.rb, line 22 def all read registry.keys end
create(id, type)
click to toggle source
# File lib/aggro/file_store.rb, line 26 def create(id, type) File.open(@registry_file, 'ab') do |registry_file| registry_file.write Marshal.dump [id, type] registry[id] = type end self end
exists?(id)
click to toggle source
# File lib/aggro/file_store.rb, line 35 def exists?(id) registry[id] == true end
read(ids)
click to toggle source
# File lib/aggro/file_store.rb, line 39 def read(ids) ids.map { |id| id_to_event_stream id } end
registry()
click to toggle source
# File lib/aggro/file_store.rb, line 43 def registry @registry ||= {} end
write(event_streams)
click to toggle source
# File lib/aggro/file_store.rb, line 47 def write(event_streams) event_streams.each do |stream| FileStore::Writer.new( event_file(stream.id, 'ab'), index_file(stream.id, 'ab') ).write stream.events end self end
write_single(id, event)
click to toggle source
# File lib/aggro/file_store.rb, line 58 def write_single(id, event) FileStore::Writer.new( event_file(id, 'ab'), index_file(id, 'ab') ).write [event] end
Private Instance Methods
event_file(id, flags = 'rb')
click to toggle source
# File lib/aggro/file_store.rb, line 67 def event_file(id, flags = 'rb') File.new [@event_directory, id].join('/'), flags end
id_to_event_stream(id)
click to toggle source
# File lib/aggro/file_store.rb, line 71 def id_to_event_stream(id) EventStream.new id, type_for_id(id), id_to_reader(id).read rescue Errno::ENOENT EventStream.new id, type_for_id(id), [] end
id_to_reader(id)
click to toggle source
# File lib/aggro/file_store.rb, line 77 def id_to_reader(id) FileStore::Reader.new event_file(id), index_file(id) end
index_file(id, flags = 'rb')
click to toggle source
# File lib/aggro/file_store.rb, line 81 def index_file(id, flags = 'rb') File.new [@index_directory, id].join('/'), flags end
initialize_registry()
click to toggle source
# File lib/aggro/file_store.rb, line 85 def initialize_registry File.open(@registry_file) do |file| MarshalStream.new(file).each do |id, type| registry[id] = type end end end
type_for_id(id)
click to toggle source
# File lib/aggro/file_store.rb, line 93 def type_for_id(id) registry[id] end