Class: SandthornDriverSequel::SnapshotAccess

Inherits:
Access::Base show all
Defined in:
lib/sandthorn_driver_sequel/access/snapshot_access.rb

Instance Method Summary collapse

Constructor Details

#initialize(storage, serializer, deserializer) ⇒ SnapshotAccess

Returns a new instance of SnapshotAccess.



6
7
8
9
10
# File 'lib/sandthorn_driver_sequel/access/snapshot_access.rb', line 6

def initialize storage, serializer, deserializer
  @serializer = serializer
  @deserializer = deserializer
  super storage
end

Instance Method Details

#find(snapshot_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sandthorn_driver_sequel/access/snapshot_access.rb', line 31

def find(snapshot_id)
  
  snapshot = storage.snapshots[snapshot_id]
  aggregate = deserialize(snapshot[:snapshot_data])

  snapshot_data = {
    aggregate: aggregate,
    snapshot_id: snapshot_id,
    aggregate_table_id: snapshot[:aggregate_table_id]
  }
  
  SnapshotWrapper.new(snapshot_data)
end

#find_by_aggregate_id(aggregate_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sandthorn_driver_sequel/access/snapshot_access.rb', line 12

def find_by_aggregate_id(aggregate_id)
  
  aggregate_from_table = aggregates.find_by_aggregate_id(aggregate_id)
  return nil if aggregate_from_table.nil?
  snapshot = storage.snapshots.first(aggregate_table_id: aggregate_from_table.id)
  if snapshot
    aggregate = deserialize(snapshot[:snapshot_data])
    
    snapshot_data = {
      aggregate: aggregate,
      snapshot_id: snapshot.id,
      aggregate_table_id: snapshot[:aggregate_table_id]
    }
    return SnapshotWrapper.new(snapshot_data)
  end
  
  return nil
end

#obsolete(aggregate_types: [], max_event_distance: 100) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sandthorn_driver_sequel/access/snapshot_access.rb', line 50

def obsolete(aggregate_types: [], max_event_distance: 100)
  aggregate_types.map!(&:to_s)
  snapshot_version = Sequel.qualify(storage.snapshots_table_name, :aggregate_version)
  aggregate_version = Sequel.qualify(storage.aggregates_table_name, :aggregate_version)
  query = storage.aggregates.left_outer_join(storage.snapshots, aggregate_table_id: :id)
  query = query.select { (aggregate_version - snapshot_version).as(distance) }
  query = query.select_append(:aggregate_id, :aggregate_type)
  query = query.where { (aggregate_version - coalesce(snapshot_version, 0)) > max_event_distance }
  if aggregate_types.any?
    query = query.where(aggregate_type: aggregate_types)
  end
  query.all
end

#record_snapshot(aggregate) ⇒ Object



45
46
47
48
# File 'lib/sandthorn_driver_sequel/access/snapshot_access.rb', line 45

def record_snapshot(aggregate)
  aggregate_from_table = aggregates.find_by_aggregate_id!(aggregate.aggregate_id)
  perform_snapshot(aggregate, aggregate_from_table.id)
end