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/subject.rb,
lib/entity_snapshot/postgres/controls/version.rb,
lib/entity_snapshot/postgres/controls/category.rb,
lib/entity_snapshot/postgres/controls/snapshot.rb,
lib/entity_snapshot/postgres/controls/specifier.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

#entity_class, #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



58
59
60
61
62
# File 'lib/entity_snapshot/postgres/postgres.rb', line 58

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

Instance Method Details

#categoryObject



15
16
17
# File 'lib/entity_snapshot/postgres/postgres.rb', line 15

def category
  StreamName.category(entity_class, specifier)
end

#configure(session: nil) ⇒ Object



19
20
21
22
23
# File 'lib/entity_snapshot/postgres/postgres.rb', line 19

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::Stream::Last.configure(self, session: self.session, attr_name: :read)
end

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



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

def put(id, entity, version, time)
  unless entity.is_a?(entity_class)
    error_msg = "Persistent storage for #{entity_class} cannot store #{entity}"
    logger.error() { error_msg }
    raise Error, error_msg
  end

  stream_name = snapshot_stream_name(id)

  logger.trace(tags: [: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: [:cache, :put]) { "Wrote snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time.utc.iso8601(3)})" }

  position
end