class TacScribe::Datastore

Acts as the interface to the back-end datastore and hides all datastore implementation details from callers. Note that ruby does not support exception chaining we are not wrapping exceptions yet. This will happen when bugs.ruby-lang.org/issues/8257 is fixed.

Attributes

db[RW]
deleted[RW]
written[RW]

Public Instance Methods

configuration() click to toggle source
# File lib/tac_scribe/datastore.rb, line 22
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/tac_scribe/datastore.rb, line 26
def configure
  configuration
  yield(@configuration) if block_given?
end
connect() click to toggle source
# File lib/tac_scribe/datastore.rb, line 45
def connect
  configure
  @db = Sequel.connect(connection_string, max_connections: 49)
  @db.extension :postgis_georuby
end
truncate_table() click to toggle source
# File lib/tac_scribe/datastore.rb, line 51
def truncate_table
  @db[:units].truncate
end
write_objects(objects) click to toggle source
# File lib/tac_scribe/datastore.rb, line 55
def write_objects(objects)
  objects = objects.map do |object|
    obj = object.clone
    obj.delete(:game_time)
    obj
  end

  @db[:units].insert_conflict(
    constraint: :units_pkey,
    update: { position: Sequel[:excluded][:position],
              altitude: Sequel[:excluded][:altitude],
              heading: Sequel[:excluded][:heading],
              speed: Sequel[:excluded][:speed],
              updated_at: Sequel[:excluded][:updated_at],
              deleted: Sequel[:excluded][:deleted] }
  )
             .multi_insert(objects)
  deleted_ids = @db[:units].where(deleted: true).select_map(:id)
  @db[:units].where(deleted: true).delete
  self.written = objects.size
  self.deleted = deleted_ids.size

  deleted_ids
end

Private Instance Methods

connection_string() click to toggle source
# File lib/tac_scribe/datastore.rb, line 82
def connection_string
  if RUBY_PLATFORM == 'java'
    "jdbc:postgresql://#{@configuration.host}:#{@configuration.port}/" \
    "#{@configuration.database}?user=#{@configuration.username}&" \
    "password=#{configuration.password}"
  else
    "postgres://#{@configuration.username}:#{@configuration.password}@" \
    "#{@configuration.host}:#{@configuration.port}" \
    "/#{@configuration.database}"
  end
end