module EventStore

Constants

Event
SNAPSHOT_DELIMITER
SerializedEvent
VERSION

Public Class Methods

clear!() click to toggle source
# File lib/event_store.rb, line 70
def self.clear!
  return unless connected?
  EventStore.db.from(fully_qualified_table).delete
  EventStore.redis.flushdb
end
connect(*args) click to toggle source
# File lib/event_store.rb, line 42
def self.connect(*args)
  @db ||= Sequel.connect(*args)
end
connected?() click to toggle source
# File lib/event_store.rb, line 66
def self.connected?
  !!EventStore.db
end
create_db() click to toggle source
# File lib/event_store.rb, line 126
def self.create_db
  self.connect(@db_config)
  schema_exits = @db.table_exists?("#{schema}__schema_info".to_sym)
  @db.run "CREATE SCHEMA #{EventStore.schema};" unless schema_exits
  Sequel::Migrator.run(@db, File.expand_path(File.join('..','..','db', self.migrations_dir), __FILE__), :table=> "#{schema}__schema_info".to_sym)
end
custom_config(database_config, redis_config, table_name = 'events', environment = 'production') click to toggle source
# File lib/event_store.rb, line 109
def self.custom_config(database_config, redis_config, table_name = 'events', environment = 'production')
  self.redis_connect(redis_config)
  database_config = database_config.inject({}) {|memo, (k,v)| memo[k.to_s] = v; memo}
  redis_config    = redis_config.inject({}) {|memo, (k,v)| memo[k.to_s] = v; memo}

  @adapter        = database_config['adapter'].to_s
  @environment    = environment
  @db_config      = database_config
  @table_name     = table_name
  @schema          = database_config['schema'].to_s
  create_db
end
db() click to toggle source
# File lib/event_store.rb, line 34
def self.db
  @db
end
db_config() click to toggle source
# File lib/event_store.rb, line 20
def self.db_config
  raw_db_config[@environment.to_s][@adapter.to_s]
end
escape_bytea(binary_string) click to toggle source
# File lib/event_store.rb, line 101
def self.escape_bytea(binary_string)
  binary_string.unpack('H*').join
end
fully_qualified_table() click to toggle source
# File lib/event_store.rb, line 62
def self.fully_qualified_table
  @fully_qualified_table ||= Sequel.lit "#{schema}.#{table_name}"
end
local_redis_config() click to toggle source
# File lib/event_store.rb, line 50
def self.local_redis_config
  @redis_connection ||= raw_db_config['redis']
end
migrations_dir() click to toggle source
# File lib/event_store.rb, line 122
def self.migrations_dir
   @adapter == 'vertica' ? 'migrations' : 'pg_migrations'
end
postgres(environment = 'test', table_name = 'events', schema = 'event_store_test') click to toggle source
# File lib/event_store.rb, line 76
def self.postgres(environment = 'test', table_name = 'events', schema = 'event_store_test')
  @schema         = schema
  @table_name     = table_name
  @environment    = environment.to_s
  @adapter        = 'postgres'
  @db_config      ||= self.db_config
  custom_config(@db_config, local_redis_config, @table_name, environment)
end
raw_db_config() click to toggle source
# File lib/event_store.rb, line 24
def self.raw_db_config
  if @raw_db_config.nil?
    file_path = File.expand_path(__FILE__ + '/../../db/database.yml')
    @config_file = File.open(file_path,'r')
    @raw_db_config = YAML.load(@config_file)
    @config_file.close
  end
  @raw_db_config
end
redis() click to toggle source
# File lib/event_store.rb, line 38
def self.redis
  @redis
end
redis_connect(config_hash) click to toggle source
# File lib/event_store.rb, line 46
def self.redis_connect(config_hash)
  @redis ||= Redis.new(config_hash)
end
schema() click to toggle source
# File lib/event_store.rb, line 54
def self.schema
  @schema ||= raw_db_config[@environment][@adapter]['schema']
end
table_name() click to toggle source
# File lib/event_store.rb, line 58
def self.table_name
  @table_name ||= raw_db_config['table_name']
end
unescape_bytea(binary_string) click to toggle source
# File lib/event_store.rb, line 105
def self.unescape_bytea(binary_string)
  [binary_string].pack("H*")
end
vertica(environment = 'test', table_name = 'events', schema = 'event_store_test') click to toggle source

To find the ip address of vertica on your local box (running in a vm)

  1. open Settings -> Network and select Wi-Fi

  2. open a terminal in the VM

  3. do /sbin/ifconfig (ifconfig is not in $PATH)

  4. the inet address for en0 is what you want

Hint: if it just hangs, you have have the wrong IP

# File lib/event_store.rb, line 91
def self.vertica(environment = 'test', table_name = 'events', schema = 'event_store_test')
  @schema         = schema
  @table_name     = table_name
  @environment    = environment.to_s
  @adapter        = 'vertica'
  @db_config      ||= self.db_config
  @db_config['host'] ||= ENV['VERTICA_HOST'] || vertica_host
  custom_config(@db_config, local_redis_config, @table_name, environment)
end
vertica_host() click to toggle source
# File lib/event_store.rb, line 133
def self.vertica_host
  File.read File.expand_path(File.join('..','..','db', 'vertica_host_address.txt'), __FILE__)
end