Class: EntitySnapshot::Postgres
- Inherits:
-
Object
- Object
- EntitySnapshot::Postgres
- Includes:
- EntityCache::Store::External, Log::Dependency
- Defined in:
- lib/entity_snapshot/postgres/log.rb,
lib/entity_snapshot/postgres/postgres.rb,
lib/entity_snapshot/postgres/controls/id.rb,
lib/entity_snapshot/postgres/controls/time.rb,
lib/entity_snapshot/postgres/controls/batch.rb,
lib/entity_snapshot/postgres/controls/write.rb,
lib/entity_snapshot/postgres/controls/entity.rb,
lib/entity_snapshot/postgres/controls/message.rb,
lib/entity_snapshot/postgres/controls/version.rb,
lib/entity_snapshot/postgres/controls/snapshot.rb,
lib/entity_snapshot/postgres/controls/stream_name.rb,
lib/entity_snapshot/postgres/controls/entity_store.rb,
lib/entity_snapshot/postgres/controls/random_value.rb
Defined Under Namespace
Modules: Controls Classes: Log
Constant Summary collapse
- Error =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
- #configure(session: nil) ⇒ Object
- #get(id) ⇒ Object
- #put(id, entity, version, time) ⇒ Object
- #snapshot_stream_name(id) ⇒ Object
Instance Attribute Details
#session ⇒ Object
Returns the value of attribute session.
9 10 11 |
# File 'lib/entity_snapshot/postgres/postgres.rb', line 9 def session @session end |
Instance Method Details
#configure(session: nil) ⇒ Object
20 21 22 23 24 |
# File 'lib/entity_snapshot/postgres/postgres.rb', line 20 def configure(session: nil) MessageStore::Postgres::Session.configure(self, session: session) MessageStore::Postgres::Put.configure(self, session: self.session, attr_name: :write) MessageStore::Postgres::Get::Last.configure(self, session: self.session, attr_name: :read) end |
#get(id) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/entity_snapshot/postgres/postgres.rb', line 56 def get(id) stream_name = snapshot_stream_name(id) logger.trace(tag: :get) { "Reading snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity_class.name})" } event_data = read.(stream_name) if event_data.nil? logger.debug(tag: :get) { "No snapshot could not be read (Stream: #{stream_name.inspect}, Entity Class: #{entity_class.name})" } return end entity_data = event_data.data[:entity_data] entity = Transform::Read.instance(entity_data, entity_class) version = event_data.data[:entity_version] time = event_data.time logger.debug(tag: :get) { "Read snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity_class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" } return entity, version, time end |
#put(id, entity, version, time) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/entity_snapshot/postgres/postgres.rb', line 26 def put(id, entity, version, time) unless entity.is_a? subject error_msg = "Persistent storage for #{subject} cannot store #{entity}" logger.error { error_msg } raise Error, error_msg end stream_name = snapshot_stream_name(id) logger.trace(tag: :put) { "Writing snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" } entity_data = Transform::Write.raw_data(entity) event_data = MessageStore::MessageData::Write.new data = { entity_data: entity_data, entity_version: version } event_data.type = 'Recorded' event_data.data = data position = write.(event_data, stream_name) logger.debug(tag: :put) { "Wrote snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" } position end |
#snapshot_stream_name(id) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/entity_snapshot/postgres/postgres.rb', line 13 def snapshot_stream_name(id) entity_class_name = entity_class.name.split('::').last entity_cateogry = Casing::Camel.(entity_class_name) Messaging::StreamName.stream_name(id, entity_cateogry, type: 'snapshot') end |