Class: EntitySnapshot::Postgres

Inherits:
Object
  • Object
show all
Includes:
EntityCache::Store::External, Get, StreamName, Log::Dependency
Defined in:
lib/entity_snapshot/postgres/get.rb,
lib/entity_snapshot/postgres/log.rb,
lib/entity_snapshot/postgres/postgres.rb,
lib/entity_snapshot/postgres/read_only.rb,
lib/entity_snapshot/postgres/controls/id.rb,
lib/entity_snapshot/postgres/stream_name.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,
lib/entity_snapshot/postgres/controls/snapshot/put.rb,
lib/entity_snapshot/postgres/controls/snapshot/read_only.rb

Defined Under Namespace

Modules: Controls, Get, StreamName Classes: Log, ReadOnly

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StreamName

category, #snapshot_stream_name

Methods included from Get

#get, prepended

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



13
14
15
# File 'lib/entity_snapshot/postgres/postgres.rb', line 13

def session
  @session
end

Class Method Details

.assure(store) ⇒ Object



60
61
62
63
64
# File 'lib/entity_snapshot/postgres/postgres.rb', line 60

def self.assure(store)
  if [store.snapshot_class, store.snapshot_interval].include?(nil)
    raise EntityCache::Store::External::Error
  end
end

Instance Method Details

#categoryObject



17
18
19
# File 'lib/entity_snapshot/postgres/postgres.rb', line 17

def category
  StreamName.category(subject)
end

#configure(session: nil) ⇒ Object



21
22
23
24
25
# File 'lib/entity_snapshot/postgres/postgres.rb', line 21

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

#put(id, entity, version, time) ⇒ Object



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
55
56
57
58
# File 'lib/entity_snapshot/postgres/postgres.rb', line 27

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(tags: [:snapshot, :cache, :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

  iso8601_time = Clock::UTC.iso8601(time)

  data = {
    entity_data: entity_data,
    entity_version: version,
    time: iso8601_time
  }

  event_data.type = 'Recorded'
  event_data.data = data

  position = write.(event_data, stream_name)

  logger.debug(tags: [:snapshot, :cache, :put]) { "Wrote snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" }

  position
end