Class: TacScribe::Datastore

Inherits:
Object
  • Object
show all
Includes:
GeoRuby::SimpleFeatures, Singleton
Defined in:
lib/tac_scribe/datastore.rb

Overview

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.

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbObject

Returns the value of attribute db.



17
18
19
# File 'lib/tac_scribe/datastore.rb', line 17

def db
  @db
end

Instance Method Details

#configurationObject



22
23
24
# File 'lib/tac_scribe/datastore.rb', line 22

def configuration
  @configuration ||= Configuration.new
end

#configure {|@configuration| ... } ⇒ Object

Yields:



26
27
28
29
# File 'lib/tac_scribe/datastore.rb', line 26

def configure
  configuration
  yield(@configuration) if block_given?
end

#connectObject



45
46
47
48
49
# File 'lib/tac_scribe/datastore.rb', line 45

def connect
  configure
  @db = Sequel.connect(connection_string, max_connections: 49)
  @db.extension :postgis_georuby
end

#delete_object(object_id) ⇒ Object



70
71
72
73
# File 'lib/tac_scribe/datastore.rb', line 70

def delete_object(object_id)
  count = @db[:units].where(id: object_id).delete
  "Deleted #{object_id} #{object_id.class} - #{count}"
end

#truncate_tableObject



51
52
53
# File 'lib/tac_scribe/datastore.rb', line 51

def truncate_table
  @db[:units].truncate
end

#write_object(event, timestamp) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tac_scribe/datastore.rb', line 55

def write_object(event, timestamp)
  unit = get_unit(event[:object_id])

  # Tacview omits values that don't change to save
  # data bandwidth and storage. If something has not changed then
  # use the old value
  current_position = get_position(event, unit)

  if unit
    update_unit(event, unit, current_position, timestamp)
  else
    insert_unit(event, current_position, timestamp)
  end
end