Class: EntitySnapshot::EventStore

Inherits:
Object
  • Object
show all
Includes:
EntityCache::Store::Persistent, Log::Dependency
Defined in:
lib/entity_snapshot/event_store/log.rb,
lib/entity_snapshot/event_store/controls/id.rb,
lib/entity_snapshot/event_store/event_store.rb,
lib/entity_snapshot/event_store/controls/time.rb,
lib/entity_snapshot/event_store/controls/batch.rb,
lib/entity_snapshot/event_store/controls/write.rb,
lib/entity_snapshot/event_store/controls/entity.rb,
lib/entity_snapshot/event_store/controls/message.rb,
lib/entity_snapshot/event_store/controls/version.rb,
lib/entity_snapshot/event_store/controls/snapshot.rb,
lib/entity_snapshot/event_store/controls/stream_name.rb,
lib/entity_snapshot/event_store/controls/entity_store.rb

Defined Under Namespace

Modules: Controls Classes: Log

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



9
10
11
# File 'lib/entity_snapshot/event_store/event_store.rb', line 9

def session
  @session
end

Instance Method Details

#configure(session: nil) ⇒ Object



20
21
22
23
24
# File 'lib/entity_snapshot/event_store/event_store.rb', line 20

def configure(session: nil)
  MessageStore::EventStore::Session.configure(self, session: session)
  MessageStore::EventStore::Write.configure(self, session: self.session, attr_name: :write)
  MessageStore::EventStore::Get::Last.configure(self, session: self.session, attr_name: :read)
end

#get(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/entity_snapshot/event_store/event_store.rb', line 54

def get(id)
  stream_name = snapshot_stream_name(id)

  logger.trace "Reading snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity_class.name})"

  event_data = read.(stream_name)

  if event_data.nil?
    logger.debug "No snapshot could not be read (Stream: #{stream_name.inspect}, Entity Class: #{entity_class.name})"
    return
  end

  entity = entity_class.build(event_data.data[:entity_data])
  version = event_data.data[:entity_version]
  time = event_data.time

  logger.debug "Read snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity_class.name}, Version: #{version.inspect}, Time: #{time})"

  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
# File 'lib/entity_snapshot/event_store/event_store.rb', line 26

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

  stream_name = snapshot_stream_name(id)

  logger.trace "Writing snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time})"

  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 "Wrote snapshot (Stream: #{stream_name.inspect}, Entity Class: #{entity.class.name}, Version: #{version.inspect}, Time: #{time})"

  position
end

#snapshot_stream_name(id) ⇒ Object



13
14
15
16
17
18
# File 'lib/entity_snapshot/event_store/event_store.rb', line 13

def snapshot_stream_name(id)
  entity_class_name = entity_class.name.split('::').last
  entity_category = Casing::Camel.(entity_class_name)

  Messaging::StreamName.stream_name(id, entity_category, type: 'snapshot')
end